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.

248 lines
6.1 KiB

  1. //
  2. // CODER.CPP
  3. // ASN1 t126 encoder/decoder
  4. //
  5. // Copyright Microsoft 1998-
  6. //
  7. // PRECOMP
  8. #include "precomp.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif /*__cplusplus*/
  12. #include "coder.hpp"
  13. #ifdef __cplusplus
  14. }
  15. #endif /*__cplusplus*/
  16. Coder::Coder()
  17. {
  18. T126_InitModule();
  19. InitCoder();
  20. }
  21. Coder::~Coder(){
  22. EnterCriticalSection(&m_critSec);
  23. T126_TermCoder(&p_Coder);
  24. T126_TermModule();
  25. LeaveCriticalSection(&m_critSec);
  26. DeleteCriticalSection(&m_critSec);
  27. }
  28. int Coder::InitCoder()
  29. {
  30. int iError = 0;
  31. InitializeCriticalSection(&m_critSec);
  32. // Call TELES Library initialization routine
  33. EnterCriticalSection(&m_critSec);
  34. iError = T126_InitCoder(&p_Coder);
  35. LeaveCriticalSection(&m_critSec);
  36. return iError;
  37. }
  38. // ASN.1 Encode a T.126 PDU
  39. // Take a T.126 structure and returns a T.126 PDU
  40. //
  41. int Coder:: Encode(SIPDU *pInputData, ASN1_BUF *pOutputOssBuf){
  42. int iError;
  43. // initialize encoding buffer structure values
  44. pOutputOssBuf->value = NULL;
  45. pOutputOssBuf->length = 0;
  46. // encode pdu
  47. EnterCriticalSection(&m_critSec);
  48. iError = T126_Encode(&p_Coder,
  49. (void *)pInputData,
  50. SIPDU_PDU,
  51. pOutputOssBuf);
  52. LeaveCriticalSection(&m_critSec);
  53. return iError;
  54. }
  55. int Coder::Decode(ASN1_BUF *pInputOssBuf, SIPDU **pOutputData){
  56. int iError;
  57. // NULL tells decoder to malloc memory for SIPDU
  58. // user must free this memory by calling Coder::FreePDU()
  59. *pOutputData = NULL;
  60. // decode the pdu
  61. EnterCriticalSection(&m_critSec);
  62. iError = T126_Decode(&p_Coder,
  63. (void **)pOutputData,
  64. SIPDU_PDU,
  65. pInputOssBuf);
  66. LeaveCriticalSection(&m_critSec);
  67. return iError;
  68. }
  69. // Used to free buffer created by decode
  70. int Coder::Free(SIPDU *pData){
  71. int iError;
  72. EnterCriticalSection(&m_critSec);
  73. iError = freePDU(&p_Coder,SIPDU_PDU,pData, T126_Module);
  74. LeaveCriticalSection(&m_critSec);
  75. return iError;
  76. }
  77. // Used to free buffer created by encode
  78. void Coder::Free(ASN1_BUF Asn1Buf){
  79. EnterCriticalSection(&m_critSec);
  80. ASN1_FreeEncoded(p_Coder.pEncInfo,(void *)(Asn1Buf.value));
  81. LeaveCriticalSection(&m_critSec);
  82. }
  83. // THE FOLLOWING IS ADDED FOR TELES ASN.1 INTEGRATION
  84. extern "C" {
  85. int T126_InitModule(void)
  86. {
  87. T126_Module_Startup();
  88. return (T126_Module != NULL) ? ASN1_SUCCESS : ASN1_ERR_MEMORY;
  89. }
  90. int T126_TermModule(void)
  91. {
  92. T126_Module_Cleanup();
  93. return ASN1_SUCCESS;
  94. }
  95. int T126_InitCoder(ASN1_CODER_INFO *pCoder)
  96. {
  97. int rc;
  98. ZeroMemory(pCoder, sizeof(*pCoder));
  99. if (T126_Module == NULL)
  100. {
  101. return ASN1_ERR_BADARGS;
  102. }
  103. rc = ASN1_CreateEncoder(
  104. T126_Module, // ptr to mdule
  105. &(pCoder->pEncInfo), // ptr to encoder info
  106. NULL, // buffer ptr
  107. 0, // buffer size
  108. NULL); // parent ptr
  109. if (rc == ASN1_SUCCESS)
  110. {
  111. ASSERT(pCoder->pEncInfo != NULL);
  112. rc = ASN1_CreateDecoder(
  113. T126_Module, // ptr to mdule
  114. &(pCoder->pDecInfo), // ptr to decoder info
  115. NULL, // buffer ptr
  116. 0, // buffer size
  117. NULL); // parent ptr
  118. ASSERT(pCoder->pDecInfo != NULL);
  119. }
  120. if (rc != ASN1_SUCCESS)
  121. {
  122. T126_TermCoder(pCoder);
  123. }
  124. return rc;
  125. }
  126. int T126_TermCoder(ASN1_CODER_INFO *pCoder)
  127. {
  128. if (T126_Module == NULL)
  129. {
  130. return ASN1_ERR_BADARGS;
  131. }
  132. ASN1_CloseEncoder(pCoder->pEncInfo);
  133. ASN1_CloseDecoder(pCoder->pDecInfo);
  134. ZeroMemory(pCoder, sizeof(*pCoder));
  135. return ASN1_SUCCESS;
  136. }
  137. int T126_Encode(ASN1_CODER_INFO *pCoder, void *pStruct, int nPDU, ASN1_BUF *pBuf)
  138. {
  139. int rc;
  140. ASN1encoding_t pEncInfo = pCoder->pEncInfo;
  141. BOOL fBufferSupplied = (pBuf->value != NULL) && (pBuf->length != 0);
  142. DWORD dwFlags = fBufferSupplied ? ASN1ENCODE_SETBUFFER : ASN1ENCODE_ALLOCATEBUFFER;
  143. // clean up out parameters
  144. if (! fBufferSupplied)
  145. {
  146. pBuf->length = 0;
  147. pBuf->value = NULL;
  148. }
  149. rc = ASN1_Encode(
  150. pEncInfo, // ptr to encoder info
  151. pStruct, // pdu data structure
  152. nPDU, // pdu id
  153. dwFlags, // flags
  154. pBuf->value, // buffer
  155. pBuf->length); // buffer size if provided
  156. if (ASN1_SUCCEEDED(rc))
  157. {
  158. if (fBufferSupplied)
  159. {
  160. ASSERT(pBuf->value == pEncInfo->buf);
  161. ASSERT(pBuf->length >= pEncInfo->len);
  162. }
  163. else
  164. {
  165. pBuf->value = pEncInfo->buf; // buffer to encode into
  166. }
  167. pBuf->length = pEncInfo->len; // len of encoded data in buffer
  168. }
  169. else
  170. {
  171. ASSERT(FALSE);
  172. }
  173. return rc;
  174. }
  175. int T126_Decode(ASN1_CODER_INFO *pCoder, void **ppStruct, int nPDU, ASN1_BUF *pBuf)
  176. {
  177. ASN1decoding_t pDecInfo = pCoder->pDecInfo;
  178. BYTE *pEncoded = pBuf->value;
  179. ULONG cbEncodedSize = pBuf->length;
  180. int rc = ASN1_Decode(
  181. pDecInfo, // ptr to encoder info
  182. ppStruct, // pdu data structure
  183. nPDU, // pdu id
  184. ASN1DECODE_SETBUFFER, // flags
  185. pEncoded, // do not provide buffer
  186. cbEncodedSize); // buffer size if provided
  187. if (ASN1_SUCCEEDED(rc))
  188. {
  189. ASSERT(pDecInfo->pos > pDecInfo->buf);
  190. pBuf->length -= (ULONG)(pDecInfo->pos - pDecInfo->buf);
  191. pBuf->value = pDecInfo->pos;
  192. }
  193. else
  194. {
  195. ASSERT(FALSE);
  196. *ppStruct = NULL;
  197. }
  198. return rc;
  199. }
  200. } // extern "C"