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.

663 lines
12 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. growbuf.c
  5. Abstract:
  6. Simple buffer management functions that allow variable blocks to
  7. be added as an array. (Initially used to build a SID array, where
  8. each SID can be a different size.)
  9. Author:
  10. Jim Schmidt (jimschm) 05-Feb-1997
  11. Revision History:
  12. marcw 2-Sep-1999 Moved over from Win9xUpg project.
  13. jimschm 11-Aug-1998 Added GrowBufAppendString
  14. calinn 15-Jan-1998 modified MultiSzAppend
  15. --*/
  16. #include "pch.h"
  17. //
  18. // Includes
  19. //
  20. // None
  21. #define DBG_GROWBUF "GrowBuffer"
  22. //
  23. // Strings
  24. //
  25. // None
  26. //
  27. // Constants
  28. //
  29. #define DEFAULT_GROW_SIZE 8192
  30. //
  31. // Macros
  32. //
  33. // None
  34. //
  35. // Types
  36. //
  37. // None
  38. //
  39. // Globals
  40. //
  41. #ifdef DEBUG
  42. DWORD g_GbCurrActiveAlloc = 0;
  43. DWORD g_GbCurrUsedAlloc = 0;
  44. DWORD g_GbMaxActiveAlloc = 0;
  45. DWORD g_GbMaxUsedAlloc = 0;
  46. #endif
  47. //
  48. // Macro expansion list
  49. //
  50. // None
  51. //
  52. // Private function prototypes
  53. //
  54. // None
  55. //
  56. // Macro expansion definition
  57. //
  58. // None
  59. //
  60. // Code
  61. //
  62. PBYTE
  63. RealGbGrow (
  64. IN OUT PGROWBUFFER GrowBuf,
  65. IN DWORD SpaceNeeded
  66. )
  67. /*++
  68. Routine Description:
  69. GrowBuffer makes sure there is enough bytes in the buffer
  70. to accomodate SpaceNeeded. It allocates an initial buffer
  71. when no buffer is allocated, and it reallocates the buffer
  72. in increments of GrowBuf->Size (or DEFAULT_GROW_SIZE) when
  73. needed.
  74. Arguments:
  75. GrowBuf - A pointer to a GROWBUFFER structure.
  76. Initialize this structure to zero for
  77. the first call to GrowBuffer.
  78. SpaceNeeded - The number of free bytes needed in the buffer
  79. Return Value:
  80. A pointer to the SpaceNeeded bytes, or NULL if a memory allocation
  81. error occurred.
  82. --*/
  83. {
  84. PBYTE NewBuffer;
  85. DWORD TotalSpaceNeeded;
  86. DWORD GrowTo;
  87. MYASSERT(SpaceNeeded);
  88. if (!GrowBuf->Buf) {
  89. GrowBuf->Size = 0;
  90. GrowBuf->End = 0;
  91. #ifdef DEBUG
  92. GrowBuf->StatEnd = 0;
  93. #endif
  94. }
  95. if (!GrowBuf->GrowSize) {
  96. GrowBuf->GrowSize = DEFAULT_GROW_SIZE;
  97. }
  98. #ifdef DEBUG
  99. g_GbCurrActiveAlloc -= GrowBuf->Size;
  100. g_GbCurrUsedAlloc -= GrowBuf->StatEnd;
  101. #endif
  102. TotalSpaceNeeded = GrowBuf->End + SpaceNeeded;
  103. if (TotalSpaceNeeded > GrowBuf->Size) {
  104. GrowTo = (TotalSpaceNeeded + GrowBuf->GrowSize) - (TotalSpaceNeeded % GrowBuf->GrowSize);
  105. } else {
  106. GrowTo = 0;
  107. }
  108. if (!GrowBuf->Buf) {
  109. GrowBuf->Buf = (PBYTE) MemAlloc (g_hHeap, 0, GrowTo);
  110. if (!GrowBuf->Buf) {
  111. DEBUGMSG ((DBG_ERROR, "GbGrow: Initial alloc failed"));
  112. return NULL;
  113. }
  114. GrowBuf->Size = GrowTo;
  115. } else if (GrowTo) {
  116. NewBuffer = MemReAlloc (g_hHeap, 0, GrowBuf->Buf, GrowTo);
  117. if (!NewBuffer) {
  118. DEBUGMSG ((DBG_ERROR, "GbGrow: Realloc failed"));
  119. return NULL;
  120. }
  121. #ifdef DEBUG
  122. if (GrowBuf->Buf != NewBuffer) {
  123. DEBUGMSG ((
  124. DBG_WARNING,
  125. "GbGrow: Realloc caused growbuffer to move %u bytes to new location; "
  126. "any pointers inside old buffer are now invalid",
  127. GrowBuf->Size
  128. ));
  129. if (GrowBuf->Size > 32767) {
  130. TRACKDUMP();
  131. }
  132. }
  133. #endif
  134. GrowBuf->Size = GrowTo;
  135. GrowBuf->Buf = NewBuffer;
  136. }
  137. NewBuffer = GrowBuf->Buf + GrowBuf->End;
  138. GrowBuf->End += SpaceNeeded;
  139. #ifdef DEBUG
  140. GrowBuf->StatEnd = GrowBuf->End;
  141. g_GbCurrActiveAlloc += GrowBuf->Size;
  142. if (g_GbMaxActiveAlloc < g_GbCurrActiveAlloc) {
  143. g_GbMaxActiveAlloc = g_GbCurrActiveAlloc;
  144. }
  145. g_GbCurrUsedAlloc += GrowBuf->StatEnd;
  146. if (g_GbMaxUsedAlloc < g_GbCurrUsedAlloc) {
  147. g_GbMaxUsedAlloc = g_GbCurrUsedAlloc;
  148. }
  149. #endif
  150. return NewBuffer;
  151. }
  152. VOID
  153. GbFree (
  154. IN PGROWBUFFER GrowBuf
  155. )
  156. /*++
  157. Routine Description:
  158. FreeGrowBuffer frees a buffer allocated by GrowBuffer.
  159. Arguments:
  160. GrowBuf - A pointer to the same structure passed to GrowBuffer
  161. Return Value:
  162. none
  163. --*/
  164. {
  165. MYASSERT(GrowBuf);
  166. #ifdef DEBUG
  167. g_GbCurrActiveAlloc -= GrowBuf->Size;
  168. g_GbCurrUsedAlloc -= GrowBuf->StatEnd;
  169. #endif
  170. if (GrowBuf->Buf) {
  171. MemFree (g_hHeap, 0, GrowBuf->Buf);
  172. ZeroMemory (GrowBuf, sizeof (GROWBUFFER));
  173. }
  174. }
  175. /*++
  176. Routine Descriptions:
  177. MultiSzAppend
  178. This function is a general-purpose function to append a string
  179. to a grow buffer.
  180. MultiSzAppendVal
  181. This function adds a key=decimal_val string, where key is a
  182. specified string, and decimal_val is a specified DWORD.
  183. MultiSzAppendString
  184. This function adds key=string to the grow buffer, where key
  185. is a specified string, and string is a specified string value.
  186. Arguments:
  187. GrowBuf - The buffer to append the string or key/value pair
  188. Key - The key part of the key=val pair
  189. Val - The val part of the key=val pair
  190. Return Value:
  191. TRUE if the function succeeded, or FALSE if a memory allocation
  192. failure occurred.
  193. --*/
  194. BOOL
  195. RealGbMultiSzAppendA (
  196. PGROWBUFFER GrowBuf,
  197. PCSTR String
  198. )
  199. {
  200. PSTR p;
  201. p = (PSTR) GbGrow (GrowBuf, SizeOfStringA (String) + sizeof(CHAR));
  202. if (!p) {
  203. return FALSE;
  204. }
  205. StringCopyA (p, String);
  206. GrowBuf->End -= sizeof (CHAR);
  207. GrowBuf->Buf[GrowBuf->End] = 0;
  208. return TRUE;
  209. }
  210. BOOL
  211. RealGbMultiSzAppendW (
  212. PGROWBUFFER GrowBuf,
  213. PCWSTR String
  214. )
  215. {
  216. PWSTR p;
  217. p = (PWSTR) GbGrow (GrowBuf, SizeOfStringW (String) + sizeof(WCHAR));
  218. if (!p) {
  219. return FALSE;
  220. }
  221. StringCopyW (p, String);
  222. GrowBuf->End -= sizeof (WCHAR);
  223. *((PWCHAR) (GrowBuf->Buf + GrowBuf->End)) = 0;
  224. return TRUE;
  225. }
  226. BOOL
  227. RealGbAppendDword (
  228. PGROWBUFFER GrowBuf,
  229. DWORD d
  230. )
  231. {
  232. PDWORD p;
  233. p = (PDWORD) GbGrow (GrowBuf, sizeof (DWORD));
  234. if (!p) {
  235. return FALSE;
  236. }
  237. *p = d;
  238. return TRUE;
  239. }
  240. BOOL
  241. RealGbAppendPvoid (
  242. PGROWBUFFER GrowBuf,
  243. PCVOID Ptr
  244. )
  245. {
  246. PCVOID *p;
  247. p = (PVOID *) GbGrow (GrowBuf, sizeof (PVOID));
  248. if (!p) {
  249. return FALSE;
  250. }
  251. *p = Ptr;
  252. return TRUE;
  253. }
  254. /*++
  255. Routine Description:
  256. GrowBufAppendString copies the specified string to the end of the grow
  257. buffer. This is the equivalent of strcat. The grow buffer is
  258. automatically expanded as necessary.
  259. Arguments:
  260. GrowBuf - Specifies the destination grow buffer
  261. String - Specifies the string to append
  262. Return Value:
  263. Always TRUE.
  264. --*/
  265. BOOL
  266. RealGbAppendStringA (
  267. IN OUT PGROWBUFFER GrowBuf,
  268. IN PCSTR String
  269. )
  270. {
  271. UINT OldEnd;
  272. PSTR p;
  273. UINT Bytes;
  274. if (String) {
  275. Bytes = SizeOfStringA (String);
  276. OldEnd = GrowBuf->End;
  277. if (OldEnd) {
  278. p = (PSTR) (GrowBuf->Buf + OldEnd - sizeof (CHAR));
  279. if (*p == 0) {
  280. OldEnd -= sizeof (CHAR);
  281. }
  282. }
  283. RealGbGrow (GrowBuf, Bytes);
  284. p = (PSTR) (GrowBuf->Buf + OldEnd);
  285. StringCopyA (p, String);
  286. GrowBuf->End = OldEnd + Bytes;
  287. }
  288. return TRUE;
  289. }
  290. BOOL
  291. RealGbAppendStringW (
  292. IN OUT PGROWBUFFER GrowBuf,
  293. IN PCWSTR String
  294. )
  295. {
  296. UINT OldEnd;
  297. PWSTR p;
  298. UINT Bytes;
  299. if (String) {
  300. Bytes = SizeOfStringW (String);
  301. OldEnd = GrowBuf->End;
  302. if (OldEnd) {
  303. p = (PWSTR) (GrowBuf->Buf + OldEnd - sizeof (WCHAR));
  304. if (*p == 0) {
  305. OldEnd -= sizeof (WCHAR);
  306. }
  307. }
  308. RealGbGrow (GrowBuf, Bytes);
  309. p = (PWSTR) (GrowBuf->Buf + OldEnd);
  310. StringCopyW (p, String);
  311. GrowBuf->End = OldEnd + Bytes;
  312. }
  313. return TRUE;
  314. }
  315. /*++
  316. Routine Description:
  317. GrowBufAppendStringAB copies the specified string range to the
  318. end of the grow buffer. This concatenates the string to the
  319. existing buffer contents, and keeps the buffer terminated.
  320. Arguments:
  321. GrowBuf - Specifies the destination grow buffer
  322. Start - Specifies the start of string to append
  323. EndPlusOne - Specifies one logical character beyond the end of
  324. the string, and can point to a nul.
  325. Return Value:
  326. Always TRUE.
  327. --*/
  328. BOOL
  329. RealGbAppendStringABA (
  330. IN OUT PGROWBUFFER GrowBuf,
  331. IN PCSTR Start,
  332. IN PCSTR EndPlusOne
  333. )
  334. {
  335. UINT OldEnd;
  336. PSTR p;
  337. UINT Bytes;
  338. if (Start && Start < EndPlusOne) {
  339. Bytes = (UINT)((UBINT) EndPlusOne - (UBINT) Start);
  340. OldEnd = GrowBuf->End;
  341. if (OldEnd) {
  342. p = (PSTR) (GrowBuf->Buf + OldEnd - sizeof (CHAR));
  343. if (*p == 0) {
  344. OldEnd -= sizeof (CHAR);
  345. }
  346. }
  347. RealGbGrow (GrowBuf, Bytes + sizeof (CHAR));
  348. p = (PSTR) (GrowBuf->Buf + OldEnd);
  349. CopyMemory (p, Start, Bytes);
  350. p = (PSTR) ((PBYTE) p + Bytes);
  351. *p = 0;
  352. GrowBuf->End = OldEnd + Bytes + sizeof (CHAR);
  353. }
  354. return TRUE;
  355. }
  356. BOOL
  357. RealGbAppendStringABW (
  358. IN OUT PGROWBUFFER GrowBuf,
  359. IN PCWSTR Start,
  360. IN PCWSTR EndPlusOne
  361. )
  362. {
  363. UINT OldEnd;
  364. PWSTR p;
  365. UINT Bytes;
  366. if (Start && Start < EndPlusOne) {
  367. Bytes = (UINT)((UBINT) EndPlusOne - (UBINT) Start);
  368. OldEnd = GrowBuf->End;
  369. if (OldEnd > sizeof (WCHAR)) {
  370. p = (PWSTR) (GrowBuf->Buf + OldEnd - sizeof (WCHAR));
  371. if (*p == 0) {
  372. OldEnd -= sizeof (WCHAR);
  373. }
  374. }
  375. RealGbGrow (GrowBuf, Bytes + sizeof (WCHAR));
  376. p = (PWSTR) (GrowBuf->Buf + OldEnd);
  377. CopyMemory (p, Start, Bytes);
  378. p = (PWSTR) ((PBYTE) p + Bytes);
  379. *p = 0;
  380. GrowBuf->End = OldEnd + Bytes + sizeof (WCHAR);
  381. }
  382. return TRUE;
  383. }
  384. /*++
  385. Routine Description:
  386. GrowBufCopyString copies the specified string to the end of the grow buffer.
  387. Arguments:
  388. GrowBuf - Specifies the grow buffer to add to, receives the updated buffer
  389. String - Specifies the string to add to GrowBuf
  390. Return Value:
  391. --*/
  392. BOOL
  393. RealGbCopyStringA (
  394. IN OUT PGROWBUFFER GrowBuf,
  395. IN PCSTR String
  396. )
  397. {
  398. PBYTE Buf;
  399. UINT Size;
  400. Size = SizeOfStringA (String);
  401. Buf = RealGbGrow (GrowBuf, Size);
  402. if (!Buf) {
  403. return FALSE;
  404. }
  405. CopyMemory (Buf, String, Size);
  406. return TRUE;
  407. }
  408. BOOL
  409. RealGbCopyStringW (
  410. IN OUT PGROWBUFFER GrowBuf,
  411. IN PCWSTR String
  412. )
  413. {
  414. PBYTE Buf;
  415. UINT Size;
  416. Size = SizeOfStringW (String);
  417. Buf = RealGbGrow (GrowBuf, Size);
  418. if (!Buf) {
  419. return FALSE;
  420. }
  421. CopyMemory (Buf, String, Size);
  422. return TRUE;
  423. }
  424. BOOL
  425. RealGbCopyQuotedStringA (
  426. IN OUT PGROWBUFFER GrowBuf,
  427. IN PCSTR String
  428. )
  429. {
  430. PBYTE Buf;
  431. UINT Size;
  432. Size = SizeOfStringA (String);
  433. Buf = RealGbGrow (GrowBuf, Size + 2 * sizeof (CHAR));
  434. if (!Buf) {
  435. return FALSE;
  436. }
  437. *((CHAR *)(Buf)) = '\"';
  438. CopyMemory (Buf + sizeof (CHAR), String, Size);
  439. *((CHAR *)(Buf + Size)) = '\"';
  440. *((CHAR *)(Buf + Size + sizeof (CHAR))) = 0;
  441. return TRUE;
  442. }
  443. BOOL
  444. RealGbCopyQuotedStringW (
  445. IN OUT PGROWBUFFER GrowBuf,
  446. IN PCWSTR String
  447. )
  448. {
  449. PBYTE Buf;
  450. UINT Size;
  451. Size = SizeOfStringW (String);
  452. Buf = RealGbGrow (GrowBuf, Size + 2 * sizeof (WCHAR));
  453. if (!Buf) {
  454. return FALSE;
  455. }
  456. *((WCHAR *)(Buf)) = L'\"';
  457. CopyMemory (Buf + sizeof (WCHAR), String, Size);
  458. *((WCHAR *)(Buf + Size)) = L'\"';
  459. *((WCHAR *)(Buf + Size + sizeof (WCHAR))) = 0;
  460. return TRUE;
  461. }
  462. #ifdef DEBUG
  463. VOID
  464. GbDumpStatistics (
  465. VOID
  466. )
  467. {
  468. DEBUGMSG ((
  469. DBG_STATS,
  470. "Grow buffers usage:\nPeak : Usable:%-8d Used:%-8d\nCurrent: Usable:%-8d Leak:%-8d",
  471. g_GbMaxActiveAlloc,
  472. g_GbMaxUsedAlloc,
  473. g_GbCurrActiveAlloc,
  474. g_GbCurrUsedAlloc
  475. ));
  476. }
  477. #endif