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.

234 lines
6.2 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  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 DBG
  31. if (NULL == (pv = malloc(cbBytes)))
  32. #else
  33. if (NULL == (pv = (LPVOID) LocalAlloc(LPTR, cbBytes)))
  34. #endif
  35. SetLastError((DWORD) E_OUTOFMEMORY);
  36. return pv;
  37. }
  38. // Calls malloc and does a memory clear when DBG is defined.
  39. // Otherwise, does a ZEROINIT LocalAlloc.
  40. LPVOID
  41. WINAPI
  42. PkiZeroAlloc(
  43. IN size_t cbBytes
  44. )
  45. {
  46. LPVOID pv;
  47. #if DBG
  48. pv = malloc(cbBytes);
  49. if (pv == NULL)
  50. SetLastError((DWORD) E_OUTOFMEMORY);
  51. else
  52. memset(pv, 0, cbBytes);
  53. #else
  54. // LPTR (OR includes ZEROINIT)
  55. pv = (LPVOID) LocalAlloc(LPTR, cbBytes);
  56. if (pv == NULL)
  57. SetLastError((DWORD) E_OUTOFMEMORY);
  58. #endif
  59. return pv;
  60. }
  61. // Calls malloc when DBG is defined. Otherwise, does a
  62. // LocalAlloc without ZEOINIT.
  63. LPVOID
  64. WINAPI
  65. PkiNonzeroAlloc(
  66. IN size_t cbBytes
  67. )
  68. {
  69. LPVOID pv;
  70. #if DBG
  71. pv = malloc(cbBytes);
  72. #else
  73. pv = (LPVOID) LocalAlloc(NONZEROLPTR, cbBytes);
  74. #endif
  75. if (pv == NULL)
  76. SetLastError((DWORD) E_OUTOFMEMORY);
  77. return pv;
  78. }
  79. LPVOID
  80. WINAPI
  81. PkiRealloc(
  82. IN LPVOID pvOrg,
  83. IN size_t cbBytes
  84. )
  85. {
  86. LPVOID pv;
  87. #if DBG
  88. if (NULL == (pv = pvOrg ? realloc(pvOrg, cbBytes) : malloc(cbBytes)))
  89. #else
  90. if (NULL == (pv = pvOrg ?
  91. (LPVOID) LocalReAlloc((HLOCAL)pvOrg, cbBytes, LMEM_MOVEABLE) :
  92. (LPVOID) LocalAlloc(NONZEROLPTR, cbBytes)))
  93. #endif
  94. SetLastError((DWORD) E_OUTOFMEMORY);
  95. return pv;
  96. }
  97. VOID
  98. WINAPI
  99. PkiFree(
  100. IN LPVOID pv
  101. )
  102. {
  103. if (pv)
  104. #if DBG
  105. free(pv);
  106. #else
  107. LocalFree((HLOCAL)pv);
  108. #endif
  109. }
  110. //+-------------------------------------------------------------------------
  111. // The following functions always use LocalAlloc and LocalFree Win32 APIs.
  112. //--------------------------------------------------------------------------
  113. LPVOID
  114. WINAPI
  115. PkiDefaultCryptAlloc(
  116. IN size_t cbSize
  117. )
  118. {
  119. LPVOID pv;
  120. if (NULL == (pv = (LPVOID) LocalAlloc(NONZEROLPTR, cbSize)))
  121. SetLastError((DWORD) E_OUTOFMEMORY);
  122. return pv;
  123. }
  124. VOID
  125. WINAPI
  126. PkiDefaultCryptFree(
  127. IN LPVOID pv
  128. )
  129. {
  130. if (pv)
  131. LocalFree((HLOCAL) pv);
  132. }
  133. //+-------------------------------------------------------------------------
  134. // The following data structure's pfnAlloc and pfnFree are set to
  135. // PkiNonzeroAlloc and PkiFree.
  136. //--------------------------------------------------------------------------
  137. CRYPT_ENCODE_PARA PkiEncodePara = {
  138. offsetof(CRYPT_ENCODE_PARA, pfnFree) + sizeof(PkiEncodePara.pfnFree),
  139. PkiNonzeroAlloc,
  140. PkiFree
  141. };
  142. //+-------------------------------------------------------------------------
  143. // If pfnAlloc isn't defined, returns PkiDefaultCryptAlloc
  144. //--------------------------------------------------------------------------
  145. PFN_CRYPT_ALLOC
  146. WINAPI
  147. PkiGetEncodeAllocFunction(
  148. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara
  149. )
  150. {
  151. if (pEncodePara &&
  152. pEncodePara->cbSize >= offsetof(CRYPT_ENCODE_PARA, pfnAlloc) +
  153. sizeof(pEncodePara->pfnAlloc) &&
  154. pEncodePara->pfnAlloc)
  155. return pEncodePara->pfnAlloc;
  156. else
  157. return PkiDefaultCryptAlloc;
  158. }
  159. //+-------------------------------------------------------------------------
  160. // If pfnFree isn't defined, returns PkiDefaultCryptFree
  161. //--------------------------------------------------------------------------
  162. PFN_CRYPT_FREE
  163. WINAPI
  164. PkiGetEncodeFreeFunction(
  165. IN OPTIONAL PCRYPT_ENCODE_PARA pEncodePara
  166. )
  167. {
  168. if (pEncodePara &&
  169. pEncodePara->cbSize >= offsetof(CRYPT_ENCODE_PARA, pfnFree) +
  170. sizeof(pEncodePara->pfnFree) &&
  171. pEncodePara->pfnFree)
  172. return pEncodePara->pfnFree;
  173. else
  174. return PkiDefaultCryptFree;
  175. }
  176. //+-------------------------------------------------------------------------
  177. // The following data structure's pfnAlloc and pfnFree are set to
  178. // PkiNonzeroAlloc and PkiFree.
  179. //--------------------------------------------------------------------------
  180. CRYPT_DECODE_PARA PkiDecodePara = {
  181. offsetof(CRYPT_DECODE_PARA, pfnFree) + sizeof(PkiDecodePara.pfnFree),
  182. PkiNonzeroAlloc,
  183. PkiFree
  184. };
  185. //+-------------------------------------------------------------------------
  186. // If pfnAlloc isn't defined, returns PkiDefaultCryptAlloc
  187. //--------------------------------------------------------------------------
  188. PFN_CRYPT_ALLOC
  189. WINAPI
  190. PkiGetDecodeAllocFunction(
  191. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara
  192. )
  193. {
  194. if (pDecodePara &&
  195. pDecodePara->cbSize >= offsetof(CRYPT_DECODE_PARA, pfnAlloc) +
  196. sizeof(pDecodePara->pfnAlloc) &&
  197. pDecodePara->pfnAlloc)
  198. return pDecodePara->pfnAlloc;
  199. else
  200. return PkiDefaultCryptAlloc;
  201. }
  202. //+-------------------------------------------------------------------------
  203. // If pfnFree isn't defined, returns PkiDefaultCryptFree
  204. //--------------------------------------------------------------------------
  205. PFN_CRYPT_FREE
  206. WINAPI
  207. PkiGetDecodeFreeFunction(
  208. IN OPTIONAL PCRYPT_DECODE_PARA pDecodePara
  209. )
  210. {
  211. if (pDecodePara &&
  212. pDecodePara->cbSize >= offsetof(CRYPT_DECODE_PARA, pfnFree) +
  213. sizeof(pDecodePara->pfnFree) &&
  214. pDecodePara->pfnFree)
  215. return pDecodePara->pfnFree;
  216. else
  217. return PkiDefaultCryptFree;
  218. }