ReactOS 0.4.15-dev-7961-gdcf9eb0
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
15VOID
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
33 IN OUT PSIZE_T FreeSize,
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
61 IN OUT PSIZE_T FreeSize,
65{
67
68 /* First reserve the memory */
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
83 IN OUT PSIZE_T FreeSize,
85 IN BOOLEAN IsUnicode)
86{
88 SIZE_T StringLength;
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
unsigned char BOOLEAN
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define CHAR(Char)
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define ULONG_PTR
Definition: config.h:101
int Align(int arg)
Definition: ehframes.cpp:89
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
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
VOID WINAPI FlatBuf_Init(IN PFLATBUFF FlatBuffer, IN PVOID Buffer, IN SIZE_T Size)
Definition: flatbuf.c:17
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
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:3004
static COORD Position
Definition: mouse.c:34
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
uint32_t * PULONG_PTR
Definition: typedefs.h:65
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
#define WINAPI
Definition: msvc.h:6
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184