makefiles: Generate dependencies to match each makefile to its subdirectory.
[wine/multimedia.git] / dlls / crypt32 / base64.c
blob4a6504a488e312fa0122e712ee741e26a218e44d
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_TRAILER "-----END CERTIFICATE-----"
34 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----"
35 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----"
36 #define X509_HEADER "-----BEGIN X509 CRL-----"
37 #define X509_TRAILER "-----END X509 CRL-----"
39 static const WCHAR CERT_HEADER_W[] = {
40 '-','-','-','-','-','B','E','G','I','N',' ','C','E','R','T','I','F','I','C',
41 'A','T','E','-','-','-','-','-',0 };
42 static const WCHAR CERT_TRAILER_W[] = {
43 '-','-','-','-','-','E','N','D',' ','C','E','R','T','I','F','I','C','A','T',
44 'E','-','-','-','-','-',0 };
45 static const WCHAR CERT_REQUEST_HEADER_W[] = {
46 '-','-','-','-','-','B','E','G','I','N',' ','N','E','W',' ','C','E','R','T',
47 'I','F','I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
48 static const WCHAR CERT_REQUEST_TRAILER_W[] = {
49 '-','-','-','-','-','E','N','D',' ','N','E','W',' ','C','E','R','T','I','F',
50 'I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
51 static const WCHAR X509_HEADER_W[] = {
52 '-','-','-','-','-','B','E','G','I','N',' ','X','5','0','9',' ','C','R','L',
53 '-','-','-','-','-',0 };
54 static const WCHAR X509_TRAILER_W[] = {
55 '-','-','-','-','-','E','N','D',' ','X','5','0','9',' ','C','R','L','-','-',
56 '-','-','-',0 };
58 static const char b64[] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
61 typedef BOOL (*BinaryToStringAFunc)(const BYTE *pbBinary,
62 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString);
63 typedef BOOL (*BinaryToStringWFunc)(const BYTE *pbBinary,
64 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString);
66 static BOOL EncodeBinaryToBinaryA(const BYTE *pbBinary,
67 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
69 BOOL ret = TRUE;
71 if (*pcchString < cbBinary)
73 if (!pszString)
74 *pcchString = cbBinary;
75 else
77 SetLastError(ERROR_INSUFFICIENT_BUFFER);
78 *pcchString = cbBinary;
79 ret = FALSE;
82 else
84 if (cbBinary)
85 memcpy(pszString, pbBinary, cbBinary);
86 *pcchString = cbBinary;
88 return ret;
91 static LONG encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep,
92 char* out_buf, DWORD *out_len)
94 int div, i;
95 const BYTE *d = in_buf;
96 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
97 DWORD needed;
98 LPSTR ptr;
100 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
101 needed = bytes + pad_bytes + 1;
102 if (sep)
103 needed += (needed / 64 + 1) * strlen(sep);
105 if (needed > *out_len)
107 *out_len = needed;
108 return ERROR_INSUFFICIENT_BUFFER;
110 else
111 *out_len = needed;
113 /* Three bytes of input give 4 chars of output */
114 div = in_len / 3;
116 ptr = out_buf;
117 i = 0;
118 while (div > 0)
120 if (sep && i && i % 64 == 0)
122 strcpy(ptr, sep);
123 ptr += strlen(sep);
125 /* first char is the first 6 bits of the first byte*/
126 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
127 /* second char is the last 2 bits of the first byte and the first 4
128 * bits of the second byte */
129 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
130 /* third char is the last 4 bits of the second byte and the first 2
131 * bits of the third byte */
132 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
133 /* fourth char is the remaining 6 bits of the third byte */
134 *ptr++ = b64[ d[2] & 0x3f];
135 i += 4;
136 d += 3;
137 div--;
140 switch(pad_bytes)
142 case 1:
143 /* first char is the first 6 bits of the first byte*/
144 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
145 /* second char is the last 2 bits of the first byte and the first 4
146 * bits of the second byte */
147 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
148 /* third char is the last 4 bits of the second byte padded with
149 * two zeroes */
150 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
151 /* fourth char is a = to indicate one byte of padding */
152 *ptr++ = '=';
153 break;
154 case 2:
155 /* first char is the first 6 bits of the first byte*/
156 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
157 /* second char is the last 2 bits of the first byte padded with
158 * four zeroes*/
159 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
160 /* third char is = to indicate padding */
161 *ptr++ = '=';
162 /* fourth char is = to indicate padding */
163 *ptr++ = '=';
164 break;
166 if (sep)
167 strcpy(ptr, sep);
169 return ERROR_SUCCESS;
172 static BOOL BinaryToBase64A(const BYTE *pbBinary,
173 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
175 static const char crlf[] = "\r\n", lf[] = "\n";
176 BOOL ret = TRUE;
177 LPCSTR header = NULL, trailer = NULL, sep;
178 DWORD charsNeeded;
180 if (dwFlags & CRYPT_STRING_NOCR)
181 sep = lf;
182 else if (dwFlags & CRYPT_STRING_NOCRLF)
183 sep = NULL;
184 else
185 sep = crlf;
186 switch (dwFlags & 0x0fffffff)
188 case CRYPT_STRING_BASE64:
189 /* no header or footer */
190 break;
191 case CRYPT_STRING_BASE64HEADER:
192 header = CERT_HEADER;
193 trailer = CERT_TRAILER;
194 break;
195 case CRYPT_STRING_BASE64REQUESTHEADER:
196 header = CERT_REQUEST_HEADER;
197 trailer = CERT_REQUEST_TRAILER;
198 break;
199 case CRYPT_STRING_BASE64X509CRLHEADER:
200 header = X509_HEADER;
201 trailer = X509_TRAILER;
202 break;
205 charsNeeded = 0;
206 encodeBase64A(pbBinary, cbBinary, sep, NULL, &charsNeeded);
207 if (sep)
208 charsNeeded += strlen(sep);
209 if (header)
210 charsNeeded += strlen(header) + strlen(sep);
211 if (trailer)
212 charsNeeded += strlen(trailer) + strlen(sep);
213 if (charsNeeded <= *pcchString)
215 LPSTR ptr = pszString;
216 DWORD size = charsNeeded;
218 if (header)
220 strcpy(ptr, header);
221 ptr += strlen(ptr);
222 if (sep)
224 strcpy(ptr, sep);
225 ptr += strlen(sep);
228 encodeBase64A(pbBinary, cbBinary, sep, ptr, &size);
229 ptr += size - 1;
230 if (trailer)
232 strcpy(ptr, trailer);
233 ptr += strlen(ptr);
234 if (sep)
236 strcpy(ptr, sep);
237 ptr += strlen(sep);
240 *pcchString = charsNeeded - 1;
242 else if (pszString)
244 *pcchString = charsNeeded;
245 SetLastError(ERROR_INSUFFICIENT_BUFFER);
246 ret = FALSE;
248 else
249 *pcchString = charsNeeded;
250 return ret;
253 BOOL WINAPI CryptBinaryToStringA(const BYTE *pbBinary,
254 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
256 BinaryToStringAFunc encoder = NULL;
258 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
259 pcchString);
261 if (!pbBinary)
263 SetLastError(ERROR_INVALID_PARAMETER);
264 return FALSE;
266 if (!pcchString)
268 SetLastError(ERROR_INVALID_PARAMETER);
269 return FALSE;
272 switch (dwFlags & 0x0fffffff)
274 case CRYPT_STRING_BINARY:
275 encoder = EncodeBinaryToBinaryA;
276 break;
277 case CRYPT_STRING_BASE64:
278 case CRYPT_STRING_BASE64HEADER:
279 case CRYPT_STRING_BASE64REQUESTHEADER:
280 case CRYPT_STRING_BASE64X509CRLHEADER:
281 encoder = BinaryToBase64A;
282 break;
283 case CRYPT_STRING_HEX:
284 case CRYPT_STRING_HEXASCII:
285 case CRYPT_STRING_HEXADDR:
286 case CRYPT_STRING_HEXASCIIADDR:
287 FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
288 /* fall through */
289 default:
290 SetLastError(ERROR_INVALID_PARAMETER);
291 return FALSE;
293 return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
296 static LONG encodeBase64W(const BYTE *in_buf, int in_len, LPCWSTR sep,
297 WCHAR* out_buf, DWORD *out_len)
299 int div, i;
300 const BYTE *d = in_buf;
301 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
302 DWORD needed;
303 LPWSTR ptr;
305 TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
306 needed = bytes + pad_bytes + 1;
307 if (sep)
308 needed += (needed / 64 + 1) * strlenW(sep);
310 if (needed > *out_len)
312 *out_len = needed;
313 return ERROR_INSUFFICIENT_BUFFER;
315 else
316 *out_len = needed;
318 /* Three bytes of input give 4 chars of output */
319 div = in_len / 3;
321 ptr = out_buf;
322 i = 0;
323 while (div > 0)
325 if (sep && i && i % 64 == 0)
327 strcpyW(ptr, sep);
328 ptr += strlenW(sep);
330 /* first char is the first 6 bits of the first byte*/
331 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
332 /* second char is the last 2 bits of the first byte and the first 4
333 * bits of the second byte */
334 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
335 /* third char is the last 4 bits of the second byte and the first 2
336 * bits of the third byte */
337 *ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
338 /* fourth char is the remaining 6 bits of the third byte */
339 *ptr++ = b64[ d[2] & 0x3f];
340 i += 4;
341 d += 3;
342 div--;
345 switch(pad_bytes)
347 case 1:
348 /* first char is the first 6 bits of the first byte*/
349 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
350 /* second char is the last 2 bits of the first byte and the first 4
351 * bits of the second byte */
352 *ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
353 /* third char is the last 4 bits of the second byte padded with
354 * two zeroes */
355 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
356 /* fourth char is a = to indicate one byte of padding */
357 *ptr++ = '=';
358 break;
359 case 2:
360 /* first char is the first 6 bits of the first byte*/
361 *ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
362 /* second char is the last 2 bits of the first byte padded with
363 * four zeroes*/
364 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
365 /* third char is = to indicate padding */
366 *ptr++ = '=';
367 /* fourth char is = to indicate padding */
368 *ptr++ = '=';
369 break;
371 if (sep)
372 strcpyW(ptr, sep);
374 return ERROR_SUCCESS;
377 static BOOL BinaryToBase64W(const BYTE *pbBinary,
378 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
380 static const WCHAR crlf[] = { '\r','\n',0 }, lf[] = { '\n',0 };
381 BOOL ret = TRUE;
382 LPCWSTR header = NULL, trailer = NULL, sep;
383 DWORD charsNeeded;
385 if (dwFlags & CRYPT_STRING_NOCR)
386 sep = lf;
387 else if (dwFlags & CRYPT_STRING_NOCRLF)
388 sep = NULL;
389 else
390 sep = crlf;
391 switch (dwFlags & 0x0fffffff)
393 case CRYPT_STRING_BASE64:
394 /* no header or footer */
395 break;
396 case CRYPT_STRING_BASE64HEADER:
397 header = CERT_HEADER_W;
398 trailer = CERT_TRAILER_W;
399 break;
400 case CRYPT_STRING_BASE64REQUESTHEADER:
401 header = CERT_REQUEST_HEADER_W;
402 trailer = CERT_REQUEST_TRAILER_W;
403 break;
404 case CRYPT_STRING_BASE64X509CRLHEADER:
405 header = X509_HEADER_W;
406 trailer = X509_TRAILER_W;
407 break;
410 charsNeeded = 0;
411 encodeBase64W(pbBinary, cbBinary, sep, NULL, &charsNeeded);
412 if (sep)
413 charsNeeded += strlenW(sep);
414 if (header)
415 charsNeeded += strlenW(header) + strlenW(sep);
416 if (trailer)
417 charsNeeded += strlenW(trailer) + strlenW(sep);
418 if (charsNeeded <= *pcchString)
420 LPWSTR ptr = pszString;
421 DWORD size = charsNeeded;
423 if (header)
425 strcpyW(ptr, header);
426 ptr += strlenW(ptr);
427 if (sep)
429 strcpyW(ptr, sep);
430 ptr += strlenW(sep);
433 encodeBase64W(pbBinary, cbBinary, sep, ptr, &size);
434 ptr += size - 1;
435 if (trailer)
437 strcpyW(ptr, trailer);
438 ptr += strlenW(ptr);
439 if (sep)
441 strcpyW(ptr, sep);
442 ptr += strlenW(sep);
445 *pcchString = charsNeeded - 1;
447 else if (pszString)
449 *pcchString = charsNeeded;
450 SetLastError(ERROR_INSUFFICIENT_BUFFER);
451 ret = FALSE;
453 else
454 *pcchString = charsNeeded;
455 return ret;
458 BOOL WINAPI CryptBinaryToStringW(const BYTE *pbBinary,
459 DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
461 BinaryToStringWFunc encoder = NULL;
463 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
464 pcchString);
466 if (!pbBinary)
468 SetLastError(ERROR_INVALID_PARAMETER);
469 return FALSE;
471 if (!pcchString)
473 SetLastError(ERROR_INVALID_PARAMETER);
474 return FALSE;
477 switch (dwFlags & 0x0fffffff)
479 case CRYPT_STRING_BASE64:
480 case CRYPT_STRING_BASE64HEADER:
481 case CRYPT_STRING_BASE64REQUESTHEADER:
482 case CRYPT_STRING_BASE64X509CRLHEADER:
483 encoder = BinaryToBase64W;
484 break;
485 case CRYPT_STRING_BINARY:
486 case CRYPT_STRING_HEX:
487 case CRYPT_STRING_HEXASCII:
488 case CRYPT_STRING_HEXADDR:
489 case CRYPT_STRING_HEXASCIIADDR:
490 FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
491 /* fall through */
492 default:
493 SetLastError(ERROR_INVALID_PARAMETER);
494 return FALSE;
496 return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
499 static inline BYTE decodeBase64Byte(int c)
501 BYTE ret;
503 if (c >= 'A' && c <= 'Z')
504 ret = c - 'A';
505 else if (c >= 'a' && c <= 'z')
506 ret = c - 'a' + 26;
507 else if (c >= '0' && c <= '9')
508 ret = c - '0' + 52;
509 else if (c == '+')
510 ret = 62;
511 else if (c == '/')
512 ret = 63;
513 else
514 ret = 64;
515 return ret;
518 static LONG decodeBase64Block(const char *in_buf, int in_len,
519 const char **nextBlock, PBYTE out_buf, DWORD *out_len)
521 int len = in_len, i;
522 const char *d = in_buf;
523 int ip0, ip1, ip2, ip3;
525 if (len < 4)
526 return ERROR_INVALID_DATA;
528 i = 0;
529 if (d[2] == '=')
531 if ((ip0 = decodeBase64Byte(d[0])) > 63)
532 return ERROR_INVALID_DATA;
533 if ((ip1 = decodeBase64Byte(d[1])) > 63)
534 return ERROR_INVALID_DATA;
536 if (out_buf)
537 out_buf[i] = (ip0 << 2) | (ip1 >> 4);
538 i++;
540 else if (d[3] == '=')
542 if ((ip0 = decodeBase64Byte(d[0])) > 63)
543 return ERROR_INVALID_DATA;
544 if ((ip1 = decodeBase64Byte(d[1])) > 63)
545 return ERROR_INVALID_DATA;
546 if ((ip2 = decodeBase64Byte(d[2])) > 63)
547 return ERROR_INVALID_DATA;
549 if (out_buf)
551 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
552 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
554 i += 2;
556 else
558 if ((ip0 = decodeBase64Byte(d[0])) > 63)
559 return ERROR_INVALID_DATA;
560 if ((ip1 = decodeBase64Byte(d[1])) > 63)
561 return ERROR_INVALID_DATA;
562 if ((ip2 = decodeBase64Byte(d[2])) > 63)
563 return ERROR_INVALID_DATA;
564 if ((ip3 = decodeBase64Byte(d[3])) > 63)
565 return ERROR_INVALID_DATA;
567 if (out_buf)
569 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
570 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
571 out_buf[i + 2] = (ip2 << 6) | ip3;
573 i += 3;
575 if (len >= 6 && d[4] == '\r' && d[5] == '\n')
576 *nextBlock = d + 6;
577 else if (len >= 5 && d[4] == '\n')
578 *nextBlock = d + 5;
579 else if (len >= 4 && d[4])
580 *nextBlock = d + 4;
581 else
582 *nextBlock = NULL;
583 *out_len = i;
584 return ERROR_SUCCESS;
587 /* Unlike CryptStringToBinaryA, cchString is guaranteed to be the length of the
588 * string to convert.
590 typedef LONG (*StringToBinaryAFunc)(LPCSTR pszString, DWORD cchString,
591 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
593 static LONG Base64ToBinaryA(LPCSTR pszString, DWORD cchString,
594 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
596 LONG ret = ERROR_SUCCESS;
597 const char *nextBlock;
598 DWORD outLen = 0;
600 nextBlock = pszString;
601 while (nextBlock && !ret)
603 DWORD len = 0;
605 ret = decodeBase64Block(nextBlock, cchString - (nextBlock - pszString),
606 &nextBlock, pbBinary ? pbBinary + outLen : NULL, &len);
607 if (!ret)
608 outLen += len;
609 if (cchString - (nextBlock - pszString) <= 0)
610 nextBlock = NULL;
612 *pcbBinary = outLen;
613 if (!ret)
615 if (pdwSkip)
616 *pdwSkip = 0;
617 if (pdwFlags)
618 *pdwFlags = CRYPT_STRING_BASE64;
620 else if (ret == ERROR_INSUFFICIENT_BUFFER)
622 if (!pbBinary)
623 ret = ERROR_SUCCESS;
625 return ret;
628 static LONG Base64WithHeaderAndTrailerToBinaryA(LPCSTR pszString,
629 DWORD cchString, LPCSTR header, LPCSTR trailer, BYTE *pbBinary,
630 DWORD *pcbBinary, DWORD *pdwSkip)
632 LONG ret;
633 LPCSTR ptr;
635 if (cchString > strlen(header) + strlen(trailer)
636 && (ptr = strstr(pszString, header)) != NULL)
638 LPCSTR trailerSpot = pszString + cchString - strlen(trailer);
640 if (pszString[cchString - 1] == '\n')
642 cchString--;
643 trailerSpot--;
645 if (pszString[cchString - 1] == '\r')
647 cchString--;
648 trailerSpot--;
650 if (!strncmp(trailerSpot, trailer, strlen(trailer)))
652 if (pdwSkip)
653 *pdwSkip = ptr - pszString;
654 ptr += strlen(header);
655 if (*ptr == '\r') ptr++;
656 if (*ptr == '\n') ptr++;
657 cchString -= ptr - pszString + strlen(trailer);
658 ret = Base64ToBinaryA(ptr, cchString, pbBinary, pcbBinary, NULL,
659 NULL);
661 else
662 ret = ERROR_INVALID_DATA;
664 else
665 ret = ERROR_INVALID_DATA;
666 return ret;
669 static LONG Base64HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
670 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
672 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
673 CERT_HEADER, CERT_TRAILER, pbBinary, pcbBinary, pdwSkip);
675 if (!ret && pdwFlags)
676 *pdwFlags = CRYPT_STRING_BASE64HEADER;
677 return ret;
680 static LONG Base64RequestHeaderToBinaryA(LPCSTR pszString, DWORD cchString,
681 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
683 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
684 CERT_REQUEST_HEADER, CERT_REQUEST_TRAILER, pbBinary, pcbBinary, pdwSkip);
686 if (!ret && pdwFlags)
687 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
688 return ret;
691 static LONG Base64X509HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
692 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
694 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
695 X509_HEADER, X509_TRAILER, pbBinary, pcbBinary, pdwSkip);
697 if (!ret && pdwFlags)
698 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
699 return ret;
702 static LONG Base64AnyToBinaryA(LPCSTR pszString, DWORD cchString,
703 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
705 LONG ret;
707 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
708 pdwSkip, pdwFlags);
709 if (ret == ERROR_INVALID_DATA)
710 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
711 pdwSkip, pdwFlags);
712 return ret;
715 static LONG DecodeBinaryToBinaryA(LPCSTR pszString, DWORD cchString,
716 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
718 LONG ret = ERROR_SUCCESS;
720 if (*pcbBinary < cchString)
722 if (!pbBinary)
723 *pcbBinary = cchString;
724 else
726 ret = ERROR_INSUFFICIENT_BUFFER;
727 *pcbBinary = cchString;
730 else
732 if (cchString)
733 memcpy(pbBinary, pszString, cchString);
734 *pcbBinary = cchString;
736 return ret;
739 static LONG DecodeAnyA(LPCSTR pszString, DWORD cchString,
740 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
742 LONG ret;
744 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
745 pdwSkip, pdwFlags);
746 if (ret == ERROR_INVALID_DATA)
747 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
748 pdwSkip, pdwFlags);
749 if (ret == ERROR_INVALID_DATA)
750 ret = DecodeBinaryToBinaryA(pszString, cchString, pbBinary, pcbBinary,
751 pdwSkip, pdwFlags);
752 return ret;
755 BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString,
756 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
757 DWORD *pdwSkip, DWORD *pdwFlags)
759 StringToBinaryAFunc decoder;
760 LONG ret;
762 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_a(pszString),
763 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
765 if (!pszString)
767 SetLastError(ERROR_INVALID_PARAMETER);
768 return FALSE;
770 /* Only the bottom byte contains valid types */
771 if (dwFlags & 0xfffffff0)
773 SetLastError(ERROR_INVALID_DATA);
774 return FALSE;
776 switch (dwFlags)
778 case CRYPT_STRING_BASE64_ANY:
779 decoder = Base64AnyToBinaryA;
780 break;
781 case CRYPT_STRING_BASE64:
782 decoder = Base64ToBinaryA;
783 break;
784 case CRYPT_STRING_BASE64HEADER:
785 decoder = Base64HeaderToBinaryA;
786 break;
787 case CRYPT_STRING_BASE64REQUESTHEADER:
788 decoder = Base64RequestHeaderToBinaryA;
789 break;
790 case CRYPT_STRING_BASE64X509CRLHEADER:
791 decoder = Base64X509HeaderToBinaryA;
792 break;
793 case CRYPT_STRING_BINARY:
794 decoder = DecodeBinaryToBinaryA;
795 break;
796 case CRYPT_STRING_ANY:
797 decoder = DecodeAnyA;
798 break;
799 case CRYPT_STRING_HEX:
800 case CRYPT_STRING_HEXASCII:
801 case CRYPT_STRING_HEXADDR:
802 case CRYPT_STRING_HEXASCIIADDR:
803 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
804 /* fall through */
805 default:
806 SetLastError(ERROR_INVALID_PARAMETER);
807 return FALSE;
809 if (!cchString)
810 cchString = strlen(pszString);
811 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
812 if (ret)
813 SetLastError(ret);
814 return (ret == ERROR_SUCCESS) ? TRUE : FALSE;
817 static LONG decodeBase64BlockW(const WCHAR *in_buf, int in_len,
818 const WCHAR **nextBlock, PBYTE out_buf, DWORD *out_len)
820 int len = in_len, i;
821 const WCHAR *d = in_buf;
822 int ip0, ip1, ip2, ip3;
824 if (len < 4)
825 return ERROR_INVALID_DATA;
827 i = 0;
828 if (d[2] == '=')
830 if ((ip0 = decodeBase64Byte(d[0])) > 63)
831 return ERROR_INVALID_DATA;
832 if ((ip1 = decodeBase64Byte(d[1])) > 63)
833 return ERROR_INVALID_DATA;
835 if (out_buf)
836 out_buf[i] = (ip0 << 2) | (ip1 >> 4);
837 i++;
839 else if (d[3] == '=')
841 if ((ip0 = decodeBase64Byte(d[0])) > 63)
842 return ERROR_INVALID_DATA;
843 if ((ip1 = decodeBase64Byte(d[1])) > 63)
844 return ERROR_INVALID_DATA;
845 if ((ip2 = decodeBase64Byte(d[2])) > 63)
846 return ERROR_INVALID_DATA;
848 if (out_buf)
850 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
851 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
853 i += 2;
855 else
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;
863 if ((ip3 = decodeBase64Byte(d[3])) > 63)
864 return ERROR_INVALID_DATA;
866 if (out_buf)
868 out_buf[i + 0] = (ip0 << 2) | (ip1 >> 4);
869 out_buf[i + 1] = (ip1 << 4) | (ip2 >> 2);
870 out_buf[i + 2] = (ip2 << 6) | ip3;
872 i += 3;
874 if (len >= 6 && d[4] == '\r' && d[5] == '\n')
875 *nextBlock = d + 6;
876 else if (len >= 5 && d[4] == '\n')
877 *nextBlock = d + 5;
878 else if (len >= 4 && d[4])
879 *nextBlock = d + 4;
880 else
881 *nextBlock = NULL;
882 *out_len = i;
883 return ERROR_SUCCESS;
886 /* Unlike CryptStringToBinaryW, cchString is guaranteed to be the length of the
887 * string to convert.
889 typedef LONG (*StringToBinaryWFunc)(LPCWSTR pszString, DWORD cchString,
890 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
892 static LONG Base64ToBinaryW(LPCWSTR pszString, DWORD cchString,
893 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
895 LONG ret = ERROR_SUCCESS;
896 const WCHAR *nextBlock;
897 DWORD outLen = 0;
899 nextBlock = pszString;
900 while (nextBlock && !ret)
902 DWORD len = 0;
904 ret = decodeBase64BlockW(nextBlock, cchString - (nextBlock - pszString),
905 &nextBlock, pbBinary ? pbBinary + outLen : NULL, &len);
906 if (!ret)
907 outLen += len;
908 if (cchString - (nextBlock - pszString) <= 0)
909 nextBlock = NULL;
911 *pcbBinary = outLen;
912 if (!ret)
914 if (pdwSkip)
915 *pdwSkip = 0;
916 if (pdwFlags)
917 *pdwFlags = CRYPT_STRING_BASE64;
919 else if (ret == ERROR_INSUFFICIENT_BUFFER)
921 if (!pbBinary)
922 ret = ERROR_SUCCESS;
924 return ret;
927 static LONG Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString,
928 DWORD cchString, LPCWSTR header, LPCWSTR trailer, BYTE *pbBinary,
929 DWORD *pcbBinary, DWORD *pdwSkip)
931 LONG ret;
932 LPCWSTR ptr;
934 if (cchString > strlenW(header) + strlenW(trailer)
935 && (ptr = strstrW(pszString, header)) != NULL)
937 LPCWSTR trailerSpot = pszString + cchString - strlenW(trailer);
939 if (pszString[cchString - 1] == '\n')
941 cchString--;
942 trailerSpot--;
944 if (pszString[cchString - 1] == '\r')
946 cchString--;
947 trailerSpot--;
949 if (!strncmpW(trailerSpot, trailer, strlenW(trailer)))
951 if (pdwSkip)
952 *pdwSkip = ptr - pszString;
953 ptr += strlenW(header);
954 if (*ptr == '\r') ptr++;
955 if (*ptr == '\n') ptr++;
956 cchString -= ptr - pszString + strlenW(trailer);
957 ret = Base64ToBinaryW(ptr, cchString, pbBinary, pcbBinary, NULL,
958 NULL);
960 else
961 ret = ERROR_INVALID_DATA;
963 else
964 ret = ERROR_INVALID_DATA;
965 return ret;
968 static LONG Base64HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
969 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
971 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
972 CERT_HEADER_W, CERT_TRAILER_W, pbBinary, pcbBinary, pdwSkip);
974 if (!ret && pdwFlags)
975 *pdwFlags = CRYPT_STRING_BASE64HEADER;
976 return ret;
979 static LONG Base64RequestHeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
980 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
982 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
983 CERT_REQUEST_HEADER_W, CERT_REQUEST_TRAILER_W, pbBinary, pcbBinary,
984 pdwSkip);
986 if (!ret && pdwFlags)
987 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
988 return ret;
991 static LONG Base64X509HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
992 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
994 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
995 X509_HEADER_W, X509_TRAILER_W, pbBinary, pcbBinary, pdwSkip);
997 if (!ret && pdwFlags)
998 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
999 return ret;
1002 static LONG Base64AnyToBinaryW(LPCWSTR pszString, DWORD cchString,
1003 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1005 LONG ret;
1007 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1008 pdwSkip, pdwFlags);
1009 if (ret == ERROR_INVALID_DATA)
1010 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1011 pdwSkip, pdwFlags);
1012 return ret;
1015 static LONG DecodeBinaryToBinaryW(LPCWSTR pszString, DWORD cchString,
1016 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1018 LONG ret = ERROR_SUCCESS;
1020 if (*pcbBinary < cchString)
1022 if (!pbBinary)
1023 *pcbBinary = cchString;
1024 else
1026 ret = ERROR_INSUFFICIENT_BUFFER;
1027 *pcbBinary = cchString;
1030 else
1032 if (cchString)
1033 memcpy(pbBinary, pszString, cchString * sizeof(WCHAR));
1034 *pcbBinary = cchString * sizeof(WCHAR);
1036 return ret;
1039 static LONG DecodeAnyW(LPCWSTR pszString, DWORD cchString,
1040 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
1042 LONG ret;
1044 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1045 pdwSkip, pdwFlags);
1046 if (ret == ERROR_INVALID_DATA)
1047 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1048 pdwSkip, pdwFlags);
1049 if (ret == ERROR_INVALID_DATA)
1050 ret = DecodeBinaryToBinaryW(pszString, cchString, pbBinary, pcbBinary,
1051 pdwSkip, pdwFlags);
1052 return ret;
1055 BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString,
1056 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
1057 DWORD *pdwSkip, DWORD *pdwFlags)
1059 StringToBinaryWFunc decoder;
1060 LONG ret;
1062 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_w(pszString),
1063 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1065 if (!pszString)
1067 SetLastError(ERROR_INVALID_PARAMETER);
1068 return FALSE;
1070 /* Only the bottom byte contains valid types */
1071 if (dwFlags & 0xfffffff0)
1073 SetLastError(ERROR_INVALID_DATA);
1074 return FALSE;
1076 switch (dwFlags)
1078 case CRYPT_STRING_BASE64_ANY:
1079 decoder = Base64AnyToBinaryW;
1080 break;
1081 case CRYPT_STRING_BASE64:
1082 decoder = Base64ToBinaryW;
1083 break;
1084 case CRYPT_STRING_BASE64HEADER:
1085 decoder = Base64HeaderToBinaryW;
1086 break;
1087 case CRYPT_STRING_BASE64REQUESTHEADER:
1088 decoder = Base64RequestHeaderToBinaryW;
1089 break;
1090 case CRYPT_STRING_BASE64X509CRLHEADER:
1091 decoder = Base64X509HeaderToBinaryW;
1092 break;
1093 case CRYPT_STRING_BINARY:
1094 decoder = DecodeBinaryToBinaryW;
1095 break;
1096 case CRYPT_STRING_ANY:
1097 decoder = DecodeAnyW;
1098 break;
1099 case CRYPT_STRING_HEX:
1100 case CRYPT_STRING_HEXASCII:
1101 case CRYPT_STRING_HEXADDR:
1102 case CRYPT_STRING_HEXASCIIADDR:
1103 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
1104 /* fall through */
1105 default:
1106 SetLastError(ERROR_INVALID_PARAMETER);
1107 return FALSE;
1109 if (!cchString)
1110 cchString = strlenW(pszString);
1111 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1112 if (ret)
1113 SetLastError(ret);
1114 return (ret == ERROR_SUCCESS) ? TRUE : FALSE;