test.c
tubo
posted @ 2014年9月03日 00:14
in 未分类
, 656 阅读
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <unistd.h> void dump_line(const unsigned char* buf, int w, int l) { #define YYYGET(X) ( X >= 32 && X <= 126) ? X : '.' unsigned int i = 0; printf("%08x: ", l); for (; i < w; ++i) printf((i % 8 == 7) ? "%02x " : "%02x ", *(buf+i)); if (w < 0x10) { for (i = 0; i < 0x10 - w; ++i) printf(" "); printf(" "); } printf ("|"); for (i = 0; i < w; ++i) printf ("%c", YYYGET(*(buf+i))); if (w < 0x10) for (i = 0; i < 0x10 - w; ++i) printf(" "); printf ("|\n"); #undef YYYGET } void dump_buffer(const unsigned char* buf, int max) { int l = max / 0x10 + ((max % 0x10) ? 1 : 0); printf ("l = %d\n",l); int i = 0; int w = l - i > 1 ? 0x10 : max; const unsigned char* ptr = buf; for (; i < l; ++i,w = l - i > 1 ? 0x10 : max - 0x10 * i) { dump_line(ptr, w, i); ptr += w; } } int main(int argc, char *argv[]) { if (argc < 3) { printf ("Usage: %s FileName, number.\n", argv[0]); exit(1); } int fd = open(argv[1], O_RDONLY); if (fd != -1) { int s = atoi(argv[2]); unsigned char* buf = malloc(s+1); memset(buf, 0, s+1); read(fd, buf, s); dump_buffer(buf, s); } return 0; }