ReactOS 0.4.15-dev-7842-g558ab78
base64.c File Reference
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include <wincrypt.h>
#include "wine/heap.h"
#include "wine/test.h"
Include dependency graph for base64.c:

Go to the source code of this file.

Classes

struct  BinTests
 
struct  BadString
 

Macros

#define CERT_HEADER   "-----BEGIN CERTIFICATE-----\r\n"
 
#define ALT_CERT_HEADER   "-----BEGIN This is some arbitrary text that goes on and on-----\r\n"
 
#define CERT_TRAILER   "-----END CERTIFICATE-----\r\n"
 
#define ALT_CERT_TRAILER   "-----END More arbitrary text------\r\n"
 
#define CERT_REQUEST_HEADER   "-----BEGIN NEW CERTIFICATE REQUEST-----\r\n"
 
#define CERT_REQUEST_TRAILER   "-----END NEW CERTIFICATE REQUEST-----\r\n"
 
#define X509_HEADER   "-----BEGIN X509 CRL-----\r\n"
 
#define X509_TRAILER   "-----END X509 CRL-----\r\n"
 
#define CERT_HEADER_NOCR   "-----BEGIN CERTIFICATE-----\n"
 
#define CERT_TRAILER_NOCR   "-----END CERTIFICATE-----\n"
 
#define CERT_REQUEST_HEADER_NOCR   "-----BEGIN NEW CERTIFICATE REQUEST-----\n"
 
#define CERT_REQUEST_TRAILER_NOCR   "-----END NEW CERTIFICATE REQUEST-----\n"
 
#define X509_HEADER_NOCR   "-----BEGIN X509 CRL-----\n"
 
#define X509_TRAILER_NOCR   "-----END X509 CRL-----\n"
 

Functions

static WCHARstrdupAtoW (const char *str)
 
static void encodeAndCompareBase64_A (const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const char *expected, const char *header, const char *trailer)
 
static void encode_compare_base64_W (const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const WCHAR *expected, const char *header, const char *trailer)
 
static void test_CryptBinaryToString (void)
 
static void decodeAndCompareBase64_A (LPCSTR toDecode, LPCSTR header, LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected, DWORD expectedLen)
 
static void decodeBase64WithLenFmtW (LPCSTR strA, int len, DWORD fmt, BOOL retA, const BYTE *bufA, DWORD bufLenA, DWORD fmtUsedA)
 
static void decodeBase64WithLenFmt (LPCSTR str, int len, DWORD fmt, LPCSTR expected, int le, BOOL isBroken)
 
static void decodeBase64WithLenBroken (LPCSTR str, int len, LPCSTR expected, int le)
 
static void decodeBase64WithLen (LPCSTR str, int len, LPCSTR expected, int le)
 
static void decodeBase64WithFmt (LPCSTR str, DWORD fmt, LPCSTR expected, int le)
 
static void testStringToBinaryA (void)
 
 START_TEST (base64)
 

Variables

static const BYTE toEncode1 [] = { 0 }
 
static const BYTE toEncode2 [] = { 1,2 }
 
static const BYTE toEncode4 []
 
static const BYTE toEncode5 []
 
static const struct BinTests tests []
 
static const struct BinTests testsNoCR []
 
static const struct BadString badStrings []
 

Macro Definition Documentation

◆ ALT_CERT_HEADER

#define ALT_CERT_HEADER   "-----BEGIN This is some arbitrary text that goes on and on-----\r\n"

Definition at line 30 of file base64.c.

◆ ALT_CERT_TRAILER

#define ALT_CERT_TRAILER   "-----END More arbitrary text------\r\n"

Definition at line 32 of file base64.c.

◆ CERT_HEADER

#define CERT_HEADER   "-----BEGIN CERTIFICATE-----\r\n"

Definition at line 29 of file base64.c.

◆ CERT_HEADER_NOCR

#define CERT_HEADER_NOCR   "-----BEGIN CERTIFICATE-----\n"

Definition at line 37 of file base64.c.

◆ CERT_REQUEST_HEADER

#define CERT_REQUEST_HEADER   "-----BEGIN NEW CERTIFICATE REQUEST-----\r\n"

Definition at line 33 of file base64.c.

◆ CERT_REQUEST_HEADER_NOCR

#define CERT_REQUEST_HEADER_NOCR   "-----BEGIN NEW CERTIFICATE REQUEST-----\n"

