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.

255 lines
6.5 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. #include <netdebug.h> // NetpAssert(), NetpKdPrint(()), FORMAT_.
  39. #include <prefix.h> // PREFIX_ equates.
  40. #include <rpcutil.h> // MIDL_user_allocate(), etc.
  41. #include <winerror.h> // NO_ERROR and ERROR_ equates.
  42. NET_API_STATUS NET_API_FUNCTION
  43. NetApiBufferAllocate(
  44. IN DWORD ByteCount,
  45. OUT LPVOID * Buffer
  46. )
  47. /*++
  48. Routine Description:
  49. NetApiBufferAllocate is an internal function that allocates buffers
  50. which the APIs will return to the application. (Usually these are for
  51. get-info operations.)
  52. Arguments:
  53. ByteCount - Supplies the size (in bytes) that must be allocated for this
  54. buffer. This may be zero, in which case a non-null pointer is
  55. passed-back and NO_ERROR is returned.
  56. Buffer - On return a pointer to the allocated area is returned in the
  57. address pointed to by Buffer. (This is set to NULL on error.)
  58. The allocated area is guaranteed to be worst-case aligned for any
  59. use whatsoever.
  60. Return Value:
  61. NET_API_STATUS - NO_ERROR if size is zero or memory was allocated.
  62. ERROR_NOT_ENOUGH_MEMORY if memory is not available.
  63. ERROR_INVALID_PARAMETER if a parameter is in error.
  64. --*/
  65. {
  66. if (Buffer == NULL) {
  67. return (ERROR_INVALID_PARAMETER);
  68. }
  69. //
  70. // Allocate the space. Note that MIDL_user_allocate must allow zero
  71. // bytes to be allocated.
  72. //
  73. *Buffer = MIDL_user_allocate(ByteCount);
  74. if (*Buffer == NULL) {
  75. return (ERROR_NOT_ENOUGH_MEMORY);
  76. }
  77. NetpAssert( POINTER_IS_ALIGNED( *Buffer, ALIGN_WORST) );
  78. return (NO_ERROR);
  79. } // NetApiBufferAllocate
  80. NET_API_STATUS NET_API_FUNCTION
  81. NetApiBufferFree (
  82. IN LPVOID Buffer
  83. )
  84. /*++
  85. Routine Description:
  86. NetApiBufferFree is called to deallocate memory which was acquired by
  87. a previous Net API call (e.g. NetApiBufferAllocate, NetWkstaGetInfo, and
  88. so on).
  89. Arguments:
  90. Buffer - Supplies a pointer to an API information buffer previously
  91. returned on a Net API call. (This would have been allocated by
  92. NetapipAllocateBuffer on behalf of one of the end-user Net API calls,
  93. e.g. NetWkstaGetInfo.)
  94. Return Value:
  95. NET_API_STATUS. Returns NO_ERROR if Buffer is null or memory was freed.
  96. Returns ERROR_INVALID_PARAMETER if Buffer points to an unaligned area.
  97. --*/
  98. {
  99. if (Buffer == NULL) {
  100. return (NO_ERROR);
  101. }
  102. if ( !POINTER_IS_ALIGNED( Buffer, ALIGN_WORST ) ) {
  103. NetpKdPrint(( PREFIX_NETAPI "NetApiBufferFree: unaligned input ptr: "
  104. FORMAT_LPVOID "!\n", (LPVOID) Buffer ));
  105. return (ERROR_INVALID_PARAMETER);
  106. }
  107. MIDL_user_free(Buffer);
  108. return (NO_ERROR);
  109. } // NetApiBufferFree
  110. NET_API_STATUS NET_API_FUNCTION
  111. NetApiBufferReallocate(
  112. IN LPVOID OldBuffer OPTIONAL,
  113. IN DWORD NewByteCount,
  114. OUT LPVOID * NewBuffer
  115. )
  116. {
  117. LPVOID NewPointer;
  118. if ( (OldBuffer==NULL) && (NewByteCount==0) ) {
  119. *NewBuffer = NULL;
  120. return (NO_ERROR);
  121. }
  122. NewPointer = (void *)MIDL_user_reallocate( // may alloc, realloc, or free.
  123. (void *) OldBuffer,
  124. (unsigned long) NewByteCount);
  125. if (NewByteCount == 0) { // free
  126. *NewBuffer = NULL;
  127. return (NO_ERROR);
  128. } else if (NewPointer == NULL) { // out of memory
  129. *NewBuffer = OldBuffer; // (don't lose old buffer)
  130. return (ERROR_NOT_ENOUGH_MEMORY);
  131. } else { // alloc or realloc
  132. *NewBuffer = NewPointer;
  133. return (NO_ERROR);
  134. }
  135. /*NOTREACHED*/
  136. } // NetApiBufferReallocate
  137. NET_API_STATUS NET_API_FUNCTION
  138. NetApiBufferSize(
  139. IN LPVOID Buffer,
  140. OUT LPDWORD ByteCount
  141. )
  142. {
  143. DWORD AllocedSize;
  144. if ( (Buffer==NULL) || (ByteCount==NULL) ) {
  145. return (ERROR_INVALID_PARAMETER);
  146. } else if (POINTER_IS_ALIGNED( ByteCount, ALIGN_DWORD ) == FALSE) {
  147. return (ERROR_INVALID_PARAMETER);
  148. } else if (POINTER_IS_ALIGNED( Buffer, ALIGN_WORST ) == FALSE) {
  149. // Caller didn't get this pointer from us!
  150. return (ERROR_INVALID_PARAMETER);
  151. }
  152. AllocedSize = (unsigned long)MIDL_user_size(
  153. (void *) Buffer);
  154. NetpAssert( AllocedSize > 0 );
  155. *ByteCount = AllocedSize;
  156. return (NO_ERROR);
  157. } // NetApiBufferSize
  158. NET_API_STATUS NET_API_FUNCTION
  159. NetapipBufferAllocate (
  160. IN DWORD ByteCount,
  161. OUT LPVOID * Buffer
  162. )
  163. /*++
  164. Routine Description:
  165. NetapipBufferAllocate is an old internal function that allocates buffers
  166. which the APIs will return to the application. All calls to this routine
  167. should eventually be replaced by calls to NetApiBufferAllocate.
  168. Arguments:
  169. (Same as NetApiBufferAllocate.)
  170. Return Value:
  171. (Same as NetApiBufferAllocate.)
  172. --*/
  173. {
  174. return (NetApiBufferAllocate( ByteCount, Buffer ));
  175. } // NetapipBufferAllocate