|
9 | 9 |
|
10 | 10 | typedef int (*addNumberProc)(int, int); |
11 | 11 |
|
| 12 | +// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708) |
| 13 | +const char *sstrstr(const char *haystack, const char *needle, size_t length) { |
| 14 | + size_t needle_length = strlen(needle); |
| 15 | + size_t i; |
| 16 | + |
| 17 | + for (i = 0; i < length; i++) { |
| 18 | + if (i + needle_length > length) { |
| 19 | + return NULL; |
| 20 | + } |
| 21 | + |
| 22 | + if (strncmp(&haystack[i], needle, needle_length) == 0) { |
| 23 | + return &haystack[i]; |
| 24 | + } |
| 25 | + } |
| 26 | + return NULL; |
| 27 | +} |
| 28 | + |
| 29 | +const wchar_t *swcsstr(const wchar_t *haystack, const wchar_t *needle, size_t length) { |
| 30 | + size_t needle_length = wcslen(needle); |
| 31 | + size_t i; |
| 32 | + |
| 33 | + for (i = 0; i < length; i++) { |
| 34 | + if (i + needle_length > length) { |
| 35 | + return NULL; |
| 36 | + } |
| 37 | + |
| 38 | + if (wcsncmp(&haystack[i], needle, needle_length) == 0) { |
| 39 | + return &haystack[i]; |
| 40 | + } |
| 41 | + } |
| 42 | + return NULL; |
| 43 | +} |
| 44 | + |
| 45 | +BOOL CheckResourceStrings(LPVOID data, DWORD size, const char *first, const wchar_t *second) { |
| 46 | + const char *first_pos; |
| 47 | + const wchar_t *second_pos; |
| 48 | + const wchar_t *src; |
| 49 | + |
| 50 | + if (data == NULL || size == 0) { |
| 51 | + return FALSE; |
| 52 | + } |
| 53 | + |
| 54 | + first_pos = sstrstr((const char *) data, first, size); |
| 55 | + if (first_pos == NULL) { |
| 56 | + fprintf(stderr, "ERROR: data doesn't start with %s\n", first); |
| 57 | + return FALSE; |
| 58 | + } |
| 59 | + |
| 60 | + src = (const wchar_t *) (((const char *) data) + strlen(first) + 1); |
| 61 | + second_pos = swcsstr(src, second, (size - strlen(first) - 1) / sizeof(wchar_t)); |
| 62 | + if (second_pos == NULL) { |
| 63 | + fprintf(stderr, "ERROR: data doesn't continue with %S\n", second); |
| 64 | + return FALSE; |
| 65 | + } |
| 66 | + |
| 67 | + return TRUE; |
| 68 | +} |
| 69 | + |
12 | 70 | BOOL LoadFromMemory(char *filename) |
13 | 71 | { |
14 | 72 | FILE *fp; |
@@ -88,6 +146,64 @@ BOOL LoadFromMemory(char *filename) |
88 | 146 | result = FALSE; |
89 | 147 | } |
90 | 148 |
|
| 149 | + resourceInfo = MemoryFindResource(handle, _T("stringres"), RT_RCDATA); |
| 150 | + _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); |
| 151 | + if (resourceInfo != NULL) { |
| 152 | + resourceSize = MemorySizeofResource(handle, resourceInfo); |
| 153 | + resourceData = MemoryLoadResource(handle, resourceInfo); |
| 154 | + |
| 155 | + _tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); |
| 156 | + if (!CheckResourceStrings(resourceData, resourceSize, "This is a ANSI string", L"This is a UNICODE string")) { |
| 157 | + result = FALSE; |
| 158 | + } |
| 159 | + } else { |
| 160 | + result = FALSE; |
| 161 | + } |
| 162 | + |
| 163 | + resourceInfo = MemoryFindResource(handle, _T("stringres1"), RT_RCDATA); |
| 164 | + _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); |
| 165 | + if (resourceInfo != NULL) { |
| 166 | + resourceSize = MemorySizeofResource(handle, resourceInfo); |
| 167 | + resourceData = MemoryLoadResource(handle, resourceInfo); |
| 168 | + |
| 169 | + _tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); |
| 170 | + if (!CheckResourceStrings(resourceData, resourceSize, "This is ANSI string 1", L"This is UNICODE string 1")) { |
| 171 | + result = FALSE; |
| 172 | + } |
| 173 | + } else { |
| 174 | + result = FALSE; |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + resourceInfo = MemoryFindResource(handle, _T("stringres2"), RT_RCDATA); |
| 179 | + _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); |
| 180 | + if (resourceInfo != NULL) { |
| 181 | + resourceSize = MemorySizeofResource(handle, resourceInfo); |
| 182 | + resourceData = MemoryLoadResource(handle, resourceInfo); |
| 183 | + |
| 184 | + _tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); |
| 185 | + if (!CheckResourceStrings(resourceData, resourceSize, "This is ANSI string 2", L"This is UNICODE string 2")) { |
| 186 | + result = FALSE; |
| 187 | + } |
| 188 | + } else { |
| 189 | + result = FALSE; |
| 190 | + } |
| 191 | + |
| 192 | + |
| 193 | + resourceInfo = MemoryFindResource(handle, _T("stringres3"), RT_RCDATA); |
| 194 | + _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); |
| 195 | + if (resourceInfo != NULL) { |
| 196 | + resourceSize = MemorySizeofResource(handle, resourceInfo); |
| 197 | + resourceData = MemoryLoadResource(handle, resourceInfo); |
| 198 | + |
| 199 | + _tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); |
| 200 | + if (!CheckResourceStrings(resourceData, resourceSize, "This is ANSI string 3", L"This is UNICODE string 3")) { |
| 201 | + result = FALSE; |
| 202 | + } |
| 203 | + } else { |
| 204 | + result = FALSE; |
| 205 | + } |
| 206 | + |
91 | 207 | MemoryFreeLibrary(handle); |
92 | 208 |
|
93 | 209 | exit: |
|
0 commit comments