Definition at line 39 of file base64.c.

◆ CERT_REQUEST_TRAILER

#define CERT_REQUEST_TRAILER   "-----END NEW CERTIFICATE REQUEST-----\r\n"

Definition at line 34 of file base64.c.

◆ CERT_REQUEST_TRAILER_NOCR

#define CERT_REQUEST_TRAILER_NOCR   "-----END NEW CERTIFICATE REQUEST-----\n"

Definition at line 40 of file base64.c.

◆ CERT_TRAILER

#define CERT_TRAILER   "-----END CERTIFICATE-----\r\n"

Definition at line 31 of file base64.c.

◆ CERT_TRAILER_NOCR

#define CERT_TRAILER_NOCR   "-----END CERTIFICATE-----\n"

Definition at line 38 of file base64.c.

◆ X509_HEADER

#define X509_HEADER   "-----BEGIN X509 CRL-----\r\n"

Definition at line 35 of file base64.c.

◆ X509_HEADER_NOCR

#define X509_HEADER_NOCR   "-----BEGIN X509 CRL-----\n"

Definition at line 41 of file base64.c.

◆ X509_TRAILER

#define X509_TRAILER   "-----END X509 CRL-----\r\n"

Definition at line 36 of file base64.c.

◆ X509_TRAILER_NOCR

#define X509_TRAILER_NOCR   "-----END X509 CRL-----\n"

Definition at line 42 of file base64.c.

Function Documentation

◆ decodeAndCompareBase64_A()

static void decodeAndCompareBase64_A ( LPCSTR  toDecode,
LPCSTR  header,
LPCSTR  trailer,
DWORD  useFormat,
DWORD  expectedFormat,
const BYTE expected,
DWORD  expectedLen 
)
static

Definition at line 366 of file base64.c.

369{
370 static const char garbage[] = "garbage\r\n";
371 LPSTR str;
372 DWORD len = strlen(toDecode) + strlen(garbage) + 1;
373
374 if (header)
375 len += strlen(header);
376 if (trailer)
377 len += strlen(trailer);
379 if (str)
380 {
381 LPBYTE buf;
382 DWORD bufLen = 0;
383 BOOL ret;
384
385 if (header)
386 strcpy(str, header);
387 else
388 *str = 0;
389 strcat(str, toDecode);
390 if (trailer)
391 strcat(str, trailer);
392 ret = CryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
393 NULL);
394 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
395 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
396 if (buf)
397 {
398 DWORD skipped, usedFormat;
399
400 /* check as normal, make sure last two parameters are optional */
401 ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen, NULL,
402 NULL);
403 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
404 ok(bufLen == expectedLen,
405 "Expected length %d, got %d\n", expectedLen, bufLen);
406 ok(!memcmp(buf, expected, bufLen), "Unexpected value\n");
407 /* check last two params */
408 ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
409 &skipped, &usedFormat);
410 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
411 ok(skipped == 0, "Expected skipped 0, got %d\n", skipped);
412 ok(usedFormat == expectedFormat, "Expected format %d, got %d\n",
413 expectedFormat, usedFormat);
415 }
416
417 /* Check again, but with garbage up front */
418 strcpy(str, garbage);
419 if (header)
420 strcat(str, header);
421 strcat(str, toDecode);
422 if (trailer)
423 strcat(str, trailer);
424 ret = CryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
425 NULL);
426 /* expect failure with no header, and success with one */
427 if (header)
428 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
429 else
431 "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret, GetLastError());
432 if (ret)
433 {
434 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
435 if (buf)
436 {
437 DWORD skipped, usedFormat;
438
439 ret = CryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
440 &skipped, &usedFormat);
441 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
442 ok(skipped == strlen(garbage),
443 "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
444 lstrlenA(garbage), str, useFormat, skipped, usedFormat);
446 }
447 }
449 }
450}
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ok(value,...)
Definition: atltest.h:57
#define NULL
Definition: types.h:112
BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString, DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
Definition: base64.c:772
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLsizei len
Definition: glext.h:6722
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
BOOL expected
Definition: store.c:2063
const WCHAR * str
unsigned char * LPBYTE
Definition: typedefs.h:53
int ret
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_INVALID_DATA
Definition: winerror.h:116
char * LPSTR
Definition: xmlstorage.h:182

