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.

198 lines
5.6 KiB

  1. #include "precomp.h"
  2. DEBUG_FILEZONE(ZONE_T120_MSMCSTCP);
  3. /*
  4. * cnpcoder.cpp
  5. *
  6. * Copyright (c) 1999 by Microsoft Corporation
  7. *
  8. * Abstract:
  9. * This is the implementation file for the CCNPCoder class. This class
  10. * is responsible for encoding and decoding CNP (T.123 annex B) PDU's using ASN.1
  11. * encoding rules via the ASN.1 toolkit. This class is also capable
  12. * of determining the size of both the encoded and decoded PDU's.
  13. *
  14. * Static Variables:
  15. *
  16. * Caveats:
  17. * Only one instance of this class should be in existance at any one time
  18. * due to the static variable.
  19. *
  20. * Author:
  21. * Xin Liu
  22. */
  23. /*
  24. * External Interfaces
  25. */
  26. #include <string.h>
  27. #include "cnpcoder.h"
  28. /*
  29. * This is a global variable that has a pointer to the one CNP coder
  30. */
  31. CCNPCoder *g_CNPCoder = NULL;
  32. /*
  33. * CCNPCoder ()
  34. *
  35. * Public
  36. *
  37. * Functional Description:
  38. * This is the constructor for the CCNPCoder class. It initializes
  39. * the ASN.1 encoder/decoder and sets the encoding rules to the
  40. * Packed-Aligned variant.
  41. */
  42. CCNPCoder::CCNPCoder ()
  43. :m_pEncInfo(NULL),
  44. m_pDecInfo(NULL)
  45. {
  46. }
  47. BOOL CCNPCoder::Init ( void )
  48. {
  49. BOOL fRet = FALSE;
  50. CNPPDU_Module_Startup();
  51. if (CNPPDU_Module != NULL)
  52. {
  53. if (ASN1_CreateEncoder(
  54. CNPPDU_Module, // ptr to mdule
  55. &m_pEncInfo, // ptr to encoder info
  56. NULL, // buffer ptr
  57. 0, // buffer size
  58. NULL) // parent ptr
  59. == ASN1_SUCCESS)
  60. {
  61. ASSERT(m_pEncInfo != NULL);
  62. fRet = (ASN1_CreateDecoder(CNPPDU_Module, // ptr to mdule
  63. &m_pDecInfo, // ptr to decoder info
  64. NULL, // buffer ptr
  65. 0, // buffer size
  66. NULL) // parent ptr
  67. == ASN1_SUCCESS);
  68. ASSERT(fRet && m_pDecInfo != NULL);
  69. }
  70. }
  71. ASSERT(fRet);
  72. return fRet;
  73. }
  74. /*
  75. * ~CCNPCoder ()
  76. *
  77. * Public Functional Description:
  78. * This is a virtual destructor. It is used to clean up after ASN.1.
  79. */
  80. CCNPCoder::~CCNPCoder ()
  81. {
  82. if (CNPPDU_Module != NULL)
  83. {
  84. ASN1_CloseEncoder(m_pEncInfo);
  85. ASN1_CloseDecoder(m_pDecInfo);
  86. CNPPDU_Module_Cleanup();
  87. }
  88. }
  89. /*
  90. * Encode ()
  91. *
  92. * Public Functional Description:
  93. * This function encodes CNP Protocol Data Units (PDU's) into ASN.1
  94. * compliant byte streams using the ASN.1 toolkit.
  95. * The coder allocates the buffer space for the encoded data.
  96. */
  97. BOOL CCNPCoder::Encode(LPVOID pdu_structure,
  98. int pdu_type,
  99. UINT nEncodingRule_not_used,
  100. LPBYTE *encoding_buffer,
  101. UINT *encoding_buffer_length)
  102. {
  103. BOOL fRet = FALSE;
  104. int return_value;
  105. return_value = ASN1_Encode(m_pEncInfo, // ptr to encoder info
  106. pdu_structure, // pdu data structure
  107. pdu_type, // pdu id
  108. ASN1ENCODE_ALLOCATEBUFFER, // flags
  109. NULL, // do not provide buffer
  110. 0); // buffer size if provided
  111. if (ASN1_FAILED(return_value))
  112. {
  113. ERROR_OUT(("CCNPCoder::Encode: ASN1_Encode failed, err=%d .",
  114. return_value));
  115. ASSERT(FALSE);
  116. fRet = FALSE;
  117. goto MyExit;
  118. }
  119. ASSERT(return_value == ASN1_SUCCESS);
  120. fRet = TRUE;
  121. // len of encoded data in buffer
  122. *encoding_buffer_length = m_pEncInfo->len;
  123. // buffer to encode into
  124. *encoding_buffer = m_pEncInfo->buf;
  125. MyExit:
  126. return fRet;
  127. }
  128. /*
  129. * Decode ()
  130. *
  131. * Public Functional Description:
  132. * This function decodes ASN.1 compliant byte streams into the
  133. * appropriate CNP PDU structures using the ASN.1 toolkit.
  134. */
  135. BOOL CCNPCoder::Decode(LPBYTE encoded_buffer,
  136. UINT encoded_buffer_length,
  137. int pdu_type,
  138. UINT nEncodingRule_not_used,
  139. LPVOID *pdecoding_buffer,
  140. UINT *pdecoding_buffer_length)
  141. {
  142. BOOL fRet = FALSE;
  143. int return_value;
  144. ASN1optionparam_s OptParam;
  145. return_value = ASN1_Decode(m_pDecInfo, // ptr to decoder info
  146. pdecoding_buffer, // destination buffer
  147. pdu_type, // pdu type
  148. ASN1DECODE_SETBUFFER, // flags
  149. encoded_buffer, // source buffer
  150. encoded_buffer_length); // source buffer size
  151. if (ASN1_FAILED(return_value))
  152. {
  153. ERROR_OUT(("CNPCoder::Decode: ASN1_Decode failed, err=%d", return_value));
  154. ASSERT(FALSE);
  155. goto MyExit;
  156. }
  157. OptParam.eOption = ASN1OPT_GET_DECODED_BUFFER_SIZE;
  158. return_value = ASN1_GetDecoderOption(m_pDecInfo, &OptParam);
  159. if (ASN1_FAILED(return_value))
  160. {
  161. ERROR_OUT(("CCNPCoder::Decode: ASN1_GetDecoderOption failed, err=%d", return_value));
  162. ASSERT(FALSE);
  163. goto MyExit;
  164. }
  165. *pdecoding_buffer_length = OptParam.cbRequiredDecodedBufSize;
  166. ASSERT(return_value == ASN1_SUCCESS);
  167. ASSERT(*pdecoding_buffer_length > 0);
  168. fRet = TRUE;
  169. MyExit:
  170. return fRet;
  171. }
  172. void CCNPCoder::FreeEncoded (PUChar encoded_buffer)
  173. {
  174. ASN1_FreeEncoded(m_pEncInfo, encoded_buffer);
  175. }
  176. void CCNPCoder::FreeDecoded (int pdu_type, LPVOID decoded_buffer)
  177. {
  178. ASN1_FreeDecoded(m_pDecInfo, decoded_buffer, pdu_type);
  179. }