From: Linus Torvalds Date: Fri, 27 Jan 2012 22:02:50 +0000 (-0800) Subject: Make cochran debug output a bit easier to use directly X-Git-Url: http://git.tdb.fi/?p=ext%2Fsubsurface.git;a=commitdiff_plain;h=e5d2bdc9ba6f6a160776717f244a05286831c1ed Make cochran debug output a bit easier to use directly Just do the hex-dump in the program, and print all the results to standard output. Avoid the need to do 'od' by hand etc to see what happens when you play with the decoder. Signed-off-by: Linus Torvalds --- diff --git a/cochran.c b/cochran.c index 907dc76..84bfcb9 100644 --- a/cochran.c +++ b/cochran.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -76,20 +77,50 @@ static int figure_out_modulus(const unsigned char *decode, const unsigned char * return best; } -static void cochran_debug_write(int dive, const unsigned char *data, unsigned size) +#define hexchar(n) ("0123456789abcdef"[(n)&15]) + +static void show_line(unsigned offset, const unsigned char *data, unsigned size) { - char buffer[60]; - int fd; - - snprintf(buffer, sizeof(buffer), "cochran.%d.out", dive); - fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666); - if (fd >= 0) { - write(fd, data, size); - close(fd); + unsigned char bits; + int i, off; + char buffer[120]; + + if (size > 16) + size = 16; + + bits = 0; + memset(buffer, ' ', sizeof(buffer)); + off = sprintf(buffer, "%06x ", offset); + for (i = 0; i < size; i++) { + char *hex = buffer + off + 3*i; + char *asc = buffer + off + 50 + i; + unsigned char byte = data[i]; + + hex[0] = hexchar(byte>>4); + hex[1] = hexchar(byte); + bits |= byte; + if (byte < 32 || byte > 126) + byte = '.'; + asc[0] = byte; + asc[1] = 0; + } + + if (bits) + puts(buffer); +} + +static void cochran_debug_write(const char *filename, int dive, const unsigned char *data, unsigned size) +{ + int i; + printf("\n%s, dive %d\n\n", filename, dive); + + for (i = 0; i < size; i += 16) { + show_line(i, data + i, size - i); } } -static void parse_cochran_dive(int dive, const unsigned char *decode, unsigned mod, +static void parse_cochran_dive(const char *filename, int dive, + const unsigned char *decode, unsigned mod, const unsigned char *in, unsigned size) { char *buf = malloc(size); @@ -117,7 +148,7 @@ static void parse_cochran_dive(int dive, const unsigned char *decode, unsigned m */ partial_decode(0x48ff, size, decode, 0, mod, in, size, buf); - cochran_debug_write(dive, buf, size); + cochran_debug_write(filename, dive, buf, size); free(buf); } @@ -146,8 +177,8 @@ int try_to_open_cochran(const char *filename, struct memblock *mem, GError **err break; if (dive2 > mem->size) break; - parse_cochran_dive(i, decode, mod, mem->buffer + dive1, dive2 - dive1); + parse_cochran_dive(filename, i, decode, mod, mem->buffer + dive1, dive2 - dive1); } - return 1; + exit(0); }