Referenced by testStringToBinaryA().

◆ decodeBase64WithFmt()

static void decodeBase64WithFmt ( LPCSTR  str,
DWORD  fmt,
LPCSTR  expected,
int  le 
)
static

Definition at line 501 of file base64.c.

502{
504}
#define FALSE
Definition: types.h:117
static void decodeBase64WithLenFmt(LPCSTR str, int len, DWORD fmt, LPCSTR expected, int le, BOOL isBroken)
Definition: base64.c:466
Definition: dsound.c:943

Referenced by testStringToBinaryA().

◆ decodeBase64WithLen()

static void decodeBase64WithLen ( LPCSTR  str,
int  len,
LPCSTR  expected,
int  le 
)
static

Definition at line 496 of file base64.c.

497{
499}
#define CRYPT_STRING_BASE64
Definition: wincrypt.h:2985

Referenced by testStringToBinaryA().

◆ decodeBase64WithLenBroken()

static void decodeBase64WithLenBroken ( LPCSTR  str,
int  len,
LPCSTR  expected,
int  le 
)
static

Definition at line 491 of file base64.c.

492{
494}
#define TRUE
Definition: types.h:120

Referenced by testStringToBinaryA().

◆ decodeBase64WithLenFmt()

static void decodeBase64WithLenFmt ( LPCSTR  str,
int  len,
DWORD  fmt,
LPCSTR  expected,
int  le,
BOOL  isBroken 
)
static

Definition at line 466 of file base64.c.

467{
468 BYTE buf[8] = {0};
469 DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
470 BOOL ret;
471 SetLastError(0xdeadbeef);
472 ret = CryptStringToBinaryA(str, len, fmt, buf, &bufLen, NULL, &fmtUsed);
473 buf[bufLen] = 0;
474 if (expected) {
475 BOOL correct = ret && strcmp(expected, (char*)buf) == 0;
476 ok(correct || (isBroken && broken(!ret)),
477 "base64 \"%s\" len %d: expected \"%s\", got \"%s\" (ret %d, le %d)\n",
478 str, len, expected, (char*)buf, ret, GetLastError());
479 if (correct)
480 ok(fmtUsed == fmt, "base64 \"%s\" len %d: expected fmt %d, used %d\n",
481 str, len, fmt, fmtUsed);
482 } else {
483 ok(!ret && GetLastError() == le,
484 "base64 \"%s\" len %d: expected failure, got \"%s\" (ret %d, le %d)\n",
485 str, len, (char*)buf, ret, GetLastError());
486 }
487
488 decodeBase64WithLenFmtW(str, len, fmt, ret, buf, bufLen, fmtUsed);
489}
#define broken(x)
Definition: _sntprintf.h:21
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define SetLastError(x)
Definition: compat.h:752
static void decodeBase64WithLenFmtW(LPCSTR strA, int len, DWORD fmt, BOOL retA, const BYTE *bufA, DWORD bufLenA, DWORD fmtUsedA)
Definition: base64.c:452
unsigned char BYTE
Definition: xxhash.c:193

Referenced by decodeBase64WithFmt(), decodeBase64WithLen(), and decodeBase64WithLenBroken().

◆ decodeBase64WithLenFmtW()

static void decodeBase64WithLenFmtW ( LPCSTR  strA,
int  len,
DWORD  fmt,
BOOL  retA,
const BYTE bufA,
DWORD  bufLenA,
DWORD  fmtUsedA 
)
static

Definition at line 452 of file base64.c.

454{
455 BYTE buf[8] = {0};
456 DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
457 BOOL ret;
458 WCHAR strW[64];
459 int i;
460 for (i = 0; (strW[i] = strA[i]) != 0; ++i);
461 ret = CryptStringToBinaryW(strW, len, fmt, buf, &bufLen, NULL, &fmtUsed);
462 ok(ret == retA && bufLen == bufLenA && memcmp(bufA, buf, bufLen) == 0
463 && fmtUsed == fmtUsedA, "base64 \"%s\" len %d: W and A differ\n", strA, len);
464}
BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString, DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
Definition: base64.c:983
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
WCHAR strW[12]
Definition: clipboard.c:2029
char strA[12]
Definition: clipboard.c:2028
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by decodeBase64WithLenFmt().

◆ encode_compare_base64_W()

