Source code of Windows XP (NT5)
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.

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