void chtmtag_attrs(const void *chtmtag, bson *b) { #if !__x86_64__ return; #endif uint16_t count = *((const uint16_t *) chtmtag + 1); const wchar_t **ptr = (const wchar_t **)((const uint8_t *) chtmtag + 32); while (count-- != 0) { const wchar_t *key = ptr[0]; uintptr_t keylen = (uintptr_t) ptr[1]; const wchar_t *value = ptr[2]; uintptr_t valuelen = (uintptr_t) ptr[3]; char *utf8key = utf8_wstring(key, keylen); char *utf8val = utf8_wstring(value, valuelen); uint32_t utf8vallen = *(uint32_t *) utf8val; bson_append_binary(b, utf8key+4, BSON_BIN_BINARY, utf8val+4, utf8vallen); mem_free(utf8val); ptr += 40 / sizeof(uintptr_t); } }
char *copy_utf8_wstring(const wchar_t *str, uint32_t length) { tls_copy_t *tls = copy_get_tls(); tls->active = 1; if(setjmp(tls->jb) == 0) { char *ret = utf8_wstring(str, length); tls->active = 0; return ret; } tls->active = 0; return NULL; }
static void log_wstring(const wchar_t *str, int length) { if (str == NULL) { bson_append_string_n( g_bson, g_istr, "", 0 ); return; } int ret; char * utf8s = utf8_wstring(str, length); int utf8len = * (int *) utf8s; ret = bson_append_binary( g_bson, g_istr, BSON_BIN_BINARY, utf8s+4, utf8len ); if (ret == BSON_ERROR) { char tmp[64]; snprintf(tmp, 64, "dbg bson err wstring %x utf8len %d", g_bson->err, utf8len); debug_message(tmp); } free(utf8s); }
int main() { pipe_init("\\\\.\\PIPE\\cuckoo", 0); hook_init(GetModuleHandle(NULL)); mem_init(); assert(native_init() == 0); uint8_t buf[16]; uint16_t val[2]; assert(utf8_encode(0x00000001, buf) == 1 && memcmp(buf, "\x01", 1) == 0); assert(utf8_encode(0x0000007f, buf) == 1 && memcmp(buf, "\x7f", 1) == 0); assert(utf8_encode(0x00000080, buf) == 2 && memcmp(buf, "\xc2\x80", 2) == 0); assert(utf8_encode(0x000007ff, buf) == 2 && memcmp(buf, "\xdf\xbf", 2) == 0); assert(utf8_encode(0x00000800, buf) == 3 && memcmp(buf, "\xe0\xa0\x80", 3) == 0); assert(utf8_encode(0x0000ffff, buf) == 3 && memcmp(buf, "\xef\xbf\xbf", 3) == 0); assert(utf8_encode(0x00010000, buf) == 4 && memcmp(buf, "\xf0\x90\x80\x80", 4) == 0); assert(utf8_encode(0x001fffff, buf) == 4 && memcmp(buf, "\xf7\xbf\xbf\xbf", 4) == 0); assert(utf8_encode(0x00200000, buf) == 5 && memcmp(buf, "\xf8\x88\x80\x80\x80", 5) == 0); assert(utf8_encode(0x03ffffff, buf) == 5 && memcmp(buf, "\xfb\xbf\xbf\xbf\xbf", 5) == 0); assert(utf8_encode(0x04000000, buf) == 6 && memcmp(buf, "\xfc\x84\x80\x80\x80\x80", 6) == 0); assert(utf8_encode(0x7fffffff, buf) == 6 && memcmp(buf, "\xfd\xbf\xbf\xbf\xbf\xbf", 6) == 0); // It's kind of a hassle to get utf8_wstring() to work here, so we'll just // do similar work with the byte count, which calculates the required // amount of bytes for a sequence. The following sequences represent the // various utf8 boundaries, i.e., their maximum values before needing an // extra byte etc. assert(utf8_bytecnt_unicode((val[0] = 0xd800, val[1] = 0xdc00, val), 2) == 1); assert(utf8_bytecnt_unicode((val[0] = 0xd801, val[1] = 0xdc00, val), 2) == 2); assert(utf8_bytecnt_unicode((val[0] = 0xd802, val[1] = 0xdc00, val), 2) == 3); assert(utf8_bytecnt_unicode((val[0] = 0xd840, val[1] = 0xdc00, val), 2) == 4); // We used to have some issues with signed chars and the MSB being set as // we wouldn't cast these as unsigned characters. This would result in // utf8_length(0xffffff81) returning -1, rather than utf8_length(0x81) // returning 2. Similarly to incorrect return values of utf8_length() we'd // also experience out-of-bounds writes as our buffer would be indexed by // -1, resulting in undefined behavior. assert(utf8_bytecnt_ascii("\x81", 1) == 2); assert(utf8_bytecnt_unicode(L"\u8081", 1) == 3); assert(memcmp(utf8_string("\x81", 1), "\x02\x00\x00\x00\xc2\x81", 6) == 0); assert(memcmp(utf8_wstring(L"\u8081", 1), "\x03\x00\x00\x00\xe8\x82\x81", 7) == 0); }
void log_wstring(bson *b, const char *idx, const wchar_t *str, int length) { if(str == NULL) { bson_append_string_n(b, idx, "", 0); return; } if(range_is_readable(str, length) != 0) { int ret, utf8len; char *utf8s = utf8_wstring(str, length); utf8len = *(int *) utf8s; ret = bson_append_binary(b, idx, BSON_BIN_BINARY, utf8s+4, utf8len); if(ret == BSON_ERROR) { pipe("CRITICAL:Error creating bson wstring, error %x, utf8len %d.", b->err, utf8len); } mem_free(utf8s); } else { bson_append_binary(b, idx, BSON_BIN_BINARY, "<INVALID POINTER>", 17); } }