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.

211 lines
5.8 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1998
  5. //
  6. // File: pkialloc.cpp
  7. //
  8. // Contents: PKI Allocation Functions
  9. //
  10. // Functions: PkiAlloc
  11. //
  12. // History: 19-Jan-98 philh created
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include <dbgdef.h>
  16. //+-------------------------------------------------------------------------
  17. // The following functions use the 'C' runtime's allocation functions
  18. // when DBG is defined. Otherwise, use LocalAlloc, LocalReAlloc or
  19. // LocalFree Win32 APIs.
  20. //--------------------------------------------------------------------------
  21. // Calls malloc when DBG is defined. Otherwise, does a
  22. // ZEROINIT LocalAlloc.
  23. LPVOID
  24. WINAPI
  25. PkiAlloc(
  26. IN size_t cbBytes
  27. )
  28. {
  29. LPVOID pv;
  30. if (NULL == (pv = (LPVOID) LocalAlloc(LPTR, cbBytes)))
  31. SetLastError((DWORD) E_OUTOFMEMORY);
  32. return pv;
  33. }
  34. // Calls malloc and does a memory clear when DBG is defined.
  35. // Otherwise, does a ZEROINIT LocalAlloc.
  36. LPVOID
  37. WINAPI
  38. PkiZeroAlloc(
  39. IN size_t cbBytes
  40. )
  41. {
  42. LPVOID pv;
  43. // LPTR (OR includes ZEROINIT)
  44. pv = (LPVOID) LocalAlloc(LPTR, cbBytes);
  45. if (pv == NULL)
  46. SetLastError((DWORD) E_OUTOFMEMORY);
  47. return pv;
  48. }
  49. // Calls malloc when DBG is defined. Otherwise, does a
  50. // LocalAlloc without ZEOINIT.
  51. LPVOID
  52. WINAPI
  53. PkiNonzeroAlloc(
  54. IN size_t cbBytes
  55. )
  56. {
  57. LPVOID pv;
  58. pv = (LPVOID) LocalAlloc(NONZEROLPTR, cbBytes);
  59. if (pv == NULL)
  60. SetLastError((DWORD) E_OUTOFMEMORY);
  61. return pv;
  62. }
  63. LPVOID
  64. WINAPI
  65. PkiRealloc(
  66. IN LPVOID pvOrg,
  67. IN size_t cbBytes
  68. )
  69. {
  70. LPVOID pv;
  71. if (NULL == (pv = pvOrg ?
  72. (LPVOID) LocalReAlloc((HLOCAL)pvOrg, cbBytes, LMEM_MOVEABLE) :
  73. (LPVOID) LocalAlloc(NONZEROLPTR, cbBytes)))
  74. SetLastError((DWORD) E_OUTOFMEMORY);
  75. return pv;
  76. }
  77. VOID
  78. WINAPI
  79. PkiFree(
  80. IN LPVOID pv
  81. )
  82. {
  83. if (pv)
  84. LocalFree((HLOCAL)pv);
  85. }
  86. //+-------------------------------------------------------------------------
  87. // The following functions always use LocalAlloc and LocalFree Win32 APIs.
  88. //--------------------------------------------------------------------------
  89. LPVOID
  90. WINAPI
  91. PkiDefaultCryptAlloc(
  92. IN size_t cbSize
  93. )
  94. {
  95. LPVOID pv;
  96. if (NULL == (pv = (LPVOID) LocalAlloc(NONZEROLPTR, cbSize)))
  97. SetLastError((DWORD) E_OUTOFMEMORY);
  98. return pv;
  99. }
  100. VOID
  101. WINAPI
  102. PkiDefaultCryptFree(
  103. IN LPVOID pv
  104. )
  105. {
  106. if (pv)
  107. LocalFree((HLOCAL) pv);
  108. }
  109. //+-------------------------------------------------------------------------
  110. // The following data structure's pfnAlloc and pfnFree are set to
  111. // PkiNonzeroAlloc and PkiFree.
  112. //--------------------------------------------------------------------------
  113. CRYPT_ENCODE_PARA PkiEncodePara = {
  114. offsetof(CRYPT_ENCODE_PARA, pfnFree) + sizeof(PkiEncodePara.pfnFree),
  115. PkiNonzeroAlloc,
  116. PkiFree
  117. };
  118. //+-------------------------------------------------------------------------
  119. // If pfnAlloc isn't defined, returns PkiDefaultCryptAlloc
  120. //--------------------------------------------------------------------------
  121. PFN_CRYPT_ALLOC
  122. WINAPI
  123. PkiGetEncodeAllocFunction(
  124. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara
  125. )
  126. {
  127. if (pEncodePara &&
  128. pEncodePara->cbSize >= offsetof(CRYPT_ENCODE_PARA, pfnAlloc) +
  129. sizeof(pEncodePara->pfnAlloc) &&
  130. pEncodePara->pfnAlloc)
  131. return pEncodePara->pfnAlloc;
  132. else
  133. return PkiDefaultCryptAlloc;
  134. }
  135. //+-------------------------------------------------------------------------
  136. // If pfnFree isn't defined, returns PkiDefaultCryptFree
  137. //--------------------------------------------------------------------------
  138. PFN_CRYPT_FREE
  139. WINAPI
  140. PkiGetEncodeFreeFunction(
  141. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara
  142. )
  143. {
  144. if (pEncodePara &&
  145. pEncodePara->cbSize >= offsetof(CRYPT_ENCODE_PARA, pfnFree) +
  146. sizeof(pEncodePara->pfnFree) &&
  147. pEncodePara->pfnFree)
  148. return pEncodePara->pfnFree;
  149. else
  150. return PkiDefaultCryptFree;
  151. }
  152. //+-------------------------------------------------------------------------
  153. // The following data structure's pfnAlloc and pfnFree are set to
  154. // PkiNonzeroAlloc and PkiFree.
  155. //--------------------------------------------------------------------------
  156. CRYPT_DECODE_PARA PkiDecodePara = {
  157. offsetof(CRYPT_DECODE_PARA, pfnFree) + sizeof(PkiDecodePara.pfnFree),
  158. PkiNonzeroAlloc,
  159. PkiFree
  160. };
  161. //+-------------------------------------------------------------------------
  162. // If pfnAlloc isn't defined, returns PkiDefaultCryptAlloc
  163. //--------------------------------------------------------------------------
  164. PFN_CRYPT_ALLOC
  165. WINAPI
  166. PkiGetDecodeAllocFunction(
  167. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara
  168. )
  169. {
  170. if (pDecodePara &&
  171. pDecodePara->cbSize >= offsetof(CRYPT_DECODE_PARA, pfnAlloc) +
  172. sizeof(pDecodePara->pfnAlloc) &&
  173. pDecodePara->pfnAlloc)
  174. return pDecodePara->pfnAlloc;
  175. else
  176. return PkiDefaultCryptAlloc;
  177. }
  178. //+-------------------------------------------------------------------------
  179. // If pfnFree isn't defined, returns PkiDefaultCryptFree
  180. //--------------------------------------------------------------------------
  181. PFN_CRYPT_FREE
  182. WINAPI
  183. PkiGetDecodeFreeFunction(
  184. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara
  185. )
  186. {
  187. if (pDecodePara &&
  188. pDecodePara->cbSize >= offsetof(CRYPT_DECODE_PARA, pfnFree) +
  189. sizeof(pDecodePara->pfnFree) &&
  190. pDecodePara->pfnFree)
  191. return pDecodePara->pfnFree;
  192. else
  193. return PkiDefaultCryptFree;
  194. }