ReactOS  0.4.14-dev-583-g2a1ba2c
marshalling.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Printing Stack Marshalling Functions
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Marshalling functions
5  * COPYRIGHT: Copyright 2015-2018 Colin Finck (colin@reactos.org)
6  */
7 
8 #define WIN32_NO_STATUS
9 #include <windef.h>
10 #include <winbase.h>
11 
13 
38 MarshallDownStructure(PVOID pStructure, const MARSHALLING_INFO* pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
39 {
40  // Sanity checks
41  if (!pStructure || !pInfo)
42  {
44  return FALSE;
45  }
46 
47  // Loop until we reach an element with offset set to MAXDWORD.
48  while (pInfo->dwOffset != MAXDWORD)
49  {
50  PULONG_PTR pCurrentField = (PULONG_PTR)((PBYTE)pStructure + pInfo->dwOffset);
51 
52  if (pInfo->bAdjustAddress && *pCurrentField)
53  {
54  // Make a relative offset out of the absolute pointer address.
55  *pCurrentField -= (ULONG_PTR)pStructure;
56  }
57 
58  // Advance to the next field description.
59  pInfo++;
60  }
61 
62  return TRUE;
63 }
64 
91 MarshallDownStructuresArray(PVOID pStructuresArray, DWORD cElements, const MARSHALLING_INFO* pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
92 {
93  PBYTE pCurrentElement = pStructuresArray;
94 
95  // Call MarshallDownStructure on all array elements given by cElements of cbStructureSize.
96  while (cElements--)
97  {
98  if (!MarshallDownStructure(pCurrentElement, pInfo, cbStructureSize, bSomeBoolean))
99  return FALSE;
100 
101  // Advance to the next array element.
102  pCurrentElement += cbStructureSize;
103  }
104 
105  return TRUE;
106 }
107 
136 BOOL WINAPI
137 MarshallUpStructure(DWORD cbSize, PVOID pStructure, const MARSHALLING_INFO* pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
138 {
139  // Sanity checks
140  if (!pStructure || !pInfo)
141  {
143  return FALSE;
144  }
145 
146  // Loop until we reach an element with offset set to MAXDWORD.
147  while (pInfo->dwOffset != MAXDWORD)
148  {
149  PULONG_PTR pCurrentField = (PULONG_PTR)((PBYTE)pStructure + pInfo->dwOffset);
150 
151  if (pInfo->bAdjustAddress && *pCurrentField)
152  {
153  // Verify that the offset in the current field is within the bounds given by cbSize.
154  if (cbSize <= *pCurrentField)
155  {
157  return FALSE;
158  }
159 
160  // Make an absolute pointer address out of the relative offset.
161  *pCurrentField += (ULONG_PTR)pStructure;
162  }
163 
164  // Advance to the next field description.
165  pInfo++;
166  }
167 
168  return TRUE;
169 }
170 
201 BOOL WINAPI
202 MarshallUpStructuresArray(DWORD cbSize, PVOID pStructuresArray, DWORD cElements, const MARSHALLING_INFO* pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
203 {
204  PBYTE pCurrentElement = pStructuresArray;
205 
206  // Call MarshallUpStructure on all array elements given by cElements of cbStructureSize.
207  while (cElements--)
208  {
209  if (!MarshallUpStructure(cbSize, pCurrentElement, pInfo, cbStructureSize, bSomeBoolean))
210  return FALSE;
211 
212  // Advance to the next array element.
213  pCurrentElement += cbStructureSize;
214  }
215 
216  return TRUE;
217 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define TRUE
Definition: types.h:120
unsigned int BOOL
Definition: ntddk_ex.h:94
#define MAXDWORD
#define WINAPI
Definition: msvc.h:6
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
BOOL WINAPI MarshallDownStructure(PVOID pStructure, const MARSHALLING_INFO *pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
Definition: marshalling.c:38
BOOL WINAPI MarshallUpStructure(DWORD cbSize, PVOID pStructure, const MARSHALLING_INFO *pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
Definition: marshalling.c:137
#define ERROR_INVALID_DATA
Definition: winerror.h:116
#define ULONG_PTR
Definition: config.h:101
uint32_t * PULONG_PTR
Definition: typedefs.h:63
BYTE * PBYTE
Definition: pedump.c:66
BOOL WINAPI MarshallDownStructuresArray(PVOID pStructuresArray, DWORD cElements, const MARSHALLING_INFO *pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
Definition: marshalling.c:91
BOOL WINAPI MarshallUpStructuresArray(DWORD cbSize, PVOID pStructuresArray, DWORD cElements, const MARSHALLING_INFO *pInfo, DWORD cbStructureSize, BOOL bSomeBoolean)
Definition: marshalling.c:202