mem fixes
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
bin/
|
bin/
|
||||||
|
|
||||||
|
vgcore.*
|
||||||
|
|||||||
56
cmd/strenc.c
56
cmd/strenc.c
@@ -8,7 +8,7 @@
|
|||||||
#include "strenc.h"
|
#include "strenc.h"
|
||||||
|
|
||||||
// Open file and return point to char containing file contents
|
// Open file and return point to char containing file contents
|
||||||
unsigned char *openFile(char *fileName) {
|
unsigned char *openFile(char *fileName, size_t *len) {
|
||||||
size_t c = 0;
|
size_t c = 0;
|
||||||
size_t size = 8;
|
size_t size = 8;
|
||||||
unsigned char *buf = malloc(size);
|
unsigned char *buf = malloc(size);
|
||||||
@@ -26,42 +26,43 @@ unsigned char *openFile(char *fileName) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf[i] = c;
|
buf[i] = c;
|
||||||
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *genKey(unsigned char *pt) {
|
unsigned char *genKey(unsigned char *pt, size_t len) {
|
||||||
unsigned char *key = malloc(strlen((char *)pt));
|
unsigned char *key = malloc(len);
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
// rand() should not normally be used for
|
// rand() should not normally be used for
|
||||||
// generating secure numbers, we do not care
|
// generating secure numbers, we do not care
|
||||||
// here
|
// here
|
||||||
for (int i = 0; i < strlen((char *)pt); i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
key[i] = rand();
|
key[i] = rand();
|
||||||
}
|
}
|
||||||
|
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *xorCharArray(unsigned char *text, unsigned char *key) {
|
unsigned char *xorCharArray(unsigned char *text, unsigned char *key, size_t len) {
|
||||||
unsigned char *ft = malloc(strlen((char *)text));
|
unsigned char *ft = malloc(len);
|
||||||
|
|
||||||
for (int i = 0; i < strlen((char *)text); i++) { // Xor pt & key
|
for (int i = 0; i < len; i++) { // Xor pt & key
|
||||||
ft[i] = text[i] ^ key[i];
|
ft[i] = text[i] ^ key[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ft;
|
return ft;
|
||||||
}
|
}
|
||||||
|
|
||||||
int printCStyleCharArray(unsigned char *text, unsigned char *key) {
|
int printCStyleCharArray(unsigned char *text, unsigned char *key, size_t len) {
|
||||||
// Print text
|
// Print text
|
||||||
printf("unsigned char text[%zu] = { ", strlen((char *)text));
|
printf("unsigned char text[%zu] = { ", len);
|
||||||
for (int i = 0; i < strlen((char *)text); i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
printf("0x%02X", text[i]);
|
printf("0x%02X", text[i]);
|
||||||
if ((i + 1) == strlen((char *)text)) {
|
if ((i + 1) == len) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
} else {
|
} else {
|
||||||
printf(", ");
|
printf(", ");
|
||||||
@@ -69,10 +70,10 @@ int printCStyleCharArray(unsigned char *text, unsigned char *key) {
|
|||||||
}
|
}
|
||||||
printf("};\n");
|
printf("};\n");
|
||||||
// Print key
|
// Print key
|
||||||
printf("unsigned char text_key[%zu] = { ", strlen((char *)key));
|
printf("unsigned char text_key[%zu] = { ", len);
|
||||||
for (int i = 0; i < strlen((char *)key); i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
printf("0x%02X", key[i]);
|
printf("0x%02X", key[i]);
|
||||||
if ((i + 1) == strlen((char *)key)) {
|
if ((i + 1) == len) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
} else {
|
} else {
|
||||||
printf(", ");
|
printf(", ");
|
||||||
@@ -83,9 +84,11 @@ int printCStyleCharArray(unsigned char *text, unsigned char *key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
unsigned char *pt = malloc(sizeof(unsigned char));
|
unsigned char *pt = NULL;
|
||||||
unsigned char *key = malloc(sizeof(unsigned char));
|
unsigned char *key = NULL;
|
||||||
unsigned char *ct = malloc(sizeof(unsigned char));
|
unsigned char *ct = NULL;
|
||||||
|
|
||||||
|
size_t pt_len = 0;
|
||||||
|
|
||||||
// Setting up options
|
// Setting up options
|
||||||
static const struct option long_options[] = {
|
static const struct option long_options[] = {
|
||||||
@@ -114,31 +117,32 @@ int main(int argc, char **argv) {
|
|||||||
printf("%s", usage);
|
printf("%s", usage);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
case 'f':
|
case 'f':
|
||||||
if (strlen((char *)pt) != 0) { // Exit if string is already set
|
if (pt != NULL) { // Exit if string is already set
|
||||||
printf("Error string input already set\n");
|
printf("Error string input already set\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pt = openFile(optarg);
|
pt = openFile(optarg, &pt_len);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
if (strlen((char *)pt) != 0) { // Exit if file is already set
|
if (pt != NULL) { // Exit if file is already set
|
||||||
printf("Error file input already set\n");
|
printf("Error file input already set\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
pt = malloc(strlen(optarg));
|
pt_len = strlen(optarg);
|
||||||
strcpy((char *)pt, optarg);
|
pt = malloc(pt_len);
|
||||||
|
memcpy(pt, optarg, pt_len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (strlen((char *)pt) == 0) { // Exit if plain text is not > len 0
|
if (pt == NULL) { // Exit if plain text is NULL
|
||||||
printf("Plain text has not been set\n");
|
printf("Plain text has not been set\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
key = genKey(pt);
|
key = genKey(pt, pt_len);
|
||||||
ct = xorCharArray(pt, key);
|
ct = xorCharArray(pt, key, pt_len);
|
||||||
|
|
||||||
printCStyleCharArray(ct, key);
|
printCStyleCharArray(ct, key, pt_len);
|
||||||
|
|
||||||
// Freeing memory and returning
|
// Freeing memory and returning
|
||||||
free(pt);
|
free(pt);
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
unsigned char *openFile(char *fileName);
|
unsigned char *openFile(char *fileName, size_t *len);
|
||||||
unsigned char *genKey(unsigned char *pt);
|
unsigned char *genKey(unsigned char *pt, size_t len);
|
||||||
unsigned char *xorCharArray(unsigned char *text, unsigned char *key);
|
unsigned char *xorCharArray(unsigned char *text, unsigned char *key, size_t len);
|
||||||
int printCStyleCharArray(unsigned char *text, unsigned char *key);
|
int printCStyleCharArray(unsigned char *text, unsigned char *key, size_t len);
|
||||||
int main(int argc, char **argv);
|
int main(int argc, char **argv);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user