static void encode_compare_base64_W ( const BYTE toEncode,
DWORD  toEncodeLen,
DWORD  format,
const WCHAR expected,
const char header,
const char trailer 
)
static

Definition at line 166 of file base64.c.

168{
169 WCHAR *headerW, *trailerW, required;
170 DWORD strLen, strLen2;
171 WCHAR *strW = NULL;
172 const WCHAR *ptr;
173 BOOL ret;
174
175 required = lstrlenW(expected) + 1;
176 if (header)
177 required += strlen(header);
178 if (trailer)
179 required += strlen(trailer);
180
181 strLen = 0;
182 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, NULL, &strLen);
183 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
184 ok(strLen == required, "Unexpected required length %u, expected %u.\n", strLen, required);
185
186 /* Same call with non-zero length value. */
187 strLen2 = strLen;
188 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, NULL, &strLen2);
189 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
190 ok(strLen == strLen2, "Unexpected required length.\n");
191
192 strLen2 = strLen - 1;
193 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, NULL, &strLen2);
194 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
195 ok(strLen == strLen2, "Unexpected required length.\n");
196
197 strLen2 = strLen - 1;
198 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, NULL, &strLen2);
199 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
200 ok(strLen == strLen2, "Unexpected required length.\n");
201
202 strW = heap_alloc(strLen * sizeof(WCHAR));
203
204 headerW = strdupAtoW(header);
205 trailerW = strdupAtoW(trailer);
206
207 strLen2 = strLen - 1;
208 strW[0] = 0x1234;
209 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, strW, &strLen2);
211 ok((!ret && GetLastError() == ERROR_MORE_DATA) || broken(ret) /* XP */, "CryptBinaryToStringW failed, %d, error %d\n",
212 ret, GetLastError());
213 if (headerW)
214 ok(strW[0] == 0x1234, "Unexpected buffer contents %#x.\n", strW[0]);
215 else
216 ok(strW[0] == 0x1234 || broken(strW[0] != 0x1234) /* XP */, "Unexpected buffer contents %#x.\n", strW[0]);
217
218 strLen2 = strLen;
219 ret = CryptBinaryToStringW(toEncode, toEncodeLen, format, strW, &strLen2);
220 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
221
222 ok(strLen2 == strLen - 1, "Expected length %d, got %d\n", strLen - 1, strLen);
223
224 ptr = strW;
225 if (headerW)
226 {
227 ok(!memcmp(headerW, ptr, lstrlenW(headerW)), "Expected header %s, got %s.\n", wine_dbgstr_w(headerW),
229 ptr += lstrlenW(headerW);
230 }
231 ok(!memcmp(expected, ptr, lstrlenW(expected)), "Expected %s, got %s.\n", wine_dbgstr_w(expected),
234 if (trailerW)
235 ok(!memcmp(trailerW, ptr, lstrlenW(trailerW)), "Expected trailer %s, got %s.\n", wine_dbgstr_w(trailerW),
237
239 heap_free(headerW);
240 heap_free(trailerW);
241}
static void * heap_alloc(size_t len)
Definition: appwiz.h:66
static BOOL heap_free(void *mem)
Definition: appwiz.h:76
#define ERROR_MORE_DATA
Definition: dderror.h:13
BOOL WINAPI CryptBinaryToStringW(const BYTE *pbBinary, DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
Definition: base64.c:473
#define lstrlenW
Definition: compat.h:750
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
#define wine_dbgstr_w
Definition: kernel32.h:34
static PVOID ptr
Definition: dispmode.c:27
static WCHAR * strdupAtoW(const char *str)
Definition: base64.c:89
#define todo_wine
Definition: custom.c:79

Referenced by test_CryptBinaryToString().

◆ encodeAndCompareBase64_A()

static void encodeAndCompareBase64_A ( const BYTE toEncode,
DWORD  toEncodeLen,
DWORD  format,
const char expected,
const char header,
const char trailer 
)
static

Definition at line 102 of file base64.c.

104{
105 DWORD strLen, strLen2, required;
106 const char *ptr;
107 LPSTR str = NULL;
108 BOOL ret;
109
110 required = strlen(expected) + 1;
111 if (header)
112 required += strlen(header);
113 if (trailer)
114 required += strlen(trailer);
115
116 strLen = 0;
117 ret = CryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen);
118 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
119 ok(strLen == required, "Unexpected required length %u, expected %u.\n", required, strLen);
120
121 strLen2 = strLen;
122 ret = CryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen2);
123 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
124 ok(strLen == strLen2, "Unexpected required length.\n");
125
126 strLen2 = strLen - 1;
127 ret = CryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen2);
128 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
129 ok(strLen == strLen2, "Unexpected required length.\n");
130
131 str = heap_alloc(strLen);
132
133 /* Partially filled output buffer. */
134 strLen2 = strLen - 1;
135 str[0] = 0x12;
136 ret = CryptBinaryToStringA(toEncode, toEncodeLen, format, str, &strLen2);
138 ok((!ret && GetLastError() == ERROR_MORE_DATA) || broken(ret) /* XP */, "CryptBinaryToStringA failed %d, error %d.\n",
139 ret, GetLastError());
140 ok(strLen2 == strLen || broken(strLen2 == strLen - 1), "Expected length %d, got %d\n", strLen - 1, strLen);
141todo_wine {
142 if (header)
143 ok(str[0] == header[0], "Unexpected buffer contents %#x.\n", str[0]);
144 else
145 ok(str[0] == expected[0], "Unexpected buffer contents %#x.\n", str[0]);
146}
147 strLen2 = strLen;
148 ret = CryptBinaryToStringA(toEncode, toEncodeLen, format, str, &strLen2);
149 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
150 ok(strLen2 == strLen - 1, "Expected length %d, got %d\n", strLen - 1, strLen);
151
152 ptr = str;
153 if (header)
154 {
155 ok(!strncmp(header, ptr, strlen(header)), "Expected header %s, got %s\n", header, ptr);
156 ptr += strlen(header);
157 }
158 ok(!strncmp(expected, ptr, strlen(expected)), "Expected %s, got %s\n", expected, ptr);
159 ptr += strlen(expected);
160 if (trailer)
161 ok(!strncmp(trailer, ptr, strlen(trailer)), "Expected trailer %s, got %s\n", trailer, ptr);
162
163 heap_free(str);
164}
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
BOOL WINAPI CryptBinaryToStringA(const BYTE *pbBinary, DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
Definition: base64.c:253

Referenced by test_CryptBinaryToString().

◆ START_TEST()

START_TEST ( base64  )

Definition at line 702 of file base64.c.

703{
706}
static void testStringToBinaryA(void)
Definition: base64.c:516
static void test_CryptBinaryToString(void)
Definition: base64.c:243

◆ strdupAtoW()

static WCHAR * strdupAtoW ( const char str)
static

Definition at line 89 of file base64.c.

90{
91 WCHAR *ret = NULL;
92 DWORD len;
93
94 if (!str) return ret;
95 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
96 ret = heap_alloc(len * sizeof(WCHAR));
97 if (ret)
99 return ret;
100}
#define CP_ACP
Definition: compat.h:109
#define MultiByteToWideChar
Definition: compat.h:110

Referenced by encode_compare_base64_W(), and test_CryptBinaryToString().

◆ test_CryptBinaryToString()

static void test_CryptBinaryToString ( void  )
static

Definition at line 243 of file base64.c.

244{
245 DWORD strLen, strLen2, i;
246 BOOL ret;
247
250 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
251
252 strLen = 123;
253 ret = CryptBinaryToStringA(NULL, 0, 0, NULL, &strLen);
255 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
256 ok(strLen == 123, "Unexpected length.\n");
257
258 if (0)
260
261 strLen = 123;
262 ret = CryptBinaryToStringW(NULL, 0, 0, NULL, &strLen);
263 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER, "Unexpected error %d\n", GetLastError());
264 ok(strLen == 123, "Unexpected length.\n");
265
266 for (i = 0; i < ARRAY_SIZE(tests); i++)
267 {
268 WCHAR *strW, *encodedW;
269 LPSTR str = NULL;
270 BOOL ret;
271
272 strLen = 0;
273 ret = CryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BINARY, NULL, &strLen);
274 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
275 ok(strLen == tests[i].toEncodeLen, "Unexpected required length %u.\n", strLen);
276
277 strLen2 = strLen;
278 str = heap_alloc(strLen);
279 ret = CryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BINARY, str, &strLen2);
280 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
281 ok(strLen == strLen2, "Expected length %u, got %u\n", strLen, strLen2);
282 ok(!memcmp(str, tests[i].toEncode, tests[i].toEncodeLen), "Unexpected value\n");
283 heap_free(str);
284
285 strLen = 0;
286 ret = CryptBinaryToStringW(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BINARY, NULL, &strLen);
287 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
288 ok(strLen == tests[i].toEncodeLen, "Unexpected required length %u.\n", strLen);
289
290 strLen2 = strLen;
291 strW = heap_alloc(strLen);
292 ret = CryptBinaryToStringW(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BINARY, strW, &strLen2);
293 ok(ret, "CryptBinaryToStringW failed: %d\n", GetLastError());
294 ok(strLen == strLen2, "Expected length %u, got %u\n", strLen, strLen2);
295 ok(!memcmp(strW, tests[i].toEncode, tests[i].toEncodeLen), "Unexpected value\n");
297
299 tests[i].base64, NULL, NULL);
301 tests[i].base64, CERT_HEADER, CERT_TRAILER);
305 tests[i].base64, X509_HEADER, X509_TRAILER);
306
307 encodedW = strdupAtoW(tests[i].base64);
308
309 encode_compare_base64_W(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BASE64, encodedW, NULL, NULL);
310 encode_compare_base64_W(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BASE64HEADER, encodedW,
314 encode_compare_base64_W(tests[i].toEncode, tests[i].toEncodeLen, CRYPT_STRING_BASE64X509CRLHEADER, encodedW,
316
317 heap_free(encodedW);
318 }
319
320 for (i = 0; i < ARRAY_SIZE(testsNoCR); i++)
321 {
322 LPSTR str = NULL;
323 WCHAR *encodedW;
324 BOOL ret;
325
326 ret = CryptBinaryToStringA(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
328 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
329
330 strLen2 = strLen;
331 str = heap_alloc(strLen);
332 ret = CryptBinaryToStringA(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
334 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
335 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen, strLen2);
336 ok(!memcmp(str, testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen), "Unexpected value\n");
337 heap_free(str);
338
340 testsNoCR[i].base64, NULL, NULL);
341 encodeAndCompareBase64_A(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
343 encodeAndCompareBase64_A(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
346 encodeAndCompareBase64_A(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
348
349 encodedW = strdupAtoW(testsNoCR[i].base64);
350
351 encode_compare_base64_W(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
353 encode_compare_base64_W(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
355 encode_compare_base64_W(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
358 encode_compare_base64_W(testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen,
361
362 heap_free(encodedW);
363 }
364}
#define ARRAY_SIZE(A)
Definition: main.h:33
#define X509_TRAILER
Definition: base64.c:40
#define CERT_TRAILER
Definition: base64.c:35
#define CERT_REQUEST_HEADER
Definition: base64.c:37
#define X509_HEADER
Definition: base64.c:39
#define CERT_HEADER
Definition: base64.c:32
#define CERT_REQUEST_TRAILER
Definition: base64.c:38
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
static void encodeAndCompareBase64_A(const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const char *expected, const char *header, const char *trailer)
Definition: base64.c:102
static const struct BinTests tests[]
Definition: base64.c:61
#define CERT_REQUEST_HEADER_NOCR
Definition: base64.c:39
#define X509_HEADER_NOCR
Definition: base64.c:41
#define CERT_TRAILER_NOCR
Definition: base64.c:38
static void encode_compare_base64_W(const BYTE *toEncode, DWORD toEncodeLen, DWORD format, const WCHAR *expected, const char *header, const char *trailer)
Definition: base64.c:166
#define CERT_HEADER_NOCR
Definition: base64.c:37
static const struct BinTests testsNoCR[]
Definition: base64.c:75
#define CERT_REQUEST_TRAILER_NOCR
Definition: base64.c:40
#define X509_TRAILER_NOCR
Definition: base64.c:42
#define CRYPT_STRING_BINARY
Definition: wincrypt.h:2986
#define CRYPT_STRING_BASE64X509CRLHEADER
Definition: wincrypt.h:2993
#define CRYPT_STRING_BASE64REQUESTHEADER
Definition: wincrypt.h:2987
#define CRYPT_STRING_BASE64HEADER
Definition: wincrypt.h:2984
#define CRYPT_STRING_NOCR
Definition: wincrypt.h:2997

Referenced by START_TEST().

◆ testStringToBinaryA()

static void testStringToBinaryA ( void  )
static

Definition at line 516 of file base64.c.

517{
518 BOOL ret;
519 DWORD bufLen = 0, i;
520 BYTE buf[8];
521
524 "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
525 ret = CryptStringToBinaryA(NULL, 0, 0, NULL, &bufLen, NULL, NULL);
527 "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
528 /* Bogus format */
529 ret = CryptStringToBinaryA(tests[0].base64, 0, 0, NULL, &bufLen, NULL,
530 NULL);
532 "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
533 /* Decoding doesn't expect the NOCR flag to be specified */
534 ret = CryptStringToBinaryA(tests[0].base64, 1,
537 "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
538 /* Bad strings */
539 for (i = 0; i < ARRAY_SIZE(badStrings); i++)
540 {
541 bufLen = 0;
543 NULL, &bufLen, NULL, NULL);
545 "%d: Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", i, ret, GetLastError());
546 }
547 /* Weird base64 strings (invalid padding, extra white-space etc.) */
554 decodeBase64WithLenBroken("V", 0, "T", 0);
555 decodeBase64WithLenBroken("VV", 0, "U", 0);
556 decodeBase64WithLenBroken("VVV", 0, "UU", 0);
557 decodeBase64WithLen("V", 1, "T", 0);
558 decodeBase64WithLen("VV", 2, "U", 0);
559 decodeBase64WithLen("VVV", 3, "UU", 0);
560 decodeBase64WithLen("V===", 0, "T", 0);
561 decodeBase64WithLen("V========", 0, "T", 0);
562 decodeBase64WithLen("V===", 4, "T", 0);
563 decodeBase64WithLen("V\nVVV", 0, "UUU", 0);
564 decodeBase64WithLen("VV\nVV", 0, "UUU", 0);
565 decodeBase64WithLen("VVV\nV", 0, "UUU", 0);
566 decodeBase64WithLen("V\nVVV", 5, "UUU", 0);
567 decodeBase64WithLen("VV\nVV", 5, "UUU", 0);
568 decodeBase64WithLen("VVV\nV", 5, "UUU", 0);
569 decodeBase64WithLen("VV VV", 0, "UUU", 0);
570 decodeBase64WithLen("V===VVVV", 0, "T", 0);
571 decodeBase64WithLen("VV==VVVV", 0, "U", 0);
572 decodeBase64WithLen("VVV=VVVV", 0, "UU", 0);
573 decodeBase64WithLen("VVVV=VVVV", 0, "UUU", 0);
574 decodeBase64WithLen("V===VVVV", 8, "T", 0);
575 decodeBase64WithLen("VV==VVVV", 8, "U", 0);
576 decodeBase64WithLen("VVV=VVVV", 8, "UU", 0);
577 decodeBase64WithLen("VVVV=VVVV", 8, "UUU", 0);
578
579 decodeBase64WithFmt("-----BEGIN-----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
580 decodeBase64WithFmt("-----BEGIN-----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
581 decodeBase64WithFmt("-----BEGIN -----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
582 decodeBase64WithFmt("-----BEGIN -----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, "UUU", 0);
583
584 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
585 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
586 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
587 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
588 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
589 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
590 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
591 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
592 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
593 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
594 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
595 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
596
597 /* Too small buffer */
598 buf[0] = 0;
599 bufLen = 4;
600 ret = CryptStringToBinaryA("VVVVVVVV", 8, CRYPT_STRING_BASE64, (BYTE*)buf, &bufLen, NULL, NULL);
601 ok(!ret && bufLen == 4 && buf[0] == 0,
602 "Expected ret 0, bufLen 4, buf[0] '\\0', got ret %d, bufLen %d, buf[0] '%c'\n",
603 ret, bufLen, buf[0]);
604
605 /* Good strings */
606 for (i = 0; i < ARRAY_SIZE(tests); i++)
607 {
608 bufLen = 0;
609 /* Bogus length--oddly enough, that succeeds, even though it's not
610 * properly padded.
611 */
613 NULL, &bufLen, NULL, NULL);
614 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
615 /* Check with the precise format */
618 tests[i].toEncodeLen);
621 tests[i].toEncode, tests[i].toEncodeLen);
624 tests[i].toEncode, tests[i].toEncodeLen);
628 tests[i].toEncodeLen);
631 tests[i].toEncode, tests[i].toEncodeLen);
632 /* And check with the "any" formats */
635 tests[i].toEncodeLen);
636 /* Don't check with no header and the string_any format, that'll
637 * always succeed.
638 */
641 tests[i].toEncodeLen);
644 tests[i].toEncodeLen);
645 /* oddly, these seem to decode using the wrong format
646 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
647 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
648 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
649 tests[i].toEncodeLen);
650 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
651 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
652 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
653 tests[i].toEncodeLen);
654 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
655 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
656 tests[i].toEncode, tests[i].toEncodeLen);
657 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
658 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
659 tests[i].toEncodeLen);
660 */
661 }
662 /* And again, with no CR--decoding handles this automatically */
663 for (i = 0; i < ARRAY_SIZE(testsNoCR); i++)
664 {
665 bufLen = 0;
666 /* Bogus length--oddly enough, that succeeds, even though it's not
667 * properly padded.
668 */
670 NULL, &bufLen, NULL, NULL);
671 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
672 /* Check with the precise format */
675 testsNoCR[i].toEncodeLen);
678 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
682 testsNoCR[i].toEncodeLen);
685 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
686 /* And check with the "any" formats */
689 testsNoCR[i].toEncodeLen);
690 /* Don't check with no header and the string_any format, that'll
691 * always succeed.
692 */
695 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
698 testsNoCR[i].toEncodeLen);
699 }
700}
static void decodeBase64WithLen(LPCSTR str, int len, LPCSTR expected, int le)
Definition: base64.c:496
static void decodeBase64WithFmt(LPCSTR str, DWORD fmt, LPCSTR expected, int le)
Definition: base64.c:501
static const struct BadString badStrings[]
Definition: base64.c:512
static void decodeAndCompareBase64_A(LPCSTR toDecode, LPCSTR header, LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected, DWORD expectedLen)
Definition: base64.c:366
#define ALT_CERT_TRAILER
Definition: base64.c:32
static void decodeBase64WithLenBroken(LPCSTR str, int len, LPCSTR expected, int le)
Definition: base64.c:491
#define ALT_CERT_HEADER
Definition: base64.c:30
#define CRYPT_STRING_BASE64_ANY
Definition: wincrypt.h:2990
#define CRYPT_STRING_ANY
Definition: wincrypt.h:2991

