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

713 lines
22 KiB

  1. /************************************************************************
  2. * *
  3. * INTEL CORPORATION PROPRIETARY INFORMATION *
  4. * *
  5. * This software is supplied under the terms of a license *
  6. * agreement or non-disclosure agreement with Intel Corporation *
  7. * and may not be copied or disclosed except in accordance *
  8. * with the terms of that agreement. *
  9. * *
  10. * Copyright (C) 1997 Intel Corp. All Rights Reserved *
  11. * *
  12. * *
  13. * $Archive: S:\sturgeon\src\gkpdu\vcs\coder.cpv $
  14. * *
  15. * $Revision: 1.5 $
  16. * $Date: 24 Jan 1997 19:38:44 $
  17. * *
  18. * $Author: BPOLING $
  19. *
  20. * $Log: S:\sturgeon\src\gkpdu\vcs\coder.cpv $
  21. //
  22. // Rev 1.5 24 Jan 1997 19:38:44 BPOLING
  23. // remove string include files, not needed.
  24. //
  25. // Rev 1.2 18 Dec 1996 21:49:12 BPOLING
  26. // builds with msdev and for windows only
  27. //
  28. // Rev 1.1 15 Nov 1996 16:17:44 BPOLING
  29. // added vcs headers.
  30. *
  31. *************************************************************************
  32. * *
  33. * CODER.CPP *
  34. * *
  35. * PURPOSE: *
  36. * *
  37. * FUNCTIONS: *
  38. * *
  39. * COMMENTS: *
  40. * *
  41. * *
  42. *************************************************************************
  43. * *
  44. * $History: CODER.CPP *
  45. * *
  46. * *
  47. ************************************************************************/
  48. /************************************************************************
  49. * Include Files *
  50. ***********************************************************************/
  51. #include "precomp.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif /*__cplusplus*/
  55. #include "h225asn.h"
  56. #include "coder.hpp"
  57. #ifdef __cplusplus
  58. }
  59. #endif /*__cplusplus*/
  60. /************************************************************************
  61. * Local Manifest Constants *
  62. ***********************************************************************/
  63. /************************************************************************
  64. * GLOBAL VARIABLES *
  65. ************************************************************************/
  66. /************************************************************************
  67. * Local Private Data *
  68. ***********************************************************************/
  69. /************************************************************************
  70. * Start of Code *
  71. ***********************************************************************/
  72. Coder::Coder(){
  73. }
  74. Coder::~Coder(){
  75. EnterCriticalSection(&m_critSec);
  76. GK_TermWorld(&m_World);
  77. LeaveCriticalSection(&m_critSec);
  78. DeleteCriticalSection(&m_critSec);
  79. }
  80. // initializes the oss library.
  81. // the oss library provide ASN.1 encoding / decoding libraries.
  82. //
  83. int Coder::InitCoder()
  84. {
  85. int iError = 0;
  86. m_protocolIdentifier1.next = &m_protocolIdentifier2;
  87. m_protocolIdentifier1.value = ITU_T;
  88. m_protocolIdentifier2.next = &m_protocolIdentifier3;
  89. m_protocolIdentifier2.value = RECOMMENDATION;
  90. m_protocolIdentifier3.next = &m_protocolIdentifier4;
  91. m_protocolIdentifier3.value = SERIES;
  92. m_protocolIdentifier4.next = &m_protocolIdentifier5;
  93. m_protocolIdentifier4.value = RECOMM_NUMBER;
  94. m_protocolIdentifier5.next = &m_protocolIdentifier6;
  95. m_protocolIdentifier5.value = VERSION;
  96. m_protocolIdentifier6.next = NULL;
  97. m_protocolIdentifier6.value = ADDITIONAL;
  98. InitializeCriticalSection(&m_critSec);
  99. // Call TELES Library initialization routine
  100. EnterCriticalSection(&m_critSec);
  101. iError = GK_InitWorld(&m_World);
  102. LeaveCriticalSection(&m_critSec);
  103. return iError;
  104. }
  105. // ASN.1 Encode a H.323 PDU
  106. // Take a H.323 structure and returns a H.323 PDU
  107. //
  108. int Coder:: Encode(RasMessage *pInputData, ASN1_BUF *pOutputOssBuf){
  109. int iError;
  110. // initialize encoding buffer structure values
  111. pOutputOssBuf->value = NULL;
  112. pOutputOssBuf->length = 0;
  113. // encode pdu
  114. EnterCriticalSection(&m_critSec);
  115. iError = GK_Encode(&m_World,
  116. (void *)pInputData,
  117. RasMessage_PDU,
  118. pOutputOssBuf);
  119. LeaveCriticalSection(&m_critSec);
  120. return iError;
  121. }
  122. int Coder::Decode(ASN1_BUF *pInputOssBuf, RasMessage **pOutputData){
  123. int iError;
  124. // NULL tells decoder to malloc memory for RasMessage
  125. // user must free this memory by calling Coder::FreePDU()
  126. *pOutputData = NULL;
  127. // decode the pdu
  128. EnterCriticalSection(&m_critSec);
  129. iError = GK_Decode(&m_World,
  130. (void **)pOutputData,
  131. RasMessage_PDU,
  132. pInputOssBuf);
  133. LeaveCriticalSection(&m_critSec);
  134. return iError;
  135. }
  136. // Used to free buffer created by decode
  137. int Coder::Free(RasMessage *pData){
  138. int iError;
  139. EnterCriticalSection(&m_critSec);
  140. // iError = freePDU(&m_World,RasMessage_PDU,pData, GKPDU_Module);
  141. iError = freePDU(&m_World,RasMessage_PDU,pData, H225ASN_Module);
  142. LeaveCriticalSection(&m_critSec);
  143. return iError;
  144. }
  145. // Used to free buffer created by encode
  146. void Coder::Free(ASN1_BUF Asn1Buf){
  147. EnterCriticalSection(&m_critSec);
  148. //GKPDU_Module->encfree(m_World.pEncInfo,(void *)(Asn1Buf.value));
  149. ASN1_FreeEncoded(m_World.pEncInfo,(void *)(Asn1Buf.value));
  150. LeaveCriticalSection(&m_critSec);
  151. }
  152. RequestSeqNum Coder::GetSequenceNumber(RasMessage *prasStruct){
  153. RequestSeqNum rNum = 0;
  154. switch(prasStruct->choice){
  155. case gatekeeperRequest_chosen:
  156. return prasStruct->u.gatekeeperRequest.requestSeqNum;
  157. case gatekeeperConfirm_chosen:
  158. return prasStruct->u.gatekeeperConfirm.requestSeqNum;
  159. case gatekeeperReject_chosen:
  160. return prasStruct->u.gatekeeperReject.requestSeqNum;
  161. case registrationRequest_chosen:
  162. return prasStruct->u.registrationRequest.requestSeqNum;
  163. case registrationConfirm_chosen:
  164. return prasStruct->u.registrationConfirm.requestSeqNum;
  165. case registrationReject_chosen:
  166. return prasStruct->u.registrationReject.requestSeqNum;
  167. case unregistrationRequest_chosen:
  168. return prasStruct->u.unregistrationRequest.requestSeqNum;
  169. case unregistrationConfirm_chosen:
  170. return prasStruct->u.unregistrationConfirm.requestSeqNum;
  171. case unregistrationReject_chosen:
  172. return prasStruct->u.unregistrationReject.requestSeqNum;
  173. case admissionRequest_chosen:
  174. return prasStruct->u.admissionRequest.requestSeqNum;
  175. case admissionConfirm_chosen:
  176. return prasStruct->u.admissionConfirm.requestSeqNum;
  177. case admissionReject_chosen:
  178. return prasStruct->u.admissionReject.requestSeqNum;
  179. case bandwidthRequest_chosen:
  180. return prasStruct->u.bandwidthRequest.requestSeqNum;
  181. case bandwidthConfirm_chosen:
  182. return prasStruct->u.bandwidthConfirm.requestSeqNum;
  183. case bandwidthReject_chosen:
  184. return prasStruct->u.bandwidthReject.requestSeqNum;
  185. case disengageRequest_chosen:
  186. return prasStruct->u.disengageRequest.requestSeqNum;
  187. case disengageConfirm_chosen:
  188. return prasStruct->u.disengageConfirm.requestSeqNum;
  189. case disengageReject_chosen:
  190. return prasStruct->u.disengageReject.requestSeqNum;
  191. case locationRequest_chosen:
  192. return prasStruct->u.locationRequest.requestSeqNum;
  193. case locationConfirm_chosen:
  194. return prasStruct->u.locationConfirm.requestSeqNum;
  195. case locationReject_chosen:
  196. return prasStruct->u.locationReject.requestSeqNum;
  197. case infoRequest_chosen:
  198. return prasStruct->u.infoRequest.requestSeqNum;
  199. case infoRequestResponse_chosen:
  200. return prasStruct->u.infoRequestResponse.requestSeqNum;
  201. case nonStandardMessage_chosen:
  202. return prasStruct->u.nonStandardMessage.requestSeqNum;
  203. case unknownMessageResponse_chosen:
  204. return prasStruct->u.unknownMessageResponse.requestSeqNum;
  205. default:
  206. return rNum;
  207. }
  208. return rNum;
  209. }
  210. RequestSeqNum Coder::SetSequenceNumber(RasMessage &rasStruct,RequestSeqNum reqNum){
  211. RequestSeqNum rNum = 0;
  212. switch(rasStruct.choice){
  213. case gatekeeperRequest_chosen:
  214. return (rasStruct.u.gatekeeperRequest.requestSeqNum = reqNum);
  215. case gatekeeperConfirm_chosen:
  216. return (rasStruct.u.gatekeeperConfirm.requestSeqNum = reqNum);
  217. case gatekeeperReject_chosen:
  218. return (rasStruct.u.gatekeeperReject.requestSeqNum = reqNum);
  219. case registrationRequest_chosen:
  220. return (rasStruct.u.registrationRequest.requestSeqNum = reqNum);
  221. case registrationConfirm_chosen:
  222. return (rasStruct.u.registrationConfirm.requestSeqNum = reqNum);
  223. case registrationReject_chosen:
  224. return (rasStruct.u.registrationReject.requestSeqNum = reqNum);
  225. case unregistrationRequest_chosen:
  226. return (rasStruct.u.unregistrationRequest.requestSeqNum = reqNum);
  227. case unregistrationConfirm_chosen:
  228. return (rasStruct.u.unregistrationConfirm.requestSeqNum = reqNum);
  229. case unregistrationReject_chosen:
  230. return (rasStruct.u.unregistrationReject.requestSeqNum = reqNum);
  231. case admissionRequest_chosen:
  232. return (rasStruct.u.admissionRequest.requestSeqNum = reqNum);
  233. case admissionConfirm_chosen:
  234. return (rasStruct.u.admissionConfirm.requestSeqNum = reqNum);
  235. case admissionReject_chosen:
  236. return (rasStruct.u.admissionReject.requestSeqNum = reqNum);
  237. case bandwidthRequest_chosen:
  238. return (rasStruct.u.bandwidthRequest.requestSeqNum = reqNum);
  239. case bandwidthConfirm_chosen:
  240. return (rasStruct.u.bandwidthConfirm.requestSeqNum = reqNum);
  241. case bandwidthReject_chosen:
  242. return (rasStruct.u.bandwidthReject.requestSeqNum = reqNum);
  243. case disengageRequest_chosen:
  244. return (rasStruct.u.disengageRequest.requestSeqNum = reqNum);
  245. case disengageConfirm_chosen:
  246. return (rasStruct.u.disengageConfirm.requestSeqNum = reqNum);
  247. case disengageReject_chosen:
  248. return (rasStruct.u.disengageReject.requestSeqNum = reqNum);
  249. case locationRequest_chosen:
  250. return (rasStruct.u.locationRequest.requestSeqNum = reqNum);
  251. case locationConfirm_chosen:
  252. return (rasStruct.u.locationConfirm.requestSeqNum = reqNum);
  253. case locationReject_chosen:
  254. return (rasStruct.u.locationReject.requestSeqNum = reqNum);
  255. case infoRequest_chosen:
  256. return (rasStruct.u.infoRequest.requestSeqNum = reqNum);
  257. case infoRequestResponse_chosen:
  258. return (rasStruct.u.infoRequestResponse.requestSeqNum = reqNum);
  259. case nonStandardMessage_chosen:
  260. return (rasStruct.u.nonStandardMessage.requestSeqNum = reqNum);
  261. case unknownMessageResponse_chosen:
  262. return (rasStruct.u.unknownMessageResponse.requestSeqNum = reqNum);
  263. default:
  264. return (rNum);
  265. }
  266. return rNum;
  267. }
  268. // Returns a pointer EndpointIdentifier for any RasMessage
  269. // NULL for RasMessage that have no EndpointIdentifier
  270. EndpointIdentifier *Coder::GetEndpointID(RasMessage *prasStruct)
  271. {
  272. switch(prasStruct->choice){
  273. // Message with endpointID
  274. case registrationConfirm_chosen:
  275. return &(prasStruct->u.registrationConfirm.endpointIdentifier);
  276. case unregistrationRequest_chosen:
  277. // Optional
  278. return &(prasStruct->u.unregistrationRequest.URt_endpntIdntfr);
  279. case admissionRequest_chosen:
  280. return &(prasStruct->u.admissionRequest.endpointIdentifier);
  281. case bandwidthRequest_chosen:
  282. return &(prasStruct->u.bandwidthRequest.endpointIdentifier);
  283. case disengageRequest_chosen:
  284. return &(prasStruct->u.disengageRequest.endpointIdentifier);
  285. case infoRequestResponse_chosen:
  286. return &(prasStruct->u.infoRequestResponse.endpointIdentifier);
  287. case locationRequest_chosen:
  288. // Optional
  289. return &(prasStruct->u.locationRequest.LctnRqst_endpntIdntfr);
  290. // Messages without an endpointID
  291. default:
  292. case gatekeeperRequest_chosen:
  293. case gatekeeperConfirm_chosen:
  294. case gatekeeperReject_chosen:
  295. case registrationRequest_chosen:
  296. case nonStandardMessage_chosen:
  297. case unknownMessageResponse_chosen:
  298. case registrationReject_chosen:
  299. case unregistrationConfirm_chosen:
  300. case unregistrationReject_chosen:
  301. case admissionConfirm_chosen:
  302. case admissionReject_chosen:
  303. case bandwidthReject_chosen:
  304. case bandwidthConfirm_chosen:
  305. case disengageConfirm_chosen:
  306. case disengageReject_chosen:
  307. case locationConfirm_chosen:
  308. case locationReject_chosen:
  309. case infoRequest_chosen:
  310. return NULL;
  311. }
  312. return NULL;
  313. }
  314. ProtocolIdentifier Coder::SetProtocolIdentifier(RasMessage &rasStruct){
  315. switch(rasStruct.choice){
  316. case gatekeeperRequest_chosen:
  317. return (rasStruct.u.gatekeeperRequest.protocolIdentifier = &m_protocolIdentifier1);
  318. case gatekeeperConfirm_chosen:
  319. return (rasStruct.u.gatekeeperConfirm.protocolIdentifier = &m_protocolIdentifier1);
  320. case gatekeeperReject_chosen:
  321. return (rasStruct.u.gatekeeperReject.protocolIdentifier = &m_protocolIdentifier1);
  322. case registrationRequest_chosen:
  323. return (rasStruct.u.registrationRequest.protocolIdentifier = &m_protocolIdentifier1);
  324. case registrationConfirm_chosen:
  325. return (rasStruct.u.registrationConfirm.protocolIdentifier = &m_protocolIdentifier1);
  326. case registrationReject_chosen:
  327. return (rasStruct.u.registrationReject.protocolIdentifier = &m_protocolIdentifier1);
  328. case unregistrationRequest_chosen:
  329. case unregistrationConfirm_chosen:
  330. case unregistrationReject_chosen:
  331. case admissionRequest_chosen:
  332. case admissionConfirm_chosen:
  333. case admissionReject_chosen:
  334. case bandwidthRequest_chosen:
  335. case bandwidthConfirm_chosen:
  336. case bandwidthReject_chosen:
  337. case disengageRequest_chosen:
  338. case disengageConfirm_chosen:
  339. case disengageReject_chosen:
  340. case locationRequest_chosen:
  341. case locationConfirm_chosen:
  342. case locationReject_chosen:
  343. case infoRequest_chosen:
  344. case infoRequestResponse_chosen:
  345. case nonStandardMessage_chosen:
  346. case unknownMessageResponse_chosen:
  347. default:
  348. return NULL;
  349. }
  350. return NULL;
  351. }
  352. // Returns TRUE if protocols match, FALSE - otherwise
  353. BOOL Coder::VerifyProtocolIdentifier(RasMessage &rasStruct){
  354. struct ObjectID_ *pprotID;
  355. struct ObjectID_ *pmprotID = &m_protocolIdentifier1;
  356. switch(rasStruct.choice){
  357. case gatekeeperRequest_chosen:
  358. {
  359. pprotID = rasStruct.u.gatekeeperRequest.protocolIdentifier;
  360. break;
  361. }
  362. case gatekeeperConfirm_chosen:
  363. {
  364. pprotID = rasStruct.u.gatekeeperConfirm.protocolIdentifier;
  365. break;
  366. }
  367. case gatekeeperReject_chosen:
  368. {
  369. pprotID = rasStruct.u.gatekeeperReject.protocolIdentifier;
  370. break;
  371. }
  372. case registrationRequest_chosen:
  373. {
  374. pprotID = rasStruct.u.registrationRequest.protocolIdentifier;
  375. break;
  376. }
  377. case registrationConfirm_chosen:
  378. {
  379. pprotID = rasStruct.u.registrationConfirm.protocolIdentifier;
  380. break;
  381. }
  382. case registrationReject_chosen:
  383. {
  384. pprotID = rasStruct.u.registrationReject.protocolIdentifier;
  385. break;
  386. }
  387. case unregistrationRequest_chosen:
  388. case unregistrationConfirm_chosen:
  389. case unregistrationReject_chosen:
  390. case admissionRequest_chosen:
  391. case admissionConfirm_chosen:
  392. case admissionReject_chosen:
  393. case bandwidthRequest_chosen:
  394. case bandwidthConfirm_chosen:
  395. case bandwidthReject_chosen:
  396. case disengageRequest_chosen:
  397. case disengageConfirm_chosen:
  398. case disengageReject_chosen:
  399. case locationRequest_chosen:
  400. case locationConfirm_chosen:
  401. case locationReject_chosen:
  402. case infoRequest_chosen:
  403. case infoRequestResponse_chosen:
  404. case nonStandardMessage_chosen:
  405. case unknownMessageResponse_chosen:
  406. default:
  407. return TRUE; // if no protocolIdentifier -> return TRUE by default;
  408. }
  409. while (!pprotID && !pmprotID)
  410. {
  411. if (pprotID->value != pmprotID->value)
  412. return FALSE;
  413. pprotID = pprotID->next;
  414. pmprotID = pmprotID->next;
  415. }
  416. if (!pprotID && !pmprotID)
  417. return TRUE;
  418. else return FALSE;
  419. }
  420. // finds the requested protocol rasAddress and copies it
  421. DWORD Coder::CopyRasAddress(TransportAddress *pDestAddress, PSEQTRANSADDS pSrcSeqRasAddress, unsigned short choice)
  422. {
  423. if(!pDestAddress) return ASN1_ERR_BADARGS;
  424. // choice 0 just grabs first address
  425. if(choice)
  426. {
  427. while(pSrcSeqRasAddress)
  428. {
  429. if(pSrcSeqRasAddress->value.choice == choice) break;
  430. pSrcSeqRasAddress = pSrcSeqRasAddress->next;
  431. }
  432. }
  433. if(pSrcSeqRasAddress)
  434. {
  435. CopyMemory(pDestAddress,&(pSrcSeqRasAddress->value),sizeof(TransportAddress));
  436. }
  437. else
  438. {
  439. return ASN1_ERR_BADARGS;
  440. }
  441. return ASN1_SUCCESS;
  442. }
  443. DWORD Coder::CopyRasAddress(TransportAddress *pDestAddress, RasMessage *prasStruct, unsigned short choice)
  444. {
  445. PSEQTRANSADDS pSeqTransAdd;
  446. if(!pDestAddress) return ASN1_ERR_BADARGS;
  447. if(prasStruct->choice != registrationRequest_chosen)
  448. return ASN1_ERR_BADARGS;
  449. pSeqTransAdd = (PSEQTRANSADDS) prasStruct->u.registrationRequest.rasAddress;
  450. while(pSeqTransAdd)
  451. {
  452. if(pSeqTransAdd->value.choice == choice) break;
  453. pSeqTransAdd = pSeqTransAdd->next;
  454. }
  455. if(pSeqTransAdd)
  456. {
  457. CopyMemory(pDestAddress,&(pSeqTransAdd->value),sizeof(TransportAddress));
  458. }
  459. else
  460. {
  461. return ASN1_ERR_BADARGS;
  462. }
  463. return ASN1_SUCCESS;
  464. }
  465. // finds the requested protocol callSignalAddress and copies it
  466. DWORD Coder::CopyCallSignal(TransportAddress *pDestCallSignalAddress, PSEQTRANSADDS pSrcSeqCSAAddress, unsigned short choice)
  467. {
  468. if(!pDestCallSignalAddress) return ASN1_ERR_BADARGS;
  469. while(pSrcSeqCSAAddress)
  470. {
  471. if(pSrcSeqCSAAddress->value.choice == choice) break;
  472. pSrcSeqCSAAddress = pSrcSeqCSAAddress->next;
  473. }
  474. if(pSrcSeqCSAAddress)
  475. {
  476. CopyMemory(pDestCallSignalAddress,&(pSrcSeqCSAAddress->value),sizeof(TransportAddress));
  477. }
  478. else
  479. {
  480. return ASN1_ERR_BADARGS;
  481. }
  482. return ASN1_SUCCESS;
  483. }
  484. DWORD Coder::CopyCallSignal(TransportAddress *pCallSignalAddress, RasMessage *prasStruct, unsigned short choice)
  485. {
  486. PSEQTRANSADDS pSeqTransAdd;
  487. if(!pCallSignalAddress) return ASN1_ERR_BADARGS;
  488. if(prasStruct->choice != registrationRequest_chosen)
  489. return ASN1_ERR_BADARGS;
  490. pSeqTransAdd = (PSEQTRANSADDS) prasStruct->u.registrationRequest.callSignalAddress;
  491. while(pSeqTransAdd)
  492. {
  493. if(pSeqTransAdd->value.choice == choice) break;
  494. pSeqTransAdd = pSeqTransAdd->next;
  495. }
  496. if(pSeqTransAdd)
  497. {
  498. CopyMemory(pCallSignalAddress,&(pSeqTransAdd->value),sizeof(TransportAddress));
  499. }
  500. else
  501. {
  502. return ASN1_ERR_BADARGS;
  503. }
  504. return ASN1_SUCCESS;
  505. }
  506. // THE FOLLOWING IS ADDED FOR TELES ASN.1 INTEGRATION
  507. extern "C" {
  508. int GK_InitWorld(ASN1_CODER_INFO *pWorld)
  509. {
  510. int rc;
  511. ZeroMemory(pWorld, sizeof(*pWorld));
  512. if (H225ASN_Module == NULL)
  513. {
  514. return ASN1_ERR_BADARGS;
  515. }
  516. rc = ASN1_CreateEncoder(
  517. H225ASN_Module, // ptr to mdule
  518. &(pWorld->pEncInfo), // ptr to encoder info
  519. NULL, // buffer ptr
  520. 0, // buffer size
  521. NULL); // parent ptr
  522. if (rc == ASN1_SUCCESS)
  523. {
  524. ASSERT(pWorld->pEncInfo != NULL);
  525. rc = ASN1_CreateDecoder(
  526. H225ASN_Module, // ptr to mdule
  527. &(pWorld->pDecInfo), // ptr to decoder info
  528. NULL, // buffer ptr
  529. 0, // buffer size
  530. NULL); // parent ptr
  531. ASSERT(pWorld->pDecInfo != NULL);
  532. }
  533. if (rc != ASN1_SUCCESS)
  534. {
  535. GK_TermWorld(pWorld);
  536. }
  537. return rc;
  538. }
  539. int GK_TermWorld(ASN1_CODER_INFO *pWorld)
  540. {
  541. if (H225ASN_Module == NULL)
  542. {
  543. return ASN1_ERR_BADARGS;
  544. }
  545. ASN1_CloseEncoder(pWorld->pEncInfo);
  546. ASN1_CloseDecoder(pWorld->pDecInfo);
  547. ZeroMemory(pWorld, sizeof(*pWorld));
  548. return ASN1_SUCCESS;
  549. }
  550. int GK_Encode(ASN1_CODER_INFO *pWorld, void *pStruct, int nPDU, ASN1_BUF *pBuf)
  551. {
  552. int rc;
  553. ASN1encoding_t pEncInfo = pWorld->pEncInfo;
  554. BOOL fBufferSupplied = (pBuf->value != NULL) && (pBuf->length != 0);
  555. DWORD dwFlags = fBufferSupplied ? ASN1ENCODE_SETBUFFER : ASN1ENCODE_ALLOCATEBUFFER;
  556. // clean up out parameters
  557. if (! fBufferSupplied)
  558. {
  559. pBuf->length = 0;
  560. pBuf->value = NULL;
  561. }
  562. rc = ASN1_Encode(
  563. pEncInfo, // ptr to encoder info
  564. pStruct, // pdu data structure
  565. nPDU, // pdu id
  566. dwFlags, // flags
  567. pBuf->value, // buffer
  568. pBuf->length); // buffer size if provided
  569. if (ASN1_SUCCEEDED(rc))
  570. {
  571. if (fBufferSupplied)
  572. {
  573. ASSERT(pBuf->value == pEncInfo->buf);
  574. ASSERT(pBuf->length >= pEncInfo->len);
  575. }
  576. else
  577. {
  578. pBuf->value = pEncInfo->buf; // buffer to encode into
  579. }
  580. pBuf->length = pEncInfo->len; // len of encoded data in buffer
  581. }
  582. else
  583. {
  584. ASSERT(FALSE);
  585. }
  586. return rc;
  587. }
  588. int GK_Decode(ASN1_CODER_INFO *pWorld, void **ppStruct, int nPDU, ASN1_BUF *pBuf)
  589. {
  590. ASN1decoding_t pDecInfo = pWorld->pDecInfo;
  591. BYTE *pEncoded = pBuf->value;
  592. ULONG cbEncodedSize = pBuf->length;
  593. int rc = ASN1_Decode(
  594. pDecInfo, // ptr to encoder info
  595. ppStruct, // pdu data structure
  596. nPDU, // pdu id
  597. ASN1DECODE_SETBUFFER, // flags
  598. pEncoded, // do not provide buffer
  599. cbEncodedSize); // buffer size if provided
  600. if (ASN1_SUCCEEDED(rc))
  601. {
  602. ASSERT(pDecInfo->pos > pDecInfo->buf);
  603. pBuf->length -= (ULONG)(pDecInfo->pos - pDecInfo->buf);
  604. pBuf->value = pDecInfo->pos;
  605. }
  606. else
  607. {
  608. ASSERT(FALSE);
  609. *ppStruct = NULL;
  610. }
  611. return rc;
  612. }
  613. } // extern "C"
  614.