Example #1
0
File: hex.c Project: ksuarz/mongo
/*
 * __wt_raw_to_esc_hex --
 *	Convert a chunk of data to a nul-terminated printable string using
 * escaped hex, as necessary.
 */
int
__wt_raw_to_esc_hex(
    WT_SESSION_IMPL *session, const uint8_t *from, size_t size, WT_ITEM *to)
{
	size_t i;
	const uint8_t *p;
	u_char *t;

	/*
	 * In the worst case, every character takes up 3 spaces, plus a
	 * trailing nul byte.
	 */
	WT_RET(__wt_buf_init(session, to, size * 3 + 1));

	for (p = from, t = to->mem, i = size; i > 0; --i, ++p)
		if (__wt_isprint((u_char)*p)) {
			if (*p == '\\')
				*t++ = '\\';
			*t++ = *p;
		} else {
			*t++ = '\\';
			*t++ = __wt_hex((*p & 0xf0) >> 4);
			*t++ = __wt_hex(*p & 0x0f);
		}
	*t++ = '\0';
	to->size = WT_PTRDIFF(t, to->mem);
	return (0);
}
Example #2
0
/*
 * __debug_bytes --
 *	Dump a single set of bytes.
 */
static int
__debug_bytes(WT_DBG *ds, const void *data_arg, size_t size)
{
	size_t i;
	const uint8_t *data;
	u_char ch;

	for (data = data_arg, i = 0; i < size; ++i, ++data) {
		ch = data[0];
		if (__wt_isprint(ch))
			WT_RET(ds->f(ds, "%c", (int)ch));
		else
			WT_RET(__debug_hex_byte(ds, data[0]));
	}
	return (0);
}
Example #3
0
/*
 * __wt_json_unpack_char --
 *	Unpack a single character into JSON escaped format.
 *	Can be called with null buf for sizing.
 */
size_t
__wt_json_unpack_char(u_char ch, u_char *buf, size_t bufsz, bool force_unicode)
{
	u_char abbrev;

	if (!force_unicode) {
		if (__wt_isprint(ch) && ch != '\\' && ch != '"') {
			if (bufsz >= 1)
				*buf = ch;
			return (1);
		} else {
			abbrev = '\0';
			switch (ch) {
			case '\\':
			case '"':
				abbrev = ch;
				break;
			case '\f':
				abbrev = 'f';
				break;
			case '\n':
				abbrev = 'n';
				break;
			case '\r':
				abbrev = 'r';
				break;
			case '\t':
				abbrev = 't';
				break;
			}
			if (abbrev != '\0') {
				if (bufsz >= 2) {
					*buf++ = '\\';
					*buf = abbrev;
				}
				return (2);
			}
		}
	}
	if (bufsz >= 6) {
		*buf++ = '\\';
		*buf++ = 'u';
		*buf++ = '0';
		*buf++ = '0';
		*buf++ = __wt_hex[(ch & 0xf0) >> 4];
		*buf++ = __wt_hex[ch & 0x0f];
	}
Example #4
0
/*
 * print_res --
 *	Write results file.
 */
void
print_res(int key, int value, int cnt)
{
	static const char hex[] = "0123456789abcdef";
	int ch;

	for (; cnt > 0; ++key, ++value, --cnt) {
		switch (page_type) {			/* Print key */
		case WT_PAGE_COL_FIX:
		case WT_PAGE_COL_VAR:
			break;
		case WT_PAGE_ROW_LEAF:
			fprintf(res_fp, "%010d KEY------\n", key);
			break;
		}

		switch (page_type) {			/* Print value */
		case WT_PAGE_COL_FIX:
			ch = value & 0x7f;
			if (__wt_isprint((u_char)ch)) {
				if (ch == '\\')
					fputc('\\', res_fp);
				fputc(ch, res_fp);
			} else {
				fputc('\\', res_fp);
				fputc(hex[(ch & 0xf0) >> 4], res_fp);
				fputc(hex[ch & 0x0f], res_fp);
			}
			fputc('\n', res_fp);
			break;
		case WT_PAGE_COL_VAR:
		case WT_PAGE_ROW_LEAF:
			fprintf(res_fp,
			    "%010d VALUE----\n", value_unique ? value : 37);
			break;
		}
	}
}