|
|
|
@ -25,7 +25,6 @@ static struct argp_option options[] = {
|
|
|
|
|
{0, 'o', "off", 0, "add <off> to the displayed file position.", 0},
|
|
|
|
|
{0, 'e', 0, 0, "little-endian dump (incompatible with -ps,-i,-r).", 0},
|
|
|
|
|
{0, 's', "seek", 0, "start at <[+][-] seek> bytes abs. infile offset.", 0},
|
|
|
|
|
{0, 'r', 0, 0, "reverse operation: convert (or patch) hexdump into binary.", 0},
|
|
|
|
|
{0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -66,9 +65,6 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state)
|
|
|
|
|
else
|
|
|
|
|
flags->coloured = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
flags->reverse_op = true;
|
|
|
|
|
flags->coloured = false;
|
|
|
|
|
case 'l':
|
|
|
|
|
flags->len = atoi(arg);
|
|
|
|
|
break;
|
|
|
|
@ -115,6 +111,26 @@ static int get_hex_lines(int len)
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_seek(int len_content)
|
|
|
|
|
{
|
|
|
|
|
uint seek = 0;
|
|
|
|
|
|
|
|
|
|
if (flags.seek >= 0) {
|
|
|
|
|
if (flags.seek > len_content)
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
seek = flags.seek;
|
|
|
|
|
} else {
|
|
|
|
|
if ((flags.seek * -1) > len_content) {
|
|
|
|
|
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
seek = (len_content - (flags.seek * -1));
|
|
|
|
|
flags.offset += seek;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return seek;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void do_text_parse(hex_chunk_t** lines, bool interactive)
|
|
|
|
|
{
|
|
|
|
|
char* file_content = NULL;
|
|
|
|
@ -129,28 +145,14 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive)
|
|
|
|
|
read_file_to_buf(flags.files[0], &file_content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags.seek >= 0) {
|
|
|
|
|
if (flags.seek > (int) strlen(file_content))
|
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
seek = flags.seek;
|
|
|
|
|
} else {
|
|
|
|
|
if ((flags.seek * -1) > (int) strlen(file_content)) {
|
|
|
|
|
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
seek = (strlen(file_content) - (flags.seek * -1));
|
|
|
|
|
flags.offset += seek;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seek = get_seek(strlen(file_content));
|
|
|
|
|
filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len;
|
|
|
|
|
|
|
|
|
|
flags.len = filesize;
|
|
|
|
|
|
|
|
|
|
hex_lines = get_hex_lines(filesize);
|
|
|
|
|
|
|
|
|
|
*lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < hex_lines; i++) {
|
|
|
|
|
(*lines)[i].line = i;
|
|
|
|
|
add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text));
|
|
|
|
@ -189,8 +191,6 @@ static void do_display(hex_chunk_t** lines)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
char* filename;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool fileout = false;
|
|
|
|
|
FILE* stream = stdout;
|
|
|
|
|
|
|
|
|
@ -199,8 +199,6 @@ static void do_display(hex_chunk_t** lines)
|
|
|
|
|
fileout = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
|
|
if (flags.c_style) {
|
|
|
|
|
parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename),
|
|
|
|
|
&filename);
|
|
|
|
@ -208,6 +206,7 @@ static void do_display(hex_chunk_t** lines)
|
|
|
|
|
printf("unsigned char %s[] = {\n", filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while ((*lines)[i].line != -1)
|
|
|
|
|
display_hex_chunk(&((*lines)[i++]), stream);
|
|
|
|
|
|
|
|
|
@ -235,7 +234,7 @@ int main(int argc, char* argv[])
|
|
|
|
|
|
|
|
|
|
init_var_defaults(&flags);
|
|
|
|
|
|
|
|
|
|
do_text_parse(&lines, (flags.files[0] == NULL ? true : false));
|
|
|
|
|
do_text_parse(&lines, (!(flags.files[0]) ? true : false));
|
|
|
|
|
|
|
|
|
|
do_display(&lines);
|
|
|
|
|
|
|
|
|
|