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.

285 lines
7.1 KiB

  1. /*++
  2. Copyright (c) 1991-1992 Microsoft Corporation
  3. Module Name:
  4. ApiBuff.c
  5. Abstract:
  6. This module contains routines for allocating and freeing API buffers.
  7. Author:
  8. John Rogers (JohnRo) 25-Jan-1991
  9. Environment:
  10. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  11. Requires ANSI C extensions: slash-slash comments, long external names.
  12. Notes:
  13. Parts of the commentary for this file are extracted from material written
  14. by Alec Barker (AlecB@Microsoft).
  15. Revision History:
  16. 15-Mar-91 JohnRo
  17. Use <netdebug.h> and netlib routines.
  18. 25-Apr-1991 JohnRo
  19. Call MIDL_user_allocate and MIDL_user_free. Delete tabs.
  20. 03-Dec-1991 JohnRo
  21. Added public NetApiBufferAllocate, NetApiBufferReallocate, and
  22. NetApiBufferSize APIs.
  23. Make sure buffers are aligned for worst case use.
  24. 10-May-1992 JohnRo
  25. Treat alloc and realloc of size zero as non-error (return NULL ptr).
  26. Use <prefix.h> equates.
  27. Include my own prototypes so compiler can check them.
  28. 18-May-1992 JohnRo
  29. RAID 9258: return non-null pointer when allocating zero bytes.
  30. --*/
  31. // These must be included first:
  32. #include <windef.h> // IN, LPVOID, etc.
  33. #include <lmcons.h> // NET_API_FUNCTION, etc.
  34. #include <rpc.h> // rpc prototypes
  35. // These may be included in any order:
  36. #include <align.h> // POINTER_IS_ALIGNED(), ALIGN_WORST.
  37. #include <lmapibuf.h> // My prototypes.
  38. #ifndef WIN32_CHICAGO
  39. #include <netdebug.h> // NetpAssert(), NetpKdPrint(()), FORMAT_.
  40. #else // WIN32_CHICAGO
  41. #include <nldebug.h> // NetpAssert(), NetpKdPrint(()), FORMAT_.
  42. #endif // WIN32_CHICAGO
  43. #include <prefix.h> // PREFIX_ equates.
  44. #include <rpcutil.h> // MIDL_user_allocate(), etc.
  45. #include <winerror.h> // NO_ERROR and ERROR_ equates.
  46. #ifdef WIN32_CHICAGO
  47. #include <assert.h>
  48. PVOID
  49. MIDL_user_allocate(
  50. IN size_t BufferSize
  51. )
  52. {
  53. return (LocalAlloc(0, BufferSize));
  54. }
  55. VOID
  56. MIDL_user_free(
  57. IN PVOID Buffer
  58. )
  59. {
  60. LocalFree(Buffer);
  61. }
  62. #endif // WIN32_CHICAGO
  63. NET_API_STATUS NET_API_FUNCTION
  64. NetApiBufferAllocate(
  65. IN DWORD ByteCount,
  66. OUT LPVOID * Buffer
  67. )
  68. /*++
  69. Routine Description:
  70. NetApiBufferAllocate is an internal function that allocates buffers
  71. which the APIs will return to the application. (Usually these are for
  72. get-info operations.)
  73. Arguments:
  74. ByteCount - Supplies the size (in bytes) that must be allocated for this
  75. buffer. This may be zero, in which case a non-null pointer is
  76. passed-back and NO_ERROR is returned.
  77. Buffer - On return a pointer to the allocated area is returned in the
  78. address pointed to by Buffer. (This is set to NULL on error.)
  79. The allocated area is guaranteed to be worst-case aligned for any
  80. use whatsoever.
  81. Return Value:
  82. NET_API_STATUS - NO_ERROR if size is zero or memory was allocated.
  83. ERROR_NOT_ENOUGH_MEMORY if memory is not available.
  84. ERROR_INVALID_PARAMETER if a parameter is in error.
  85. --*/
  86. {
  87. if (Buffer == NULL) {
  88. return (ERROR_INVALID_PARAMETER);
  89. }
  90. //
  91. // Allocate the space. Note that MIDL_user_allocate must allow zero
  92. // bytes to be allocated.
  93. //
  94. *Buffer = MIDL_user_allocate(ByteCount);
  95. if (*Buffer == NULL) {
  96. return (ERROR_NOT_ENOUGH_MEMORY);
  97. }
  98. #ifndef WIN32_CHICAGO
  99. NetpAssert( POINTER_IS_ALIGNED( *Buffer, ALIGN_WORST) );
  100. #else // WIN32_CHICAGO
  101. assert( POINTER_IS_ALIGNED( *Buffer, ALIGN_WORST) );
  102. #endif // WIN32_CHICAGO
  103. return (NO_ERROR);
  104. } // NetApiBufferAllocate
  105. NET_API_STATUS NET_API_FUNCTION
  106. NetApiBufferFree (
  107. IN LPVOID Buffer
  108. )
  109. /*++
  110. Routine Description:
  111. NetApiBufferFree is called to deallocate memory which was acquired by
  112. a previous Net API call (e.g. NetApiBufferAllocate, NetWkstaGetInfo, and
  113. so on).
  114. Arguments:
  115. Buffer - Supplies a pointer to an API information buffer previously
  116. returned on a Net API call. (This would have been allocated by
  117. NetapipAllocateBuffer on behalf of one of the end-user Net API calls,
  118. e.g. NetWkstaGetInfo.)
  119. Return Value:
  120. NET_API_STATUS. Returns NO_ERROR if Buffer is null or memory was freed.
  121. Returns ERROR_INVALID_PARAMETER if Buffer points to an unaligned area.
  122. --*/
  123. {
  124. if (Buffer == NULL) {
  125. return (NO_ERROR);
  126. }
  127. if ( !POINTER_IS_ALIGNED( Buffer, ALIGN_WORST ) ) {
  128. #ifndef WIN32_CHICAGO
  129. NetpKdPrint(( PREFIX_NETAPI "NetApiBufferFree: unaligned input ptr: "
  130. FORMAT_LPVOID "!\n", (LPVOID) Buffer ));
  131. #endif // WIN32_CHICAGO
  132. return (ERROR_INVALID_PARAMETER);
  133. }
  134. MIDL_user_free(Buffer);
  135. return (NO_ERROR);
  136. } // NetApiBufferFree
  137. #ifndef WIN32_CHICAGO
  138. NET_API_STATUS NET_API_FUNCTION
  139. NetApiBufferReallocate(
  140. IN LPVOID OldBuffer OPTIONAL,
  141. IN DWORD NewByteCount,
  142. OUT LPVOID * NewBuffer
  143. )
  144. {
  145. LPVOID NewPointer;
  146. if ( (OldBuffer==NULL) && (NewByteCount==0) ) {
  147. *NewBuffer = NULL;
  148. return (NO_ERROR);
  149. }
  150. NewPointer = (void *)MIDL_user_reallocate( // may alloc, realloc, or free.
  151. (void *) OldBuffer,
  152. (unsigned long) NewByteCount);
  153. if (NewByteCount == 0) { // free
  154. *NewBuffer = NULL;
  155. return (NO_ERROR);
  156. } else if (NewPointer == NULL) { // out of memory
  157. *NewBuffer = OldBuffer; // (don't lose old buffer)
  158. return (ERROR_NOT_ENOUGH_MEMORY);
  159. } else { // alloc or realloc
  160. *NewBuffer = NewPointer;
  161. return (NO_ERROR);
  162. }
  163. /*NOTREACHED*/
  164. } // NetApiBufferReallocate
  165. NET_API_STATUS NET_API_FUNCTION
  166. NetApiBufferSize(
  167. IN LPVOID Buffer,
  168. OUT LPDWORD ByteCount
  169. )
  170. {
  171. DWORD AllocedSize;
  172. if ( (Buffer==NULL) || (ByteCount==NULL) ) {
  173. return (ERROR_INVALID_PARAMETER);
  174. } else if (POINTER_IS_ALIGNED( ByteCount, ALIGN_DWORD ) == FALSE) {
  175. return (ERROR_INVALID_PARAMETER);
  176. } else if (POINTER_IS_ALIGNED( Buffer, ALIGN_WORST ) == FALSE) {
  177. // Caller didn't get this pointer from us!
  178. return (ERROR_INVALID_PARAMETER);
  179. }
  180. AllocedSize = (unsigned long)MIDL_user_size(
  181. (void *) Buffer);
  182. NetpAssert( AllocedSize > 0 );
  183. *ByteCount = AllocedSize;
  184. return (NO_ERROR);
  185. } // NetApiBufferSize
  186. #endif // WIN32_CHICAGO
  187. NET_API_STATUS NET_API_FUNCTION
  188. NetapipBufferAllocate (
  189. IN DWORD ByteCount,
  190. OUT LPVOID * Buffer
  191. )
  192. /*++
  193. Routine Description:
  194. NetapipBufferAllocate is an old internal function that allocates buffers
  195. which the APIs will return to the application. All calls to this routine
  196. should eventually be replaced by calls to NetApiBufferAllocate.
  197. Arguments:
  198. (Same as NetApiBufferAllocate.)
  199. Return Value:
  200. (Same as NetApiBufferAllocate.)
  201. --*/
  202. {
  203. return (NetApiBufferAllocate( ByteCount, Buffer ));
  204. } // NetapipBufferAllocate