Referenced by START_TEST().

Variable Documentation

◆ badStrings

const struct BadString badStrings[]
static
Initial value:
= {
{ "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER },
}

Definition at line 512 of file base64.c.

Referenced by testStringToBinaryA().

◆ tests

const struct BinTests tests[]
static
Initial value:
= {
{ toEncode1, sizeof(toEncode1), "AA==\r\n", },
{ toEncode2, sizeof(toEncode2), "AQI=\r\n", },
{ toEncode4, sizeof(toEncode4),
"YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
"TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
"d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
"Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
"SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
{ toEncode5, sizeof(toEncode5),
"YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISQA=\r\n" },
}
static const BYTE toEncode4[]
Definition: base64.c:54
static const BYTE toEncode1[]
Definition: base64.c:51
static const BYTE toEncode2[]
Definition: base64.c:52
static const BYTE toEncode5[]
Definition: base64.c:58

Definition at line 61 of file base64.c.

Referenced by test_CryptBinaryToString(), and testStringToBinaryA().

◆ testsNoCR

const struct BinTests testsNoCR[]
static
Initial value:
= {
{ toEncode1, sizeof(toEncode1), "AA==\n", },
{ toEncode2, sizeof(toEncode2), "AQI=\n", },
{ toEncode4, sizeof(toEncode4),
"YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
"TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
"d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
"Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
"SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
{ toEncode5, sizeof(toEncode5),
"YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISQA=\n" },
}

Definition at line 75 of file base64.c.

Referenced by test_CryptBinaryToString(), and testStringToBinaryA().

◆ toEncode1

const BYTE toEncode1[] = { 0 }
static

Definition at line 51 of file base64.c.

◆ toEncode2

const BYTE toEncode2[] = { 1,2 }
static

Definition at line 52 of file base64.c.

◆ toEncode4

const BYTE toEncode4[]
static
Initial value:
=
"abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
"abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
"abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"

Definition at line 54 of file base64.c.

◆ toEncode5

const BYTE toEncode5[]
static
Initial value:
=
"abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHI"

Definition at line 58 of file base64.c.