#ifndef DUMP_BUF_H #define DUMP_BUF_H #ifndef DUMP_MIN_STRLEN #define DUMP_MIN_STRLEN 1 #endif #ifndef DUMP_PARALLEL #define DUMP_PARALLEL 0 #endif #ifndef DUMP_PRINT_BUF_ADDR #define DUMP_PRINT_BUF_ADDR 0 #endif #ifndef DUMP_PRINT_HEX #define DUMP_PRINT_HEX 0 #endif #ifndef DUMP_PRINT_STRING #define DUMP_PRINT_STRING 0 #endif #if DUMP_PARALLEL pthread_mutex_t out_mutex = PTHREAD_MUTEX_INITIALIZER; #endif void dump_buf(unsigned char *buf, int len) { int i, nz = 0; for (i = 0; i < len; i++) { if (buf[i]) { nz = 1; break; } } if (!nz) { // The buffer is empty. return; } else { #if DUMP_PARALLEL pthread_mutex_lock(&out_mutex); #endif #if DUMP_PRINT_BUF_ADDR fprintf(stderr, "nonempty buffer at %p\n", buf); #endif #if DUMP_PRINT_HEX for (i=0; i < len; i++) { if (buf[i]) { fprintf(stderr, "buf[%d]: %x (%p)\n", i, buf[i], *(void**)&buf[i]); } } #endif // DUMP_PRINT_HEX #if DUMP_PARALLEL pthread_mutex_unlock(&out_mutex); #endif } #if DUMP_PARALLEL pthread_mutex_lock(&out_mutex); #endif #if DUMP_PRINT_STRING for (i = 0; i < len; i++) { if (buf[i]) { int str_len = strlen(&buf[i]); // Short string pieces are too boring. if (str_len >= DUMP_MIN_STRLEN) { unsigned char *c; for (c = &buf[i]; c < &buf[i + str_len]; c++) { if ((*c > 127) || ((*c < 32) && (*c != 10) && (*c != 13))) { *c = ' '; continue; } } // Dump the buffer. fprintf(stderr, "%s\n", &buf[i]); } i += str_len; } } #endif // DUMP_PRINT_STRING #if DUMP_PARALLEL pthread_mutex_unlock(&out_mutex); #endif } #endif