From f4ab18171a0a476487f5a1c6030e6750d4810fa8 Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Wed, 12 Feb 2025 14:06:13 -0500 Subject: [PATCH] adds seek flag --- src/hex.c | 1 + src/include/hex.h | 1 + src/main.c | 25 ++++++++++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/hex.c b/src/hex.c index 6949204..6b10293 100644 --- a/src/hex.c +++ b/src/hex.c @@ -17,6 +17,7 @@ void init_flags(struct flags* flags) flags->coloured = true; flags->offset = 0; + flags->seek = 0; flags->littleendian = false; diff --git a/src/include/hex.h b/src/include/hex.h index 5766682..9e343a4 100644 --- a/src/include/hex.h +++ b/src/include/hex.h @@ -30,6 +30,7 @@ struct flags { bool littleendian; uint offset; + uint seek; uint octets; // number of octets per line (default 2) // done bool customoctets; diff --git a/src/main.c b/src/main.c index 4866623..5045b14 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ static struct argp_option options[] = { {0, 'n', "name", 0, "set the variable name used in C include output (-i).", 0}, {0, 'o', "off", 0, "add 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} }; @@ -46,7 +47,12 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state) flags->customoctets = true; break; case 'o': - flags->offset = atoi(arg); + flags->offset += atoi(arg); + break; + case 's': + flags->seek = atoi(arg); + if (flags->seek >= 0) + flags->offset += atoi(arg); break; case 'e': flags->littleendian = true; @@ -110,6 +116,7 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive) char* file_content = NULL; int hex_lines, i; size_t filesize, max_len; + uint seek; if (interactive) { max_len = INT_MAX; @@ -118,7 +125,19 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive) read_file_to_buf(flags.files[0], &file_content); } - filesize = (flags.len == -1) ? strlen(file_content) : flags.len; + if (flags.seek >= 0) { + if (flags.seek > strlen(file_content)) + exit(EXIT_SUCCESS); + seek = flags.seek; + } else { + if ((flags.seek * -1) > strlen(file_content)) { + fprintf(stderr, "xxd: Sorry, cannot seek.\n"); + exit(EXIT_FAILURE); + } + seek = (strlen(file_content) - flags.seek); + } + + filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len; flags.len = filesize; @@ -129,7 +148,7 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive) for (i = 0; i < hex_lines; i++) { (*lines)[i].line = i; - add_text_to_chunk(file_content + (i * (flags.cols)), &((*lines)[i].text)); + add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text)); convert_text_to_hex(&(*lines)[i]); }