41#define NONAMELESSUNION
53#define MAX_ENCODED_LEN 0x02000000
55#define ASN_FLAGS_MASK 0xe0
56#define ASN_TYPE_MASK 0x1f
113#define GET_LEN_BYTES(b) ((b) <= 0x80 ? 1 : 1 + ((b) & 0x7f))
153 if (lenLen >
sizeof(
DWORD) + 1)
158 else if (lenLen + 2 > cbEncoded)
173 if (
out + lenLen + 1 > cbEncoded)
215 if (pDecodePara && pDecodePara->
pfnAlloc)
216 *(
BYTE **)pvStructInfo = pDecodePara->
pfnAlloc(bytesNeeded);
219 if (!*(
BYTE **)pvStructInfo)
222 *pcbStructInfo = bytesNeeded;
224 else if (*pcbStructInfo < bytesNeeded)
226 *pcbStructInfo = bytesNeeded;
231 *pcbStructInfo = bytesNeeded;
237 if (pDecodePara && pDecodePara->
pfnFree)
250 if (*pcbStructInfo < bytesNeeded)
252 *pcbStructInfo = bytesNeeded;
258 *pcbStructInfo = bytesNeeded;
298#define FINALMEMBERSIZE(s, member) (sizeof(s) - offsetof(s, member))
299#define MEMBERSIZE(s, member, nextmember) \
300 (offsetof(s, nextmember) - offsetof(s, member))
311 void *pvStructInfo,
BYTE *nextData,
DWORD *cbDecoded)
317 TRACE(
"%p, %d, %p, %d, %08x, %p, %p, %p\n",
items, cItem,
pbEncoded,
318 cbEncoded,
dwFlags, pvStructInfo, nextData, cbDecoded);
333 DWORD itemEncodedLen;
338 itemEncodedLen = 1 + itemLenBytes + itemLen;
339 if (nextData && pvStructInfo &&
items[
i].hasPointer)
341 TRACE(
"Setting next pointer to %p\n",
344 items[
i].pointerOffset) = nextData;
351 TRACE(
"decoding item %d\n",
i);
353 TRACE(
"sizing item %d\n",
i);
368 if (nextData &&
items[
i].hasPointer &&
371 if (itemDecoded > itemEncodedLen)
373 WARN(
"decoded length %d exceeds encoded %d\n",
374 itemDecoded, itemEncodedLen);
381 decoded += itemDecoded;
382 TRACE(
"item %d: decoded %d bytes\n",
i,
389 TRACE(
"skipping optional item %d\n",
i);
395 TRACE(
"item %d failed: %08x\n",
i,
400 ERR(
"can't use indefinite length encoding without a decoder\n");
406 TRACE(
"item %d: decoded %d bytes\n",
i, itemEncodedLen);
407 ptr += itemEncodedLen;
408 decoded += itemEncodedLen;
414 TRACE(
"skipping optional item %d\n",
i);
419 TRACE(
"item %d: tag %02x doesn't match expected %02x\n",
428 TRACE(
"missing optional item %d, skipping\n",
i);
433 TRACE(
"not enough bytes for item %d, failing\n",
i);
439 *cbDecoded = decoded;
454 DWORD *pcbDecoded,
void *startingPointer)
458 TRACE(
"%p, %d, %p, %d, %08x, %p, %p, %d, %p\n",
items, cItem,
pbEncoded,
459 cbEncoded,
dwFlags, pDecodePara, pvStructInfo, *pcbStructInfo,
477 cbEncoded -= 1 + lenBytes;
484 else if (cbEncoded < dataLen)
486 TRACE(
"dataLen %d exceeds cbEncoded %d, failing\n", dataLen,
497 if (cbDecoded > cbEncoded - 2)
503 else if (*(
ptr + cbDecoded) != 0 ||
504 *(
ptr + cbDecoded + 1) != 0)
506 TRACE(
"expected 0 TLV\n");
514 if (
ret && !indefinite && cbDecoded != dataLen)
516 TRACE(
"expected %d decoded, got %d, failing\n", dataLen,
523 DWORD i, bytesNeeded = 0, structSize = 0;
525 for (
i = 0;
i < cItem;
i++)
531 bytesNeeded += structSize;
533 *pcbDecoded = 1 + lenBytes + cbDecoded;
535 *pcbStructInfo = bytesNeeded;
537 pDecodePara, pvStructInfo, pcbStructInfo, bytesNeeded)))
542 pvStructInfo = *(
BYTE **)pvStructInfo;
544 nextData = startingPointer;
546 nextData = (
BYTE *)pvStructInfo + structSize;
547 memset(pvStructInfo, 0, structSize);
549 ptr, dataLen,
dwFlags, pvStructInfo, nextData,
621 cbEncoded, pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
640 decoded = 1 + lenBytes;
666 DWORD itemEncoded, itemDataLen, itemDecoded,
size = 0;
687 if (itemSizes != &itemSize)
696 *itemSizes = itemSize;
700 decoded += itemDecoded;
701 itemSizes[cItems - 1].
encodedLen = itemEncoded;
702 itemSizes[cItems - 1].size =
size;
715 *pcbDecoded = decoded;
717 *pcbStructInfo = bytesNeeded;
719 pvStructInfo, pcbStructInfo, bytesNeeded)))
727 pvStructInfo = *(
void **)pvStructInfo;
732 rgItems = (
BYTE *)pvStructInfo +
741 nextData = (
BYTE *)rgItems + cItems * arrayDesc->
itemSize;
755 &itemSizes[
i].size, &itemDecoded);
758 nextData += itemSizes[
i].size - arrayDesc->
itemSize;
766 if (itemSizes != &itemSize)
796 bytesNeeded += 1 + lenBytes + dataLen;
799 *pcbDecoded = 1 + lenBytes + dataLen;
801 *pcbStructInfo = bytesNeeded;
807 pvStructInfo = *(
BYTE **)pvStructInfo;
809 blob->cbData = 1 + lenBytes + dataLen;
838 pvStructInfo, *pcbStructInfo, pcbDecoded);
846 if (
ret && pvStructInfo)
855 for (
i = 0;
i <
blob->cbData / 2;
i++)
874 pDecodePara, pvStructInfo, *pcbStructInfo);
921 *pcbDecoded = 1 + lenBytes + dataLen;
956 pvStructInfo, *pcbStructInfo, pcbDecoded);
959 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
975 dataLen,
dwFlags, pvStructInfo, pcbStructInfo,
NULL);
976 if (
ret && pcbDecoded)
977 *pcbDecoded = 1 + lenBytes + dataLen;
1008 SubjectPublicKeyInfo.Algorithm.Parameters.pbData), 0 },
1021 pDecodePara, pvStructInfo, *pcbStructInfo);
1026 if (
ret && pvStructInfo)
1033 info = pvStructInfo;
1034 if (!
info->SerialNumber.cbData || !
info->Issuer.cbData ||
1035 !
info->Subject.cbData)
1056 pDecodePara, pvStructInfo, *pcbStructInfo);
1069 &signedCert, &
size);
1076 pvStructInfo, pcbStructInfo);
1086 pDecodePara, pvStructInfo, pcbStructInfo);
1111 pvStructInfo, *pcbStructInfo, pcbDecoded);
1114 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
1143 WARN(
"empty CRL entry serial number\n");
1164 pvStructInfo, *pcbStructInfo, pcbDecoded);
1167 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
1184 pvStructInfo, *pcbStructInfo, pcbDecoded);
1187 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
1203 dataLen,
dwFlags, pvStructInfo, pcbStructInfo,
NULL);
1204 if (
ret && pcbDecoded)
1205 *pcbDecoded = 1 + lenBytes + dataLen;
1237 pDecodePara, pvStructInfo, *pcbStructInfo);
1240 pDecodePara, pvStructInfo, pcbStructInfo,
NULL,
NULL);
1253 pDecodePara, pvStructInfo, *pcbStructInfo);
1273 pvStructInfo, pcbStructInfo);
1283 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo);
1303 pvStructInfo, *pcbStructInfo);
1348 *pcbDecoded = 1 + lenBytes + dataLen;
1350 *pcbStructInfo = bytesNeeded;
1351 else if (*pcbStructInfo < bytesNeeded)
1353 *pcbStructInfo = bytesNeeded;
1368 pszObjId +=
strlen(pszObjId);
1384 pszObjId +=
strlen(pszObjId);
1389 *pcbStructInfo = bytesNeeded;
1401 pvStructInfo, *pcbStructInfo);
1405 pvStructInfo, pcbStructInfo, pcbDecoded);
1434 TRACE(
"ext->pszObjId is %p\n",
ext->pszObjId);
1439 TRACE(
"ext->pszObjId is %p (%s)\n",
ext->pszObjId,
1452 pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
1467 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
NULL);
1500 bytesNeeded += dataLen;
1505 bytesNeeded += dataLen;
1510 bytesNeeded += dataLen;
1515 bytesNeeded += dataLen;
1520 bytesNeeded += dataLen;
1525 bytesNeeded += dataLen;
1530 bytesNeeded += dataLen;
1535 bytesNeeded += dataLen;
1540 bytesNeeded += dataLen;
1543 FIXME(
"ASN_UNIVERSALSTRING: unimplemented\n");
1548 bytesNeeded += dataLen;
1561 *pcbDecoded = 1 + lenBytes + dataLen;
1563 *pcbStructInfo = bytesNeeded;
1564 else if (*pcbStructInfo < bytesNeeded)
1566 *pcbStructInfo = bytesNeeded;
1572 *pcbStructInfo = bytesNeeded;
1573 value->dwValueType = valueType;
1590 value->Value.cbData = dataLen;
1605 value->Value.cbData = dataLen;
1606 for (
i = 0;
i < dataLen / 2;
i++)
1624 value->Value.cbData = 0;
1642 if (
ret && pvStructInfo)
1645 pcbStructInfo, *pcbStructInfo);
1651 pvStructInfo = *(
BYTE **)pvStructInfo;
1652 value = pvStructInfo;
1656 pcbStructInfo,
NULL);
1689 bytesNeeded += (dataLen + 1) * 2;
1694 bytesNeeded += (dataLen + 1) * 2;
1699 bytesNeeded += (dataLen + 1) * 2;
1704 bytesNeeded += (dataLen + 1) * 2;
1709 bytesNeeded += (dataLen + 1) * 2;
1714 bytesNeeded += (dataLen + 1) * 2;
1719 bytesNeeded += (dataLen + 1) * 2;
1724 bytesNeeded += (dataLen + 1) * 2;
1729 bytesNeeded += dataLen / 2 +
sizeof(
WCHAR);
1734 bytesNeeded += dataLen +
sizeof(
WCHAR);
1748 *pcbDecoded = 1 + lenBytes + dataLen;
1750 *pcbStructInfo = bytesNeeded;
1751 else if (*pcbStructInfo < bytesNeeded)
1753 *pcbStructInfo = bytesNeeded;
1759 *pcbStructInfo = bytesNeeded;
1760 value->dwValueType = valueType;
1777 value->Value.cbData = dataLen * 2;
1778 for (
i = 0;
i < dataLen;
i++)
1783 value->Value.cbData = dataLen / 2;
1784 for (
i = 0;
i < dataLen / 4;
i++)
1790 value->Value.cbData = dataLen;
1791 for (
i = 0;
i < dataLen / 2;
i++)
1807 value->Value.cbData = 0;
1825 if (
ret && pvStructInfo)
1828 pcbStructInfo, *pcbStructInfo);
1834 pvStructInfo = *(
BYTE **)pvStructInfo;
1835 value = pvStructInfo;
1839 pcbStructInfo,
NULL);
1869 pvStructInfo, *pcbStructInfo);
1872 TRACE(
"attr->pszObjId is %p\n",
attr->pszObjId);
1878 TRACE(
"attr->pszObjId is %p (%s)\n",
attr->pszObjId,
1880 TRACE(
"attr->dwValueType is %d\n",
attr->dwValueType);
1897 NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
1914 DWORD bytesNeeded = 0;
1922 *pcbStructInfo = bytesNeeded;
1924 pvStructInfo, pcbStructInfo, bytesNeeded)))
1929 pvStructInfo = *(
BYTE **)pvStructInfo;
1930 info = pvStructInfo;
1935 &bytesNeeded,
NULL);
1966 pvStructInfo, *pcbStructInfo);
1969 TRACE(
"attr->pszObjId is %p\n",
attr->pszObjId);
1975 TRACE(
"attr->pszObjId is %p (%s)\n",
attr->pszObjId,
1977 TRACE(
"attr->dwValueType is %d\n",
attr->dwValueType);
1994 NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2011 DWORD bytesNeeded = 0;
2019 *pcbStructInfo = bytesNeeded;
2021 pvStructInfo, pcbStructInfo, bytesNeeded)))
2026 pvStructInfo = *(
BYTE **)pvStructInfo;
2027 info = pvStructInfo;
2032 &bytesNeeded,
NULL);
2051 DWORD indefiniteNestingLevels = 0, decoded = 0;
2067 indefiniteNestingLevels++;
2069 cbEncoded -= 1 + lenBytes;
2070 decoded += 1 + lenBytes;
2071 TRACE(
"indefiniteNestingLevels = %d\n",
2072 indefiniteNestingLevels);
2077 indefiniteNestingLevels)
2079 indefiniteNestingLevels--;
2080 TRACE(
"indefiniteNestingLevels = %d\n",
2081 indefiniteNestingLevels);
2084 cbEncoded -= 1 + lenBytes + dataLen;
2085 decoded += 1 + lenBytes + dataLen;
2086 if (!indefiniteNestingLevels)
2090 }
while (
ret && !done);
2092 if (
ret && indefiniteNestingLevels)
2098 *pcbDecoded = decoded;
2099 TRACE(
"returning %d (%d)\n",
ret,
ret ? *pcbDecoded : 0);
2111 pvStructInfo, *pcbStructInfo);
2116 bytesNeeded += encodedLen;
2118 *pcbStructInfo = bytesNeeded;
2119 else if (*pcbStructInfo < bytesNeeded)
2122 *pcbStructInfo = bytesNeeded;
2129 *pcbStructInfo = bytesNeeded;
2130 blob->cbData = encodedLen;
2145 *pcbDecoded = encodedLen;
2161 NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2177 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2216 pvStructInfo, *pcbStructInfo, pcbDecoded);
2219 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2235 pvStructInfo, *pcbStructInfo, pcbDecoded);
2238 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2254 dataLen,
dwFlags, pvStructInfo, pcbStructInfo,
NULL);
2255 if (
ret && pcbDecoded)
2256 *pcbDecoded = 1 + lenBytes + dataLen;
2268 pDecodePara, pvStructInfo, *pcbStructInfo);
2330 pvStructInfo, *pcbStructInfo);
2346 pDecodePara, pvStructInfo, *pcbStructInfo);
2361 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
NULL);
2378 LPSTR *pStr = pvStructInfo;
2383 DWORD bytesNeeded =
sizeof(
LPSTR) +
sizeof(
char);
2392 bytesNeeded += dataLen;
2394 *pcbDecoded = 1 + lenBytes + dataLen;
2396 *pcbStructInfo = bytesNeeded;
2397 else if (*pcbStructInfo < bytesNeeded)
2399 *pcbStructInfo = bytesNeeded;
2405 *pcbStructInfo = bytesNeeded;
2434 pvStructInfo, pvStructInfo ? *pcbDecoded : 0);
2437 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2455 rgNoticeNumbers), 0 },
2457 DWORD bytesNeeded = 0;
2460 pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
2475 *pcbStructInfo = bytesNeeded;
2476 else if (*pcbStructInfo < bytesNeeded)
2478 *pcbStructInfo = bytesNeeded;
2486 *pcbStructInfo = bytesNeeded;
2522 bytesNeeded += (dataLen + 1) * 2;
2526 bytesNeeded += (dataLen + 1) * 2;
2530 bytesNeeded += (dataLen + 1) * 2;
2534 bytesNeeded += (dataLen + 1) * 2;
2538 bytesNeeded += (dataLen + 1) * 2;
2542 bytesNeeded += (dataLen + 1) * 2;
2546 bytesNeeded += (dataLen + 1) * 2;
2550 bytesNeeded += (dataLen + 1) * 2;
2554 bytesNeeded += dataLen / 2 +
sizeof(
WCHAR);
2558 bytesNeeded += dataLen +
sizeof(
WCHAR);
2571 *pcbDecoded = 1 + lenBytes + dataLen;
2573 *pcbStructInfo = bytesNeeded;
2574 else if (*pcbStructInfo < bytesNeeded)
2576 *pcbStructInfo = bytesNeeded;
2582 LPWSTR *pStr = pvStructInfo;
2584 *pcbStructInfo = bytesNeeded;
2601 for (
i = 0;
i < dataLen;
i++)
2606 for (
i = 0;
i < dataLen / 4;
i++)
2612 for (
i = 0;
i < dataLen / 2;
i++)
2651 pvStructInfo, *pcbStructInfo);
2663 void *pvStructInfo,
DWORD *pcbStructInfo)
2668 pDecodePara, pvStructInfo, *pcbStructInfo);
2672 DWORD bytesNeeded = 0;
2680 *pcbStructInfo = bytesNeeded;
2682 pvStructInfo, pcbStructInfo, bytesNeeded)))
2687 pvStructInfo = *(
BYTE **)pvStructInfo;
2689 notice->pNoticeReference =
2691 ((
BYTE *)pvStructInfo +
2695 pvStructInfo, &bytesNeeded,
NULL);
2722 pvStructInfo, pvStructInfo ? *pcbStructInfo : 0, pcbDecoded);
2725 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2746 pvStructInfo, *pcbStructInfo);
2762 pDecodePara, pvStructInfo, *pcbStructInfo);
2766 DWORD bytesNeeded = 0;
2773 *pcbStructInfo = bytesNeeded;
2775 pvStructInfo, pcbStructInfo, bytesNeeded)))
2780 pvStructInfo = *(
BYTE **)pvStructInfo;
2781 attr = pvStructInfo;
2813 NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
2824 pDecodePara, pvStructInfo, *pcbStructInfo);
2838 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
NULL);
2864 pvStructInfo, *pcbStructInfo, pcbDecoded);
2869 if (
ret && pvStructInfo)
2886 Algorithm.pszObjId) },
2895 pcbDecoded,
info ?
info->Algorithm.Parameters.pbData :
NULL);
2907 DWORD bytesNeeded = 0;
2913 *pcbStructInfo = bytesNeeded;
2915 pvStructInfo, pcbStructInfo, bytesNeeded)))
2920 pvStructInfo = *(
BYTE **)pvStructInfo;
2921 info = pvStructInfo;
2922 info->Algorithm.Parameters.pbData = (
BYTE *)pvStructInfo +
2926 &bytesNeeded,
NULL);
2965 *pcbStructInfo =
sizeof(
BOOL);
2968 else if (*pcbStructInfo <
sizeof(
BOOL))
2970 *pcbStructInfo =
sizeof(
BOOL);
2976 *pcbStructInfo =
sizeof(
BOOL);
2992 pvStructInfo, *pcbStructInfo);
3000 if (1 + lenBytes > cbEncoded)
3018 bytesNeeded += (dataLen + 1) *
sizeof(
WCHAR);
3022 bytesNeeded += dataLen;
3032 bytesNeeded += dataLen -
sizeof(
LPSTR);
3054 *pcbDecoded = 1 + lenBytes + dataLen;
3056 *pcbStructInfo = bytesNeeded;
3057 else if (*pcbStructInfo < bytesNeeded)
3059 *pcbStructInfo = bytesNeeded;
3065 *pcbStructInfo = bytesNeeded;
3076 for (
i = 0;
i < dataLen;
i++)
3093 entry->u.IPAddress.cbData = dataLen;
3099 &
entry->u.pszRegisteredID, &dataLen,
NULL);
3121 pvStructInfo, *pcbStructInfo, pcbDecoded);
3124 NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
3179 AuthorityCertIssuer.rgAltEntry), 0 },
3184 AuthorityCertSerialNumber.pbData), 0 },
3226 pDecodePara, pvStructInfo, *pcbStructInfo);
3241 dwFlags, pDecodePara, pvStructInfo, pcbStructInfo,
NULL);
3259 pvStructInfo, *pcbStructInfo, pcbDecoded);
3270 cbEncoded -= 1 + lenBytes;
3279 pvStructInfo, pcbStructInfo, &decodedLen);
3285 TRACE(
"expected 0 TLV, got {%02x,%02x}\n",
3294 if (
ret && pcbDecoded)
3296 *pcbDecoded = 1 + lenBytes + decodedLen;
3297 TRACE(
"decoded %d bytes\n", *pcbDecoded);
3321 pvStructInfo, *pcbStructInfo, pcbDecoded);
3336 pDecodePara, pvStructInfo, *pcbStructInfo);
3342 if (
ret && pvStructInfo)
3345 pcbStructInfo, *pcbStructInfo);
3351 pvStructInfo = *(
BYTE **)pvStructInfo;
3352 info = pvStructInfo;
3357 pcbStructInfo,
NULL);
3386 ContentInfo.pszObjId), 0 },
3405 pDecodePara, pvStructInfo, *pcbStructInfo);
3409 DWORD bytesNeeded = 0;
3415 *pcbStructInfo = bytesNeeded;
3417 pvStructInfo, pcbStructInfo, bytesNeeded)))
3422 pvStructInfo = *(
BYTE **)pvStructInfo;
3423 name = pvStructInfo;
3428 &bytesNeeded,
NULL);
3457 pvStructInfo, *pcbStructInfo, pcbDecoded);
3463 *pcbStructInfo = bytesNeeded;
3465 else if (*pcbStructInfo < bytesNeeded)
3468 *pcbStructInfo = bytesNeeded;
3475 *pcbStructInfo = bytesNeeded;
3481 TRACE(
"got an int, dwPathLenConstraint is %d\n",
3501 pvStructInfo, *pcbStructInfo, pcbDecoded);
3504 dwFlags,
NULL, pvStructInfo, pcbStructInfo, pcbDecoded);
3590 pvStructInfo, pvStructInfo ? *pcbStructInfo : 0);
3610 TRACE(
"%p, %d, %08x, %p, %d\n&quo