2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS DNS Shared Library
4 * FILE: lib/dnslib/flatbuf.c
5 * PURPOSE: Functions for managing the Flat Buffer Implementation (FLATBUF)
8 /* INCLUDES ******************************************************************/
11 /* DATA **********************************************************************/
13 /* FUNCTIONS *****************************************************************/
17 FlatBuf_Init(IN PFLATBUFF FlatBuffer
,
21 /* Set up the Flat Buffer start, current and ending position */
22 FlatBuffer
->Buffer
= Buffer
;
23 FlatBuffer
->BufferPos
= (ULONG_PTR
)Buffer
;
24 FlatBuffer
->BufferEnd
= (PVOID
)(FlatBuffer
->BufferPos
+ Size
);
26 /* Setup the current size and the available size */
27 FlatBuffer
->BufferSize
= FlatBuffer
->BufferFreeSize
= Size
;
32 FlatBuf_Arg_Reserve(IN OUT PULONG_PTR Position
,
33 IN OUT PSIZE_T FreeSize
,
37 ULONG_PTR NewPosition
, OldPosition
= *Position
;
38 SIZE_T NewFreeSize
= *FreeSize
;
40 /* Start by aligning our position */
41 if (Align
) OldPosition
+= (Align
- 1) & ~Align
;
44 NewPosition
= OldPosition
+ Size
;
46 /* Update Free Size */
47 NewFreeSize
+= (OldPosition
- NewPosition
);
50 *Position
= NewPosition
;
51 *FreeSize
= NewFreeSize
;
53 /* Check if we're out of space or not */
54 if (NewFreeSize
> 0) return (PVOID
)OldPosition
;
60 FlatBuf_Arg_CopyMemory(IN OUT PULONG_PTR Position
,
61 IN OUT PSIZE_T FreeSize
,
68 /* First reserve the memory */
69 Destination
= FlatBuf_Arg_Reserve(Position
, FreeSize
, Size
, Align
);
72 /* We have space, do the copy */
73 RtlCopyMemory(Destination
, Buffer
, Size
);
76 /* Return the pointer to the data */
82 FlatBuf_Arg_WriteString(IN OUT PULONG_PTR Position
,
83 IN OUT PSIZE_T FreeSize
,
91 /* Calculate the string length */
94 /* Get the length in bytes and use WCHAR alignment */
95 StringLength
= (wcslen((LPWSTR
)String
) + 1) * sizeof(WCHAR
);
96 Align
= sizeof(WCHAR
);
100 /* Get the length in bytes and use CHAR alignment */
101 StringLength
= strlen((LPSTR
)String
) + 1;
102 Align
= sizeof(CHAR
);
105 /* Now reserve the memory */
106 Destination
= FlatBuf_Arg_Reserve(Position
, FreeSize
, StringLength
, Align
);
109 /* We have space, do the copy */
110 RtlCopyMemory(Destination
, String
, StringLength
);
113 /* Return the pointer to the data */