Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
15 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1992, Microsoft Corporation
  4. //
  5. // File: DfsMrshl.h
  6. //
  7. // Contents: Defines for Dfs marshalling routines
  8. //
  9. // Classes:
  10. //
  11. // Functions:
  12. //
  13. // History: March 29, 1994 Milans Created from Peterco's dfsrtl.h
  14. //
  15. //-----------------------------------------------------------------------------
  16. #ifndef _DFSMRSHL_
  17. #define _DFSMRSHL_
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include <stddef.h>
  22. #include <guiddef.h>
  23. //
  24. // MARSHALLING AND UNMARSHALLING SUPPORT
  25. //
  26. #ifdef KERNEL_MODE
  27. #define MarshalBufferAllocate(x) ExAllocatePoolWithTag(PagedPool, x, ' puM')
  28. #define MarshalBufferFree(x) ExFreePool(x)
  29. #else
  30. #include <stdlib.h>
  31. #define MarshalBufferAllocate(x) malloc(x)
  32. #define MarshalBufferFree(x) free(x)
  33. #endif // KERNEL_MODE
  34. #define DfsAllocate MarshalBufferAllocate
  35. #define DfsFree MarshalBufferFree
  36. //
  37. // Structure used when marshalling and unmarhalling
  38. //
  39. typedef struct _MARSHAL_BUFFER {
  40. PUCHAR First;
  41. PUCHAR Current;
  42. PUCHAR Last;
  43. } MARSHAL_BUFFER, *PMARSHAL_BUFFER;
  44. typedef struct _MARSHAL_TYPE_INFO {
  45. ULONG _type; // the type of item to be marshalled
  46. ULONG _off; // offset of item (in the struct)
  47. ULONG _cntsize; // size of counter for counted array
  48. ULONG _cntoff; // else, offset count item (in the struct)
  49. struct _MARSHAL_INFO * _subinfo;// if compound type, need info
  50. } MARSHAL_TYPE_INFO, *PMARSHAL_TYPE_INFO;
  51. typedef struct _MARSHAL_INFO {
  52. ULONG _size; // size of item
  53. ULONG _typecnt; // number of type infos
  54. PMARSHAL_TYPE_INFO _typeInfo; // type infos
  55. } MARSHAL_INFO, *PMARSHAL_INFO;
  56. #define _mkMarshalInfo(s, i)\
  57. {(ULONG)sizeof(s),(ULONG)(sizeof(i)/sizeof(MARSHAL_TYPE_INFO)),i}
  58. #define MTYPE_BASE_TYPE (0x0000ffffL)
  59. #define MTYPE_COMPOUND (0x00000001L)
  60. #define MTYPE_GUID (0x00000002L)
  61. #define MTYPE_STRING (0x00000003L)
  62. #define MTYPE_UNICODE_STRING (0x00000004L)
  63. #define MTYPE_ULONG (0x00000005L)
  64. #define MTYPE_USHORT (0x00000006L)
  65. #define MTYPE_PWSTR (0x00000007L)
  66. #define MTYPE_UCHAR (0x00000008L)
  67. #define MTYPE_CONFORMANT_CNT (0x00000009L)
  68. #define MTYPE_INDIRECT (0x80000000L)
  69. #define MTYPE_COMPLEX_TYPE (0x7fff0000L)
  70. #define MTYPE_STATIC_ARRAY (0x00010000L)
  71. #define MTYPE_COUNTED_ARRAY (0x00020000L)
  72. #define _MCode_conformant(s,m,c)\
  73. {MTYPE_CONFORMANT_CNT, sizeof(((s *) 0)->m[0]), sizeof(((s *) 0)->c), offsetof(s,c), 0L}
  74. #define _MCode_Base(t,s,m,i)\
  75. {t,offsetof(s,m),0L,0L,i}
  76. #define _MCode_struct(s,m,i)\
  77. _MCode_Base(MTYPE_COMPOUND,s,m,i)
  78. #define _MCode_guid(s,m)\
  79. _MCode_Base(MTYPE_GUID,s,m,NULL)
  80. #define _MCode_str(s,m)\
  81. _MCode_Base(MTYPE_STRING,s,m,NULL)
  82. #define _MCode_ustr(s,m)\
  83. _MCode_Base(MTYPE_UNICODE_STRING,s,m,NULL)
  84. #define _MCode_pwstr(s,m)\
  85. _MCode_Base(MTYPE_PWSTR,s,m,NULL)
  86. #define _MCode_ul(s,m)\
  87. _MCode_Base(MTYPE_ULONG,s,m,NULL)
  88. #define _MCode_ush(s,m)\
  89. _MCode_Base(MTYPE_USHORT,s,m,NULL)
  90. #define _MCode_uch(s,m)\
  91. _MCode_Base(MTYPE_UCHAR,s,m,NULL)
  92. #define _MCode_pstruct(s,m,i)\
  93. _MCode_Base(MTYPE_COMPOUND|MTYPE_INDIRECT,s,m,i)
  94. #define _MCode_pguid(s,m)\
  95. _MCode_Base(MTYPE_GUID|MTYPE_INDIRECT,s,m,NULL)
  96. #define _MCode_pstr(s,m)\
  97. _MCode_Base(MTYPE_STRING|MTYPE_INDIRECT,s,m,NULL)
  98. #define _MCode_pustr(s,m)\
  99. _MCode_Base(MTYPE_UNICODE_STRING|MTYPE_INDIRECT,s,m,NULL)
  100. #define _MCode_pul(s,m)\
  101. _MCode_Base(MTYPE_ULONG|MTYPE_INDIRECT,s,m,NULL)
  102. #define _MCode_push(s,m)\
  103. _MCode_Base(MTYPE_USHORT|MTYPE_INDIRECT,s,m,NULL)
  104. #define _MCode_puch(s,m)\
  105. _MCode_Base(MTYPE_UCHAR|MTYPE_INDIRECT,s,m,NULL)
  106. #define _MCode_aStatic(t,s,m,c,i)\
  107. {t|MTYPE_STATIC_ARRAY,offsetof(s,m),0L,c,i}
  108. #define _MCode_astruct(s,m,c,i)\
  109. _MCode_aStatic(MTYPE_COMPOUND,s,m,c,i)
  110. #define _MCode_aguid(s,m,c)\
  111. _MCode_aStatic(MTYPE_GUID,s,m,c,NULL)
  112. #define _MCode_astr(s,m,c)\
  113. _MCode_aStatic(MTYPE_STRING,s,m,c,NULL)
  114. #define _MCode_austr(s,m,c)\
  115. _MCode_aStatic(MTYPE_UNICODE_STRING,s,m,c,NULL)
  116. #define _MCode_aul(s,m,c)\
  117. _MCode_aStatic(MTYPE_ULONG,s,m,c,NULL)
  118. #define _MCode_aush(s,m,c)\
  119. _MCode_aStatic(MTYPE_USHORT,s,m,c,NULL)
  120. #define _MCode_auch(s,m,c)\
  121. _MCode_aStatic(MTYPE_UCHAR,s,m,c,NULL)
  122. #define _MCode_pastruct(s,m,c,i)\
  123. _MCode_aStatic(MTYPE_COMPOUND|MTYPE_INDIRECT,s,m,c,i)
  124. #define _MCode_paguid(s,m,c)\
  125. _MCode_aStatic(MTYPE_GUID|MTYPE_INDIRECT,s,m,c,NULL)
  126. #define _MCode_pastr(s,m,c)\
  127. _MCode_aStatic(MTYPE_STRING|MTYPE_INDIRECT,s,m,c,NULL)
  128. #define _MCode_paustr(s,m,c)\
  129. _MCode_aStatic(MTYPE_UNICODE_STRING|MTYPE_INDIRECT,s,m,c,NULL)
  130. #define _MCode_paul(s,m,c)\
  131. _MCode_aStatic(MTYPE_ULONG|MTYPE_INDIRECT,s,m,c,NULL)
  132. #define _MCode_paush(s,m,c)\
  133. _MCode_aStatic(MTYPE_USHORT|MTYPE_INDIRECT,s,m,c,NULL)
  134. #define _MCode_pauch(s,m,c)\
  135. _MCode_aStatic(MTYPE_UCHAR|MTYPE_INDIRECT,s,m,c,NULL)
  136. #define _MCode_aCounted(t,s,m,c,i) {\
  137. t|MTYPE_COUNTED_ARRAY,\
  138. offsetof(s,m),\
  139. sizeof(((s *)0)->c),\
  140. offsetof(s,c),\
  141. i\
  142. }
  143. #define _MCode_castruct(s,m,c,i)\
  144. _MCode_aCounted(MTYPE_COMPOUND,s,m,c,i)
  145. #define _MCode_caguid(s,m,c)\
  146. _MCode_aCounted(MTYPE_GUID,s,m,c,NULL)
  147. #define _MCode_capwstr(s,m,c)\
  148. _MCode_aCounted(MTYPE_PWSTR,s,m,c,NULL)
  149. #define _MCode_castr(s,m,c)\
  150. _MCode_aCounted(MTYPE_STRING,s,m,c,NULL)
  151. #define _MCode_caustr(s,m,c)\
  152. _MCode_aCounted(MTYPE_UNICODE_STRING,s,m,c,NULL)
  153. #define _MCode_caul(s,m,c)\
  154. _MCode_aCounted(MTYPE_ULONG,s,m,c,NULL)
  155. #define _MCode_caush(s,m,c)\
  156. _MCode_aCounted(MTYPE_USHORT,s,m,c,NULL)
  157. #define _MCode_cauch(s,m,c)\
  158. _MCode_aCounted(MTYPE_UCHAR,s,m,c,NULL)
  159. #define _MCode_pcastruct(s,m,c,i)\
  160. _MCode_aCounted(MTYPE_COMPOUND|MTYPE_INDIRECT,s,m,c,i)
  161. #define _MCode_pcaguid(s,m,c)\
  162. _MCode_aCounted(MTYPE_GUID|MTYPE_INDIRECT,s,m,c,NULL)
  163. #define _MCode_pcapwstr(s,m,c)\
  164. _MCode_aCounted(MTYPE_PWSTR|MTYPE_INDIRECT,s,m,c,NULL)
  165. #define _MCode_pcastr(s,m,c)\
  166. _MCode_aCounted(MTYPE_STRING|MTYPE_INDIRECT,s,m,c,NULL)
  167. #define _MCode_pcaustr(s,m,c)\
  168. _MCode_aCounted(MTYPE_UNICODE_STRING|MTYPE_INDIRECT,s,m,c,NULL)
  169. #define _MCode_pcaul(s,m,c)\
  170. _MCode_aCounted(MTYPE_ULONG|MTYPE_INDIRECT,s,m,c,NULL)
  171. #define _MCode_pcaush(s,m,c)\
  172. _MCode_aCounted(MTYPE_USHORT|MTYPE_INDIRECT,s,m,c,NULL)
  173. #define MarshalBufferInitialize( MarshalBuffer, BufferLength, Buffer ) {\
  174. (MarshalBuffer)->First = (PUCHAR)(Buffer); \
  175. (MarshalBuffer)->Current = (PUCHAR)(Buffer); \
  176. (MarshalBuffer)->Last = &(MarshalBuffer)->Current[(BufferLength)]; \
  177. }
  178. //
  179. // Defines and functions for unmarshalling base types.
  180. // The BYTE masks are perfectly fine and they dont care if
  181. // we are on working on LITTLE_ENDIAN or BIG_ENDIAN etc.
  182. //
  183. #define BYTE_0_MASK 0xFF
  184. #define BYTE_0(Value) (UCHAR)( (Value) & BYTE_0_MASK)
  185. #define BYTE_1(Value) (UCHAR)( ((Value) >> 8) & BYTE_0_MASK)
  186. #define BYTE_2(Value) (UCHAR)( ((Value) >> 16) & BYTE_0_MASK)
  187. #define BYTE_3(Value) (UCHAR)( ((Value) >> 24) & BYTE_0_MASK)
  188. #define DfsRtlGetUshort(MarshalBuffer, pValue) ( \
  189. ((MarshalBuffer)->Current + 2 <= (MarshalBuffer)->Last) ? \
  190. *(pValue) = (USHORT)((MarshalBuffer)->Current[0] ) | \
  191. ((MarshalBuffer)->Current[1] << 8), \
  192. (MarshalBuffer)->Current += 2, \
  193. STATUS_SUCCESS \
  194. : STATUS_DATA_ERROR \
  195. )
  196. #define DfsRtlPutUshort(MarshalBuffer, pValue) ( \
  197. ((MarshalBuffer)->Current + 2 <= (MarshalBuffer)->Last) ? \
  198. (MarshalBuffer)->Current[0] = BYTE_0(*pValue), \
  199. (MarshalBuffer)->Current[1] = BYTE_1(*pValue), \
  200. (MarshalBuffer)->Current += 2, \
  201. STATUS_SUCCESS \
  202. : STATUS_BUFFER_TOO_SMALL \
  203. )
  204. #define DfsRtlGetUlong(MarshalBuffer, pValue) ( \
  205. ((MarshalBuffer)->Current + 4 <= (MarshalBuffer)->Last) ? \
  206. *(pValue) = (ULONG) ((MarshalBuffer)->Current[0] ) | \
  207. ((MarshalBuffer)->Current[1] << 8) | \
  208. ((MarshalBuffer)->Current[2] << 16) | \
  209. ((MarshalBuffer)->Current[3] << 24), \
  210. (MarshalBuffer)->Current += 4, \
  211. STATUS_SUCCESS \
  212. : STATUS_DATA_ERROR \
  213. )
  214. #define DfsRtlPutUlong(MarshalBuffer, pValue) ( \
  215. ((MarshalBuffer)->Current + 4 <= (MarshalBuffer)->Last) ? \
  216. (MarshalBuffer)->Current[0] = BYTE_0(*pValue), \
  217. (MarshalBuffer)->Current[1] = BYTE_1(*pValue), \
  218. (MarshalBuffer)->Current[2] = BYTE_2(*pValue), \
  219. (MarshalBuffer)->Current[3] = BYTE_3(*pValue), \
  220. (MarshalBuffer)->Current += 4, \
  221. STATUS_SUCCESS \
  222. : STATUS_BUFFER_TOO_SMALL \
  223. )
  224. #define DfsRtlGetGuid(MarshalBuffer, pValue) ( \
  225. ((MarshalBuffer)->Current + 16 <= (MarshalBuffer)->Last) ? \
  226. (pValue)->Data1 = (ULONG) ((MarshalBuffer)->Current[0] ) | \
  227. ((MarshalBuffer)->Current[1] << 8) | \
  228. ((MarshalBuffer)->Current[2] << 16) | \
  229. ((MarshalBuffer)->Current[3] << 24) , \
  230. (pValue)->Data2 = (USHORT)((MarshalBuffer)->Current[4] ) | \
  231. ((MarshalBuffer)->Current[5] << 8) , \
  232. (pValue)->Data3 = (USHORT)((MarshalBuffer)->Current[6] ) | \
  233. ((MarshalBuffer)->Current[7] << 8) , \
  234. memcpy((pValue)->Data4, &(MarshalBuffer)->Current[8], 8), \
  235. (MarshalBuffer)->Current += 16, \
  236. STATUS_SUCCESS \
  237. : STATUS_DATA_ERROR \
  238. )
  239. //
  240. // This routine is being used for DFS_UPD_REFERRAL_BUFFER. These
  241. // routines will continue to be used in the future as well since
  242. // we want to keep the structure of DFS_UPD_REFERRAL_BUFFER well
  243. // defined rather than using the Marshalling routines provided here.
  244. //
  245. #define _PutGuid(cp, pguid) \
  246. cp[0] = BYTE_0((pguid)->Data1), \
  247. cp[1] = BYTE_1((pguid)->Data1), \
  248. cp[2] = BYTE_2((pguid)->Data1), \
  249. cp[3] = BYTE_3((pguid)->Data1), \
  250. cp[4] = BYTE_0((pguid)->Data2), \
  251. cp[5] = BYTE_1((pguid)->Data2), \
  252. cp[6] = BYTE_0((pguid)->Data3), \
  253. cp[7] = BYTE_1((pguid)->Data3), \
  254. memcpy(&cp[8], (pguid)->Data4, 8)
  255. #define _PutULong(cp, ularg) \
  256. cp[0] = BYTE_0(ularg), \
  257. cp[1] = BYTE_1(ularg), \
  258. cp[2] = BYTE_2(ularg), \
  259. cp[3] = BYTE_3(ularg)
  260. #define _GetULong(cp, ularg) \
  261. ularg = (ULONG) (cp[0]) | \
  262. (cp[1] << 8) | \
  263. (cp[2] << 16) | \
  264. (cp[3] << 24)
  265. #define DfsRtlPutGuid(MarshalBuffer, pValue) ( \
  266. ((MarshalBuffer)->Current + 16 <= (MarshalBuffer)->Last) ? \
  267. (MarshalBuffer)->Current[0] = BYTE_0((pValue)->Data1), \
  268. (MarshalBuffer)->Current[1] = BYTE_1((pValue)->Data1), \
  269. (MarshalBuffer)->Current[2] = BYTE_2((pValue)->Data1), \
  270. (MarshalBuffer)->Current[3] = BYTE_3((pValue)->Data1), \
  271. (MarshalBuffer)->Current[4] = BYTE_0((pValue)->Data2), \
  272. (MarshalBuffer)->Current[5] = BYTE_1((pValue)->Data2), \
  273. (MarshalBuffer)->Current[6] = BYTE_0((pValue)->Data3), \
  274. (MarshalBuffer)->Current[7] = BYTE_1((pValue)->Data3), \
  275. memcpy(&(MarshalBuffer)->Current[8], (pValue)->Data4, 8), \
  276. (MarshalBuffer)->Current += 16, \
  277. STATUS_SUCCESS \
  278. : STATUS_BUFFER_TOO_SMALL \
  279. )
  280. #define DfsRtlSizeString(pString, pSize) ( \
  281. ((pString)->Length > 0) ? ( \
  282. ((pString)->Buffer != NULL) ? \
  283. (*(pSize)) += (2 + (pString)->Length), \
  284. STATUS_SUCCESS \
  285. : STATUS_DATA_ERROR \
  286. ) \
  287. : ((*(pSize)) += 2, \
  288. STATUS_SUCCESS) \
  289. )
  290. #define DfsRtlSizepwString(pString, pSize) ( \
  291. (*pString != NULL) ? \
  292. (*(pSize)) += ((1 + wcslen(*pString))*sizeof(WCHAR)), \
  293. STATUS_SUCCESS \
  294. : ((*(pSize)) += 2, \
  295. STATUS_SUCCESS) \
  296. )
  297. #define DfsRtlSizeUnicodeString(pUnicodeString, pSize) \
  298. DfsRtlSizeString(pUnicodeString, pSize)
  299. NTSTATUS
  300. DfsRtlGet(
  301. IN OUT PMARSHAL_BUFFER MarshalBuffer,
  302. IN PMARSHAL_INFO MarshalInfo,
  303. OUT PVOID Item
  304. );
  305. NTSTATUS
  306. DfsRtlPut(
  307. IN OUT PMARSHAL_BUFFER MarshalBuffer,
  308. IN PMARSHAL_INFO MarshalInfo,
  309. OUT PVOID Item
  310. );
  311. NTSTATUS
  312. DfsRtlSize(
  313. IN PMARSHAL_INFO MarshalInfo,
  314. IN PVOID Item,
  315. OUT PULONG Size
  316. );
  317. VOID
  318. DfsRtlUnwindGet(
  319. IN PMARSHAL_INFO MarshalInfo,
  320. IN PMARSHAL_TYPE_INFO LastTypeInfo,
  321. IN PVOID Item
  322. );
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif // _DFSMRSHL_