Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/* * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS DNS Shared Library * FILE: lib/dnslib/flatbuf.c * PURPOSE: Functions for managing the Flat Buffer Implementation (FLATBUF) */
/* INCLUDES ******************************************************************/#include "precomp.h"
/* DATA **********************************************************************/
/* FUNCTIONS *****************************************************************/
VOIDWINAPIFlatBuf_Init(IN PFLATBUFF FlatBuffer, IN PVOID Buffer, IN SIZE_T Size){ /* Set up the Flat Buffer start, current and ending position */ FlatBuffer->Buffer = Buffer; FlatBuffer->BufferPos = (ULONG_PTR)Buffer; FlatBuffer->BufferEnd = (PVOID)(FlatBuffer->BufferPos + Size);
/* Setup the current size and the available size */ FlatBuffer->BufferSize = FlatBuffer->BufferFreeSize = Size;}
PVOIDWINAPIFlatBuf_Arg_Reserve(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN SIZE_T Size, IN ULONG Align){ ULONG_PTR NewPosition, OldPosition = *Position; SIZE_T NewFreeSize = *FreeSize;
/* Start by aligning our position */ if (Align) OldPosition += (Align - 1) & ~Align;
/* Update it */ NewPosition = OldPosition + Size;
/* Update Free Size */ NewFreeSize += (OldPosition - NewPosition);
/* Save new values */ *Position = NewPosition; *FreeSize = NewFreeSize;
/* Check if we're out of space or not */ if (NewFreeSize > 0) return (PVOID)OldPosition; return NULL;}
PVOIDWINAPIFlatBuf_Arg_CopyMemory(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN PVOID Buffer, IN SIZE_T Size, IN ULONG Align){ PVOID Destination;
/* First reserve the memory */ Destination = FlatBuf_Arg_Reserve(Position, FreeSize, Size, Align); if (Destination) { /* We have space, do the copy */ RtlCopyMemory(Destination, Buffer, Size); }
/* Return the pointer to the data */ return Destination;}
PVOIDWINAPIFlatBuf_Arg_WriteString(IN OUT PULONG_PTR Position, IN OUT PSIZE_T FreeSize, IN PVOID String, IN BOOLEAN IsUnicode){ PVOID Destination; SIZE_T StringLength; ULONG Align;
/* Calculate the string length */ if (IsUnicode) { /* Get the length in bytes and use WCHAR alignment */ StringLength = (wcslen((LPWSTR)String) + 1) * sizeof(WCHAR); Align = sizeof(WCHAR); } else { /* Get the length in bytes and use CHAR alignment */ StringLength = strlen((LPSTR)String) + 1; Align = sizeof(CHAR); }
/* Now reserve the memory */ Destination = FlatBuf_Arg_Reserve(Position, FreeSize, StringLength, Align); if (Destination) { /* We have space, do the copy */ RtlCopyMemory(Destination, String, StringLength); }
/* Return the pointer to the data */ return Destination;}