Compare commits

..

No commits in common. "d73ed7fa1d4d6046a3012dba920954260ec294d1" and "1eda825308d5b51ce71996ca4bc8df40024398e7" have entirely different histories.

3 changed files with 24 additions and 23 deletions

View File

@ -21,6 +21,8 @@ void init_flags(struct flags* flags)
flags->littleendian = false; flags->littleendian = false;
flags->reverse_op = false;
flags->octets = 2; flags->octets = 2;
flags->customoctets = false; flags->customoctets = false;

View File

@ -29,6 +29,8 @@ struct flags {
bool littleendian; bool littleendian;
bool reverse_op;
uint offset; uint offset;
int seek; int seek;

View File

@ -115,26 +115,6 @@ static int get_hex_lines(int len)
return out; 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) static void do_text_parse(hex_chunk_t** lines, bool interactive)
{ {
char* file_content = NULL; char* file_content = NULL;
@ -149,14 +129,28 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive)
read_file_to_buf(flags.files[0], &file_content); read_file_to_buf(flags.files[0], &file_content);
} }
seek = get_seek(strlen(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;
}
filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len; filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len;
flags.len = filesize; flags.len = filesize;
hex_lines = get_hex_lines(filesize); hex_lines = get_hex_lines(filesize);
*lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1)); *lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1));
for (i = 0; i < hex_lines; i++) { for (i = 0; i < hex_lines; i++) {
(*lines)[i].line = i; (*lines)[i].line = i;
add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text)); add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text));
@ -195,6 +189,8 @@ static void do_display(hex_chunk_t** lines)
{ {
int i; int i;
char* filename; char* filename;
bool fileout = false; bool fileout = false;
FILE* stream = stdout; FILE* stream = stdout;
@ -203,6 +199,8 @@ static void do_display(hex_chunk_t** lines)
fileout = true; fileout = true;
} }
i = 0;
if (flags.c_style) { if (flags.c_style) {
parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename), parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename),
&filename); &filename);
@ -210,7 +208,6 @@ static void do_display(hex_chunk_t** lines)
printf("unsigned char %s[] = {\n", filename); printf("unsigned char %s[] = {\n", filename);
} }
i = 0;
while ((*lines)[i].line != -1) while ((*lines)[i].line != -1)
display_hex_chunk(&((*lines)[i++]), stream); display_hex_chunk(&((*lines)[i++]), stream);
@ -238,7 +235,7 @@ int main(int argc, char* argv[])
init_var_defaults(&flags); init_var_defaults(&flags);
do_text_parse(&lines, (!(flags.files[0]) ? true : false)); do_text_parse(&lines, (flags.files[0] == NULL ? true : false));
do_display(&lines); do_display(&lines);