bytebuffer.c (1207B)
1 #include <string.h> 2 #include <unistd.h> 3 4 #include "bytebuffer.h" 5 6 int bbcmp(const bytebuffer a, const bytebuffer b) { 7 return a.len == b.len ? memcmp(a.data, b.data, b.len) : (a.len < b.len ? -1 : 1); 8 } 9 10 int strbbcmp(const bytebuffer bb, const char *s) { 11 return strncmp(bb.data, s, bb.len); 12 } 13 14 void dbg_print_bb(const bytebuffer bb) { 15 static const char digit[] = "0123456789abcdef"; 16 char ascii[16]; 17 char hex[32]; 18 size_t i, j, a_off=0, h_off=0; 19 for (i = 0; i < bb.len; ++i) { 20 if (i % 16 == 0) { 21 if(i) { 22 write(2, "|", 1); 23 write(2, hex, h_off); 24 write(2, "| [", 3); 25 write(2, ascii, a_off); 26 write(2, "]\n", 2); 27 } 28 a_off=0; 29 h_off=0; 30 } 31 unsigned char c = bb.data[i]; 32 ascii[a_off++] = (c >= ' ' && c <= '~') ? c : '.'; 33 hex[h_off++] = digit[0xf & (c >> 4)]; 34 hex[h_off++] = digit[0xf & c]; 35 } 36 while(h_off < 32) { 37 hex[h_off++] = ' '; 38 } 39 write(2, "|", 1); 40 write(2, hex, h_off); 41 write(2, "| [", 3); 42 write(2, ascii, a_off); 43 write(2, "]\n", 2); 44 } 45 46 void dbg_print_bb1(const char *text, const bytebuffer bb) { 47 write(2, "\n", 1); 48 write(2, text, strlen(text)); 49 write(2, ":\n", 2); 50 dbg_print_bb(bb); 51 }