Add CRYPT_STRING_HEX format implementation for CryptStringToBinaryW()
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=48487 Signed-off-by: Aaro Altonen a.altonen@hotmail.com --- v2: - remove wcstok() and string duplication entirely - properly check wchars (thanks Nikolay) --- dlls/crypt32/base64.c | 76 +++++++++++++++++++++++++++++++++++ dlls/crypt32/tests/base64.c | 80 ++++++++++++++++++------------------- 2 files changed, 116 insertions(+), 40 deletions(-)
diff --git a/dlls/crypt32/base64.c b/dlls/crypt32/base64.c index 73619421ab..ac2ec54334 100644 --- a/dlls/crypt32/base64.c +++ b/dlls/crypt32/base64.c @@ -1036,6 +1036,80 @@ static LONG DecodeAnyW(LPCWSTR pszString, DWORD cchString, return ret; }
+static BYTE char_to_byte(WCHAR c) +{ + BYTE lb = c & 0xff; + + /* upper byte is allowed to be 0xff */ + if (c >> 8 != 0xff && c >> 8 != 0x00) + return 0xff; + + if (lb >= '0' && lb <= '9') + return lb - '0'; + if (lb >= 'a' && lb <= 'f') + return lb - 'a' + 10; + if (lb >= 'A' && lb <= 'F') + return lb - 'A' + 10; + + return 0xff; +} + +static LONG DecodeHexToBinaryW(const WCHAR *instr, DWORD slen, + BYTE *out, DWORD *outlen, DWORD *skip, DWORD *flags) +{ + BYTE un, ln; + const WCHAR delim[] = { ' ', '\n', '\r', '\t' }; + const WCHAR *ptr = instr; + LONG ret = ERROR_SUCCESS; + DWORD i = 0, processed = 0; + + if (!outlen) + return ERROR_INVALID_PARAMETER; + + if (skip) + *skip = 0; + + while (i < slen) { + while (strchrW(delim, *ptr) && *ptr) ptr++, i++; + + if (i == slen) + break; + + if (i + 2 > slen) + { + if (!out) + *outlen = 0; + return ERROR_INVALID_DATA; + } + + un = char_to_byte(*ptr); + ln = char_to_byte(*(ptr + 1)); + + if (un == 0xff || ln == 0xff) + { + if (!out) + *outlen = 0; + return ERROR_INVALID_DATA; + } + + if (processed >= *outlen) + { + if (out) + return ERROR_MORE_DATA; + ret = ERROR_MORE_DATA; + } + else if (out) + out[processed] = (un << 4) | ln; + + processed++; + ptr += 2; + i += 2; + } + + *outlen = processed; + return ret; +} + BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString, DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags) @@ -1081,6 +1155,8 @@ BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString, decoder = DecodeAnyW; break; case CRYPT_STRING_HEX: + decoder = DecodeHexToBinaryW; + break; case CRYPT_STRING_HEXASCII: case CRYPT_STRING_HEXADDR: case CRYPT_STRING_HEXASCIIADDR: diff --git a/dlls/crypt32/tests/base64.c b/dlls/crypt32/tests/base64.c index 845b5b28f3..5a65b516ca 100644 --- a/dlls/crypt32/tests/base64.c +++ b/dlls/crypt32/tests/base64.c @@ -816,8 +816,8 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len); heap_free(input);
/* length is uneven -> 13 */ @@ -826,8 +826,8 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 3, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len); heap_free(input);
/* invalid 0x prefix -> 13 */ @@ -836,8 +836,8 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len); heap_free(input);
/* invalid characters -> 13 */ @@ -846,8 +846,8 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len); heap_free(input);
/* insufficient buffer -> 234 */ @@ -861,9 +861,9 @@ static void testStringToBinaryW(void) input = strdupAtoW("213c73796d6c696e6b3efffe"); ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_MORE_DATA, "Got %d, expected 234\n", GetLastError()); + ok(GetLastError() == ERROR_MORE_DATA, "Got %d, expected 234\n", GetLastError()); ok(data_len == 4, "Got %u, expected 4\n", data_len); - todo_wine ok(!memcmp(out, expected, 4), "Invalid output from CryptStringToBinaryW()!\n"); + ok(!memcmp(out, expected, 4), "Invalid output from CryptStringToBinaryW()!\n"); heap_free(input);
/* valid data */ @@ -871,9 +871,9 @@ static void testStringToBinaryW(void) input = strdupAtoW("213c73796d6c696e6b3efffe"); data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 12, "Got %u, expected 12\n", data_len); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 12, "Got %u, expected 12\n", data_len); heap_free(input);
/* valid data with white spaces */ @@ -882,9 +882,9 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; SetLastError(0xdeadbeef); ret = CryptStringToBinaryW(input, 25, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %d, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 7, "Got %u, expected 7\n", data_len); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %d, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 7, "Got %u, expected 7\n", data_len); heap_free(input);
/* valid data with white spaces but spacing breaks the valid data into invalid chunks */ @@ -893,8 +893,8 @@ static void testStringToBinaryW(void) data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, NULL, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len); heap_free(input);
/* if "input" contains both valid and invalid data and "out" is valid, "out" shall contain all valid bytes @@ -905,7 +905,7 @@ static void testStringToBinaryW(void) input = strdupAtoW("21 3 c ff"); ret = CryptStringToBinaryW(input, 0, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); ok(data_len == 4, "Got %u, expected 4\n", data_len); heap_free(input);
@@ -918,10 +918,10 @@ static void testStringToBinaryW(void) input = strdupAtoW("213c73796d6c696e6b3efffe"); data_len = 256; ret = CryptStringToBinaryW(input, 24, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 12, "Got %u, expected 12\n", data_len); - todo_wine ok(!memcmp(out, expected, 12), "Invalid output from CryptStringToBinaryW()!\n"); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 12, "Got %u, expected 12\n", data_len); + ok(!memcmp(out, expected, 12), "Invalid output from CryptStringToBinaryW()!\n"); heap_free(input);
/* invalid data but length small enough that it's never detected */ @@ -929,9 +929,9 @@ static void testStringToBinaryW(void) input = strdupAtoW("abcdefhhh"); data_len = 0xdeadbeef; ret = CryptStringToBinaryW(input, 4, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 2, "Got %u, expected 2\n", data_len); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 2, "Got %u, expected 2\n", data_len); heap_free(input);
/* invalid data but length small enough that it's never detected, with whitespaces */ @@ -940,33 +940,33 @@ static void testStringToBinaryW(void) input = strdupAtoW("\t\t21 fe f f f"); data_len = 256; ret = CryptStringToBinaryW(input, 5, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 1, "Got %u, expected 1\n", data_len); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 1, "Got %u, expected 1\n", data_len); heap_free(input);
SetLastError(0xdeadbeef); data_len = 0xdeadbeef; ret = CryptStringToBinaryW(winput1, 6, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL); - todo_wine ok(ret || broken(!ret), "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef || broken(GetLastError() == 122), + ok(ret || broken(!ret), "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef || broken(GetLastError() == 122), "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 3 || broken(data_len == 0xdeadbeef), "Got %u, expected 3\n", data_len); + ok(data_len == 3 || broken(data_len == 0xdeadbeef), "Got %u, expected 3\n", data_len);
SetLastError(0xdeadbeef); data_len = 0xdeadbeef; ret = CryptStringToBinaryW(winput2, 6, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0, "Got %u, expected 0\n", data_len); + ok(GetLastError() == ERROR_INVALID_DATA, "Got %d, expected 13\n", GetLastError()); + ok(data_len == 0, "Got %u, expected 0\n", data_len);
SetLastError(0xdeadbeef); data_len = 0xdeadbeef; ret = CryptStringToBinaryW(winput3, 6, CRYPT_STRING_HEX, NULL, &data_len, NULL, NULL); ok(!ret, "Got %u, expected zero\n", ret); - todo_wine ok(GetLastError() == ERROR_INVALID_DATA || broken(GetLastError() == 122), + ok(GetLastError() == ERROR_INVALID_DATA || broken(GetLastError() == 122), "Got %d, expected 13\n", GetLastError()); - todo_wine ok(data_len == 0 || broken(data_len == 0xdeadbeef), "Got %u, expected 0\n", data_len); + ok(data_len == 0 || broken(data_len == 0xdeadbeef), "Got %u, expected 0\n", data_len);
/* valid data but parse only the first 6 bytes (12 chars) */ SetLastError(0xdeadbeef); @@ -976,10 +976,10 @@ static void testStringToBinaryW(void) input = strdupAtoW("213c73796d6c696e6b3efffe"); data_len = 256; ret = CryptStringToBinaryW(input, 12, CRYPT_STRING_HEX, out, &data_len, &skip_, &flags); - todo_wine ok(ret, "Got %u, expected one\n", ret); - todo_wine ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); - todo_wine ok(data_len == 6, "Got %u, expected 6\n", data_len); - todo_wine ok(!memcmp(out, expected, 6), "Invalid output from CryptStringToBinaryW()!\n"); + ok(ret, "Got %u, expected one\n", ret); + ok(GetLastError() == 0xdeadbeef, "Got %x, expected 0xdeadbeef\n", GetLastError()); + ok(data_len == 6, "Got %u, expected 6\n", data_len); + ok(!memcmp(out, expected, 6), "Invalid output from CryptStringToBinaryW()!\n"); heap_free(input); }