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.

568 lines
11 KiB

  1. /*++
  2. Copyright (c) 2000-2000 Microsoft Corporation
  3. Module Name:
  4. flatbuf.c
  5. Abstract:
  6. Domain Name System (DNS) Library
  7. Flat buffer sizing routines.
  8. Author:
  9. Jim Gilroy (jamesg) December 22, 2000
  10. Revision History:
  11. --*/
  12. #include "local.h"
  13. //
  14. // Flat buffer routines -- argument versions
  15. //
  16. // These versions have the actual code so that we can
  17. // easily use this stuff with existing code that has
  18. // independent pCurrent and BytesLeft variables.
  19. //
  20. // FLATBUF structure versions just call these inline.
  21. //
  22. PBYTE
  23. FlatBuf_Arg_Reserve(
  24. IN OUT PBYTE * ppCurrent,
  25. IN OUT PINT pBytesLeft,
  26. IN DWORD Size,
  27. IN DWORD Alignment
  28. )
  29. /*++
  30. Routine Description:
  31. Reserve space in a flat buffer -- properly aligned.
  32. Arguments:
  33. ppCurrent -- address of buffer current pointer
  34. updated with buf pointer after reservation
  35. pBytesLeft -- address of buf bytes left
  36. updated with bytes left after reservation
  37. Size -- size required
  38. Alignment -- alignment (size in bytes) required
  39. Return Value:
  40. Ptr to aligned spot in buffer reserved for write.
  41. NULL on error.
  42. --*/
  43. {
  44. register PBYTE pb = *ppCurrent;
  45. register INT bytesLeft = *pBytesLeft;
  46. register PBYTE pstart;
  47. register PBYTE palign;
  48. //
  49. // align pointer
  50. //
  51. pstart = pb;
  52. if ( Alignment )
  53. {
  54. Alignment--;
  55. pb = (PBYTE) ( (UINT_PTR)(pb + Alignment) & ~(UINT_PTR)Alignment );
  56. }
  57. palign = pb;
  58. //
  59. // reserve space
  60. //
  61. pb += Size;
  62. bytesLeft -= (INT) (pb - pstart);
  63. *pBytesLeft = bytesLeft;
  64. *ppCurrent = pb;
  65. //
  66. // indicate space adequate\not
  67. //
  68. if ( bytesLeft < 0 )
  69. {
  70. palign = NULL;
  71. }
  72. return palign;
  73. }
  74. PBYTE
  75. FlatBuf_Arg_WriteString(
  76. IN OUT PBYTE * ppCurrent,
  77. IN OUT PINT pBytesLeft,
  78. IN PSTR pString,
  79. IN BOOL fUnicode
  80. )
  81. /*++
  82. Routine Description:
  83. Write string to flat buffer.
  84. Arguments:
  85. ppCurrent -- address of buffer current pointer
  86. updated with buf pointer after reservation
  87. pBytesLeft -- address of buf bytes left
  88. updated with bytes left after reservation
  89. pString -- ptr to string to write
  90. fUnicode -- TRUE for unicode string
  91. Return Value:
  92. Ptr to location string was written in buffer.
  93. NULL on error.
  94. --*/
  95. {
  96. register PBYTE pwrite;
  97. register DWORD length;
  98. register DWORD align;
  99. //
  100. // determine length
  101. //
  102. if ( fUnicode )
  103. {
  104. length = (wcslen( (PWSTR)pString ) + 1) * sizeof(WCHAR);
  105. align = sizeof(WCHAR);
  106. }
  107. else
  108. {
  109. length = strlen( pString ) + 1;
  110. align = 0;
  111. }
  112. //
  113. // reserve space and copy string
  114. //
  115. pwrite = FlatBuf_Arg_Reserve(
  116. ppCurrent,
  117. pBytesLeft,
  118. length,
  119. align );
  120. if ( pwrite )
  121. {
  122. RtlCopyMemory(
  123. pwrite,
  124. pString,
  125. length );
  126. }
  127. return pwrite;
  128. }
  129. PBYTE
  130. FlatBuf_Arg_CopyMemory(
  131. IN OUT PBYTE * ppCurrent,
  132. IN OUT PINT pBytesLeft,
  133. IN PVOID pMemory,
  134. IN DWORD Length,
  135. IN DWORD Alignment
  136. )
  137. /*++
  138. Routine Description:
  139. Write memory to flat buffer.
  140. Arguments:
  141. ppCurrent -- address of buffer current pointer
  142. updated with buf pointer after reservation
  143. pBytesLeft -- address of buf bytes left
  144. updated with bytes left after reservation
  145. pMemory -- memory to copy
  146. Length -- length to copy
  147. Alignment -- alignment (size in bytes) required
  148. Return Value:
  149. Ptr to location string was written in buffer.
  150. NULL on error.
  151. --*/
  152. {
  153. register PBYTE pwrite;
  154. //
  155. // reserve space and copy memory
  156. //
  157. pwrite = FlatBuf_Arg_Reserve(
  158. ppCurrent,
  159. pBytesLeft,
  160. Length,
  161. Alignment );
  162. if ( pwrite )
  163. {
  164. RtlCopyMemory(
  165. pwrite,
  166. pMemory,
  167. Length );
  168. }
  169. return pwrite;
  170. }
  171. #if 0
  172. //
  173. // Flatbuf inline functions -- defined in dnslib.h
  174. //
  175. __inline
  176. PBYTE
  177. FlatBuf_Arg_ReserveAlignPointer(
  178. IN OUT PBYTE * ppCurrent,
  179. IN OUT PINT pBytesLeft,
  180. IN DWORD Size
  181. )
  182. {
  183. return FlatBuf_Arg_Reserve(
  184. ppCurrent,
  185. pBytesLeft,
  186. Size,
  187. sizeof(PVOID) );
  188. }
  189. __inline
  190. PBYTE
  191. FlatBuf_Arg_ReserveAlignQword(
  192. IN OUT PBYTE * ppCurrent,
  193. IN OUT PINT pBytesLeft,
  194. IN DWORD Size
  195. )
  196. {
  197. return FlatBuf_Arg_Reserve(
  198. ppCurrent,
  199. pBytesLeft,
  200. Size,
  201. sizeof(QWORD) );
  202. }
  203. __inline
  204. PBYTE
  205. FlatBuf_Arg_ReserveAlignDword(
  206. IN OUT PBYTE * ppCurrent,
  207. IN OUT PINT pBytesLeft,
  208. IN DWORD Size
  209. )
  210. {
  211. return FlatBuf_Arg_Reserve(
  212. ppCurrent,
  213. pBytesLeft,
  214. Size,
  215. sizeof(DWORD) );
  216. }
  217. __inline
  218. PBYTE
  219. FlatBuf_Arg_ReserveAlignWord(
  220. IN OUT PBYTE * ppCurrent,
  221. IN OUT PINT pBytesLeft,
  222. IN DWORD Size
  223. )
  224. {
  225. return FlatBuf_Arg_Reserve(
  226. ppCurrent,
  227. pBytesLeft,
  228. Size,
  229. sizeof(WORD) );
  230. }
  231. __inline
  232. PBYTE
  233. FlatBuf_Arg_ReserveAlignByte(
  234. IN OUT PBYTE * ppCurrent,
  235. IN OUT PINT pBytesLeft,
  236. IN DWORD Size
  237. )
  238. {
  239. return FlatBuf_Arg_Reserve(
  240. ppCurrent,
  241. pBytesLeft,
  242. Size,
  243. 0 );
  244. }
  245. PBYTE
  246. __inline
  247. FlatBuf_Arg_WriteString_A(
  248. IN OUT PBYTE * ppCurrent,
  249. IN OUT PINT pBytesLeft,
  250. IN PSTR pString
  251. )
  252. {
  253. return FlatBuf_Arg_WriteString(
  254. ppCurrent,
  255. pBytesLeft,
  256. pString,
  257. FALSE // not unicode
  258. );
  259. }
  260. PBYTE
  261. __inline
  262. FlatBuf_Arg_WriteString_W(
  263. IN OUT PBYTE * ppCurrent,
  264. IN OUT PINT pBytesLeft,
  265. IN PWSTR pString
  266. )
  267. {
  268. return FlatBuf_Arg_WriteString(
  269. ppCurrent,
  270. pBytesLeft,
  271. (PSTR) pString,
  272. TRUE // unicode
  273. );
  274. }
  275. #endif
  276. //
  277. // Flat buffer routines -- structure versions
  278. //
  279. VOID
  280. FlatBuf_Init(
  281. IN OUT PFLATBUF pFlatBuf,
  282. IN PBYTE pBuffer,
  283. IN INT Size
  284. )
  285. /*++
  286. Routine Description:
  287. Init a FLATBUF struct with given buffer and size.
  288. Note, ok to init to zero for size determination.
  289. Arguments:
  290. pFlatBuf -- ptr to FLATBUF to init
  291. pBuffer -- buffer ptr
  292. Size -- size required
  293. Return Value:
  294. None
  295. --*/
  296. {
  297. pFlatBuf->pBuffer = pBuffer;
  298. pFlatBuf->pCurrent = pBuffer;
  299. pFlatBuf->pEnd = pBuffer + Size;
  300. pFlatBuf->Size = Size;
  301. pFlatBuf->BytesLeft = Size;
  302. }
  303. #if 0
  304. //
  305. // Flatbuf inline functions -- defined in dnslib.h
  306. //
  307. __inline
  308. PBYTE
  309. FlatBuf_Reserve(
  310. IN OUT PFLATBUF pBuf,
  311. IN DWORD Size,
  312. IN DWORD Alignment
  313. )
  314. {
  315. return FlatBuf_Arg_Reserve(
  316. & pBuf->pCurrent,
  317. & pBuf->BytesLeft,
  318. Size,
  319. Alignment );
  320. }
  321. __inline
  322. PBYTE
  323. FlatBuf_ReserveAlignPointer(
  324. IN OUT PFLATBUF pBuf,
  325. IN DWORD Size
  326. )
  327. {
  328. return FlatBuf_Arg_Reserve(
  329. & pBuf->pCurrent,
  330. & pBuf->BytesLeft,
  331. Size,
  332. sizeof(PVOID) );
  333. }
  334. __inline
  335. PBYTE
  336. FlatBuf_ReserveAlignQword(
  337. IN OUT PFLATBUF pBuf,
  338. IN DWORD Size
  339. )
  340. {
  341. return FlatBuf_Arg_Reserve(
  342. & pBuf->pCurrent,
  343. & pBuf->BytesLeft,
  344. Size,
  345. sizeof(QWORD) );
  346. }
  347. __inline
  348. PBYTE
  349. FlatBuf_ReserveAlignDword(
  350. IN OUT PFLATBUF pBuf,
  351. IN DWORD Size
  352. )
  353. {
  354. return FlatBuf_Arg_Reserve(
  355. & pBuf->pCurrent,
  356. & pBuf->BytesLeft,
  357. Size,
  358. sizeof(DWORD) );
  359. }
  360. __inline
  361. PBYTE
  362. FlatBuf_ReserveAlignWord(
  363. IN OUT PFLATBUF pBuf,
  364. IN DWORD Size
  365. )
  366. {
  367. return FlatBuf_Arg_Reserve(
  368. & pBuf->pCurrent,
  369. & pBuf->BytesLeft,
  370. Size,
  371. sizeof(WORD) );
  372. }
  373. __inline
  374. PBYTE
  375. FlatBuf_ReserveAlignByte(
  376. IN OUT PFLATBUF pBuf,
  377. IN DWORD Size
  378. )
  379. {
  380. return FlatBuf_Arg_Reserve(
  381. & pBuf->pCurrent,
  382. & pBuf->BytesLeft,
  383. Size,
  384. 0 );
  385. }
  386. PBYTE
  387. __inline
  388. FlatBuf_WriteString(
  389. IN OUT PFLATBUF pBuf,
  390. IN PSTR pString,
  391. IN BOOL fUnicode
  392. )
  393. {
  394. return FlatBuf_Arg_WriteString(
  395. & pBuf->pCurrent,
  396. & pBuf->BytesLeft,
  397. pString,
  398. fUnicode
  399. );
  400. }
  401. PBYTE
  402. __inline
  403. FlatBuf_WriteString_A(
  404. IN OUT PFLATBUF pBuf,
  405. IN PSTR pString
  406. )
  407. {
  408. return FlatBuf_Arg_WriteString(
  409. & pBuf->pCurrent,
  410. & pBuf->BytesLeft,
  411. pString,
  412. FALSE // not unicode
  413. );
  414. }
  415. PBYTE
  416. __inline
  417. FlatBuf_WriteString_W(
  418. IN OUT PFLATBUF pBuf,
  419. IN PWSTR pString
  420. )
  421. {
  422. return FlatBuf_Arg_WriteString(
  423. & pBuf->pCurrent,
  424. & pBuf->BytesLeft,
  425. (PSTR) pString,
  426. TRUE // unicode
  427. );
  428. }
  429. PBYTE
  430. __inline
  431. FlatBuf_CopyMemory(
  432. IN OUT PFLATBUF pBuf,
  433. IN PVOID pMemory,
  434. IN DWORD Length,
  435. IN DWORD Alignment
  436. )
  437. {
  438. return FlatBuf_Arg_CopyMemory(
  439. & pBuf->pCurrent,
  440. & pBuf->BytesLeft,
  441. pMemory,
  442. Length,
  443. Alignment );
  444. }
  445. #endif
  446. //
  447. // End flatbuf.c
  448. //