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.

1290 lines
35 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. #include "cintern.h"
  5. #ifdef TEST_CODER
  6. typedef struct ASN1testcoder_s
  7. {
  8. struct ASN1INTERNencoding_s e;
  9. struct ASN1INTERNdecoding_s d;
  10. } *ASN1testcoder_t;
  11. #define ASN1_TEST_CODER_SIZE (sizeof(struct ASN1testcoder_s))
  12. int TestEnc_InitCoder(ASN1INTERNencoding_t e, ASN1module_t mod);
  13. int TestDec_InitCoder(ASN1INTERNdecoding_t d, ASN1module_t mod);
  14. int TestEnc_Compare(ASN1INTERNencoding_t e, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize);
  15. int TestDec_Compare(ASN1INTERNdecoding_t d, ASN1uint32_t id, void *valref, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize);
  16. #else
  17. #define ASN1_TEST_CODER_SIZE 0
  18. #endif
  19. /* init an ASN1encoding_t */
  20. ASN1error_e ASN1_CreateEncoder
  21. (
  22. ASN1module_t mod,
  23. ASN1encoding_t *enc,
  24. ASN1octet_t *pbBuf,
  25. ASN1uint32_t cbBufSize,
  26. ASN1encoding_t pParent
  27. )
  28. {
  29. if (NULL != mod && NULL != enc)
  30. {
  31. ASN1INTERNencoding_t e;
  32. *enc = NULL;
  33. /* construct ASN1encoding_t */
  34. e = (ASN1INTERNencoding_t)MemAlloc(sizeof(*e) + ASN1_TEST_CODER_SIZE, mod->nModuleName);
  35. if (NULL != e)
  36. {
  37. ZeroMemory(e, sizeof(*e) + ASN1_TEST_CODER_SIZE);
  38. e->info.magic = MAGIC_ENCODER;
  39. e->info.err = ASN1_SUCCESS;
  40. // e->info.pos = e->info.buf = NULL;
  41. // e->info.size = e->info.len = e->info.bit = 0;
  42. e->info.dwFlags = mod->dwFlags;
  43. e->info.module = mod;
  44. // e->child = NULL;
  45. /* set buffer if given */
  46. if (NULL != pbBuf && cbBufSize)
  47. {
  48. e->info.dwFlags |= ASN1ENCODE_SETBUFFER;
  49. e->info.pos = e->info.buf = pbBuf;
  50. e->info.size = cbBufSize;
  51. }
  52. /* set parent if parented, otherwise, initialized to itself */
  53. if (NULL != pParent)
  54. {
  55. e->parent = (ASN1INTERNencoding_t) pParent;
  56. e->info.eRule = pParent->eRule;
  57. }
  58. else
  59. {
  60. e->parent = e;
  61. e->info.eRule = mod->eRule;
  62. }
  63. // e->mem = NULL;
  64. // e->memlength = 0;
  65. // e->memsize = 0;
  66. // e->epi = NULL;
  67. // e->epilength = 0;
  68. // e->episize = 0;
  69. // e->csi = NULL;
  70. // e->csilength = 0;
  71. // e->csisize = 0;
  72. if (! (e->info.dwFlags & ASN1ENCODE_SETBUFFER) && (NULL != pParent))
  73. {
  74. // lonchanc: make sure we have a minimum 256 bytes available.
  75. BOOL fRet = FALSE;
  76. if (ASN1_PER_RULE & e->info.eRule)
  77. {
  78. // this is required for h245.
  79. fRet = ASN1PEREncCheck((ASN1encoding_t) e, 1);
  80. }
  81. #ifdef ENABLE_BER
  82. else
  83. if (ASN1_BER_RULE & e->info.eRule)
  84. {
  85. // this is required for h245.
  86. fRet = ASN1BEREncCheck((ASN1encoding_t) e, 1);
  87. }
  88. #endif // ENABLE_BER
  89. else
  90. {
  91. EncAssert((ASN1encoding_t) e, 0);
  92. MemFree(e);
  93. return ASN1_ERR_RULE;
  94. }
  95. if (fRet)
  96. {
  97. // lonchanc: make sure the first byte is zeroed out, which
  98. // is required for h245.
  99. e->info.buf[0] = '\0';
  100. }
  101. else
  102. {
  103. MemFree(e);
  104. return ASN1_ERR_MEMORY;
  105. }
  106. }
  107. #if defined(TEST_CODER) && defined(_DEBUG)
  108. TestEnc_InitCoder(e, mod);
  109. #endif // defined(TEST_CODER) && defined(_DEBUG)
  110. if (NULL != pParent)
  111. {
  112. EncAssert((ASN1encoding_t) e, NULL == ((ASN1INTERNencoding_t) pParent)->child);
  113. ((ASN1INTERNencoding_t) pParent)->child = e;
  114. }
  115. *enc = (ASN1encoding_t) e;
  116. return ASN1_SUCCESS;
  117. }
  118. else
  119. {
  120. return ASN1_ERR_MEMORY;
  121. }
  122. }
  123. return ASN1_ERR_BADARGS;
  124. }
  125. /* encode a value */
  126. ASN1error_e ASN1_Encode
  127. (
  128. ASN1encoding_t enc,
  129. void *value,
  130. ASN1uint32_t id,
  131. ASN1uint32_t flags,
  132. ASN1octet_t *pbBuf,
  133. ASN1uint32_t cbBufSize
  134. )
  135. {
  136. if (NULL != enc)
  137. {
  138. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  139. /* check magic numbers */
  140. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  141. /* clear error */
  142. ASN1EncSetError(enc, ASN1_SUCCESS);
  143. /* new buffer given? */
  144. if (flags & ASN1ENCODE_SETBUFFER)
  145. {
  146. e->info.dwFlags |= ASN1ENCODE_SETBUFFER;
  147. enc->pos = enc->buf = pbBuf;
  148. enc->size = cbBufSize;
  149. enc->len = enc->bit = 0;
  150. }
  151. /* use a new buffer? */
  152. else if ((e->info.dwFlags | flags) & ASN1ENCODE_ALLOCATEBUFFER)
  153. {
  154. e->info.dwFlags &= ~ASN1ENCODE_SETBUFFER;
  155. enc->pos = enc->buf = NULL;
  156. enc->size = enc->len = enc->bit = 0;
  157. }
  158. /* reuse buffer? */
  159. else if ((flags & ASN1ENCODE_REUSEBUFFER) || !((e->info.dwFlags | flags) & ASN1ENCODE_APPEND))
  160. {
  161. EncAssert(enc, NULL != enc->buf);
  162. enc->pos = enc->buf;
  163. enc->bit = enc->len = 0;
  164. }
  165. /* otherwise append to buffer */
  166. /* check id number */
  167. if (id < enc->module->cPDUs)
  168. {
  169. if (ASN1_PER_RULE & enc->eRule)
  170. {
  171. /* encode value */
  172. ASN1PerEncFun_t pfnPER;
  173. if (NULL != (pfnPER = enc->module->PER.apfnEncoder[id]))
  174. {
  175. if ((*pfnPER)(enc, value))
  176. {
  177. ASN1PEREncFlush(enc);
  178. }
  179. else
  180. {
  181. // the error code must be an error
  182. if (ASN1_SUCCEEDED(e->parent->info.err))
  183. {
  184. // cannot return here immediately because we need to do cleanup
  185. ASN1EncSetError(enc, ASN1_ERR_CORRUPT);
  186. }
  187. }
  188. }
  189. else
  190. {
  191. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  192. }
  193. }
  194. #ifdef ENABLE_BER
  195. else
  196. if (ASN1_BER_RULE & enc->eRule)
  197. {
  198. /* encode value */
  199. ASN1BerEncFun_t pfnBER;
  200. if (NULL != (pfnBER = enc->module->BER.apfnEncoder[id]))
  201. {
  202. if ((*pfnBER)(enc, 0, value)) // lonchanc: tag is 0 to make it compiled
  203. {
  204. ASN1BEREncFlush(enc);
  205. }
  206. else
  207. {
  208. // the error code must be an error
  209. if (ASN1_SUCCEEDED(e->parent->info.err))
  210. {
  211. // cannot return here immediately because we need to do cleanup
  212. ASN1EncSetError(enc, ASN1_ERR_CORRUPT);
  213. }
  214. }
  215. }
  216. else
  217. {
  218. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  219. }
  220. }
  221. #endif // ENABLE_BER
  222. else
  223. {
  224. return ASN1EncSetError(enc, ASN1_ERR_RULE);
  225. }
  226. /* call abort/done function for non-parented encoding stream */
  227. if (ASN1_SUCCEEDED(e->parent->info.err))
  228. {
  229. // not parented
  230. if (e == e->parent)
  231. {
  232. #if defined(TEST_CODER) && defined(_DEBUG)
  233. if (ASN1_PER_RULE & enc->eRule)
  234. {
  235. if (! TestEnc_Compare(e, id, enc->buf + enc->cbExtraHeader, enc->len - enc->cbExtraHeader))
  236. {
  237. MyDebugBreak();
  238. }
  239. }
  240. #ifdef ENABLE_BER
  241. else
  242. if (ASN1_BER_RULE & enc->eRule)
  243. {
  244. if (! TestEnc_Compare(e, id, enc->buf + enc->cbExtraHeader, enc->len - enc->cbExtraHeader))
  245. {
  246. MyDebugBreak();
  247. }
  248. }
  249. #endif // ENABLE_BER
  250. #endif
  251. ASN1EncDone(enc);
  252. }
  253. }
  254. else
  255. {
  256. ASN1INTERNencoding_t child, child2;
  257. // not parented
  258. if (e == e->parent)
  259. {
  260. ASN1EncAbort(enc);
  261. }
  262. // clean up...
  263. if ((e->info.dwFlags | flags) & ASN1ENCODE_ALLOCATEBUFFER)
  264. {
  265. ASN1_FreeEncoded(enc, enc->buf);
  266. enc->pos = enc->buf = NULL;
  267. enc->size = enc->len = enc->bit = 0;
  268. }
  269. for (child = e->child; child; child = child2)
  270. {
  271. child2 = child->child;
  272. // make sure it does not touch its parent which may already be freed
  273. child->parent = child;
  274. ASN1_CloseEncoder2((ASN1encoding_t) child);
  275. }
  276. e->child = NULL;
  277. }
  278. /* return error code */
  279. return e->parent->info.err;
  280. }
  281. else
  282. {
  283. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  284. }
  285. }
  286. return ASN1_ERR_BADARGS;
  287. }
  288. /* control function for encoding */
  289. ASN1error_e ASN1_SetEncoderOption
  290. (
  291. ASN1encoding_t enc,
  292. ASN1optionparam_t *pOptParam
  293. )
  294. {
  295. if (NULL != enc && NULL != pOptParam)
  296. {
  297. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  298. ASN1error_e rc = ASN1_SUCCESS;
  299. /* check magic number */
  300. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  301. switch (pOptParam->eOption)
  302. {
  303. case ASN1OPT_CHANGE_RULE:
  304. enc->eRule = pOptParam->eRule;
  305. break;
  306. case ASN1OPT_NOT_REUSE_BUFFER:
  307. e->info.dwFlags &= ~ASN1ENCODE_SETBUFFER;
  308. enc->buf = enc->pos = NULL;
  309. enc->size = enc->bit = enc->len = 0;
  310. break;
  311. case ASN1OPT_REWIND_BUFFER:
  312. enc->pos = enc->buf;
  313. enc->bit = enc->len = 0;
  314. break;
  315. default:
  316. rc = ASN1_ERR_BADARGS;
  317. break;
  318. }
  319. return ASN1EncSetError(enc, rc);
  320. }
  321. return ASN1_ERR_BADARGS;
  322. }
  323. ASN1error_e ASN1_GetEncoderOption
  324. (
  325. ASN1encoding_t enc,
  326. ASN1optionparam_t *pOptParam
  327. )
  328. {
  329. if (NULL != enc && NULL != pOptParam)
  330. {
  331. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  332. ASN1error_e rc = ASN1_SUCCESS;
  333. /* check magic number */
  334. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  335. switch (pOptParam->eOption)
  336. {
  337. case ASN1OPT_GET_RULE:
  338. pOptParam->eRule = enc->eRule;
  339. break;
  340. default:
  341. rc = ASN1_ERR_BADARGS;
  342. break;
  343. }
  344. return ASN1EncSetError(enc, rc);
  345. }
  346. return ASN1_ERR_BADARGS;
  347. }
  348. /* destroy encoding stream */
  349. void ASN1_CloseEncoder
  350. (
  351. ASN1encoding_t enc
  352. )
  353. {
  354. if (NULL != enc)
  355. {
  356. ASN1INTERNencoding_t e = (ASN1INTERNencoding_t)enc;
  357. /* check magic number */
  358. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  359. if (e != e->parent)
  360. {
  361. EncAssert(enc, e == e->parent->child);
  362. e->parent->child = NULL;
  363. }
  364. /* free encoding stream */
  365. MemFree(e);
  366. }
  367. }
  368. /* destroy encoding stream */
  369. void ASN1_CloseEncoder2
  370. (
  371. ASN1encoding_t enc
  372. )
  373. {
  374. if (NULL != enc)
  375. {
  376. /* check magic number */
  377. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  378. EncMemFree(enc, enc->buf);
  379. ASN1_CloseEncoder(enc);
  380. }
  381. }
  382. /* init an ASN1decoding_t */
  383. ASN1error_e ASN1_CreateDecoder
  384. (
  385. ASN1module_t mod,
  386. ASN1decoding_t *dec,
  387. ASN1octet_t *pbBuf,
  388. ASN1uint32_t cbBufSize,
  389. ASN1decoding_t pParent
  390. )
  391. {
  392. return ASN1_CreateDecoderEx(mod, dec, pbBuf, cbBufSize, pParent, ASN1FLAGS_NONE);
  393. }
  394. ASN1error_e ASN1_CreateDecoderEx
  395. (
  396. ASN1module_t mod,
  397. ASN1decoding_t *dec,
  398. ASN1octet_t *pbBuf,
  399. ASN1uint32_t cbBufSize,
  400. ASN1decoding_t pParent,
  401. ASN1uint32_t dwFlags
  402. )
  403. {
  404. if (NULL != mod && NULL != dec)
  405. {
  406. ASN1INTERNdecoding_t d;
  407. *dec = NULL;
  408. /* construct ASN1decoding_t */
  409. d = (ASN1INTERNdecoding_t)MemAlloc(sizeof(*d) + ASN1_TEST_CODER_SIZE, mod->nModuleName);
  410. if (NULL != d)
  411. {
  412. ZeroMemory(d, sizeof(*d) + ASN1_TEST_CODER_SIZE);
  413. d->info.magic = MAGIC_DECODER;
  414. d->info.err = ASN1_SUCCESS;
  415. d->info.dwFlags = mod->dwFlags;
  416. d->info.module = mod;
  417. // d->child = NULL;
  418. /* set parent if parented */
  419. if (NULL != pParent)
  420. {
  421. DecAssert((ASN1decoding_t) d, NULL == ((ASN1INTERNdecoding_t) pParent)->child);
  422. ((ASN1INTERNdecoding_t) pParent)->child = d;
  423. d->parent = (ASN1INTERNdecoding_t) pParent;
  424. d->info.eRule = pParent->eRule;
  425. }
  426. else
  427. /* initialize otherwise */
  428. {
  429. d->parent = d;
  430. d->info.eRule = mod->eRule;
  431. }
  432. /* set buffer if given */
  433. // lonchanc: it is ok to have a zero buffer size here
  434. if (NULL != pbBuf)
  435. {
  436. d->info.dwFlags |= ASN1DECODE_SETBUFFER;
  437. d->info.buf = d->info.pos = pbBuf;
  438. d->info.size = cbBufSize;
  439. // d->info.len = d->info.bit = 0;
  440. if ((dwFlags & ASN1DECODE_AUTOFREEBUFFER)
  441. && !d->parent->fExtBuf)
  442. {
  443. // dbarlow: It's possible the buffer isn't really
  444. // allocated, but instead came from the
  445. // parent's Extension buffer.
  446. d->info.dwFlags |= ASN1DECODE_AUTOFREEBUFFER;
  447. }
  448. }
  449. // else
  450. // {
  451. // d->info.buf = d->info.pos = NULL;
  452. // d->info.size = d->info.len = d->info.bit = 0;
  453. // }
  454. // d->mem = NULL;
  455. // d->memlength = 0;
  456. // d->memsize = 0;
  457. // d->epi = NULL;
  458. // d->epilength = 0;
  459. // d->episize = 0;
  460. // d->csi = NULL;
  461. // d->csilength = 0;
  462. // d->csisize = 0;
  463. #if defined(TEST_CODER) && defined(_DEBUG)
  464. TestDec_InitCoder(d, mod);
  465. #endif // defined(TEST_CODER) && defined(_DEBUG)
  466. *dec = (ASN1decoding_t) d;
  467. return ASN1_SUCCESS;
  468. }
  469. else
  470. {
  471. if (dwFlags & ASN1DECODE_AUTOFREEBUFFER)
  472. {
  473. // dbarlow: It's possible the buffer isn't really
  474. // allocated, but instead came from the
  475. // parent's Extension buffer.
  476. d = (ASN1INTERNdecoding_t)pParent;
  477. if ((NULL == d) || !d->fExtBuf)
  478. {
  479. MemFree(pbBuf);
  480. }
  481. }
  482. return ASN1_ERR_MEMORY;
  483. }
  484. }
  485. return ASN1_ERR_BADARGS;
  486. }
  487. /* decode a value */
  488. ASN1error_e ASN1_Decode
  489. (
  490. ASN1decoding_t dec,
  491. void **valref,
  492. ASN1uint32_t id,
  493. ASN1uint32_t flags,
  494. ASN1octet_t *pbBuf,
  495. ASN1uint32_t cbBufSize
  496. )
  497. {
  498. if (NULL != dec && NULL != valref)
  499. {
  500. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  501. /* check magic numbers */
  502. DecAssert(dec, MAGIC_DECODER == dec->magic);
  503. /* clear error */
  504. ASN1DecSetError(dec, ASN1_SUCCESS);
  505. /* new buffer given? */
  506. if (flags & ASN1DECODE_SETBUFFER)
  507. {
  508. if (NULL != pbBuf && 0 != cbBufSize)
  509. {
  510. dec->pos = dec->buf = pbBuf;
  511. dec->size = cbBufSize;
  512. dec->bit = dec->len = 0;
  513. }
  514. else
  515. {
  516. return ASN1DecSetError(dec, ASN1_ERR_BADARGS);
  517. }
  518. }
  519. /* rewind buffer? */
  520. else if ((flags & ASN1DECODE_REWINDBUFFER) ||
  521. !((d->info.dwFlags | flags ) & ASN1DECODE_APPENDED))
  522. {
  523. dec->pos = dec->buf;
  524. dec->bit = dec->len = 0;
  525. }
  526. /* otherwise continue reading from last buffer */
  527. /* check id number */
  528. if (id < dec->module->cPDUs)
  529. {
  530. ASN1uint32_t cbTopLevelStruct;
  531. /* clear length of linear buffer required */
  532. d->cbLinearBufSize = 0;
  533. /* double check for the availability of destination buffer */
  534. if (d->lpOrigExtBuf == NULL || d->cbOrigExtBufSize == 0)
  535. {
  536. d->fExtBuf = FALSE;
  537. }
  538. cbTopLevelStruct = dec->module->acbStructSize[id];
  539. if (NULL != (*valref = DecMemAlloc(dec, cbTopLevelStruct)))
  540. {
  541. if (ASN1_PER_RULE & dec->eRule)
  542. {
  543. ASN1PerDecFun_t pfnPER;
  544. /* decode value */
  545. if (NULL != (pfnPER = dec->module->PER.apfnDecoder[id]))
  546. {
  547. if ((*pfnPER)(dec, *valref))
  548. {
  549. ASN1PERDecFlush(dec);
  550. }
  551. else
  552. {
  553. // the error code must be an error
  554. if (ASN1_SUCCEEDED(d->parent->info.err))
  555. {
  556. // cannot return here immediately because we need to do cleanup
  557. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  558. }
  559. }
  560. }
  561. else
  562. {
  563. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  564. }
  565. }
  566. #ifdef ENABLE_BER
  567. else
  568. if (ASN1_BER_RULE & dec->eRule)
  569. {
  570. ASN1BerDecFun_t pfnBER;
  571. /* decode value */
  572. if (NULL != (pfnBER = dec->module->BER.apfnDecoder[id]))
  573. {
  574. if ((*pfnBER)(dec, 0, *valref)) // lonchanc: tag is 0 to make it compiled
  575. {
  576. ASN1BERDecFlush(dec);
  577. }
  578. else
  579. {
  580. // the error code must be an error
  581. if (ASN1_SUCCEEDED(d->parent->info.err))
  582. {
  583. // cannot return here immediately because we need to do cleanup
  584. ASN1DecSetError(dec, ASN1_ERR_CORRUPT);
  585. }
  586. }
  587. }
  588. else
  589. {
  590. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  591. }
  592. }
  593. #endif // ENABLE_BER
  594. else
  595. {
  596. return ASN1DecSetError(dec, ASN1_ERR_RULE);
  597. }
  598. /* call abort/done function for non-parented decoding stream */
  599. if (ASN1_SUCCEEDED(d->parent->info.err))
  600. {
  601. // not parented
  602. if (d == d->parent)
  603. {
  604. #if defined(TEST_CODER) && defined(_DEBUG)
  605. if (ASN1_PER_RULE & dec->eRule)
  606. {
  607. if (! TestDec_Compare(d, id, *valref, dec->buf, dec->len))
  608. {
  609. MyDebugBreak();
  610. }
  611. }
  612. #ifdef ENABLE_BER
  613. else
  614. if (ASN1_BER_RULE & dec->eRule)
  615. {
  616. if (! TestDec_Compare(d, id, *valref, dec->buf, dec->len))
  617. {
  618. MyDebugBreak();
  619. }
  620. }
  621. #endif // ENABLE_BER
  622. #endif
  623. ASN1DecDone(dec);
  624. }
  625. }
  626. else
  627. {
  628. ASN1INTERNdecoding_t child, child2;
  629. // not parented
  630. if (d == d->parent)
  631. {
  632. ASN1DecAbort(dec);
  633. }
  634. // clean up...
  635. ASN1_FreeDecoded(dec ,*valref, id);
  636. *valref = NULL;
  637. for (child = d->child; child; child = child2)
  638. {
  639. child2 = child->child;
  640. // make sure it does not touch its parent which may already be freed
  641. child->parent = child;
  642. ASN1_CloseDecoder((ASN1decoding_t) child);
  643. }
  644. d->child = NULL;
  645. }
  646. /* return error code */
  647. return d->parent->info.err;
  648. }
  649. else
  650. {
  651. return ASN1_ERR_MEMORY;
  652. }
  653. }
  654. else
  655. {
  656. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  657. }
  658. }
  659. return ASN1_ERR_BADARGS;
  660. }
  661. /* control function for decoding */
  662. ASN1error_e ASN1_SetDecoderOption
  663. (
  664. ASN1decoding_t dec,
  665. ASN1optionparam_t *pOptParam
  666. )
  667. {
  668. if (NULL != dec)
  669. {
  670. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  671. ASN1error_e rc = ASN1_SUCCESS;
  672. /* check magic number */
  673. DecAssert(dec, MAGIC_DECODER == dec->magic);
  674. switch (pOptParam->eOption)
  675. {
  676. case ASN1OPT_CHANGE_RULE:
  677. dec->eRule = pOptParam->eRule;
  678. break;
  679. case ASN1OPT_SET_DECODED_BUFFER:
  680. if (NULL != pOptParam->Buffer.pbBuf && 0 != pOptParam->Buffer.cbBufSize)
  681. {
  682. d->fExtBuf = TRUE;
  683. d->lpOrigExtBuf = pOptParam->Buffer.pbBuf;
  684. d->cbOrigExtBufSize = pOptParam->Buffer.cbBufSize;
  685. d->lpRemExtBuf = d->lpOrigExtBuf;
  686. d->cbRemExtBufSize = d->cbOrigExtBufSize;
  687. }
  688. else
  689. {
  690. rc = ASN1_ERR_BADARGS;
  691. }
  692. break;
  693. case ASN1OPT_DEL_DECODED_BUFFER:
  694. d->fExtBuf = FALSE;
  695. d->lpOrigExtBuf = NULL;
  696. d->cbOrigExtBufSize = 0;
  697. d->lpRemExtBuf = NULL;
  698. d->cbRemExtBufSize = 0;
  699. break;
  700. default:
  701. rc = ASN1_ERR_BADARGS;
  702. break;
  703. }
  704. return ASN1DecSetError(dec, rc);
  705. }
  706. return ASN1_ERR_BADARGS;
  707. }
  708. /* control function for decoding */
  709. ASN1error_e ASN1_GetDecoderOption
  710. (
  711. ASN1decoding_t dec,
  712. ASN1optionparam_t *pOptParam
  713. )
  714. {
  715. if (NULL != dec)
  716. {
  717. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  718. ASN1error_e rc = ASN1_SUCCESS;
  719. /* check magic number */
  720. DecAssert(dec, MAGIC_DECODER == dec->magic);
  721. switch (pOptParam->eOption)
  722. {
  723. case ASN1OPT_GET_RULE:
  724. pOptParam->eRule = dec->eRule;
  725. break;
  726. case ASN1OPT_GET_DECODED_BUFFER_SIZE:
  727. pOptParam->cbRequiredDecodedBufSize = d->cbLinearBufSize;
  728. break;
  729. default:
  730. rc = ASN1_ERR_BADARGS;
  731. break;
  732. }
  733. return ASN1DecSetError(dec, rc);
  734. }
  735. return ASN1_ERR_BADARGS;
  736. }
  737. /* destroy decoding stream */
  738. void ASN1_CloseDecoder
  739. (
  740. ASN1decoding_t dec
  741. )
  742. {
  743. if (NULL != dec)
  744. {
  745. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  746. /* check magic number */
  747. DecAssert(dec, MAGIC_DECODER == dec->magic);
  748. if (d != d->parent)
  749. {
  750. DecAssert(dec, d == d->parent->child);
  751. d->parent->child = NULL;
  752. }
  753. if ((NULL != d->info.buf)
  754. && (d->info.dwFlags & ASN1DECODE_AUTOFREEBUFFER))
  755. MemFree(d->info.buf);
  756. /* free decoding stream */
  757. MemFree(d);
  758. }
  759. }
  760. /* free an encoded value */
  761. void ASN1_FreeEncoded
  762. (
  763. ASN1encoding_t enc,
  764. void *val
  765. )
  766. {
  767. if (NULL != enc)
  768. {
  769. /* check magic number */
  770. EncAssert(enc, MAGIC_ENCODER == enc->magic);
  771. EncMemFree(enc, val);
  772. }
  773. }
  774. /* free a unencoded value */
  775. void ASN1_FreeDecoded
  776. (
  777. ASN1decoding_t dec,
  778. void *val,
  779. ASN1uint32_t id
  780. )
  781. {
  782. if (NULL != dec)
  783. {
  784. ASN1INTERNdecoding_t d = (ASN1INTERNdecoding_t)dec;
  785. /* check magic number */
  786. DecAssert(dec, MAGIC_DECODER == dec->magic);
  787. // same behavior of LocalFree
  788. if (val != NULL)
  789. {
  790. if (id != ASN1DECFREE_NON_PDU_ID)
  791. {
  792. ASN1FreeFun_t pfnFreeMemory;
  793. /* free value */
  794. if (id < dec->module->cPDUs)
  795. {
  796. if (NULL != (pfnFreeMemory = dec->module->apfnFreeMemory[id]))
  797. {
  798. (*pfnFreeMemory)(val);
  799. }
  800. }
  801. else
  802. {
  803. return;
  804. }
  805. }
  806. // free the top-level structure
  807. DecMemFree(dec, val);
  808. }
  809. }
  810. }
  811. ASN1module_t ASN1_CreateModule
  812. (
  813. ASN1uint32_t version,
  814. ASN1encodingrule_e eEncodingRule,
  815. ASN1uint32_t dwFlags,
  816. ASN1uint32_t cPDUs,
  817. const ASN1GenericFun_t apfnEncoder[],
  818. const ASN1GenericFun_t apfnDecoder[],
  819. const ASN1FreeFun_t apfnFreeMemory[],
  820. const ASN1uint32_t acbStructSize[],
  821. ASN1magic_t nModuleName
  822. )
  823. {
  824. ASN1module_t module = NULL;
  825. /* compiler output and library version match together? */
  826. if (
  827. // version <= ASN1_THIS_VERSION &&
  828. NULL != apfnEncoder &&
  829. NULL != apfnDecoder &&
  830. NULL != apfnFreeMemory &&
  831. NULL != acbStructSize)
  832. {
  833. if (NULL != (module = (ASN1module_t)MemAlloc(sizeof(*module), nModuleName)))
  834. {
  835. module->nModuleName = nModuleName;
  836. module->eRule = eEncodingRule;
  837. module->dwFlags = dwFlags;
  838. module->cPDUs = cPDUs;
  839. module->apfnFreeMemory = apfnFreeMemory;
  840. module->acbStructSize = acbStructSize;
  841. if (ASN1_PER_RULE & eEncodingRule)
  842. {
  843. module->PER.apfnEncoder = (const ASN1PerEncFun_t *) apfnEncoder;
  844. module->PER.apfnDecoder = (const ASN1PerDecFun_t *) apfnDecoder;
  845. }
  846. #ifdef ENABLE_BER
  847. else
  848. if (ASN1_BER_RULE & eEncodingRule)
  849. {
  850. module->BER.apfnEncoder = (const ASN1BerEncFun_t *) apfnEncoder;
  851. module->BER.apfnDecoder = (const ASN1BerDecFun_t *) apfnDecoder;
  852. }
  853. #endif // ENABLE_BER
  854. }
  855. }
  856. return module;
  857. }
  858. void ASN1_CloseModule(ASN1module_t pModule)
  859. {
  860. MemFree(pModule);
  861. }
  862. #ifdef TEST_CODER
  863. static int MyMemCmp(ASN1octet_t *p1, ASN1octet_t *p2, ASN1uint32_t c)
  864. {
  865. BYTE diff;
  866. while (c--)
  867. {
  868. if ((diff = *p1++ - *p2++) != 0)
  869. return (int) diff;
  870. }
  871. return 0;
  872. }
  873. __inline ASN1INTERNencoding_t TestEnc_GetEnc(ASN1INTERNencoding_t e)
  874. { return &(((ASN1testcoder_t) (e+1))->e); }
  875. __inline ASN1INTERNdecoding_t TestEnc_GetDec(ASN1INTERNencoding_t e)
  876. { return &(((ASN1testcoder_t) (e+1))->d); }
  877. __inline ASN1INTERNencoding_t TestDec_GetEnc(ASN1INTERNdecoding_t d)
  878. { return &(((ASN1testcoder_t) (d+1))->e); }
  879. __inline ASN1INTERNdecoding_t TestDec_GetDec(ASN1INTERNdecoding_t d)
  880. { return &(((ASN1testcoder_t) (d+1))->d); }
  881. static void Test_InitEnc(ASN1INTERNencoding_t e, ASN1module_t mod, ASN1encodingrule_e eRule)
  882. {
  883. ZeroMemory(e, sizeof(*e));
  884. e->info.magic = MAGIC_ENCODER;
  885. e->info.err = ASN1_SUCCESS;
  886. e->info.module = mod;
  887. e->info.eRule = eRule;
  888. e->parent = e;
  889. e->child = NULL;
  890. }
  891. static void Test_InitDec(ASN1INTERNdecoding_t d, ASN1module_t mod, ASN1encodingrule_e eRule)
  892. {
  893. ZeroMemory(d, sizeof(*d));
  894. d->info.magic = MAGIC_DECODER;
  895. d->info.err = ASN1_SUCCESS;
  896. d->info.module = mod;
  897. d->info.eRule = eRule;
  898. d->parent = d;
  899. d->child = NULL;
  900. }
  901. static int TestEnc_InitCoder(ASN1INTERNencoding_t e, ASN1module_t mod)
  902. {
  903. ASN1INTERNencoding_t ee = TestEnc_GetEnc(e);
  904. ASN1INTERNdecoding_t ed = TestEnc_GetDec(e);
  905. Test_InitEnc(ee, mod, e->info.eRule);
  906. Test_InitDec(ed, mod, e->info.eRule);
  907. return 1;
  908. }
  909. static int TestDec_InitCoder(ASN1INTERNdecoding_t d, ASN1module_t mod)
  910. {
  911. ASN1INTERNencoding_t de = TestDec_GetEnc(d);
  912. ASN1INTERNdecoding_t dd = TestDec_GetDec(d);
  913. Test_InitEnc(de, mod, d->info.eRule);
  914. Test_InitDec(dd, mod, d->info.eRule);
  915. return 1;
  916. }
  917. static int Test_Encode(ASN1INTERNencoding_t e, void *value, ASN1uint32_t id)
  918. {
  919. ASN1encoding_t enc = (ASN1encoding_t) e;
  920. /* clear error */
  921. ASN1EncSetError(enc, ASN1_SUCCESS);
  922. // clean buffer
  923. enc->pos = enc->buf;
  924. enc->bit = enc->len = 0;
  925. if (ASN1_PER_RULE & enc->eRule)
  926. {
  927. /* encode value */
  928. ASN1PerEncFun_t pfnPER;
  929. if (NULL != (pfnPER = enc->module->PER.apfnEncoder[id]))
  930. {
  931. if ((*pfnPER)(enc, value))
  932. {
  933. ASN1PEREncFlush(enc);
  934. }
  935. }
  936. else
  937. {
  938. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  939. }
  940. }
  941. #ifdef ENABLE_BER
  942. else
  943. if (ASN1_BER_RULE & enc->eRule)
  944. {
  945. /* encode value */
  946. ASN1BerEncFun_t pfnBER;
  947. if (NULL != (pfnBER = enc->module->BER.apfnEncoder[id]))
  948. {
  949. if ((*pfnBER)(enc, 0, value)) // lonchanc: tag is 0 to make it compiled
  950. {
  951. ASN1BEREncFlush(enc);
  952. }
  953. }
  954. else
  955. {
  956. return ASN1EncSetError(enc, ASN1_ERR_BADPDU);
  957. }
  958. }
  959. #endif // ENABLE_BER
  960. else
  961. {
  962. return ASN1EncSetError(enc, ASN1_ERR_RULE);
  963. }
  964. /* call abort/done function for non-parented encoding stream */
  965. if (e->parent->info.err >= 0)
  966. {
  967. if (e == e->parent)
  968. {
  969. ASN1EncDone(enc);
  970. }
  971. }
  972. else
  973. {
  974. ASN1INTERNencoding_t child, child2;
  975. if (e == e->parent)
  976. {
  977. ASN1EncAbort(enc);
  978. }
  979. // clean up...
  980. ASN1_FreeEncoded(enc, enc->buf);
  981. enc->pos = enc->buf = NULL;
  982. enc->size = enc->len = enc->bit = 0;
  983. for (child = e->child; child; child = child2)
  984. {
  985. child2 = child->child;
  986. // make sure it does not touch its parent which may already be freed
  987. child->parent = child;
  988. ASN1_CloseEncoder((ASN1encoding_t) child);
  989. }
  990. e->child = NULL;
  991. }
  992. /* return error code */
  993. return e->parent->info.err;
  994. }
  995. static int Test_Decode(ASN1INTERNdecoding_t d, void ** valref, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  996. {
  997. ASN1decoding_t dec = (ASN1decoding_t) d;
  998. ASN1uint32_t cbTopLevelStruct;
  999. /* clear error */
  1000. ASN1DecSetError(dec, ASN1_SUCCESS);
  1001. // set up buffer containing encoded data
  1002. dec->pos = dec->buf = pbBuf;
  1003. dec->size = cbBufSize;
  1004. dec->bit = dec->len = 0;
  1005. /* clear length of linear buffer required */
  1006. d->cbLinearBufSize = 0;
  1007. d->fExtBuf = FALSE;
  1008. cbTopLevelStruct = dec->module->acbStructSize[id];
  1009. if (NULL != (*valref = DecMemAlloc(dec, cbTopLevelStruct)))
  1010. {
  1011. if (ASN1_PER_RULE & dec->eRule)
  1012. {
  1013. ASN1PerDecFun_t pfnPER;
  1014. /* decode value */
  1015. if (NULL != (pfnPER = dec->module->PER.apfnDecoder[id]))
  1016. {
  1017. if ((*pfnPER)(dec, *valref))
  1018. {
  1019. ASN1PERDecFlush(dec);
  1020. }
  1021. }
  1022. else
  1023. {
  1024. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  1025. }
  1026. }
  1027. #ifdef ENABLE_BER
  1028. else
  1029. if (ASN1_BER_RULE & dec->eRule)
  1030. {
  1031. ASN1BerDecFun_t pfnBER;
  1032. /* decode value */
  1033. if (NULL != (pfnBER = dec->module->BER.apfnDecoder[id]))
  1034. {
  1035. if ((*pfnBER)(dec, 0, *valref))
  1036. {
  1037. ASN1BERDecFlush(dec);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. return ASN1DecSetError(dec, ASN1_ERR_BADPDU);
  1043. }
  1044. }
  1045. #endif // ENABLE_BER
  1046. else
  1047. {
  1048. return ASN1DecSetError(dec, ASN1_ERR_RULE);
  1049. }
  1050. /* call abort/done function for non-parented decoding stream */
  1051. if (d->parent->info.err >= 0)
  1052. {
  1053. // not parented
  1054. if (d == d->parent)
  1055. {
  1056. ASN1DecDone(dec);
  1057. }
  1058. }
  1059. else
  1060. {
  1061. ASN1INTERNdecoding_t child, child2;
  1062. // not parented
  1063. if (d == d->parent)
  1064. {
  1065. ASN1DecAbort(dec);
  1066. }
  1067. // clean up...
  1068. ASN1_FreeDecoded(dec ,*valref, id);
  1069. *valref = NULL;
  1070. for (child = d->child; child; child = child2)
  1071. {
  1072. child2 = child->child;
  1073. // make sure it does not touch its parent which may already be freed
  1074. child->parent = child;
  1075. ASN1_CloseDecoder((ASN1decoding_t) child);
  1076. }
  1077. d->child = NULL;
  1078. }
  1079. }
  1080. else
  1081. {
  1082. return ASN1_ERR_MEMORY;
  1083. }
  1084. /* return error code */
  1085. return d->parent->info.err;
  1086. }
  1087. static void Test_CleanEnc(ASN1INTERNencoding_t e)
  1088. {
  1089. if (e->info.buf)
  1090. {
  1091. EncMemFree((ASN1encoding_t) e, e->info.buf);
  1092. }
  1093. Test_InitEnc(e, e->info.module, e->info.eRule);
  1094. }
  1095. static void Test_CleanDec(ASN1INTERNdecoding_t d)
  1096. {
  1097. Test_InitDec(d, d->info.module, d->info.eRule);
  1098. }
  1099. static int TestEnc_Compare(ASN1INTERNencoding_t e, ASN1uint32_t id, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  1100. {
  1101. ASN1INTERNencoding_t ee = TestEnc_GetEnc(e);
  1102. ASN1INTERNdecoding_t ed = TestEnc_GetDec(e);
  1103. int ret;
  1104. void *val = NULL;
  1105. int fRet = 0;
  1106. ee->info.eRule = e->info.eRule;
  1107. ed->info.eRule = e->info.eRule;
  1108. ret = Test_Decode(ed, &val, id, pbBuf, cbBufSize);
  1109. if (ret == ASN1_SUCCESS)
  1110. {
  1111. ret = Test_Encode(ee, val, id);
  1112. if (ret == ASN1_SUCCESS)
  1113. {
  1114. if (ee->info.len == cbBufSize)
  1115. {
  1116. fRet = (MyMemCmp(pbBuf, ee->info.buf, cbBufSize) == 0);
  1117. }
  1118. }
  1119. }
  1120. if (val)
  1121. {
  1122. ASN1_FreeDecoded((ASN1decoding_t) ed, val, id);
  1123. }
  1124. Test_CleanEnc(ee);
  1125. Test_CleanDec(ed);
  1126. return fRet;
  1127. }
  1128. static int TestDec_Compare(ASN1INTERNdecoding_t d, ASN1uint32_t id, void *val, ASN1octet_t *pbBuf, ASN1uint32_t cbBufSize)
  1129. {
  1130. ASN1INTERNencoding_t de = TestDec_GetEnc(d);
  1131. int ret;
  1132. int fRet = 0;
  1133. de->info.eRule = d->info.eRule;
  1134. ret = Test_Encode(de, val, id);
  1135. if (ret == ASN1_SUCCESS)
  1136. {
  1137. if (de->info.len == cbBufSize)
  1138. {
  1139. fRet = (MyMemCmp(pbBuf, de->info.buf, cbBufSize) == 0);
  1140. }
  1141. }
  1142. Test_CleanEnc(de);
  1143. return fRet;
  1144. }
  1145. #endif