ReactOS  0.4.14-dev-115-g4576127
flatbuf.c
Go to the documentation of this file.
1 /*
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)
6  */
7 
8 /* INCLUDES ******************************************************************/
9 #include "precomp.h"
10 
11 /* DATA **********************************************************************/
12 
13 /* FUNCTIONS *****************************************************************/
14 
15 VOID
16 WINAPI
18  IN PVOID Buffer,
19  IN SIZE_T Size)
20 {
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);
25 
26  /* Setup the current size and the available size */
27  FlatBuffer->BufferSize = FlatBuffer->BufferFreeSize = Size;
28 }
29 
30 PVOID
31 WINAPI
33  IN OUT PSIZE_T FreeSize,
34  IN SIZE_T Size,
35  IN ULONG Align)
36 {
37  ULONG_PTR NewPosition, OldPosition = *Position;
38  SIZE_T NewFreeSize = *FreeSize;
39 
40  /* Start by aligning our position */
41  if (Align) OldPosition += (Align - 1) & ~Align;
42 
43  /* Update it */
44  NewPosition = OldPosition + Size;
45 
46  /* Update Free Size */
47  NewFreeSize += (OldPosition - NewPosition);
48 
49  /* Save new values */
50  *Position = NewPosition;
51  *FreeSize = NewFreeSize;
52 
53  /* Check if we're out of space or not */
54  if (NewFreeSize > 0) return (PVOID)OldPosition;
55  return NULL;
56 }
57 
58 PVOID
59 WINAPI
61  IN OUT PSIZE_T FreeSize,
62  IN PVOID Buffer,
63  IN SIZE_T Size,
64  IN ULONG Align)
65 {
67 
68  /* First reserve the memory */
69  Destination = FlatBuf_Arg_Reserve(Position, FreeSize, Size, Align);
70  if (Destination)
71  {
72  /* We have space, do the copy */
74  }
75 
76  /* Return the pointer to the data */
77  return Destination;
78 }
79 
80 PVOID
81 WINAPI
83  IN OUT PSIZE_T FreeSize,
84  IN PVOID String,
85  IN BOOLEAN IsUnicode)
86 {
88  SIZE_T StringLength;
89  ULONG Align;
90 
91  /* Calculate the string length */
92  if (IsUnicode)
93  {
94  /* Get the length in bytes and use WCHAR alignment */
95  StringLength = (wcslen((LPWSTR)String) + 1) * sizeof(WCHAR);
96  Align = sizeof(WCHAR);
97  }
98  else
99  {
100  /* Get the length in bytes and use CHAR alignment */
101  StringLength = strlen((LPSTR)String) + 1;
102  Align = sizeof(CHAR);
103  }
104 
105  /* Now reserve the memory */
106  Destination = FlatBuf_Arg_Reserve(Position, FreeSize, StringLength, Align);
107  if (Destination)
108  {
109  /* We have space, do the copy */
110  RtlCopyMemory(Destination, String, StringLength);
111  }
112 
113  /* Return the pointer to the data */
114  return Destination;
115 }
116 
#define IN
Definition: typedefs.h:38
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
static COORD Position
Definition: mouse.c:34
static WCHAR String[]
Definition: stringtable.c:55
char * LPSTR
Definition: xmlstorage.h:182
uint32_t ULONG_PTR
Definition: typedefs.h:63
VOID WINAPI FlatBuf_Init(IN PFLATBUFF FlatBuffer, IN PVOID Buffer, IN SIZE_T Size)
Definition: flatbuf.c:17
ULONG_PTR * PSIZE_T
Definition: typedefs.h:78
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
PVOID WINAPI FlatBuf_Arg_Reserve(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN SIZE_T Size, IN ULONG Align)
Definition: flatbuf.c:32
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define WINAPI
Definition: msvc.h:8
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
PVOID WINAPI FlatBuf_Arg_WriteString(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN PVOID String, IN BOOLEAN IsUnicode)
Definition: flatbuf.c:82
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
uint32_t * PULONG_PTR
Definition: typedefs.h:63
PVOID WINAPI FlatBuf_Arg_CopyMemory(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN PVOID Buffer, IN SIZE_T Size, IN ULONG Align)
Definition: flatbuf.c:60
WCHAR * LPWSTR
Definition: xmlstorage.h:184
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define CHAR(Char)
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)