/** * * dump some hex values -- also * show the ascii version of the dump. * */ void debug_hex_dump(unsigned char level, uint8_t *hextodump, int size) { int i; char buf[80]; int str_idx = 0; int chr_idx = 0; int count; int total; int tmp; if ((!(debug_level & level)) && (level != 0)) return; if (hextodump == NULL) return; /* Initialize constant fields */ memset(buf, ' ', sizeof(buf)); buf[4] = '|'; buf[54] = '|'; buf[72] = '\n'; buf[73] = 0; count = 0; total = 0; for (i = 0; i < size; i++) { if (count == 0) { str_idx = 6; chr_idx = 56; buf[0] = to_hex_char(total >> 8); buf[1] = to_hex_char(total >> 4); buf[2] = to_hex_char(total); } /* store the number */ tmp = hextodump[i]; buf[str_idx++] = to_hex_char(tmp >> 4); buf[str_idx++] = to_hex_char(tmp); str_idx++; /* store the character */ buf[chr_idx++] = isprint(tmp) ? tmp : '.'; total++; count++; if (count >= 16) { count = 0; ufprintf(logfile, buf, level); } }
//Taken from boost::json std::string non_printable_to_string(unsigned int c) { std::string result( 6, '\\' ); result[1] = 'u'; result[ 5 ] = to_hex_char(c & 0x000F); c >>= 4; result[ 4 ] = to_hex_char(c & 0x000F); c >>= 4; result[ 3 ] = to_hex_char(c & 0x000F); c >>= 4; result[ 2 ] = to_hex_char(c & 0x000F); return result; }
/** * Dumps hex characters inline, no newlines inserted * * @param sev The severity * @param data The data to log * @param len The number of bytes to log */ void log_hex(log_sev_t sev, const void *vdata, int len) { const UINT8 *dat = (const UINT8 *)vdata; int idx; char buf[80]; if ((vdata == NULL) || !log_sev_on(sev)) { return; } idx = 0; while (len-- > 0) { buf[idx++] = to_hex_char(*dat >> 4); buf[idx++] = to_hex_char(*dat); dat++; if (idx >= (int)(sizeof(buf) - 3)) { buf[idx] = 0; log_str(sev, buf, idx); idx = 0; } } if (idx > 0) { buf[idx] = 0; log_str(sev, buf, idx); } }
/** * Logs a block of data in a pretty hex format * Numbers on the left, characters on the right, just like I like it. * * @param sev The severity * @param data The data to log * @param len The number of bytes to log */ void log_hex_blk(log_sev_t sev, const void *data, int len) { static char buf[80] = "nnn | XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | cccccccccccccccc\n"; const UINT8 *dat = (const UINT8 *)data; int idx; int count; int str_idx = 0; int chr_idx = 0; int tmp; int total; if ((data == NULL) || (len <= 0) || !log_sev_on(sev)) { return; } /* * Dump the specified number of bytes in hex, 16 byte per line by * creating a string and then calling log_str() */ /* Loop through the data of the current iov */ count = 0; total = 0; for (idx = 0; idx < len; idx++) { if (count == 0) { str_idx = 6; chr_idx = 56; buf[0] = to_hex_char(total >> 12); buf[1] = to_hex_char(total >> 8); buf[2] = to_hex_char(total >> 4); } tmp = dat[idx]; buf[str_idx] = to_hex_char(tmp >> 4); buf[str_idx + 1] = to_hex_char(tmp); str_idx += 3; buf[chr_idx++] = unc_isprint(tmp) ? tmp : '.'; total++; count++; if (count >= 16) { count = 0; log_str(sev, buf, 73); } }
FCITX_EXPORT_API void fcitx_color_to_string(const FcitxColor* color, char* str) { str[0] = '#'; str++; int v[4]; v[0] = (int)(color->red * 255); v[1] = (int)(color->green * 255); v[2] = (int)(color->blue * 255); v[3] = (int)(color->alpha * 255); for (size_t i = 0; i < 4; i ++) { int value = RoundColor(v[i]); int hi = value / 16; int lo = value % 16; *str = to_hex_char(hi); str++; *str = to_hex_char(lo); str++; } *str = '\0'; }
/** * * Dump hex values, without the ascii versions. * */ void debug_hex_printf(uint32_t level, uint8_t *hextodump, int size) { int i; int len = 0; char *logstr = NULL; logstr = (char *)calloc(1, (size * 3) + 2); if (logstr == NULL) { printf("Couldn't allocate memory to store temporary logging string!\n"); return; } memset(logstr, 0x00, ((size * 3)+2)); if ((!(debug_level & level)) && (level != 0)) { free(logstr); return; } if (hextodump == NULL) { free(logstr); return; } for (i = 0; i < size; i++) { logstr[len++] = to_hex_char(hextodump[i] >> 4); logstr[len++] = to_hex_char(hextodump[i]); logstr[len++] = ' '; } logstr[len++] = '\n'; logstr[len] = 0; ufprintf(logfile, logstr, level); free(logstr); }