diff options
author | Vicent Marti <tanoku@gmail.com> | 2010-08-12 18:47:32 +0200 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2010-08-12 18:49:04 +0200 |
commit | 0e465f979bd946d7476c86cfaa912d0737f54682 (patch) | |
tree | e38cd3ac1f961aa211f65ede3dc0503586fcd179 /src/util.c | |
parent | 003c26909451715cf36ea37ae4a6237813b4a25e (diff) | |
download | libgit2-0e465f979bd946d7476c86cfaa912d0737f54682.tar.gz |
Add auxiliary method git__hexdump
New function in util.c to do a dump of a buffer's contents in
hexadecimal to stdout.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/util.c')
-rw-r--r-- | src/util.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c index 84a5237f8..eda4a4e11 100644 --- a/src/util.c +++ b/src/util.c @@ -121,3 +121,47 @@ int git__basename(char *base, size_t n, char *path) return len; } +void git__hexdump(const char *buffer, size_t len) +{ + static const size_t LINE_WIDTH = 16; + + size_t line_count, last_line, i, j; + const char *line; + + line_count = (len / LINE_WIDTH); + last_line = (len % LINE_WIDTH); + + for (i = 0; i < line_count; ++i) { + line = buffer + (i * LINE_WIDTH); + for (j = 0; j < LINE_WIDTH; ++j, ++line) + printf("%02X ", (unsigned char)*line & 0xFF); + + printf("| "); + + line = buffer + (i * LINE_WIDTH); + for (j = 0; j < LINE_WIDTH; ++j, ++line) + printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); + + printf("\n"); + } + + if (last_line > 0) { + + line = buffer + (line_count * LINE_WIDTH); + for (j = 0; j < last_line; ++j, ++line) + printf("%02X ", (unsigned char)*line & 0xFF); + + for (j = 0; j < (LINE_WIDTH - last_line); ++j) + printf(" "); + + printf("| "); + + line = buffer + (line_count * LINE_WIDTH); + for (j = 0; j < last_line; ++j, ++line) + printf("%c", (*line >= 32 && *line <= 126) ? *line : '.'); + + printf("\n"); + } + + printf("\n"); +} |