kernel32: Do not omit mandatory argument to DeviceIoControl.
[wine.git] / dlls / crypt32 / base64.c
blob9d6dcb468d25a4803734c9b0df4b32bef5f67b95
1 /*
2 * base64 encoder/decoder
4 * Copyright 2005 by Kai Blin
5 * Copyright 2006 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wincrypt.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32 #define CERT_HEADER "-----BEGIN CERTIFICATE-----"
33 #define CERT_HEADER_START "-----BEGIN"
34 #define CERT_DELIMITER "-----"
35 #define CERT_TRAILER "-----END CERTIFICATE-----"
36 #define CERT_TRAILER_START "-----END"
37 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----"
38 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----"
39 #define X509_HEADER "-----BEGIN X509 CRL-----"
40 #define X509_TRAILER "-----END X509 CRL-----"
42 static const WCHAR CERT_HEADER_W[] = {
43 '-','-','-','-','-','B','E','G','I','N',' ','C','E','R','T','I','F','I','C',
44 'A','T','E','-','-','-','-','-',0 };
45 static const WCHAR CERT_HEADER_START_W[] = {
46 '-','-','-','-','-','B','E','G','I','N',0 };
47 static const WCHAR CERT_DELIMITER_W[] = {
48 '-','-','-','-','-',0 };
49 static const WCHAR CERT_TRAILER_W[] = {
50 '-','-','-','-','-','E','N','D',0 };
51 static const WCHAR CERT_TRAILER_START_W[] = {
52 '-','-','-','-','-','E','N','D',' ','C','E','R','T','I','F','I','C','A','T',
53 'E','-','-','-','-','-',0 };
54 static const WCHAR CERT_REQUEST_HEADER_W[] = {
55 '-','-','-','-','-','B','E','G','I','N',' ','N','E','W',' ','C','E','R','T',
56 'I','F','I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
57 static const WCHAR CERT_REQUEST_TRAILER_W[] = {
58 '-','-','-','-','-','E','N','D',' ','N','E','W',' ','C','E','R','T','I','F',
59 'I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
60 static const WCHAR X509_HEADER_W[] = {
61 '-','-','-','-','-','B','E','G','I','N',' ','X','5','0','9',' ','C','R','L',
62 '-','-','-','-','-',0 };
63 static const WCHAR X509_TRAILER_W[] = {
64 '-','-','-','-','-','E','N','D',' ','X','5','0','9',' ','C','R','L','-','-',
65 '-','-','-',0 };
67 static const char b64[] =
68 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70 typedef BOOL (*BinaryToStringAFunc)(const BYTE *pbBinary,
71 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString);
72 typedef BOOL (*BinaryToStringWFunc)(const BYTE *pbBinary,
73 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString);
75 static BOOL EncodeBinaryToBinaryA(const BYTE *pbBinary,
76 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
78 BOOL ret = TRUE;
80 if (*pcchString < cbBinary)
82 if (!pszString)
83 *pcchString = cbBinary;
84 else
86 SetLastError(ERROR_INSUFFICIENT_BUFFER);
87 *pcchString = cbBinary;
88 ret = FALSE;
91 else
93 if (cbBinary)
94 memcpy(pszString, pbBinary, cbBinary);
95 *pcchString = cbBinary;
97 return ret;
100 static LONG encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep,
101 char* out_buf, DWORD *out_len)
103 int div, i;
104 const BYTE *d = in_buf;
105 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
106 DWORD needed;
107 LPSTR ptr;
109 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
110 needed = bytes + pad_bytes + 1;
111 needed += (needed / 64 + 1) * strlen(sep);
113 if (needed > *out_len)
115 *out_len = needed;
116 return ERROR_INSUFFICIENT_BUFFER;
118 else
119 *out_len = needed;
121 /* Three bytes of input give 4 chars of output */
122 div = in_len / 3;
124 ptr = out_buf;
125 i = 0;
126 while (div > 0)
128 if (i && i % 64 == 0)
130 strcpy(ptr, sep);
131 ptr += strlen(sep);
133 /* first char is the first 6 bits of the first byte*/
134 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
135 /* second char is the last 2 bits of the first byte and the first 4
136 * bits of the second byte */
137 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
138 /* third char is the last 4 bits of the second byte and the first 2
139 * bits of the third byte */
140 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
141 /* fourth char is the remaining 6 bits of the third byte */
142 *ptr++ = b64[ d[2] & 0x3f];
143 i += 4;
144 d += 3;
145 div--;
148 switch(pad_bytes)
150 case 1:
151 /* first char is the first 6 bits of the first byte*/
152 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
153 /* second char is the last 2 bits of the first byte and the first 4
154 * bits of the second byte */
155 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
156 /* third char is the last 4 bits of the second byte padded with
157 * two zeroes */
158 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
159 /* fourth char is a = to indicate one byte of padding */
160 *ptr++ = '=';
161 break;
162 case 2:
163 /* first char is the first 6 bits of the first byte*/
164 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
165 /* second char is the last 2 bits of the first byte padded with
166 * four zeroes*/
167 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
168 /* third char is = to indicate padding */
169 *ptr++ = '=';
170 /* fourth char is = to indicate padding */
171 *ptr++ = '=';
172 break;
174 strcpy(ptr, sep);
176 return ERROR_SUCCESS;
179 static BOOL BinaryToBase64A(const BYTE *pbBinary,
180 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
182 static const char crlf[] = "\r\n", lf[] = "\n";
183 BOOL ret = TRUE;
184 LPCSTR header = NULL, trailer = NULL, sep;
185 DWORD charsNeeded;
187 if (dwFlags & CRYPT_STRING_NOCR)
188 sep = lf;
189 else if (dwFlags & CRYPT_STRING_NOCRLF)
190 sep = "";
191 else
192 sep = crlf;
193 switch (dwFlags & 0x0fffffff)
195 case CRYPT_STRING_BASE64:
196 /* no header or footer */
197 break;
198 case CRYPT_STRING_BASE64HEADER:
199 header = CERT_HEADER;
200 trailer = CERT_TRAILER;
201 break;
202 case CRYPT_STRING_BASE64REQUESTHEADER:
203 header = CERT_REQUEST_HEADER;
204 trailer = CERT_REQUEST_TRAILER;
205 break;
206 case CRYPT_STRING_BASE64X509CRLHEADER:
207 header = X509_HEADER;
208 trailer = X509_TRAILER;
209 break;
212 charsNeeded = 0;
213 encodeBase64A(pbBinary, cbBinary, sep, NULL, &charsNeeded);
214 if (header)
215 charsNeeded += strlen(header) + strlen(sep);
216 if (trailer)
217 charsNeeded += strlen(trailer) + strlen(sep);
218 if (charsNeeded <= *pcchString)
220 LPSTR ptr = pszString;
221 DWORD size = charsNeeded;
223 if (header)
225 strcpy(ptr, header);
226 ptr += strlen(ptr);
227 strcpy(ptr, sep);
228 ptr += strlen(sep);
230 encodeBase64A(pbBinary, cbBinary, sep, ptr, &size);
231 ptr += size - 1;
232 if (trailer)
234 strcpy(ptr, trailer);
235 ptr += strlen(ptr);
236 strcpy(ptr, sep);
238 *pcchString = charsNeeded - 1;
240 else if (pszString)
242 *pcchString = charsNeeded;
243 SetLastError(ERROR_INSUFFICIENT_BUFFER);
244 ret = FALSE;
246 else
247 *pcchString = charsNeeded;
248 return ret;
251 BOOL WINAPI CryptBinaryToStringA(const BYTE *pbBinary,
252 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
254 BinaryToStringAFunc encoder = NULL;
256 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
257 pcchString);
259 if (!pbBinary)
261 SetLastError(ERROR_INVALID_PARAMETER);
262 return FALSE;
264 if (!pcchString)
266 SetLastError(ERROR_INVALID_PARAMETER);
267 return FALSE;
270 switch (dwFlags & 0x0fffffff)
272 case CRYPT_STRING_BINARY:
273 encoder = EncodeBinaryToBinaryA;
274 break;
275 case CRYPT_STRING_BASE64:
276 case CRYPT_STRING_BASE64HEADER:
277 case CRYPT_STRING_BASE64REQUESTHEADER:
278 case CRYPT_STRING_BASE64X509CRLHEADER:
279 encoder = BinaryToBase64A;
280 break;
281 case CRYPT_STRING_HEX:
282 case CRYPT_STRING_HEXASCII:
283 case CRYPT_STRING_HEXADDR:
284 case CRYPT_STRING_HEXASCIIADDR:
285 FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
286 /* fall through */
287 default:
288 SetLastError(ERROR_INVALID_PARAMETER);
289 return FALSE;
291 return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
294 static LONG encodeBase64W(const BYTE *in_buf, int in_len, LPCWSTR sep,
295 WCHAR* out_buf, DWORD *out_len)
297 int div, i;
298 const BYTE *d = in_buf;
299 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
300 DWORD needed;
301 LPWSTR ptr;
303 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
304 needed = bytes + pad_bytes + 1;
305 needed += (needed / 64 + 1) * strlenW(sep);
307 if (needed > *out_len)
309 *out_len = needed;
310 return ERROR_INSUFFICIENT_BUFFER;
312 else
313 *out_len = needed;
315 /* Three bytes of input give 4 chars of output */
316 div = in_len / 3;
318 ptr = out_buf;
319 i = 0;
320 while (div > 0)
322 if (i && i % 64 == 0)
324 strcpyW(ptr, sep);
325 ptr += strlenW(sep);
327 /* first char is the first 6 bits of the first byte*/
328 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
329 /* second char is the last 2 bits of the first byte and the first 4
330 * bits of the second byte */
331 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
332 /* third char is the last 4 bits of the second byte and the first 2
333 * bits of the third byte */
334 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
335 /* fourth char is the remaining 6 bits of the third byte */
336 *ptr++ = b64[ d[2] & 0x3f];
337 i += 4;
338 d += 3;
339 div--;
342 switch(pad_bytes)
344 case 1:
345 /* first char is the first 6 bits of the first byte*/
346 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
347 /* second char is the last 2 bits of the first byte and the first 4
348 * bits of the second byte */
349 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
350 /* third char is the last 4 bits of the second byte padded with
351 * two zeroes */
352 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
353 /* fourth char is a = to indicate one byte of padding */
354 *ptr++ = '=';
355 break;
356 case 2:
357 /* first char is the first 6 bits of the first byte*/
358 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
359 /* second char is the last 2 bits of the first byte padded with
360 * four zeroes*/
361 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
362 /* third char is = to indicate padding */
363 *ptr++ = '=';
364 /* fourth char is = to indicate padding */
365 *ptr++ = '=';
366 break;
368 strcpyW(ptr, sep);
370 return ERROR_SUCCESS;
373 static BOOL BinaryToBase64W(const BYTE *pbBinary,
374 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
376 static const WCHAR crlf[] = { '\r','\n',0 }, lf[] = { '\n',0 }, empty[] = {0};
377 BOOL ret = TRUE;
378 LPCWSTR header = NULL, trailer = NULL, sep;
379 DWORD charsNeeded;
381 if (dwFlags & CRYPT_STRING_NOCR)
382 sep = lf;
383 else if (dwFlags & CRYPT_STRING_NOCRLF)
384 sep = empty;
385 else
386 sep = crlf;
387 switch (dwFlags & 0x0fffffff)
389 case CRYPT_STRING_BASE64:
390 /* no header or footer */
391 break;
392 case CRYPT_STRING_BASE64HEADER:
393 header = CERT_HEADER_W;
394 trailer = CERT_TRAILER_W;
395 break;
396 case CRYPT_STRING_BASE64REQUESTHEADER:
397 header = CERT_REQUEST_HEADER_W;
398 trailer = CERT_REQUEST_TRAILER_W;
399 break;
400 case CRYPT_STRING_BASE64X509CRLHEADER:
401 header = X509_HEADER_W;
402 trailer = X509_TRAILER_W;
403 break;
406 charsNeeded = 0;
407 encodeBase64W(pbBinary, cbBinary, sep, NULL, &charsNeeded);
408 if (header)
409 charsNeeded += strlenW(header) + strlenW(sep);
410 if (trailer)
411 charsNeeded += strlenW(trailer) + strlenW(sep);
412 if (charsNeeded <= *pcchString)
414 LPWSTR ptr = pszString;
415 DWORD size = charsNeeded;
417 if (header)
419 strcpyW(ptr, header);
420 ptr += strlenW(ptr);
421 strcpyW(ptr, sep);
422 ptr += strlenW(sep);
424 encodeBase64W(pbBinary, cbBinary, sep, ptr, &size);
425 ptr += size - 1;
426 if (trailer)
428 strcpyW(ptr, trailer);
429 ptr += strlenW(ptr);
430 strcpyW(ptr, sep);
432 *pcchString = charsNeeded - 1;
434 else if (pszString)
436 *pcchString = charsNeeded;
437 SetLastError(ERROR_INSUFFICIENT_BUFFER);
438 ret = FALSE;
440 else
441 *pcchString = charsNeeded;
442 return ret;
445 BOOL WINAPI CryptBinaryToStringW(const BYTE *pbBinary,
446 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
448 BinaryToStringWFunc encoder = NULL;
450 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
451 pcchString);
453 if (!pbBinary)
455 SetLastError(ERROR_INVALID_PARAMETER);
456 return FALSE;
458 if (!pcchString)
460 SetLastError(ERROR_INVALID_PARAMETER);
461 return FALSE;
464 switch (dwFlags & 0x0fffffff)
466 case CRYPT_STRING_BASE64:
467 case CRYPT_STRING_BASE64HEADER:
468 case CRYPT_STRING_BASE64REQUESTHEADER:
469 case CRYPT_STRING_BASE64X509CRLHEADER:
470 encoder = BinaryToBase64W;
471 break;
472 case CRYPT_STRING_BINARY:
473 case CRYPT_STRING_HEX:
474 case CRYPT_STRING_HEXASCII:
475 case CRYPT_STRING_HEXADDR:
476 case CRYPT_STRING_HEXASCIIADDR:
477 FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
478 /* fall through */
479 default:
480 SetLastError(ERROR_INVALID_PARAMETER);
481 return FALSE;
483 return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
486 static inline BYTE decodeBase64Byte(int c)
488 BYTE ret;
490 if (c >= 'A' && c <= 'Z')
491 ret = c - 'A';
492 else if (c >= 'a' && c <= 'z')
493 ret = c - 'a' + 26;
494 else if (c >= '0' && c <= '9')
495 ret = c - '0' + 52;
496 else if (c == '+')
497 ret = 62;
498 else if (c == '/')
499 ret = 63;
500 else
501 ret = 64;
502 return ret;
505 static LONG decodeBase64Block(const char *in_buf, int in_len,
506 const char **nextBlock, PBYTE out_buf, DWORD *out_len)
508 int len = in_len;
509 const char *d = in_buf;
510 int ip0, ip1, ip2, ip3;
512 if (len < 4)
513 return ERROR_INVALID_DATA;
515 if (d[2] == '=')
517 if ((ip0 = decodeBase64Byte(d[0])) > 63)
518 return ERROR_INVALID_DATA;
519 if ((ip1 = decodeBase64Byte(d[1])) > 63)
520 return ERROR_INVALID_DATA;
522 if (out_buf)
523 out_buf[0] = (ip0 << 2) | (ip1 >> 4);
524 *out_len = 1;
526 else if (d[3] == '=')
528 if ((ip0 = decodeBase64Byte(d[0])) > 63)
529 return ERROR_INVALID_DATA;
530 if ((ip1 = decodeBase64Byte(d[1])) > 63)
531 return ERROR_INVALID_DATA;
532 if ((ip2 = decodeBase64Byte(d[2])) > 63)
533 return ERROR_INVALID_DATA;
535 if (out_buf)
537 out_buf[0] = (ip0 << 2) | (ip1 >> 4);
538 out_buf[1] = (ip1 << 4) | (ip2 >> 2);
540 *out_len = 2;
542 else
544 if ((ip0 = decodeBase64Byte(d[0])) > 63)
545 return ERROR_INVALID_DATA;
546 if ((ip1 = decodeBase64Byte(d[1])) > 63)
547 return ERROR_INVALID_DATA;
548 if ((ip2 = decodeBase64Byte(d[2])) > 63)
549 return ERROR_INVALID_DATA;
550 if ((ip3 = decodeBase64Byte(d[3])) > 63)
551 return ERROR_INVALID_DATA;
553 if (out_buf)
555 out_buf[0] = (ip0 << 2) | (ip1 >> 4);
556 out_buf[1] = (ip1 << 4) | (ip2 >> 2);
557 out_buf[2] = (ip2 << 6) | ip3;
559 *out_len = 3;
561 if (len >= 6 && d[4] == '\r' && d[5] == '\n')
562 *nextBlock = d + 6;
563 else if (len >= 5 && d[4] == '\n')
564 *nextBlock = d + 5;
565 else if (len >= 4 && d[4])
566 *nextBlock = d + 4;
567 else
568 *nextBlock = NULL;
569 return ERROR_SUCCESS;
572 /* Unlike CryptStringToBinaryA, cchString is guaranteed to be the length of the
573 * string to convert.
575 typedef LONG (*StringToBinaryAFunc)(LPCSTR pszString, DWORD cchString,
576 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
578 static LONG Base64ToBinaryA(LPCSTR pszString, DWORD cchString,
579 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
581 LONG ret = ERROR_SUCCESS;
582 const char *nextBlock;
583 DWORD outLen = 0;
585 nextBlock = pszString;
586 while (nextBlock && !ret)
588 DWORD len = 0;
590 ret = decodeBase64Block(nextBlock, cchString - (nextBlock - pszString),
591 &nextBlock, pbBinary ? pbBinary + outLen : NULL, &len);
592 if (!ret)
593 outLen += len;
594 if (cchString - (nextBlock - pszString) <= 0)
595 nextBlock = NULL;
597 *pcbBinary = outLen;
598 if (!ret)
600 if (pdwSkip)
601 *pdwSkip = 0;
602 if (pdwFlags)
603 *pdwFlags = CRYPT_STRING_BASE64;
605 else if (ret == ERROR_INSUFFICIENT_BUFFER)
607 if (!pbBinary)
608 ret = ERROR_SUCCESS;
610 return ret;
613 static LONG Base64WithHeaderAndTrailerToBinaryA(LPCSTR pszString,
614 DWORD cchString, LPCSTR header, LPCSTR trailer, BYTE *pbBinary,
615 DWORD *pcbBinary, DWORD *pdwSkip, BOOL exactHeaderAndTrailerMatch)
617 LONG ret;
619 LPCSTR headerBegins;
620 LPCSTR dataBegins;
621 LPCSTR trailerBegins;
622 size_t dataLength;
624 if ((strlen(header) + strlen(trailer)) > cchString)
626 return ERROR_INVALID_DATA;
629 if (!(headerBegins = strstr(pszString, header)))
631 TRACE("Can't find %s in %s.\n", header, pszString);
632 return ERROR_INVALID_DATA;
635 dataBegins = headerBegins + strlen(header);
636 if (!exactHeaderAndTrailerMatch)
638 if ((dataBegins = strstr(dataBegins, CERT_DELIMITER)))
640 dataBegins += strlen(CERT_DELIMITER);
642 else
644 return ERROR_INVALID_DATA;
647 if (*dataBegins == '\r') dataBegins++;
648 if (*dataBegins == '\n') dataBegins++;
650 if (exactHeaderAndTrailerMatch)
652 trailerBegins = pszString + cchString - strlen(trailer);
653 if (pszString[cchString - 1] == '\n') trailerBegins--;
654 if (pszString[cchString - 2] == '\r') trailerBegins--;
656 if (*(trailerBegins-1) == '\n') trailerBegins--;
657 if (*(trailerBegins-1) == '\r') trailerBegins--;
659 if (!strncmp(trailerBegins, trailer, strlen(trailer)))
661 return ERROR_INVALID_DATA;
664 else
666 if (!(trailerBegins = strstr(dataBegins, trailer)))
668 return ERROR_INVALID_DATA;
670 if (*(trailerBegins-1) == '\n') trailerBegins--;
671 if (*(trailerBegins-1) == '\r') trailerBegins--;
674 if (pdwSkip)
675 *pdwSkip = headerBegins - pszString;
677 dataLength = trailerBegins - dataBegins;
679 ret = Base64ToBinaryA(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
680 NULL);
682 return ret;
685 static LONG Base64HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
686 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
688 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
689 CERT_HEADER_START, CERT_TRAILER_START, pbBinary, pcbBinary, pdwSkip, FALSE);
691 if (!ret && pdwFlags)
692 *pdwFlags = CRYPT_STRING_BASE64HEADER;
693 return ret;
696 static LONG Base64RequestHeaderToBinaryA(LPCSTR pszString, DWORD cchString,
697 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
699 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
700 CERT_REQUEST_HEADER, CERT_REQUEST_TRAILER, pbBinary, pcbBinary, pdwSkip, TRUE);
702 if (!ret && pdwFlags)
703 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
704 return ret;
707 static LONG Base64X509HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
708 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
710 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
711 X509_HEADER, X509_TRAILER, pbBinary, pcbBinary, pdwSkip, TRUE);
713 if (!ret && pdwFlags)
714 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
715 return ret;
718 static LONG Base64AnyToBinaryA(LPCSTR pszString, DWORD cchString,
719 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
721 LONG ret;
723 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
724 pdwSkip, pdwFlags);
725 if (ret == ERROR_INVALID_DATA)
726 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
727 pdwSkip, pdwFlags);
728 return ret;
731 static LONG DecodeBinaryToBinaryA(LPCSTR pszString, DWORD cchString,
732 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
734 LONG ret = ERROR_SUCCESS;
736 if (*pcbBinary < cchString)
738 if (!pbBinary)
739 *pcbBinary = cchString;
740 else
742 ret = ERROR_INSUFFICIENT_BUFFER;
743 *pcbBinary = cchString;
746 else
748 if (cchString)
749 memcpy(pbBinary, pszString, cchString);
750 *pcbBinary = cchString;
752 return ret;
755 static LONG DecodeAnyA(LPCSTR pszString, DWORD cchString,
756 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
758 LONG ret;
760 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
761 pdwSkip, pdwFlags);
762 if (ret == ERROR_INVALID_DATA)
763 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
764 pdwSkip, pdwFlags);
765 if (ret == ERROR_INVALID_DATA)
766 ret = DecodeBinaryToBinaryA(pszString, cchString, pbBinary, pcbBinary,
767 pdwSkip, pdwFlags);
768 return ret;
771 BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString,
772 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
773 DWORD *pdwSkip, DWORD *pdwFlags)
775 StringToBinaryAFunc decoder;
776 LONG ret;
778 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_a(pszString),
779 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
781 if (!pszString)
783 SetLastError(ERROR_INVALID_PARAMETER);
784 return FALSE;
786 /* Only the bottom byte contains valid types */
787 if (dwFlags & 0xfffffff0)
789 SetLastError(ERROR_INVALID_DATA);
790 return FALSE;
792 switch (dwFlags)
794 case CRYPT_STRING_BASE64_ANY:
795 decoder = Base64AnyToBinaryA;
796 break;
797 case CRYPT_STRING_BASE64:
798 decoder = Base64ToBinaryA;
799 break;
800 case CRYPT_STRING_BASE64HEADER:
801 decoder = Base64HeaderToBinaryA;
802 break;
803 case CRYPT_STRING_BASE64REQUESTHEADER:
804 decoder = Base64RequestHeaderToBinaryA;
805 break;
806 case CRYPT_STRING_BASE64X509CRLHEADER:
807 decoder = Base64X509HeaderToBinaryA;
808 break;
809 case CRYPT_STRING_BINARY:
810 decoder = DecodeBinaryToBinaryA;
811 break;
812 case CRYPT_STRING_ANY:
813 decoder = DecodeAnyA;
814 break;
815 case CRYPT_STRING_HEX:
816 case CRYPT_STRING_HEXASCII:
817 case CRYPT_STRING_HEXADDR:
818 case CRYPT_STRING_HEXASCIIADDR:
819 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
820 /* fall through */
821 default:
822 SetLastError(ERROR_INVALID_PARAMETER);
823 return FALSE;
825 if (!cchString)
826 cchString = strlen(pszString);
827 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
828 if (ret)
829 SetLastError(ret);
830 return ret == ERROR_SUCCESS;
833 static LONG decodeBase64BlockW(const WCHAR *in_buf, int in_len,
834 const WCHAR **nextBlock, PBYTE out_buf, DWORD *out_len)
836 int len = in_len, i;
837 const WCHAR *d = in_buf;
838 int ip0, ip1, ip2, ip3;
840 if (len < 4)
841 return ERROR_INVALID_DATA;
843 i = 0;
844 if (d[2] == '=')
846 if ((ip0 = decodeBase64Byte(d[0])) > 63)
847 return ERROR_INVALID_DATA;
848 if ((ip1 = decodeBase64Byte(d[1])) > 63)
849 return ERROR_INVALID_DATA;
851 if (out_buf)
852 out_buf[i] = (ip0 << 2) | (ip1 >> 4);
853 i++;
855 else if (d[3] == '=')
857 if ((ip0 = decodeBase64Byte(d[0])) > 63)
858 return ERROR_INVALID_DATA;
859 if ((ip1 = decodeBase64Byte(d[1])) > 63)
860 return ERROR_INVALID_DATA;
861 if ((ip2 = decodeBase64Byte(d[2])) > 63)
862 return ERROR_INVALID_DATA;
864 if (out_buf)
866 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
867 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
869 i += 2;
871 else
873 if ((ip0 = decodeBase64Byte(d[0])) > 63)
874 return ERROR_INVALID_DATA;
875 if ((ip1 = decodeBase64Byte(d[1])) > 63)
876 return ERROR_INVALID_DATA;
877 if ((ip2 = decodeBase64Byte(d[2])) > 63)
878 return ERROR_INVALID_DATA;
879 if ((ip3 = decodeBase64Byte(d[3])) > 63)
880 return ERROR_INVALID_DATA;
882 if (out_buf)
884 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
885 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
886 out_buf[i + 2] = (ip2 << 6) | ip3;
888 i += 3;
890 if (len >= 6 && d[4] == '\r' && d[5] == '\n')
891 *nextBlock = d + 6;
892 else if (len >= 5 && d[4] == '\n')
893 *nextBlock = d + 5;
894 else if (len >= 4 && d[4])
895 *nextBlock = d + 4;
896 else
897 *nextBlock = NULL;
898 *out_len = i;
899 return ERROR_SUCCESS;
902 /* Unlike CryptStringToBinaryW, cchString is guaranteed to be the length of the
903 * string to convert.
905 typedef LONG (*StringToBinaryWFunc)(LPCWSTR pszString, DWORD cchString,
906 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
908 static LONG Base64ToBinaryW(LPCWSTR pszString, DWORD cchString,
909 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
911 LONG ret = ERROR_SUCCESS;
912 const WCHAR *nextBlock;
913 DWORD outLen = 0;
915 nextBlock = pszString;
916 while (nextBlock && !ret)
918 DWORD len = 0;
920 ret = decodeBase64BlockW(nextBlock, cchString - (nextBlock - pszString),
921 &nextBlock, pbBinary ? pbBinary + outLen : NULL, &len);
922 if (!ret)
923 outLen += len;
924 if (cchString - (nextBlock - pszString) <= 0)
925 nextBlock = NULL;
927 *pcbBinary = outLen;
928 if (!ret)
930 if (pdwSkip)
931 *pdwSkip = 0;
932 if (pdwFlags)
933 *pdwFlags = CRYPT_STRING_BASE64;
935 else if (ret == ERROR_INSUFFICIENT_BUFFER)
937 if (!pbBinary)
938 ret = ERROR_SUCCESS;
940 return ret;
943 static LONG Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString,
944 DWORD cchString, LPCWSTR header, LPCWSTR trailer, BYTE *pbBinary,
945 DWORD *pcbBinary, DWORD *pdwSkip, BOOL exactHeaderAndTrailerMatch)
947 LONG ret;
949 LPCWSTR headerBegins;
950 LPCWSTR dataBegins;
951 LPCWSTR trailerBegins;
952 size_t dataLength;
954 if ((strlenW(header) + strlenW(trailer)) > cchString)
956 return ERROR_INVALID_DATA;
959 if (!(headerBegins = strstrW(pszString, header)))
961 TRACE("Can't find %s in %s.\n", debugstr_w(header), debugstr_w(pszString));
962 return ERROR_INVALID_DATA;
965 dataBegins = headerBegins + strlenW(header);
966 if (!exactHeaderAndTrailerMatch)
968 if ((dataBegins = strstrW(dataBegins, CERT_DELIMITER_W)))
970 dataBegins += strlenW(CERT_DELIMITER_W);
972 else
974 return ERROR_INVALID_DATA;
977 if (*dataBegins == '\r') dataBegins++;
978 if (*dataBegins == '\n') dataBegins++;
980 if (exactHeaderAndTrailerMatch)
982 trailerBegins = pszString + cchString - strlenW(trailer);
983 if (pszString[cchString - 1] == '\n') trailerBegins--;
984 if (pszString[cchString - 2] == '\r') trailerBegins--;
986 if (*(trailerBegins-1) == '\n') trailerBegins--;
987 if (*(trailerBegins-1) == '\r') trailerBegins--;
989 if (!strncmpW(trailerBegins, trailer, strlenW(trailer)))
991 return ERROR_INVALID_DATA;
994 else
996 if (!(trailerBegins = strstrW(dataBegins, trailer)))
998 return ERROR_INVALID_DATA;
1000 if (*(trailerBegins-1) == '\n') trailerBegins--;
1001 if (*(trailerBegins-1) == '\r') trailerBegins--;
1004 if (pdwSkip)
1005 *pdwSkip = headerBegins - pszString;
1007 dataLength = trailerBegins - dataBegins;
1009 ret = Base64ToBinaryW(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
1010 NULL);
1012 return ret;
1015 static LONG Base64HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
1016 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1018 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
1019 CERT_HEADER_START_W, CERT_TRAILER_START_W, pbBinary, pcbBinary,
1020 pdwSkip, FALSE);
1022 if (!ret && pdwFlags)
1023 *pdwFlags = CRYPT_STRING_BASE64HEADER;
1024 return ret;
1027 static LONG Base64RequestHeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
1028 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1030 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
1031 CERT_REQUEST_HEADER_W, CERT_REQUEST_TRAILER_W, pbBinary, pcbBinary,
1032 pdwSkip, TRUE);
1034 if (!ret && pdwFlags)
1035 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
1036 return ret;
1039 static LONG Base64X509HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
1040 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1042 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
1043 X509_HEADER_W, X509_TRAILER_W, pbBinary, pcbBinary, pdwSkip, TRUE);
1045 if (!ret && pdwFlags)
1046 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
1047 return ret;
1050 static LONG Base64AnyToBinaryW(LPCWSTR pszString, DWORD cchString,
1051 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1053 LONG ret;
1055 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1056 pdwSkip, pdwFlags);
1057 if (ret == ERROR_INVALID_DATA)
1058 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1059 pdwSkip, pdwFlags);
1060 return ret;
1063 static LONG DecodeBinaryToBinaryW(LPCWSTR pszString, DWORD cchString,
1064 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1066 LONG ret = ERROR_SUCCESS;
1068 if (*pcbBinary < cchString)
1070 if (!pbBinary)
1071 *pcbBinary = cchString;
1072 else
1074 ret = ERROR_INSUFFICIENT_BUFFER;
1075 *pcbBinary = cchString;
1078 else
1080 if (cchString)
1081 memcpy(pbBinary, pszString, cchString * sizeof(WCHAR));
1082 *pcbBinary = cchString * sizeof(WCHAR);
1084 return ret;
1087 static LONG DecodeAnyW(LPCWSTR pszString, DWORD cchString,
1088 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1090 LONG ret;
1092 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1093 pdwSkip, pdwFlags);
1094 if (ret == ERROR_INVALID_DATA)
1095 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1096 pdwSkip, pdwFlags);
1097 if (ret == ERROR_INVALID_DATA)
1098 ret = DecodeBinaryToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1099 pdwSkip, pdwFlags);
1100 return ret;
1103 BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString,
1104 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
1105 DWORD *pdwSkip, DWORD *pdwFlags)
1107 StringToBinaryWFunc decoder;
1108 LONG ret;
1110 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_w(pszString),
1111 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1113 if (!pszString)
1115 SetLastError(ERROR_INVALID_PARAMETER);
1116 return FALSE;
1118 /* Only the bottom byte contains valid types */
1119 if (dwFlags & 0xfffffff0)
1121 SetLastError(ERROR_INVALID_DATA);
1122 return FALSE;
1124 switch (dwFlags)
1126 case CRYPT_STRING_BASE64_ANY:
1127 decoder = Base64AnyToBinaryW;
1128 break;
1129 case CRYPT_STRING_BASE64:
1130 decoder = Base64ToBinaryW;
1131 break;
1132 case CRYPT_STRING_BASE64HEADER:
1133 decoder = Base64HeaderToBinaryW;
1134 break;
1135 case CRYPT_STRING_BASE64REQUESTHEADER:
1136 decoder = Base64RequestHeaderToBinaryW;
1137 break;
1138 case CRYPT_STRING_BASE64X509CRLHEADER:
1139 decoder = Base64X509HeaderToBinaryW;
1140 break;
1141 case CRYPT_STRING_BINARY:
1142 decoder = DecodeBinaryToBinaryW;
1143 break;
1144 case CRYPT_STRING_ANY:
1145 decoder = DecodeAnyW;
1146 break;
1147 case CRYPT_STRING_HEX:
1148 case CRYPT_STRING_HEXASCII:
1149 case CRYPT_STRING_HEXADDR:
1150 case CRYPT_STRING_HEXASCIIADDR:
1151 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
1152 /* fall through */
1153 default:
1154 SetLastError(ERROR_INVALID_PARAMETER);
1155 return FALSE;
1157 if (!cchString)
1158 cchString = strlenW(pszString);
1159 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1160 if (ret)
1161 SetLastError(ret);
1162 return ret == ERROR_SUCCESS;