client/tests/wide_utf8.c (2879B)
1 #include <assert.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <string.h> 5 #ifdef _WIN32 6 #include <windows.h> 7 #else 8 #include "linux_platform.h" 9 #include "guest_memory.h" 10 #endif 11 12 int main(void) 13 { 14 const uint16_t inputs[][16] = {{'i'}, {0}, {0x7f,0x80,0x7ff,0x800,0xd7ff,0xe000,0xffff}, 15 {0x20ac}, {0xd83d,0xde00}, {0xdbff,0xdfff}, {0xd800}, {0xdc00}, 16 {'a',0xd800,'b'}, {0xdc00,0xd800}, {0xd800,0xd800,0xdc00}, {'a',0,'b'}}; 17 const int counts[] = {1,1,7,1,2,2,1,1,3,2,3,3}; 18 #ifndef _WIN32 19 assert(guest_map("UTF conversion", 0x50000000, 0x30000)); 20 g_fs_base = 0x50000000; 21 #endif 22 uint32_t digest = 2166136261u; 23 for (unsigned sample = 0; sample < 12; ++sample) 24 for (unsigned terminated = 0; terminated < 3; ++terminated) 25 for (unsigned strict = 0; strict < 2; ++strict) 26 for (int capacity = 0; capacity <= 24; ++capacity) { 27 unsigned char output[32]; 28 memset(output, 0xa5, sizeof(output)); 29 int count = terminated ? -(int)terminated : counts[sample]; 30 uint32_t flags = strict ? 0x80 : 0; 31 #ifdef _WIN32 32 SetLastError(777); 33 #else 34 memcpy((void *)ADDR(0x5001fff9), inputs[sample], 32); 35 memcpy((void *)ADDR(0x5000fff9), output, 32); 36 MEM32(g_fs_base + 0x34) = 777; 37 g_esp = 0x50000300; 38 const uint32_t args[] = {RECOMP_RETADDR, 65001, flags, 0x5001fff9, count, 0x5000fff9, capacity, 0, 0}; 39 memcpy((void *)ADDR(g_esp), args, sizeof(args)); 40 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 41 #endif 42 43 #ifdef _WIN32 44 int result = WideCharToMultiByte(65001, flags, (const WCHAR *)inputs[sample], count, 45 (char *)output, capacity, NULL, NULL); 46 uint32_t error = GetLastError(); 47 #else 48 linux_platform_lookup("kernel32.dll", "WideCharToMultiByte")(); 49 int result = g_eax; 50 uint32_t error = MEM32(g_fs_base + 0x34); 51 memcpy(output, (void *)ADDR(0x5000fff9), 32); 52 #endif 53 54 if (result) assert(error == 777); 55 else assert(error == 122 || error == 1113); 56 for (unsigned i = capacity; i < sizeof(output); ++i) assert(output[i] == 0xa5); 57 if (!sample && capacity) assert(result == (terminated ? 2 : 1) || capacity == 1); 58 if (!sample && capacity >= 2) assert(output[0] == 'i' && output[1] == (terminated ? 0 : 0xa5)); 59 #ifndef _WIN32 60 assert(g_esp == 0x50000324 && g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 61 assert(!memcmp((void *)ADDR(0x5001fff9), inputs[sample], 32)); 62 #endif 63 digest = (digest ^ result) * 16777619u; 64 digest = (digest ^ error) * 16777619u; 65 for (unsigned i = 0; i < sizeof(output); ++i) digest = (digest ^ output[i]) * 16777619u; 66 } 67 printf("WideCharToMultiByte UTF-8: 1800 length/capacity/surrogate cases, bytes, errors, canaries and ABI; digest %08x\n", digest); 68 }