Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2653 lines
78 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. // lonchanc: things to optimize
  4. // (1) merge ASN1PERDecCharString() and ASN1PERDecZeroCharString().
  5. // (2) merge ASN1PERDecChar16String() and ASN1PERDecZeroChar16String().
  6. // (3) merge ASN1PERDecChar32String() and ASN1PERDecZeroChar32String().
  7. // (4) merge ASN1PERDecTableCharString() and ASN1PERDecZeroTableCharString().
  8. // (5) merge ASN1PERDecTableChar16String() and ASN1PERDecZeroTableChar16String().
  9. // (6) merge ASN1PERDecTableChar32String() and ASN1PERDecZeroTableChar32String().
  10. // (7) merge ASN1PERDecFragmentedCharString() and ASN1PERDecFragmentedZeroCharString().
  11. // (8) merge ASN1PERDecFragmentedChar16String() and ASN1PERDecFragmentedZeroChar16String().
  12. // (9) merge ASN1PERDecFragmentedChar32String() and ASN1PERDecFragmentedZeroChar32String().
  13. // (10) merge ASN1PERDecFragmentedTableCharString() and ASN1PERDecFragmentedZeroTableCharString().
  14. // (11) merge ASN1PERDecFragmentedTableChar16String() and ASN1PERDecFragmentedZeroTableChar16String().
  15. // (12) merge ASN1PERDecFragmentedTableChar32String() and ASN1PERDecFragmentedZeroTableChar32String().
  16. #include "precomp.h"
  17. #include <math.h>
  18. #include "cintern.h"
  19. void PerDecAdvance(ASN1decoding_t dec, ASN1uint32_t nbits)
  20. {
  21. dec->pos += ((dec->bit + nbits) >> 3);
  22. dec->bit = (dec->bit + nbits) & 7;
  23. }
  24. static const ASN1uint8_t c_aBitMask3[] =
  25. {
  26. 0x00, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80
  27. };
  28. #define TO64(x) ((unsigned __int64)(x))
  29. /* check if sufficient data is in decoding buffer */
  30. int ASN1PERDecCheck(ASN1decoding_t dec, ASN1uint32_t nbits)
  31. {
  32. unsigned __int64 overflowCheck;
  33. // Ensure that our computation doesn't overflow:
  34. overflowCheck = (TO64(dec->pos) - TO64(dec->buf)) * 8 + TO64(dec->bit) + TO64(nbits);
  35. if (overflowCheck & (0xFFFFFFFFi64 << 32))
  36. {
  37. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  38. return 0;
  39. }
  40. // we're confident that no arithmetic overflow will occur. Perform the check.
  41. if ((dec->pos - dec->buf) * 8 + dec->bit + nbits <= dec->size * 8)
  42. {
  43. return 1;
  44. }
  45. ASN1DecSetError(dec, ASN1_ERR_EOD);
  46. return 0;
  47. }
  48. /* compare function for binary search in stringtable */
  49. static int __cdecl ASN1CmpStringTableEntriesByIndex(const void *a1, const void *a2)
  50. {
  51. ASN1stringtableentry_t *c1 = (ASN1stringtableentry_t *)a1;
  52. ASN1stringtableentry_t *c2 = (ASN1stringtableentry_t *)a2;
  53. if (c1->value > c2->value + (c2->upper - c2->lower))
  54. return 1;
  55. if (c2->value > c1->value + (c1->upper - c1->lower))
  56. return -1;
  57. return 0;
  58. }
  59. /* skip nbits bits */
  60. int ASN1PERDecSkipBits(ASN1decoding_t dec, ASN1uint32_t nbits)
  61. {
  62. ASN1uint32_t n;
  63. /* check if enough bits present */
  64. if (ASN1PERDecCheck(dec, nbits))
  65. {
  66. /* skip bits */
  67. n = dec->bit + nbits;
  68. dec->pos += n / 8;
  69. dec->bit = n & 7;
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. /* skip a fragmented value */
  75. int ASN1PERDecSkipFragmented(ASN1decoding_t dec, ASN1uint32_t itemsize)
  76. {
  77. ASN1uint32_t n, m;
  78. /* skip a fragments one by one */
  79. do {
  80. if (ASN1PERDecFragmentedLength(dec, &n))
  81. {
  82. if (!n)
  83. break;
  84. m = n * itemsize;
  85. if (ASN1PERDecCheck(dec, m))
  86. {
  87. dec->pos += m / 8;
  88. dec->bit = m & 7;
  89. }
  90. else
  91. {
  92. return 0;
  93. }
  94. }
  95. else
  96. {
  97. return 0;
  98. }
  99. } while (n >= 0x4000);
  100. return 1;
  101. }
  102. /* decode one bit */
  103. int ASN1PERDecBit(ASN1decoding_t dec, ASN1uint32_t *val)
  104. {
  105. if (ASN1PERDecCheck(dec, 1))
  106. {
  107. *val = (*dec->pos >> (7 - dec->bit)) & 1;
  108. if (dec->bit < 7)
  109. {
  110. dec->bit++;
  111. }
  112. else
  113. {
  114. dec->bit = 0;
  115. dec->pos++;
  116. }
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. /* decode unsigned 32 bit integer value */
  122. int ASN1PERDecU32Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1uint32_t *val)
  123. {
  124. if (ASN1PERDecCheck(dec, nbits))
  125. {
  126. if (nbits <= 32)
  127. {
  128. *val = ASN1bitgetu(dec->pos, dec->bit, nbits);
  129. PerDecAdvance(dec, nbits);
  130. return 1;
  131. }
  132. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  133. }
  134. return 0;
  135. }
  136. /* decode unsigned 16 bit integer value */
  137. int ASN1PERDecU16Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1uint16_t *val)
  138. {
  139. if (ASN1PERDecCheck(dec, nbits))
  140. {
  141. if (nbits <= 16)
  142. {
  143. *val = (ASN1uint16_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  144. PerDecAdvance(dec, nbits);
  145. return 1;
  146. }
  147. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  148. }
  149. return 0;
  150. }
  151. /* decode unsigned 8 bit integer value */
  152. int ASN1PERDecU8Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1uint8_t *val)
  153. {
  154. if (ASN1PERDecCheck(dec, nbits))
  155. {
  156. if (nbits <= 8)
  157. {
  158. *val = (ASN1uint8_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  159. PerDecAdvance(dec, nbits);
  160. return 1;
  161. }
  162. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  163. }
  164. return 0;
  165. }
  166. /* decode unsigned intx_t integer value */
  167. int ASN1PERDecUXVal(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1intx_t *val)
  168. {
  169. if (ASN1PERDecCheck(dec, nbits))
  170. {
  171. val->length = (*dec->pos & (0x80 >> dec->bit)) ? (nbits + 7) / 8 : (nbits + 7 + 1) / 8;
  172. if (NULL != (val->value = (ASN1octet_t *)DecMemAlloc(dec, val->length)))
  173. {
  174. val->value[0] = 0;
  175. ASN1bitcpy(val->value, val->length * 8 - nbits, dec->pos, dec->bit, nbits);
  176. PerDecAdvance(dec, nbits);
  177. return 1;
  178. }
  179. }
  180. return 0;
  181. }
  182. /* decode signed 32 bit integer value */
  183. int ASN1PERDecS32Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1int32_t *val)
  184. {
  185. if (ASN1PERDecCheck(dec, nbits))
  186. {
  187. if (nbits <= 32)
  188. {
  189. *val = ASN1bitget(dec->pos, dec->bit, nbits);
  190. PerDecAdvance(dec, nbits);
  191. return 1;
  192. }
  193. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  194. }
  195. return 0;
  196. }
  197. /* decode signed 16 bit value */
  198. #ifdef ENABLE_ALL
  199. int ASN1PERDecS16Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1int16_t *val)
  200. {
  201. if (ASN1PERDecCheck(dec, nbits))
  202. {
  203. if (nbits <= 16)
  204. {
  205. *val = (ASN1int16_t) ASN1bitget(dec->pos, dec->bit, nbits);
  206. PerDecAdvance(dec, nbits);
  207. return 1;
  208. }
  209. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  210. }
  211. return 0;
  212. }
  213. #endif // ENABLE_ALL
  214. /* decode signed 8 bit value */
  215. #ifdef ENABLE_ALL
  216. int ASN1PERDecS8Val(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1int8_t *val)
  217. {
  218. if (ASN1PERDecCheck(dec, nbits))
  219. {
  220. if (nbits <= 8)
  221. {
  222. *val = (ASN1int8_t) ASN1bitget(dec->pos, dec->bit, nbits);
  223. PerDecAdvance(dec, nbits);
  224. return 1;
  225. }
  226. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  227. }
  228. return 0;
  229. }
  230. #endif // ENABLE_ALL
  231. /* decode signed intx_t value */
  232. #ifdef ENABLE_ALL
  233. int ASN1PERDecSXVal(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1intx_t *val)
  234. {
  235. if (ASN1PERDecCheck(dec, nbits))
  236. {
  237. val->length = (nbits + 7) / 8;
  238. if (NULL != (val->value = (ASN1octet_t *)DecMemAlloc(dec, val->length)))
  239. {
  240. val->value[0] = (*dec->pos & (0x80 >> dec->bit)) ? c_aBitMask3[nbits & 7] : 0;
  241. ASN1bitcpy(val->value, val->length * 8 - nbits, dec->pos, dec->bit, nbits);
  242. PerDecAdvance(dec, nbits);
  243. return 1;
  244. }
  245. }
  246. return 0;
  247. }
  248. #endif // ENABLE_ALL
  249. /* decode length of a fragment */
  250. int ASN1PERDecFragmentedLength(ASN1decoding_t dec, ASN1uint32_t *nitems)
  251. {
  252. ASN1PERDecAlignment(dec);
  253. if (ASN1PERDecCheck(dec, 8))
  254. {
  255. ASN1uint32_t n = *dec->pos++;
  256. if (n < 0x80)
  257. {
  258. *nitems = n;
  259. }
  260. else
  261. if (n < 0xc0)
  262. {
  263. if (ASN1PERDecCheck(dec, 8))
  264. {
  265. *nitems = ((n & 0x3f) << 8) | *dec->pos++;
  266. }
  267. else
  268. {
  269. return 0;
  270. }
  271. }
  272. else
  273. {
  274. *nitems = 0x4000 * (n & 0x3f);
  275. }
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. /* decode a fragment */
  281. int ASN1PERDecFragmented(ASN1decoding_t dec, ASN1uint32_t *nitems, ASN1octet_t **val, ASN1uint32_t itemsize)
  282. {
  283. ASN1uint32_t n, m, l;
  284. *nitems = 0;
  285. *val = 0;
  286. m = 0;
  287. do {
  288. if (ASN1PERDecFragmentedLength(dec, &n))
  289. {
  290. if (!n)
  291. break;
  292. l = n * itemsize;
  293. if (ASN1PERDecCheck(dec, l))
  294. {
  295. *nitems += n;
  296. if (NULL != (*val = (ASN1octet_t *)DecMemReAlloc(dec, *val, (m + l + 7) / 8)))
  297. {
  298. ASN1bitcpy(*val, m, dec->pos, dec->bit, l);
  299. PerDecAdvance(dec, l);
  300. m += l;
  301. }
  302. else
  303. {
  304. return 0;
  305. }
  306. }
  307. else
  308. {
  309. return 0;
  310. }
  311. }
  312. else
  313. {
  314. return 0;
  315. }
  316. } while (n >= 0x4000);
  317. return 1;
  318. }
  319. /* end of decoding */
  320. int ASN1PERDecFlush(ASN1decoding_t dec)
  321. {
  322. /* complete broken byte */
  323. ASN1PERDecAlignment(dec);
  324. /* get zero-octet if encoding is empty bitstring */
  325. if (dec->buf == dec->pos)
  326. {
  327. if (ASN1PERDecCheck(dec, 8))
  328. {
  329. if (*dec->pos == 0)
  330. {
  331. dec->pos++;
  332. }
  333. else
  334. {
  335. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  336. return 0;
  337. }
  338. }
  339. else
  340. {
  341. return 0;
  342. }
  343. }
  344. /* calculate length */
  345. dec->len = (ASN1uint32_t) (dec->pos - dec->buf);
  346. /* set WRN_NOEOD if data left */
  347. if (dec->len >= dec->size)
  348. {
  349. return 1;
  350. }
  351. ASN1DecSetError(dec, ASN1_WRN_NOEOD);
  352. return 1;
  353. }
  354. /* skip up to octet boundary */
  355. void ASN1PERDecAlignment(ASN1decoding_t dec)
  356. {
  357. if (dec->bit)
  358. {
  359. dec->bit = 0;
  360. dec->pos++;
  361. }
  362. }
  363. /* decode normally small 32 bit integer */
  364. #ifdef ENABLE_ALL
  365. int ASN1PERDecN32Val(ASN1decoding_t dec, ASN1uint32_t *val)
  366. {
  367. ASN1uint32_t n;
  368. /* is normally small really small? */
  369. if (ASN1PERDecBit(dec, &n))
  370. {
  371. if (!n)
  372. {
  373. return ASN1PERDecU32Val(dec, 6, val);
  374. }
  375. /* large */
  376. if (ASN1PERDecFragmentedLength(dec, &n))
  377. {
  378. if (n <= 4)
  379. {
  380. if (n)
  381. {
  382. if (ASN1PERDecCheck(dec, n * 8))
  383. {
  384. *val = ASN1octetget(dec->pos, n);
  385. dec->pos += n;
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  391. }
  392. else
  393. {
  394. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  395. }
  396. }
  397. }
  398. return 0;
  399. }
  400. #endif // ENABLE_ALL
  401. /* decode normally small 16 bit integer */
  402. int ASN1PERDecN16Val(ASN1decoding_t dec, ASN1uint16_t *val)
  403. {
  404. ASN1uint32_t n;
  405. /* is normally small really small? */
  406. if (ASN1PERDecBit(dec, &n))
  407. {
  408. if (!n)
  409. {
  410. return ASN1PERDecU16Val(dec, 6, val);
  411. }
  412. /* large */
  413. if (ASN1PERDecFragmentedLength(dec, &n))
  414. {
  415. if (n <= 2)
  416. {
  417. if (n)
  418. {
  419. if (ASN1PERDecCheck(dec, n * 8))
  420. {
  421. *val = (ASN1uint16_t) ASN1octetget(dec->pos, n);
  422. dec->pos += n;
  423. return 1;
  424. }
  425. return 0;
  426. }
  427. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  428. }
  429. else
  430. {
  431. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  432. }
  433. }
  434. }
  435. return 0;
  436. }
  437. /* decode normally small 8 bit integer */
  438. #ifdef ENABLE_ALL
  439. int ASN1PERDecN8Val(ASN1decoding_t dec, ASN1uint8_t *val)
  440. {
  441. ASN1uint32_t n;
  442. /* is normally small really small? */
  443. if (ASN1PERDecBit(dec, &n))
  444. {
  445. if (!n)
  446. {
  447. return ASN1PERDecU8Val(dec, 6, val);
  448. }
  449. /* large */
  450. if (ASN1PERDecFragmentedLength(dec, &n))
  451. {
  452. if (n)
  453. {
  454. if (n <= 1)
  455. {
  456. if (ASN1PERDecCheck(dec, n * 8))
  457. {
  458. *val = (ASN1uint8_t) ASN1octetget(dec->pos, n);
  459. dec->pos += n;
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  465. }
  466. else
  467. {
  468. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  469. }
  470. }
  471. }
  472. return 0;
  473. }
  474. #endif // ENABLE_ALL
  475. /* skip normally small integer */
  476. int ASN1PERDecSkipNormallySmall(ASN1decoding_t dec)
  477. {
  478. ASN1uint32_t n;
  479. /* is normally small really small? */
  480. if (ASN1PERDecBit(dec, &n))
  481. {
  482. if (!n)
  483. {
  484. return ASN1PERDecSkipBits(dec, 6);
  485. }
  486. /* large */
  487. return ASN1PERDecSkipFragmented(dec, 8);
  488. }
  489. return 0;
  490. }
  491. /* decode extension bits with normally small length */
  492. int ASN1PERDecNormallySmallExtension(ASN1decoding_t dec, ASN1uint32_t *nextensions, ASN1uint32_t nbits, ASN1octet_t *val)
  493. {
  494. ASN1uint32_t n, m;
  495. *nextensions = 0;
  496. memset(val, 0, (nbits + 7) / 8);
  497. /* is normally small length really small? */
  498. if (ASN1PERDecBit(dec, &n))
  499. {
  500. if (n)
  501. {
  502. /* no, get extension bits as fragments */
  503. m = 0;
  504. do {
  505. if (ASN1PERDecFragmentedLength(dec, &n))
  506. {
  507. if (!n)
  508. break;
  509. if (ASN1PERDecCheck(dec, n))
  510. {
  511. if (n <= nbits)
  512. {
  513. ASN1bitcpy(val, m, dec->pos, 0, n);
  514. m += n;
  515. nbits -= n;
  516. }
  517. else
  518. if (nbits)
  519. {
  520. ASN1bitcpy(val, m, dec->pos, 0, nbits);
  521. *nextensions += ASN1bitcount(dec->pos, nbits, n - nbits);
  522. nbits = 0;
  523. }
  524. else
  525. {
  526. *nextensions += ASN1bitcount(dec->pos, 0, n);
  527. }
  528. PerDecAdvance(dec, n);
  529. }
  530. else
  531. {
  532. return 0;
  533. }
  534. }
  535. else
  536. {
  537. return 0;
  538. }
  539. } while (n >= 0x4000);
  540. return 1;
  541. }
  542. /* yes, get length of extension bit string */
  543. if (ASN1PERDecU32Val(dec, 6, &n))
  544. {
  545. n++;
  546. /* copy extension bits */
  547. if (ASN1PERDecCheck(dec, n))
  548. {
  549. if (n <= nbits)
  550. {
  551. ASN1bitcpy(val, 0, dec->pos, dec->bit, n);
  552. }
  553. else
  554. {
  555. ASN1bitcpy(val, 0, dec->pos, dec->bit, nbits);
  556. *nextensions = ASN1bitcount(dec->pos, dec->bit + nbits, n - nbits);
  557. }
  558. PerDecAdvance(dec, n);
  559. return 1;
  560. }
  561. }
  562. }
  563. return 0;
  564. }
  565. /* skip extension bits with normally small length */
  566. int ASN1PERDecSkipNormallySmallExtension(ASN1decoding_t dec, ASN1uint32_t *nextensions)
  567. {
  568. ASN1uint32_t n;
  569. *nextensions = 0;
  570. /* is normally small length really small? */
  571. if (ASN1PERDecBit(dec, &n))
  572. {
  573. if (n)
  574. {
  575. /* no, get extension bits as fragments */
  576. do {
  577. if (ASN1PERDecFragmentedLength(dec, &n))
  578. {
  579. if (!n)
  580. break;
  581. if (ASN1PERDecCheck(dec, n))
  582. {
  583. *nextensions += ASN1bitcount(dec->pos, 0, n);
  584. PerDecAdvance(dec, n);
  585. }
  586. else
  587. {
  588. return 0;
  589. }
  590. }
  591. else
  592. {
  593. return 0;
  594. }
  595. } while (n >= 0x4000);
  596. return 1;
  597. }
  598. /* yes, get length of extension bit string */
  599. if (ASN1PERDecU32Val(dec, 6, &n))
  600. {
  601. n++;
  602. if (ASN1PERDecCheck(dec, n))
  603. {
  604. *nextensions = ASN1bitcount(dec->pos, dec->bit, n);
  605. PerDecAdvance(dec, n);
  606. return 1;
  607. }
  608. }
  609. }
  610. return 0;
  611. }
  612. /* decode bit string of optionals of sequence/set */
  613. // lonchanc: decode octet string with length constraint.
  614. int ASN1PERDecExtension(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1octet_t *val)
  615. {
  616. if (ASN1PERDecCheck(dec, nbits))
  617. {
  618. ASN1bitcpy(val, 0, dec->pos, dec->bit, nbits);
  619. PerDecAdvance(dec, nbits);
  620. return 1;
  621. }
  622. return 0;
  623. }
  624. /* decode bit string */
  625. int ASN1PERDecBits(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1octet_t **val)
  626. {
  627. if (NULL != (*val = (ASN1octet_t *)DecMemAlloc(dec, (nbits + 7) / 8)))
  628. {
  629. if (ASN1PERDecCheck(dec, nbits))
  630. {
  631. ASN1bitcpy(*val, 0, dec->pos, dec->bit, nbits);
  632. PerDecAdvance(dec, nbits);
  633. return 1;
  634. }
  635. }
  636. return 0;
  637. }
  638. /* decode real value */
  639. int ASN1PERDecDouble(ASN1decoding_t dec, double *val)
  640. {
  641. ASN1uint32_t head;
  642. ASN1int32_t exponent;
  643. ASN1uint32_t baselog2;
  644. ASN1uint32_t len;
  645. ASN1uint32_t i;
  646. ASN1octet_t *p, *q;
  647. double v;
  648. char buf[256], *b;
  649. if (ASN1PERDecFragmentedLength(dec, &len))
  650. {
  651. if (len < 0x4000)
  652. {
  653. if (len)
  654. {
  655. p = q = dec->pos;
  656. dec->pos += len;
  657. head = *p++;
  658. /* binary encoding? */
  659. if (head & 0x80)
  660. {
  661. /* get base */
  662. switch (head & 0x30)
  663. {
  664. case 0:
  665. /* base 2 */
  666. baselog2 = 1;
  667. break;
  668. case 0x10:
  669. /* base 8 */
  670. baselog2 = 3;
  671. break;
  672. case 0x20:
  673. /* base 16 */
  674. baselog2 = 4;
  675. break;
  676. default:
  677. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  678. return 0;
  679. }
  680. /* get exponent */
  681. switch (head & 0x03)
  682. {
  683. case 0:
  684. /* 8 bit exponent */
  685. exponent = (ASN1int8_t)*p++;
  686. break;
  687. case 1:
  688. /* 16 bit exponent */
  689. exponent = (ASN1int16_t)((*p << 8) | p[1]);
  690. p += 2;
  691. break;
  692. case 2:
  693. /* 24 bit exponent */
  694. exponent = ((*p << 16) | (p[1] << 8) | p[2]);
  695. if (exponent & 0x800000)
  696. exponent -= 0x1000000;
  697. break;
  698. default:
  699. /* variable length exponent */
  700. exponent = (p[1] & 0x80) ? -1 : 0;
  701. for (i = 1; i <= *p; i++)
  702. exponent = (exponent << 8) | p[i];
  703. p += *p + 1;
  704. break;
  705. }
  706. /* calculate remaining length */
  707. len -= (ASN1uint32_t) (p - q);
  708. /* get mantissa */
  709. v = 0.0;
  710. for (i = 0; i < len; i++)
  711. v = v * 256.0 + *p++;
  712. /* scale mantissa */
  713. switch (head & 0x0c)
  714. {
  715. case 0x04:
  716. /* scaling factor 1 */
  717. v *= 2.0;
  718. break;
  719. case 0x08:
  720. /* scaling factor 2 */
  721. v *= 4.0;
  722. break;
  723. case 0x0c:
  724. /* scaling factor 3 */
  725. v *= 8.0;
  726. break;
  727. }
  728. /* check sign */
  729. if (head & 0x40)
  730. v = -v;
  731. /* calculate value */
  732. *val = ldexp(v, exponent * baselog2);
  733. /* special real values? */
  734. }
  735. else
  736. if (head & 0x40)
  737. {
  738. switch (head)
  739. {
  740. case 0x40:
  741. /* PLUS-INFINITY */
  742. *val = ASN1double_pinf();
  743. break;
  744. case 0x41:
  745. /* MINUS-INFINITY */
  746. *val = ASN1double_minf();
  747. break;
  748. default:
  749. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  750. return 0;
  751. }
  752. /* decimal encoding */
  753. }
  754. else
  755. {
  756. if (len <= 256)
  757. {
  758. CopyMemory(buf, p, len - 1);
  759. }
  760. else
  761. {
  762. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  763. return 0;
  764. }
  765. buf[len - 1] = 0;
  766. b = strchr(buf, ',');
  767. if (b)
  768. *b = '.';
  769. *val = strtod((char *)buf, &b);
  770. if (*b)
  771. {
  772. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  773. return 0;
  774. }
  775. }
  776. }
  777. else
  778. {
  779. *val = 0.0;
  780. }
  781. return 1;
  782. }
  783. else
  784. {
  785. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  786. }
  787. }
  788. return 0;
  789. }
  790. /* decode external value */
  791. #ifdef ENABLE_EXTERNAL
  792. int ASN1PERDecExternal(ASN1decoding_t dec, ASN1external_t *val)
  793. {
  794. ASN1uint32_t l, u;
  795. /* get optional bits */
  796. if (ASN1PERDecU32Val(dec, 3, &u))
  797. {
  798. /* get identification */
  799. switch (u & 6)
  800. {
  801. case 4:
  802. val->identification.o = ASN1external_identification_syntax_o;
  803. if (!ASN1PERDecObjectIdentifier(dec, &val->identification.u.syntax))
  804. return 0;
  805. break;
  806. case 2:
  807. val->identification.o =
  808. ASN1external_identification_presentation_context_id_o;
  809. if (!ASN1PERDecFragmentedLength(dec, &l))
  810. return 0;
  811. if (!ASN1PERDecU32Val(dec, l * 8,
  812. &val->identification.u.presentation_context_id))
  813. return 0;
  814. break;
  815. case 6:
  816. val->identification.o =
  817. ASN1external_identification_context_negotiation_o;
  818. if (!ASN1PERDecObjectIdentifier(dec,
  819. &val->identification.u.context_negotiation.transfer_syntax))
  820. return 0;
  821. if (!ASN1PERDecFragmentedLength(dec, &l))
  822. return 0;
  823. if (!ASN1PERDecU32Val(dec, l * 8,
  824. &val->identification.u.context_negotiation.presentation_context_id))
  825. return 0;
  826. break;
  827. default:
  828. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  829. return 0;
  830. }
  831. /* get value descriptor */
  832. val->o[0] = (ASN1octet_t) ((u & 1) << 7);
  833. if (u & 1)
  834. {
  835. if (!ASN1PERDecFragmentedZeroCharString(dec, &val->data_value_descriptor, 8))
  836. return 0;
  837. }
  838. else
  839. {
  840. val->data_value_descriptor = NULL;
  841. }
  842. /* get value */
  843. if (ASN1PERDecU32Val(dec, 2, &u))
  844. {
  845. switch (u)
  846. {
  847. case 0:
  848. val->data_value.o = ASN1external_data_value_notation_o;
  849. return ASN1PERDecFragmented(dec,
  850. &val->data_value.u.notation.length,
  851. (ASN1octet_t **) &val->data_value.u.notation.encoded, 8);
  852. break;
  853. case 1:
  854. val->data_value.o = ASN1external_data_value_encoded_o;
  855. if (ASN1PERDecFragmented(dec,
  856. &val->data_value.u.encoded.length,
  857. &val->data_value.u.encoded.value, 8))
  858. {
  859. val->data_value.u.encoded.length *= 8;
  860. return 1;
  861. }
  862. break;
  863. case 2:
  864. val->data_value.o = ASN1external_data_value_encoded_o;
  865. return ASN1PERDecFragmented(dec,
  866. &val->data_value.u.encoded.length,
  867. &val->data_value.u.encoded.value, 1);
  868. break;
  869. default:
  870. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  871. }
  872. }
  873. }
  874. return 0;
  875. }
  876. #endif // ENABLE_EXTERNAL
  877. /* decode an embedded pdv value */
  878. #ifdef ENABLE_EMBEDDED_PDV
  879. int ASN1PERDecEmbeddedPdv(ASN1decoding_t dec, ASN1embeddedpdv_t *val)
  880. {
  881. ASN1uint32_t flag;
  882. ASN1uint32_t index;
  883. ASN1uint32_t l;
  884. ASN1embeddedpdv_identification_t *identification;
  885. /* get EP-A/EP-B encoding bit */
  886. if (!ASN1PERDecBit(dec, &flag))
  887. return 0;
  888. /* get index value */
  889. if (!ASN1PERDecN32Val(dec, &index))
  890. return 0;
  891. if (flag)
  892. {
  893. /* EP-A encoding */
  894. /* get identification */
  895. if (!ASN1PERDecU8Val(dec, 3, &val->identification.o))
  896. return 0;
  897. switch (val->identification.o)
  898. {
  899. case ASN1embeddedpdv_identification_syntaxes_o:
  900. if (!ASN1PERDecObjectIdentifier(dec,
  901. &val->identification.u.syntaxes.abstract))
  902. return 0;
  903. if (!ASN1PERDecObjectIdentifier(dec,
  904. &val->identification.u.syntaxes.transfer))
  905. return 0;
  906. break;
  907. case ASN1embeddedpdv_identification_syntax_o:
  908. if (!ASN1PERDecObjectIdentifier(dec,
  909. &val->identification.u.syntax))
  910. return 0;
  911. break;
  912. case ASN1embeddedpdv_identification_presentation_context_id_o:
  913. if (!ASN1PERDecFragmentedLength(dec, &l))
  914. return 0;
  915. if (!ASN1PERDecU32Val(dec, l * 8,
  916. &val->identification.u.presentation_context_id))
  917. return 0;
  918. break;
  919. case ASN1embeddedpdv_identification_context_negotiation_o:
  920. if (!ASN1PERDecFragmentedLength(dec, &l))
  921. return 0;
  922. if (!ASN1PERDecU32Val(dec, l * 8, &val->
  923. identification.u.context_negotiation.presentation_context_id))
  924. return 0;
  925. if (!ASN1PERDecObjectIdentifier(dec,
  926. &val->identification.u.context_negotiation.transfer_syntax))
  927. return 0;
  928. break;
  929. case ASN1embeddedpdv_identification_transfer_syntax_o:
  930. if (!ASN1PERDecObjectIdentifier(dec,
  931. &val->identification.u.transfer_syntax))
  932. return 0;
  933. break;
  934. case ASN1embeddedpdv_identification_fixed_o:
  935. break;
  936. default:
  937. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  938. return 0;
  939. }
  940. /* save identification */
  941. if (!ASN1DecAddEmbeddedPdvIdentification(((ASN1INTERNdecoding_t) dec)->parent,
  942. &val->identification))
  943. return 0;
  944. }
  945. else
  946. {
  947. /* EP-B encoding */
  948. /* get identification */
  949. identification = ASN1DecGetEmbeddedPdvIdentification(((ASN1INTERNdecoding_t) dec)->parent, index);
  950. if (!identification)
  951. return 0;
  952. val->identification.o = identification->o;
  953. switch (identification->o)
  954. {
  955. case ASN1embeddedpdv_identification_syntaxes_o:
  956. if (!ASN1DecDupObjectIdentifier(dec,
  957. &val->identification.u.syntaxes.abstract,
  958. &identification->u.syntaxes.abstract))
  959. return 0;
  960. if (!ASN1DecDupObjectIdentifier(dec,
  961. &val->identification.u.syntaxes.transfer,
  962. &identification->u.syntaxes.transfer))
  963. return 0;
  964. break;
  965. case ASN1embeddedpdv_identification_syntax_o:
  966. if (!ASN1DecDupObjectIdentifier(dec,
  967. &val->identification.u.syntax,
  968. &identification->u.syntax))
  969. return 0;
  970. break;
  971. case ASN1embeddedpdv_identification_presentation_context_id_o:
  972. val->identification.u.presentation_context_id =
  973. identification->u.presentation_context_id;
  974. break;
  975. case ASN1embeddedpdv_identification_context_negotiation_o:
  976. val->identification.u.context_negotiation.presentation_context_id =
  977. identification->u.context_negotiation.presentation_context_id;
  978. if (!ASN1DecDupObjectIdentifier(dec,
  979. &val->identification.u.context_negotiation.transfer_syntax,
  980. &identification->u.context_negotiation.transfer_syntax))
  981. return 0;
  982. break;
  983. case ASN1embeddedpdv_identification_transfer_syntax_o:
  984. if (!ASN1DecDupObjectIdentifier(dec,
  985. &val->identification.u.transfer_syntax,
  986. &identification->u.transfer_syntax))
  987. return 0;
  988. break;
  989. case ASN1embeddedpdv_identification_fixed_o:
  990. break;
  991. }
  992. }
  993. /* get value */
  994. ASN1PERDecAlignment(dec);
  995. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  996. return ASN1PERDecFragmented(dec,
  997. &val->data_value.u.encoded.length,
  998. &val->data_value.u.encoded.value, 1);
  999. }
  1000. #endif // ENABLE_EMBEDDED_PDV
  1001. /* decode an optimized embedded pdv value */
  1002. #ifdef ENABLE_EMBEDDED_PDV
  1003. int ASN1PERDecEmbeddedPdvOpt(ASN1decoding_t dec, ASN1embeddedpdv_t *val, ASN1objectidentifier_t *abstract, ASN1objectidentifier_t *transfer)
  1004. {
  1005. /* set identification */
  1006. if (abstract && transfer)
  1007. {
  1008. val->identification.o = ASN1embeddedpdv_identification_syntaxes_o;
  1009. if (!ASN1DecDupObjectIdentifier(dec,
  1010. &val->identification.u.syntaxes.abstract, abstract))
  1011. return 0;
  1012. if (!ASN1DecDupObjectIdentifier(dec,
  1013. &val->identification.u.syntaxes.transfer, transfer))
  1014. return 0;
  1015. }
  1016. else
  1017. {
  1018. val->identification.o = ASN1embeddedpdv_identification_fixed_o;
  1019. }
  1020. /* get value */
  1021. val->data_value.o = ASN1embeddedpdv_data_value_encoded_o;
  1022. return ASN1PERDecFragmented(dec,
  1023. &val->data_value.u.encoded.length,
  1024. &val->data_value.u.encoded.value, 1);
  1025. }
  1026. #endif // ENABLE_EMBEDDED_PDV
  1027. /* decode character string */
  1028. #ifdef ENABLE_GENERALIZED_CHAR_STR
  1029. int ASN1PERDecCharacterString(ASN1decoding_t dec, ASN1characterstring_t *val)
  1030. {
  1031. ASN1uint32_t flag;
  1032. ASN1uint32_t index;
  1033. ASN1uint32_t l;
  1034. ASN1characterstring_identification_t *identification;
  1035. /* get CS-A/CS-B encoding bit */
  1036. if (!ASN1PERDecBit(dec, &flag))
  1037. return 0;
  1038. /* get index value */
  1039. if (!ASN1PERDecN32Val(dec, &index))
  1040. return 0;
  1041. if (flag)
  1042. {
  1043. /* CS-A encoding */
  1044. /* get identification */
  1045. if (!ASN1PERDecU8Val(dec, 3, &val->identification.o))
  1046. return 0;
  1047. switch (val->identification.o)
  1048. {
  1049. case ASN1characterstring_identification_syntaxes_o:
  1050. if (!ASN1PERDecObjectIdentifier(dec,
  1051. &val->identification.u.syntaxes.abstract))
  1052. return 0;
  1053. if (!ASN1PERDecObjectIdentifier(dec,
  1054. &val->identification.u.syntaxes.transfer))
  1055. return 0;
  1056. break;
  1057. case ASN1characterstring_identification_syntax_o:
  1058. if (!ASN1PERDecObjectIdentifier(dec,
  1059. &val->identification.u.syntax))
  1060. return 0;
  1061. break;
  1062. case ASN1characterstring_identification_presentation_context_id_o:
  1063. if (!ASN1PERDecFragmentedLength(dec, &l))
  1064. return 0;
  1065. if (!ASN1PERDecU32Val(dec, l * 8,
  1066. &val->identification.u.presentation_context_id))
  1067. return 0;
  1068. break;
  1069. case ASN1characterstring_identification_context_negotiation_o:
  1070. if (!ASN1PERDecFragmentedLength(dec, &l))
  1071. return 0;
  1072. if (!ASN1PERDecU32Val(dec, l * 8, &val->
  1073. identification.u.context_negotiation.presentation_context_id))
  1074. return 0;
  1075. if (!ASN1PERDecObjectIdentifier(dec,
  1076. &val->identification.u.context_negotiation.transfer_syntax))
  1077. return 0;
  1078. break;
  1079. case ASN1characterstring_identification_transfer_syntax_o:
  1080. if (!ASN1PERDecObjectIdentifier(dec,
  1081. &val->identification.u.transfer_syntax))
  1082. return 0;
  1083. break;
  1084. case ASN1characterstring_identification_fixed_o:
  1085. break;
  1086. default:
  1087. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1088. return 0;
  1089. }
  1090. /* save identification */
  1091. if (!ASN1DecAddCharacterStringIdentification((ASN1INTERNdecoding_t) dec, &val->identification))
  1092. return 0;
  1093. }
  1094. else
  1095. {
  1096. /* CS-B encoding */
  1097. /* get identification */
  1098. identification = ASN1DecGetCharacterStringIdentification((ASN1INTERNdecoding_t) dec, index);
  1099. if (!identification)
  1100. return 0;
  1101. val->identification.o = identification->o;
  1102. switch (identification->o)
  1103. {
  1104. case ASN1characterstring_identification_syntaxes_o:
  1105. if (!ASN1DecDupObjectIdentifier(dec,
  1106. &val->identification.u.syntaxes.abstract,
  1107. &identification->u.syntaxes.abstract))
  1108. return 0;
  1109. if (!ASN1DecDupObjectIdentifier(dec,
  1110. &val->identification.u.syntaxes.transfer,
  1111. &identification->u.syntaxes.transfer))
  1112. return 0;
  1113. break;
  1114. case ASN1characterstring_identification_syntax_o:
  1115. if (!ASN1DecDupObjectIdentifier(dec,
  1116. &val->identification.u.syntax,
  1117. &identification->u.syntax))
  1118. return 0;
  1119. break;
  1120. case ASN1characterstring_identification_presentation_context_id_o:
  1121. val->identification.u.presentation_context_id =
  1122. identification->u.presentation_context_id;
  1123. break;
  1124. case ASN1characterstring_identification_context_negotiation_o:
  1125. val->identification.u.context_negotiation.presentation_context_id =
  1126. identification->u.context_negotiation.presentation_context_id;
  1127. if (!ASN1DecDupObjectIdentifier(dec,
  1128. &val->identification.u.context_negotiation.transfer_syntax,
  1129. &identification->u.context_negotiation.transfer_syntax))
  1130. return 0;
  1131. break;
  1132. case ASN1characterstring_identification_transfer_syntax_o:
  1133. if (!ASN1DecDupObjectIdentifier(dec,
  1134. &val->identification.u.transfer_syntax,
  1135. &identification->u.transfer_syntax))
  1136. return 0;
  1137. break;
  1138. case ASN1characterstring_identification_fixed_o:
  1139. break;
  1140. }
  1141. }
  1142. /* get value */
  1143. ASN1PERDecAlignment(dec);
  1144. val->data_value.o = ASN1characterstring_data_value_encoded_o;
  1145. return ASN1PERDecFragmented(dec,
  1146. &val->data_value.u.encoded.length,
  1147. &val->data_value.u.encoded.value, 8);
  1148. }
  1149. #endif // ENABLE_GENERALIZED_CHAR_STR
  1150. /* decode an optimized character string value */
  1151. #ifdef ENABLE_GENERALIZED_CHAR_STR
  1152. int ASN1PERDecCharacterStringOpt(ASN1decoding_t dec, ASN1characterstring_t *val, ASN1objectidentifier_t *abstract, ASN1objectidentifier_t *transfer)
  1153. {
  1154. /* set identification */
  1155. if (abstract && transfer)
  1156. {
  1157. val->identification.o = ASN1characterstring_identification_syntaxes_o;
  1158. if (!ASN1DecDupObjectIdentifier(dec,
  1159. &val->identification.u.syntaxes.abstract, abstract))
  1160. return 0;
  1161. if (!ASN1DecDupObjectIdentifier(dec,
  1162. &val->identification.u.syntaxes.transfer, transfer))
  1163. return 0;
  1164. }
  1165. else
  1166. {
  1167. val->identification.o = ASN1characterstring_identification_fixed_o;
  1168. }
  1169. /* get value */
  1170. val->data_value.o = ASN1characterstring_data_value_encoded_o;
  1171. return ASN1PERDecFragmented(dec,
  1172. &val->data_value.u.encoded.length,
  1173. &val->data_value.u.encoded.value, 8);
  1174. }
  1175. #endif // ENABLE_GENERALIZED_CHAR_STR
  1176. /* decode a multibyte string */
  1177. #ifdef ENABLE_ALL
  1178. int ASN1PERDecMultibyteString(ASN1decoding_t dec, ASN1char_t **val)
  1179. {
  1180. return ASN1PERDecFragmentedZeroCharString(dec, val, 8);
  1181. }
  1182. #endif // ENABLE_ALL
  1183. /* decode a string */
  1184. int ASN1PERDecCharStringNoAlloc(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits)
  1185. {
  1186. ASN1char_t *p = val;
  1187. if (ASN1PERDecCheck(dec, nchars * nbits))
  1188. {
  1189. if (nbits == 8)
  1190. {
  1191. ASN1bitcpy((ASN1octet_t *)p, 0, dec->pos, dec->bit, nchars * 8);
  1192. PerDecAdvance(dec, nchars * 8);
  1193. return 1;
  1194. }
  1195. while (nchars--)
  1196. {
  1197. *p++ = (ASN1char_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1198. PerDecAdvance(dec, nbits);
  1199. }
  1200. return 1;
  1201. }
  1202. return 0;
  1203. }
  1204. #ifdef ENABLE_ALL
  1205. int ASN1PERDecCharString(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t **val, ASN1uint32_t nbits)
  1206. {
  1207. if (ASN1PERDecCheck(dec, nchars * nbits))
  1208. {
  1209. *val = (ASN1char_t *)DecMemAlloc(dec, nchars);
  1210. return ((*val) ? ASN1PERDecCharStringNoAlloc(dec, nchars, *val, nbits) : 0);
  1211. }
  1212. return 0;
  1213. }
  1214. #endif // ENABLE_ALL
  1215. /* decode a 16 bit string */
  1216. int ASN1PERDecChar16String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char16_t **val, ASN1uint32_t nbits)
  1217. {
  1218. ASN1char16_t *p;
  1219. if (ASN1PERDecCheck(dec, nchars * nbits))
  1220. {
  1221. p = *val = (ASN1char16_t *)DecMemAlloc(dec, nchars * sizeof(ASN1char16_t));
  1222. if (p)
  1223. {
  1224. if (!dec->bit && nbits == 16)
  1225. {
  1226. while (nchars--)
  1227. {
  1228. *p++ = (dec->pos[0] << 8) | dec->pos[1];
  1229. dec->pos += 2;
  1230. }
  1231. return 1;
  1232. }
  1233. while (nchars--)
  1234. {
  1235. *p++ = (ASN1char16_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1236. PerDecAdvance(dec, nbits);
  1237. }
  1238. return 1;
  1239. }
  1240. }
  1241. return 0;
  1242. }
  1243. /* decode a 32 bit string */
  1244. #ifdef ENABLE_ALL
  1245. int ASN1PERDecChar32String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char32_t **val, ASN1uint32_t nbits)
  1246. {
  1247. ASN1char32_t *p;
  1248. if (ASN1PERDecCheck(dec, nchars * nbits))
  1249. {
  1250. p = *val = (ASN1char32_t *)DecMemAlloc(dec, nchars * sizeof(ASN1char32_t));
  1251. if (p)
  1252. {
  1253. if (!dec->bit && nbits == 32)
  1254. {
  1255. while (nchars--)
  1256. {
  1257. *p++ = (dec->pos[0] << 24) | (dec->pos[1] << 16) |
  1258. (dec->pos[2] << 8) | dec->pos[3];
  1259. dec->pos += 4;
  1260. }
  1261. return 1;
  1262. }
  1263. while (nchars--)
  1264. {
  1265. *p++ = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1266. PerDecAdvance(dec, nbits);
  1267. }
  1268. return 1;
  1269. }
  1270. }
  1271. return 0;
  1272. }
  1273. #endif // ENABLE_ALL
  1274. /* decode a zero-terminated string */
  1275. int ASN1PERDecZeroCharStringNoAlloc(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits)
  1276. {
  1277. ASN1char_t *p = val;
  1278. if (ASN1PERDecCheck(dec, nchars * nbits))
  1279. {
  1280. if (nbits == 8)
  1281. {
  1282. ASN1bitcpy((ASN1octet_t *)p, 0, dec->pos, dec->bit, nchars * 8);
  1283. PerDecAdvance(dec, nchars * 8);
  1284. p[nchars] = 0;
  1285. return 1;
  1286. }
  1287. while (nchars--)
  1288. {
  1289. *p++ = (ASN1char_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1290. PerDecAdvance(dec, nbits);
  1291. }
  1292. *p = 0;
  1293. return 1;
  1294. }
  1295. return 0;
  1296. }
  1297. #ifdef ENABLE_ALL
  1298. int ASN1PERDecZeroCharString(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t **val, ASN1uint32_t nbits)
  1299. {
  1300. if (ASN1PERDecCheck(dec, nchars * nbits))
  1301. {
  1302. *val = (ASN1char_t *)DecMemAlloc(dec, nchars + 1);
  1303. return ((*val) ? ASN1PERDecZeroCharStringNoAlloc(dec, nchars, *val, nbits) : 0);
  1304. }
  1305. return 0;
  1306. }
  1307. #endif // ENABLE_ALL
  1308. /* decode a zero-terminated 16 bit string */
  1309. #ifdef ENABLE_ALL
  1310. int ASN1PERDecZeroChar16String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char16_t **val, ASN1uint32_t nbits)
  1311. {
  1312. ASN1char16_t *p;
  1313. if (ASN1PERDecCheck(dec, nchars * nbits))
  1314. {
  1315. p = *val = (ASN1char16_t *)DecMemAlloc(dec, (nchars + 1) * sizeof(ASN1char16_t));
  1316. if (p)
  1317. {
  1318. if (!dec->bit && nbits == 16)
  1319. {
  1320. while (nchars--)
  1321. {
  1322. *p++ = (dec->pos[0] << 8) | dec->pos[1];
  1323. dec->pos += 2;
  1324. }
  1325. *p = 0;
  1326. return 1;
  1327. }
  1328. while (nchars--)
  1329. {
  1330. *p++ = (ASN1char16_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1331. PerDecAdvance(dec, nbits);
  1332. }
  1333. *p = 0;
  1334. return 1;
  1335. }
  1336. }
  1337. return 0;
  1338. }
  1339. #endif // ENABLE_ALL
  1340. /* decode a zero-terminated 32 bit string */
  1341. #ifdef ENABLE_ALL
  1342. int ASN1PERDecZeroChar32String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char32_t **val, ASN1uint32_t nbits)
  1343. {
  1344. ASN1char32_t *p;
  1345. if (ASN1PERDecCheck(dec, nchars * nbits))
  1346. {
  1347. p = *val = (ASN1char32_t *)DecMemAlloc(dec, (nchars + 1) * sizeof(ASN1char32_t));
  1348. if (p)
  1349. {
  1350. if (!dec->bit && nbits == 32)
  1351. {
  1352. while (nchars--)
  1353. {
  1354. *p++ = (dec->pos[0] << 24) | (dec->pos[1] << 16) |
  1355. (dec->pos[2] << 8) | dec->pos[3];
  1356. dec->pos += 4;
  1357. }
  1358. *p = 0;
  1359. return 1;
  1360. }
  1361. while (nchars--)
  1362. {
  1363. *p++ = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1364. PerDecAdvance(dec, nbits);
  1365. }
  1366. *p = 0;
  1367. return 1;
  1368. }
  1369. }
  1370. return 0;
  1371. }
  1372. #endif // ENABLE_ALL
  1373. /* decode a table string */
  1374. #ifdef ENABLE_ALL
  1375. int ASN1PERDecTableCharStringNoAlloc(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1376. {
  1377. ASN1char_t *p = val;
  1378. ASN1stringtableentry_t chr, *entry;
  1379. if (ASN1PERDecCheck(dec, nchars * nbits))
  1380. {
  1381. chr.lower = chr.upper = 0;
  1382. while (nchars--)
  1383. {
  1384. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1385. PerDecAdvance(dec, nbits);
  1386. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1387. table->length, sizeof(ASN1stringtableentry_t),
  1388. ASN1CmpStringTableEntriesByIndex);
  1389. if (entry)
  1390. {
  1391. *p++ = (ASN1char_t) (entry->lower + (chr.value - entry->value));
  1392. }
  1393. else
  1394. {
  1395. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1396. return 0;
  1397. }
  1398. }
  1399. return 1;
  1400. }
  1401. return 0;
  1402. }
  1403. #endif // ENABLE_ALL
  1404. #ifdef ENABLE_ALL
  1405. int ASN1PERDecTableCharString(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1406. {
  1407. ASN1stringtableentry_t chr, *entry;
  1408. if (ASN1PERDecCheck(dec, nchars * nbits))
  1409. {
  1410. *val = (ASN1char_t *)DecMemAlloc(dec, nchars);
  1411. return ((*val) ? ASN1PERDecTableCharStringNoAlloc(dec, nchars, *val, nbits, table) : 0);
  1412. }
  1413. return 0;
  1414. }
  1415. #endif // ENABLE_ALL
  1416. /* decode a 16 bit table string */
  1417. int ASN1PERDecTableChar16String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char16_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1418. {
  1419. ASN1char16_t *p;
  1420. ASN1stringtableentry_t chr, *entry;
  1421. if (ASN1PERDecCheck(dec, nchars * nbits))
  1422. {
  1423. *val = p = (ASN1char16_t *)DecMemAlloc(dec, nchars * sizeof(ASN1char16_t));
  1424. if (p)
  1425. {
  1426. chr.lower = chr.upper = 0;
  1427. while (nchars--)
  1428. {
  1429. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1430. PerDecAdvance(dec, nbits);
  1431. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1432. table->length, sizeof(ASN1stringtableentry_t),
  1433. ASN1CmpStringTableEntriesByIndex);
  1434. if (entry)
  1435. {
  1436. *p++ = (ASN1char16_t) (entry->lower + (chr.value - entry->value));
  1437. }
  1438. else
  1439. {
  1440. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1441. return 0;
  1442. }
  1443. }
  1444. return 1;
  1445. }
  1446. }
  1447. return 0;
  1448. }
  1449. /* decode a 32 bit table string */
  1450. #ifdef ENABLE_ALL
  1451. int ASN1PERDecTableChar32String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char32_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1452. {
  1453. ASN1char32_t *p;
  1454. ASN1stringtableentry_t chr, *entry;
  1455. if (ASN1PERDecCheck(dec, nchars * nbits))
  1456. {
  1457. *val = p = (ASN1char32_t *)DecMemAlloc(dec, nchars * sizeof(ASN1char32_t));
  1458. if (p)
  1459. {
  1460. chr.lower = chr.upper = 0;
  1461. while (nchars--)
  1462. {
  1463. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1464. PerDecAdvance(dec, nbits);
  1465. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1466. table->length, sizeof(ASN1stringtableentry_t),
  1467. ASN1CmpStringTableEntriesByIndex);
  1468. if (entry)
  1469. {
  1470. *p++ = entry->lower + (chr.value - entry->value);
  1471. }
  1472. else
  1473. {
  1474. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1475. return 0;
  1476. }
  1477. }
  1478. return 1;
  1479. }
  1480. }
  1481. return 0;
  1482. }
  1483. #endif // ENABLE_ALL
  1484. /* decode a zero-terminated table string */
  1485. int ASN1PERDecZeroTableCharStringNoAlloc(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t *val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1486. {
  1487. ASN1char_t *p = val;
  1488. ASN1stringtableentry_t chr, *entry;
  1489. chr.lower = chr.upper = 0;
  1490. while (nchars--)
  1491. {
  1492. if (ASN1PERDecU32Val(dec, nbits, &chr.value))
  1493. {
  1494. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1495. table->length, sizeof(ASN1stringtableentry_t),
  1496. ASN1CmpStringTableEntriesByIndex);
  1497. if (entry)
  1498. {
  1499. *p++ = (ASN1char_t) (entry->lower + (chr.value - entry->value));
  1500. }
  1501. else
  1502. {
  1503. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1504. return 0;
  1505. }
  1506. }
  1507. else
  1508. {
  1509. return 0;
  1510. }
  1511. }
  1512. *p = 0;
  1513. return 1;
  1514. }
  1515. /* decode a zero-terminated table string */
  1516. #ifdef ENABLE_ALL
  1517. int ASN1PERDecZeroTableCharString(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1518. {
  1519. ASN1char_t *p;
  1520. ASN1stringtableentry_t chr, *entry;
  1521. *val = (ASN1char_t *)DecMemAlloc(dec, nchars + 1);
  1522. return ((*val) ? ASN1PERDecZeroTableCharStringNoAlloc(dec, nchars, *val, nbits, table) : 0);
  1523. }
  1524. #endif // ENABLE_ALL
  1525. /* decode a zero-terminated 16 bit table string */
  1526. #ifdef ENABLE_ALL
  1527. int ASN1PERDecZeroTableChar16String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char16_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1528. {
  1529. ASN1char16_t *p;
  1530. ASN1stringtableentry_t chr, *entry;
  1531. *val = p = (ASN1char16_t *)DecMemAlloc(dec, (nchars + 1) * sizeof(ASN1char16_t));
  1532. if (p)
  1533. {
  1534. chr.lower = chr.upper = 0;
  1535. while (nchars--)
  1536. {
  1537. if (ASN1PERDecU32Val(dec, nbits, &chr.value))
  1538. {
  1539. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1540. table->length, sizeof(ASN1stringtableentry_t),
  1541. ASN1CmpStringTableEntriesByIndex);
  1542. if (entry)
  1543. {
  1544. *p++ = (ASN1char16_t) (entry->lower + (chr.value - entry->value));
  1545. }
  1546. else
  1547. {
  1548. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1549. return 0;
  1550. }
  1551. }
  1552. else
  1553. {
  1554. return 0;
  1555. }
  1556. }
  1557. *p = 0;
  1558. return 1;
  1559. }
  1560. return 0;
  1561. }
  1562. #endif // ENABLE_ALL
  1563. /* decode a zero-terminated 32 bit table string */
  1564. #ifdef ENABLE_ALL
  1565. int ASN1PERDecZeroTableChar32String(ASN1decoding_t dec, ASN1uint32_t nchars, ASN1char32_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  1566. {
  1567. ASN1char32_t *p;
  1568. ASN1stringtableentry_t chr, *entry;
  1569. *val = p = (ASN1char32_t *)DecMemAlloc(dec, (nchars + 1) * sizeof(ASN1char32_t));
  1570. if (p)
  1571. {
  1572. chr.lower = chr.upper = 0;
  1573. while (nchars--)
  1574. {
  1575. if (ASN1PERDecU32Val(dec, nbits, &chr.value))
  1576. {
  1577. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  1578. table->length, sizeof(ASN1stringtableentry_t),
  1579. ASN1CmpStringTableEntriesByIndex);
  1580. if (entry)
  1581. {
  1582. *p++ = entry->lower + (chr.value - entry->value);
  1583. }
  1584. else
  1585. {
  1586. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1587. return 0;
  1588. }
  1589. }
  1590. else
  1591. {
  1592. return 0;
  1593. }
  1594. }
  1595. *p = 0;
  1596. return 1;
  1597. }
  1598. return 0;
  1599. }
  1600. #endif // ENABLE_ALL
  1601. /* decode an object identifier */
  1602. int ASN1PERDecObjectIdentifier(ASN1decoding_t dec, ASN1objectidentifier_t *val)
  1603. {
  1604. ASN1uint32_t len, i, v;
  1605. ASN1octet_t *data, *p;
  1606. ASN1uint32_t nelem;
  1607. ASN1objectidentifier_t q;
  1608. if (ASN1PERDecFragmented(dec, &len, &data, 8))
  1609. {
  1610. int rc = 0;
  1611. nelem = 1;
  1612. for (i = 0, p = data; i < len; i++, p++)
  1613. {
  1614. if (!(*p & 0x80))
  1615. nelem++;
  1616. }
  1617. *val = q = DecAllocObjectIdentifier(dec, nelem);
  1618. if (q)
  1619. {
  1620. v = 0;
  1621. for (i = 0, p = data; i < len; i++, p++)
  1622. {
  1623. v = (v << 7) | (*p & 0x7f);
  1624. if (!(*p & 0x80))
  1625. {
  1626. if (q == *val)
  1627. { // first id
  1628. q->value = v / 40;
  1629. if (q->value > 2)
  1630. q->value = 2;
  1631. q->next->value = v - 40 * q->value;
  1632. q = q->next->next;
  1633. }
  1634. else
  1635. {
  1636. q->value = v;
  1637. q = q->next;
  1638. }
  1639. v = 0;
  1640. }
  1641. }
  1642. rc = 1;
  1643. }
  1644. DecMemFree(dec, data);
  1645. return rc;
  1646. }
  1647. return 0;
  1648. }
  1649. /* decode an object identifier */
  1650. int ASN1PERDecObjectIdentifier2(ASN1decoding_t dec, ASN1objectidentifier2_t *val)
  1651. {
  1652. ASN1uint32_t len, i, v;
  1653. ASN1octet_t *data, *p;
  1654. ASN1uint32_t nelem;
  1655. ASN1objectidentifier_t q;
  1656. int rc = 0;
  1657. if (ASN1PERDecFragmented(dec, &len, &data, 8))
  1658. {
  1659. if (len <= 16) // lonchanc: hard-coded value 16 to be consistent with ASN1objectidentifier2_t
  1660. {
  1661. val->count = 0;
  1662. v = 0;
  1663. for (i = 0, p = data; i < len; i++, p++)
  1664. {
  1665. v = (v << 7) | (*p & 0x7f);
  1666. if (!(*p & 0x80))
  1667. {
  1668. if (! val->count)
  1669. { // first id
  1670. val->value[0] = v / 40;
  1671. if (val->value[0] > 2)
  1672. val->value[0] = 2;
  1673. val->value[1] = v - 40 * val->value[0];
  1674. val->count = 2;
  1675. }
  1676. else
  1677. {
  1678. val->value[val->count++] = v;
  1679. }
  1680. v = 0;
  1681. }
  1682. }
  1683. // success
  1684. rc = 1;
  1685. }
  1686. else
  1687. {
  1688. ASN1DecSetError(dec, ASN1_ERR_LARGE);
  1689. }
  1690. DecMemFree(dec, data);
  1691. }
  1692. return rc;
  1693. }
  1694. /* decode a fragmented signed intx value */
  1695. #ifdef ENABLE_ALL
  1696. int ASN1PERDecFragmentedIntx(ASN1decoding_t dec, ASN1intx_t *val)
  1697. {
  1698. return ASN1PERDecFragmented(dec, &val->length, &val->value, 8);
  1699. }
  1700. #endif // ENABLE_ALL
  1701. /* decode a fragmented unsigned intx value */
  1702. #ifdef ENABLE_ALL
  1703. int ASN1PERDecFragmentedUIntx(ASN1decoding_t dec, ASN1intx_t *val)
  1704. {
  1705. if (ASN1PERDecFragmented(dec, &val->length, &val->value, 8))
  1706. {
  1707. if (val->length && val->value[0] > 0x7f)
  1708. {
  1709. ASN1octet_t *p;
  1710. if (NULL != (p = (ASN1octet_t *)DecMemAlloc(dec, val->length + 1)))
  1711. {
  1712. *p = 0;
  1713. CopyMemory(p + 1, val->value, val->length);
  1714. DecMemFree(dec, val->value);
  1715. val->value = p;
  1716. val->length++;
  1717. return 1;
  1718. }
  1719. return 0;
  1720. }
  1721. return 1;
  1722. }
  1723. return 0;
  1724. }
  1725. #endif // ENABLE_ALL
  1726. /* decode fragmented extension bits */
  1727. #ifdef ENABLE_ALL
  1728. int ASN1PERDecFragmentedExtension(ASN1decoding_t dec, ASN1uint32_t nbits, ASN1octet_t *val)
  1729. {
  1730. ASN1uint32_t m, n;
  1731. m = 0;
  1732. do {
  1733. if (ASN1PERDecFragmentedLength(dec, &n))
  1734. {
  1735. if (m + n <= nbits)
  1736. {
  1737. if (ASN1PERDecCheck(dec, n))
  1738. {
  1739. ASN1bitcpy(val, m, dec->pos, dec->bit, n);
  1740. PerDecAdvance(dec, n);
  1741. m += n;
  1742. }
  1743. else
  1744. {
  1745. return 0;
  1746. }
  1747. }
  1748. else
  1749. {
  1750. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  1751. return 0;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. return 0;
  1757. }
  1758. } while (n >= 0x4000);
  1759. return 1;
  1760. }
  1761. #endif // ENABLE_ALL
  1762. /* decode a fragmented string */
  1763. #ifdef ENABLE_ALL
  1764. int ASN1PERDecFragmentedCharString(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char_t **val, ASN1uint32_t nbits)
  1765. {
  1766. ASN1uint32_t m, n, i;
  1767. ASN1char_t *p;
  1768. *val = 0;
  1769. m = 0;
  1770. do {
  1771. if (ASN1PERDecFragmentedLength(dec, &n))
  1772. {
  1773. if (!n)
  1774. break;
  1775. if (ASN1PERDecCheck(dec, n * nbits))
  1776. {
  1777. if (NULL != (*val = (ASN1char_t *)DecMemReAlloc(dec, *val, m + n)))
  1778. {
  1779. p = *val + m;
  1780. m += n;
  1781. if (nbits == 8)
  1782. {
  1783. ASN1bitcpy((ASN1octet_t *)p, 0, dec->pos, dec->bit, n * 8);
  1784. PerDecAdvance(dec, n * 8);
  1785. }
  1786. else
  1787. {
  1788. for (i = n; i; i--)
  1789. {
  1790. *p++ = (ASN1char_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1791. PerDecAdvance(dec, nbits);
  1792. }
  1793. }
  1794. }
  1795. else
  1796. {
  1797. return 0;
  1798. }
  1799. }
  1800. else
  1801. {
  1802. return 0;
  1803. }
  1804. }
  1805. else
  1806. {
  1807. return 0;
  1808. }
  1809. } while (n >= 0x4000);
  1810. *nchars = m;
  1811. return 1;
  1812. }
  1813. #endif // ENABLE_ALL
  1814. /* decode a fragmented 16 bit string */
  1815. #ifdef ENABLE_ALL
  1816. int ASN1PERDecFragmentedChar16String(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char16_t **val, ASN1uint32_t nbits)
  1817. {
  1818. ASN1uint32_t m, n, i;
  1819. ASN1char16_t *p;
  1820. *val = 0;
  1821. m = 0;
  1822. do {
  1823. if (ASN1PERDecFragmentedLength(dec, &n))
  1824. {
  1825. if (!n)
  1826. break;
  1827. if (ASN1PERDecCheck(dec, n * nbits))
  1828. {
  1829. if (NULL != (*val = (ASN1char16_t *)DecMemReAlloc(dec, *val, (m + n) * sizeof(ASN1char16_t))))
  1830. {
  1831. p = *val + m;
  1832. m += n;
  1833. if (!dec->bit && nbits == 16)
  1834. {
  1835. for (i = n; i; i--)
  1836. {
  1837. *p++ = (dec->pos[0] << 8) | dec->pos[1];
  1838. dec->pos += 2;
  1839. }
  1840. }
  1841. else
  1842. {
  1843. for (i = n; i; i--)
  1844. {
  1845. *p++ = (ASN1char16_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1846. PerDecAdvance(dec, nbits);
  1847. }
  1848. }
  1849. }
  1850. else
  1851. {
  1852. return 0;
  1853. }
  1854. }
  1855. else
  1856. {
  1857. return 0;
  1858. }
  1859. }
  1860. else
  1861. {
  1862. return 0;
  1863. }
  1864. } while (n >= 0x4000);
  1865. *nchars = m;
  1866. return 1;
  1867. }
  1868. #endif // ENABLE_ALL
  1869. /* decode a fragmented 32 bit string */
  1870. #ifdef ENABLE_ALL
  1871. int ASN1PERDecFragmentedChar32String(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char32_t **val, ASN1uint32_t nbits)
  1872. {
  1873. ASN1uint32_t m, n, i;
  1874. ASN1char32_t *p;
  1875. *val = 0;
  1876. m = 0;
  1877. do {
  1878. if (ASN1PERDecFragmentedLength(dec, &n))
  1879. {
  1880. if (!n)
  1881. break;
  1882. if (ASN1PERDecCheck(dec, n * nbits))
  1883. {
  1884. if (NULL != (*val = (ASN1char32_t *)DecMemReAlloc(dec, *val, (m + n) * sizeof(ASN1char32_t))))
  1885. {
  1886. p = *val + m;
  1887. m += n;
  1888. if (!dec->bit && nbits == 32)
  1889. {
  1890. for (i = n; i; i--)
  1891. {
  1892. *p++ = (dec->pos[0] << 24) | (dec->pos[1] << 16) |
  1893. (dec->pos[2] << 8) | dec->pos[3];
  1894. dec->pos += 4;
  1895. }
  1896. }
  1897. else
  1898. {
  1899. for (i = n; i; i--)
  1900. {
  1901. *p++ = ASN1bitgetu(dec->pos, dec->bit, nbits);
  1902. PerDecAdvance(dec, nbits);
  1903. }
  1904. }
  1905. }
  1906. else
  1907. {
  1908. return 0;
  1909. }
  1910. }
  1911. else
  1912. {
  1913. return 0;
  1914. }
  1915. }
  1916. else
  1917. {
  1918. return 0;
  1919. }
  1920. } while (n >= 0x4000);
  1921. *nchars = m;
  1922. return 1;
  1923. }
  1924. #endif // ENABLE_ALL
  1925. /* decode a fragmented zero-terminated string */
  1926. int ASN1PERDecFragmentedZeroCharString(ASN1decoding_t dec, ASN1char_t **val, ASN1uint32_t nbits)
  1927. {
  1928. ASN1uint32_t m, n, i;
  1929. ASN1char_t *p;
  1930. *val = 0;
  1931. m = 0;
  1932. do {
  1933. if (ASN1PERDecFragmentedLength(dec, &n))
  1934. {
  1935. if (!n)
  1936. break;
  1937. if (ASN1PERDecCheck(dec, n * nbits))
  1938. {
  1939. if (NULL != (*val = (ASN1char_t *)DecMemReAlloc(dec, *val, m + n + 1)))
  1940. {
  1941. p = *val + m;
  1942. m += n;
  1943. if (nbits == 8)
  1944. {
  1945. ASN1bitcpy((ASN1octet_t *)p, 0, dec->pos, dec->bit, n * 8);
  1946. PerDecAdvance(dec, n * 8);
  1947. }
  1948. else
  1949. {
  1950. for (i = n; i; i--)
  1951. {
  1952. *p++ = (ASN1char_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  1953. PerDecAdvance(dec, nbits);
  1954. }
  1955. }
  1956. }
  1957. else
  1958. {
  1959. return 0;
  1960. }
  1961. }
  1962. else
  1963. {
  1964. return 0;
  1965. }
  1966. }
  1967. else
  1968. {
  1969. return 0;
  1970. }
  1971. } while (n >= 0x4000);
  1972. if (!*val)
  1973. *val = (ASN1char_t *)DecMemAlloc(dec, 1);
  1974. if (*val)
  1975. (*val)[m] = 0;
  1976. return 1;
  1977. }
  1978. /* decode a fragmented zero-terminated 16 bit string */
  1979. #ifdef ENABLE_ALL
  1980. int ASN1PERDecFragmentedZeroChar16String(ASN1decoding_t dec, ASN1char16_t **val, ASN1uint32_t nbits)
  1981. {
  1982. ASN1uint32_t m, n, i;
  1983. ASN1char16_t *p;
  1984. *val = 0;
  1985. m = 0;
  1986. do {
  1987. if (ASN1PERDecFragmentedLength(dec, &n))
  1988. {
  1989. if (!n)
  1990. break;
  1991. if (ASN1PERDecCheck(dec, n * nbits))
  1992. {
  1993. if (NULL != (*val = (ASN1char16_t *)DecMemReAlloc(dec, *val, (m + n + 1) * sizeof(ASN1char16_t))))
  1994. {
  1995. p = *val + m;
  1996. m += n;
  1997. if (!dec->bit && nbits == 16)
  1998. {
  1999. for (i = n; i; i--)
  2000. {
  2001. *p++ = (dec->pos[0] << 8) | dec->pos[1];
  2002. dec->pos += 2;
  2003. }
  2004. }
  2005. else
  2006. {
  2007. for (i = n; i; i--)
  2008. {
  2009. *p++ = (ASN1char16_t) ASN1bitgetu(dec->pos, dec->bit, nbits);
  2010. PerDecAdvance(dec, nbits);
  2011. }
  2012. }
  2013. }
  2014. else
  2015. {
  2016. return 0;
  2017. }
  2018. }
  2019. else
  2020. {
  2021. return 0;
  2022. }
  2023. }
  2024. else
  2025. {
  2026. return 0;
  2027. }
  2028. } while (n >= 0x4000);
  2029. if (!*val)
  2030. *val = (ASN1char16_t *)DecMemAlloc(dec, sizeof(ASN1char16_t));
  2031. if (*val)
  2032. (*val)[m] = 0;
  2033. return 1;
  2034. }
  2035. #endif // ENABLE_ALL
  2036. /* decode a fragmented zero-terminated 32 bit string */
  2037. #ifdef ENABLE_ALL
  2038. int ASN1PERDecFragmentedZeroChar32String(ASN1decoding_t dec, ASN1char32_t **val, ASN1uint32_t nbits)
  2039. {
  2040. ASN1uint32_t m, n, i;
  2041. ASN1char32_t *p;
  2042. *val = 0;
  2043. m = 0;
  2044. do {
  2045. if (ASN1PERDecFragmentedLength(dec, &n))
  2046. {
  2047. if (!n)
  2048. break;
  2049. if (ASN1PERDecCheck(dec, n * nbits))
  2050. {
  2051. if (NULL != (*val = (ASN1char32_t *)DecMemReAlloc(dec, *val, (m + n + 1) * sizeof(ASN1char32_t))))
  2052. {
  2053. p = *val + m;
  2054. m += n;
  2055. if (!dec->bit && nbits == 32)
  2056. {
  2057. for (i = n; i; i--)
  2058. {
  2059. *p++ = (dec->pos[0] << 24) | (dec->pos[1] << 16) |
  2060. (dec->pos[2] << 8) | dec->pos[3];
  2061. dec->pos += 4;
  2062. }
  2063. }
  2064. else
  2065. {
  2066. for (i = n; i; i--)
  2067. {
  2068. *p++ = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2069. PerDecAdvance(dec, nbits);
  2070. }
  2071. }
  2072. }
  2073. else
  2074. {
  2075. return 0;
  2076. }
  2077. }
  2078. else
  2079. {
  2080. return 0;
  2081. }
  2082. }
  2083. else
  2084. {
  2085. return 0;
  2086. }
  2087. } while (n >= 0x4000);
  2088. if (!*val)
  2089. *val = (ASN1char32_t *)DecMemAlloc(dec, sizeof(ASN1char32_t));
  2090. if (*val)
  2091. (*val)[m] = 0;
  2092. return 1;
  2093. }
  2094. #endif // ENABLE_ALL
  2095. /* decode a fragmented table string */
  2096. #ifdef ENABLE_ALL
  2097. int ASN1PERDecFragmentedTableCharString(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2098. {
  2099. ASN1uint32_t m, n, i;
  2100. ASN1stringtableentry_t chr, *entry;
  2101. ASN1char_t *p;
  2102. *val = 0;
  2103. m = 0;
  2104. chr.lower = chr.upper = 0;
  2105. do {
  2106. if (ASN1PERDecFragmentedLength(dec, &n))
  2107. {
  2108. if (!n)
  2109. break;
  2110. if (ASN1PERDecCheck(dec, n * nbits))
  2111. {
  2112. if (NULL != (*val = (ASN1char_t *)DecMemReAlloc(dec, *val, m + n)))
  2113. {
  2114. p = *val + m;
  2115. m += n;
  2116. for (i = n; i; i--)
  2117. {
  2118. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2119. PerDecAdvance(dec, nbits);
  2120. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2121. table->length, sizeof(ASN1stringtableentry_t),
  2122. ASN1CmpStringTableEntriesByIndex);
  2123. if (entry)
  2124. {
  2125. *p++ = (ASN1char_t) (entry->lower + (chr.value - entry->value));
  2126. }
  2127. else
  2128. {
  2129. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2130. return 0;
  2131. }
  2132. }
  2133. }
  2134. else
  2135. {
  2136. return 0;
  2137. }
  2138. }
  2139. else
  2140. {
  2141. return 0;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. return 0;
  2147. }
  2148. } while (n >= 0x4000);
  2149. *nchars = m;
  2150. return 1;
  2151. }
  2152. #endif // ENABLE_ALL
  2153. /* decode a fragmented 16 bit table string */
  2154. #ifdef ENABLE_ALL
  2155. int ASN1PERDecFragmentedTableChar16String(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char16_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2156. {
  2157. ASN1uint32_t m, n, i;
  2158. ASN1stringtableentry_t chr, *entry;
  2159. ASN1char16_t *p;
  2160. *val = 0;
  2161. m = 0;
  2162. chr.lower = chr.upper = 0;
  2163. do {
  2164. if (ASN1PERDecFragmentedLength(dec, &n))
  2165. {
  2166. if (!n)
  2167. break;
  2168. if (ASN1PERDecCheck(dec, n * nbits))
  2169. {
  2170. if (NULL != (*val = (ASN1char16_t *)DecMemReAlloc(dec, *val, (m + n) * sizeof(ASN1char16_t))))
  2171. {
  2172. p = *val + m;
  2173. m += n;
  2174. for (i = n; i; i--)
  2175. {
  2176. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2177. PerDecAdvance(dec, nbits);
  2178. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2179. table->length, sizeof(ASN1stringtableentry_t),
  2180. ASN1CmpStringTableEntriesByIndex);
  2181. if (entry)
  2182. {
  2183. *p++ = (ASN1char16_t) (entry->lower + (chr.value - entry->value));
  2184. }
  2185. else
  2186. {
  2187. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2188. return 0;
  2189. }
  2190. }
  2191. }
  2192. else
  2193. {
  2194. return 0;
  2195. }
  2196. }
  2197. else
  2198. {
  2199. return 0;
  2200. }
  2201. }
  2202. else
  2203. {
  2204. return 0;
  2205. }
  2206. } while (n >= 0x4000);
  2207. *nchars = m;
  2208. return 1;
  2209. }
  2210. #endif // ENABLE_ALL
  2211. /* decode a fragmented 32 bit table string */
  2212. #ifdef ENABLE_ALL
  2213. int ASN1PERDecFragmentedTableChar32String(ASN1decoding_t dec, ASN1uint32_t *nchars, ASN1char32_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2214. {
  2215. ASN1uint32_t m, n, i;
  2216. ASN1stringtableentry_t chr, *entry;
  2217. ASN1char32_t *p;
  2218. *val = 0;
  2219. m = 0;
  2220. chr.lower = chr.upper = 0;
  2221. do {
  2222. if (ASN1PERDecFragmentedLength(dec, &n))
  2223. {
  2224. if (!n)
  2225. break;
  2226. if (ASN1PERDecCheck(dec, n * nbits))
  2227. {
  2228. if (NULL != (*val = (ASN1char32_t *)DecMemReAlloc(dec, *val, (m + n) * sizeof(ASN1char32_t))))
  2229. {
  2230. p = *val + m;
  2231. m += n;
  2232. for (i = n; i; i--)
  2233. {
  2234. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2235. PerDecAdvance(dec, nbits);
  2236. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2237. table->length, sizeof(ASN1stringtableentry_t),
  2238. ASN1CmpStringTableEntriesByIndex);
  2239. if (entry)
  2240. {
  2241. *p++ = entry->lower + (chr.value - entry->value);
  2242. }
  2243. else
  2244. {
  2245. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2246. return 0;
  2247. }
  2248. }
  2249. }
  2250. else
  2251. {
  2252. return 0;
  2253. }
  2254. }
  2255. else
  2256. {
  2257. return 0;
  2258. }
  2259. }
  2260. else
  2261. {
  2262. return 0;
  2263. }
  2264. } while (n >= 0x4000);
  2265. *nchars = m;
  2266. return 1;
  2267. }
  2268. #endif // ENABLE_ALL
  2269. /* decode a fragmented zero-terminated table string */
  2270. #ifdef ENABLE_ALL
  2271. int ASN1PERDecFragmentedZeroTableCharString(ASN1decoding_t dec, ASN1char_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2272. {
  2273. ASN1uint32_t m, n, i;
  2274. ASN1stringtableentry_t chr, *entry;
  2275. ASN1char_t *p;
  2276. *val = 0;
  2277. m = 0;
  2278. chr.lower = chr.upper = 0;
  2279. do {
  2280. if (ASN1PERDecFragmentedLength(dec, &n))
  2281. {
  2282. if (!n)
  2283. break;
  2284. if (ASN1PERDecCheck(dec, n * nbits))
  2285. {
  2286. if (NULL != (*val = (ASN1char_t *)DecMemReAlloc(dec, *val, m + n + 1)))
  2287. {
  2288. p = *val + m;
  2289. m += n;
  2290. for (i = n; i; i--)
  2291. {
  2292. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2293. PerDecAdvance(dec, nbits);
  2294. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2295. table->length, sizeof(ASN1stringtableentry_t),
  2296. ASN1CmpStringTableEntriesByIndex);
  2297. if (entry)
  2298. {
  2299. *p++ = (ASN1char_t) (entry->lower + (chr.value - entry->value));
  2300. }
  2301. else
  2302. {
  2303. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2304. return 0;
  2305. }
  2306. }
  2307. }
  2308. else
  2309. {
  2310. return 0;
  2311. }
  2312. }
  2313. else
  2314. {
  2315. return 0;
  2316. }
  2317. }
  2318. else
  2319. {
  2320. return 0;
  2321. }
  2322. } while (n >= 0x4000);
  2323. if (!*val)
  2324. *val = (ASN1char_t *)DecMemAlloc(dec, 1);
  2325. if (*val)
  2326. (*val)[m] = 0;
  2327. return 1;
  2328. }
  2329. #endif // ENABLE_ALL
  2330. /* decode a fragmented zero-terminated 16 bit table string */
  2331. #ifdef ENABLE_ALL
  2332. int ASN1PERDecFragmentedZeroTableChar16String(ASN1decoding_t dec, ASN1char16_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2333. {
  2334. ASN1uint32_t m, n, i;
  2335. ASN1stringtableentry_t chr, *entry;
  2336. ASN1char16_t *p;
  2337. *val = 0;
  2338. m = 0;
  2339. chr.lower = chr.upper = 0;
  2340. do {
  2341. if (ASN1PERDecFragmentedLength(dec, &n))
  2342. {
  2343. if (!n)
  2344. break;
  2345. if (ASN1PERDecCheck(dec, n * nbits))
  2346. {
  2347. if (NULL != (*val = (ASN1char16_t *)DecMemReAlloc(dec, *val, (m + n + 1) * sizeof(ASN1char16_t))))
  2348. {
  2349. p = *val + m;
  2350. m += n;
  2351. for (i = n; i; i--)
  2352. {
  2353. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2354. PerDecAdvance(dec, nbits);
  2355. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2356. table->length, sizeof(ASN1stringtableentry_t),
  2357. ASN1CmpStringTableEntriesByIndex);
  2358. if (entry)
  2359. {
  2360. *p++ = (ASN1char16_t) (entry->lower + (chr.value - entry->value));
  2361. }
  2362. else
  2363. {
  2364. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2365. return 0;
  2366. }
  2367. }
  2368. }
  2369. else
  2370. {
  2371. return 0;
  2372. }
  2373. }
  2374. else
  2375. {
  2376. return 0;
  2377. }
  2378. }
  2379. else
  2380. {
  2381. return 0;
  2382. }
  2383. } while (n >= 0x4000);
  2384. if (!*val)
  2385. *val = (ASN1char16_t *)DecMemAlloc(dec, sizeof(ASN1char16_t));
  2386. if (*val)
  2387. (*val)[m] = 0;
  2388. return 1;
  2389. }
  2390. #endif // ENABLE_ALL
  2391. /* decode a fragmented zero-terminated 32 bit table string */
  2392. #ifdef ENABLE_ALL
  2393. int ASN1PERDecFragmentedZeroTableChar32String(ASN1decoding_t dec, ASN1char32_t **val, ASN1uint32_t nbits, ASN1stringtable_t *table)
  2394. {
  2395. ASN1uint32_t m, n, i;
  2396. ASN1stringtableentry_t chr, *entry;
  2397. ASN1char32_t *p;
  2398. *val = 0;
  2399. m = 0;
  2400. chr.lower = chr.upper = 0;
  2401. do {
  2402. if (ASN1PERDecFragmentedLength(dec, &n))
  2403. {
  2404. if (!n)
  2405. break;
  2406. if (ASN1PERDecCheck(dec, n * nbits))
  2407. {
  2408. if (NULL != (*val = (ASN1char32_t *)DecMemReAlloc(dec, *val, (m + n + 1) * sizeof(ASN1char32_t))))
  2409. {
  2410. p = *val + m;
  2411. m += n;
  2412. for (i = n; i; i--)
  2413. {
  2414. chr.value = ASN1bitgetu(dec->pos, dec->bit, nbits);
  2415. PerDecAdvance(dec, nbits);
  2416. entry = (ASN1stringtableentry_t *)ms_bSearch(&chr, table->values,
  2417. table->length, sizeof(ASN1stringtableentry_t),
  2418. ASN1CmpStringTableEntriesByIndex);
  2419. if (entry)
  2420. {
  2421. *p++ = entry->lower + (chr.value - entry->value);
  2422. }
  2423. else
  2424. {
  2425. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2426. return 0;
  2427. }
  2428. }
  2429. }
  2430. else
  2431. {
  2432. return 0;
  2433. }
  2434. }
  2435. else
  2436. {
  2437. return 0;
  2438. }
  2439. }
  2440. else
  2441. {
  2442. return 0;
  2443. }
  2444. } while (n >= 0x4000);
  2445. if (!*val)
  2446. *val = (ASN1char32_t *)DecMemAlloc(dec, sizeof(ASN1char32_t));
  2447. if (*val)
  2448. (*val)[m] = 0;
  2449. return 1;
  2450. }
  2451. #endif // ENABLE_ALL
  2452. /* decode a generalized time */
  2453. int ASN1PERDecGeneralizedTime(ASN1decoding_t dec, ASN1generalizedtime_t *val, ASN1uint32_t nbits)
  2454. {
  2455. ASN1ztcharstring_t time;
  2456. if (ASN1PERDecFragmentedZeroCharString(dec, &time, nbits))
  2457. {
  2458. int rc = ASN1string2generalizedtime(val, time);
  2459. DecMemFree(dec, time);
  2460. if (rc)
  2461. {
  2462. return 1;
  2463. }
  2464. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2465. }
  2466. return 0;
  2467. }
  2468. /* decode a utc time */
  2469. #ifdef ENABLE_ALL
  2470. int ASN1PERDecUTCTime(ASN1decoding_t dec, ASN1utctime_t *val, ASN1uint32_t nbits)
  2471. {
  2472. ASN1ztcharstring_t time;
  2473. if (ASN1PERDecFragmentedZeroCharString(dec, &time, nbits))
  2474. {
  2475. int rc = ASN1string2utctime(val, time);
  2476. DecMemFree(dec, time);
  2477. if (rc)
  2478. {
  2479. return 1;
  2480. }
  2481. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  2482. }
  2483. return 0;
  2484. }
  2485. #endif // ENABLE_ALL