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.

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