diff --git a/src/hex.c b/src/hex.c index 4f3c537..634fb6a 100644 --- a/src/hex.c +++ b/src/hex.c @@ -1,9 +1,24 @@ #include #include #include +#include #include "include/hex.h" +extern struct flags flags; + +void init_flags(struct flags* flags) +{ + flags->autoskip = false; + flags->binary = false; + flags->cols = 16; + flags->octets = 2; + flags->len = -1; // -1 means til EOF + flags->uppercase = false; + flags->decimaloffset = false; + +} + void free_hex_chunk(hex_chunk_t* chunk) { free(chunk->text); @@ -22,12 +37,13 @@ void convert_text_to_hex(hex_chunk_t* chunk) { int i, j; chunk->hex = malloc(sizeof(char) * (HEX_LINE_LEN + 1)); - + for (i = 0, j = 0; i < HEX_LINE_LEN; i += 2, j += 1) { - if (chunk->text[j] == '\0') + if (chunk->text[j] == '\0') { snprintf(chunk->hex + i, 3, " "); - else - snprintf(chunk->hex + i, 3, "%02x", chunk->text[j]); + } else { + snprintf(chunk->hex + i, 3, flags.uppercase ? "%02X ": "%02x", chunk->text[j]); + } } } diff --git a/src/include/hex.h b/src/include/hex.h index 70af091..1659291 100644 --- a/src/include/hex.h +++ b/src/include/hex.h @@ -1,4 +1,6 @@ #include +#include +#include #ifndef HEX_H #define HEX_H @@ -12,6 +14,21 @@ typedef struct { char* hex; } hex_chunk_t; +struct flags { + bool autoskip; // should we condense null lines into * + bool binary; // do binary dump instead of hex + uint cols; // choose the amount of columns to display (default 16) + + uint octets; // number of octets per line (default 2) + int len; // max len to stop at + bool uppercase; // do uppercase hex chars + bool decimaloffset; // do decimal offset instead of hex +}; + +// TODO: implement flags and stuff + +void init_flags(struct flags* flags); + void free_hex_chunk(hex_chunk_t* chunk); void add_text_to_chunk(char* src, char** dst); void convert_text_to_hex(hex_chunk_t* chunk); diff --git a/src/main.c b/src/main.c index a513c07..fc3f182 100644 --- a/src/main.c +++ b/src/main.c @@ -6,31 +6,6 @@ #include "include/hex.h" #include "include/file.h" -struct flags { - bool autoskip; // should we condense null lines into * - bool binary; // do binary dump instead of hex - uint cols; // choose the amount of columns to display (default 16) - - uint octets; // number of octets per line (default 2) - int len; // max len to stop at - bool uppercase; // do uppercase hex chars - bool decimaloffset; // do decimal offset instead of hex -}; - -// TODO: implement flags and stuff - -void init_flags(struct flags* flags) -{ - flags->autoskip = false; - flags->binary = false; - flags->cols = 16; - flags->octets = 2; - flags->len = -1; // -1 means til EOF - flags->uppercase = false; - flags->decimaloffset = false; - -} - int get_hex_lines(int len) { int out; @@ -42,6 +17,8 @@ int get_hex_lines(int len) return out; } +struct flags flags; + int main(int argc, char* argv[]) { char* file_content; @@ -49,6 +26,8 @@ int main(int argc, char* argv[]) bool outfile; int hex_lines, i; + init_flags(&flags); + if (argc < 2) { fprintf(stderr, "Usage: %s [filename]\n", argv[0]);