Source code of Windows XP (NT5)
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.

501 lines
9.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: ntumrshl.cxx
  7. //
  8. // Contents:
  9. //
  10. // Functions:
  11. //
  12. //
  13. // History: 17-June-1996 RamV Created.
  14. //
  15. //
  16. // Notes : Need to add functionality to do NulledString to NTTYPE
  17. // conversions
  18. //
  19. //
  20. //----------------------------------------------------------------------------
  21. #include "winnt.hxx"
  22. #pragma hdrstop
  23. #define INITGUID
  24. HRESULT
  25. NTTypeInit(
  26. PNTOBJECT pNtType
  27. )
  28. {
  29. memset(pNtType, 0, sizeof(NTOBJECT));
  30. RRETURN(S_OK);
  31. }
  32. HRESULT
  33. NTTypeClear(
  34. PNTOBJECT pNtObject
  35. )
  36. {
  37. if(!pNtObject)
  38. RRETURN(S_OK);
  39. switch (pNtObject->NTType) {
  40. case NT_SYNTAX_ID_LPTSTR:
  41. case NT_SYNTAX_ID_DelimitedString:
  42. case NT_SYNTAX_ID_EncryptedString:
  43. FreeADsStr((pNtObject->NTValue).pszValue);
  44. break;
  45. case NT_SYNTAX_ID_NulledString:
  46. FreeADsMem((pNtObject->NTValue).pszValue);
  47. break;
  48. case NT_SYNTAX_ID_OCTETSTRING:
  49. FreeADsMem((pNtObject->NTValue).octetstring.pByte);
  50. break;
  51. default:
  52. break;
  53. }
  54. RRETURN(S_OK);
  55. }
  56. void
  57. NTTypeFreeNTObjects(
  58. PNTOBJECT pNtObject,
  59. DWORD dwNumValues
  60. )
  61. {
  62. DWORD i = 0;
  63. if (!pNtObject) // just in case
  64. return;
  65. for (i = 0; i < dwNumValues; i++ ) {
  66. NTTypeClear(pNtObject + i);
  67. }
  68. FreeADsMem(pNtObject);
  69. pNtObject = NULL;
  70. return;
  71. }
  72. HRESULT
  73. CopyOctetToNTOBJECT(
  74. PBYTE pOctetString,
  75. PNTOBJECT lpNtObject
  76. )
  77. {
  78. if(!lpNtObject){
  79. RRETURN(E_POINTER);
  80. }
  81. lpNtObject->NTType = NT_SYNTAX_ID_OCTETSTRING;
  82. if(!pOctetString){
  83. (lpNtObject->NTValue).octetstring.dwSize = 0;
  84. (lpNtObject->NTValue).octetstring.pByte = NULL;
  85. } else {
  86. OctetString *pOctString = (OctetString*)pOctetString;
  87. (lpNtObject->NTValue).octetstring.dwSize = pOctString->dwSize;
  88. (lpNtObject->NTValue).octetstring.pByte = (BYTE*)AllocADsMem(sizeof(BYTE)*pOctString->dwSize);
  89. if (!(lpNtObject->NTValue).octetstring.pByte) {
  90. RRETURN(E_OUTOFMEMORY);
  91. }
  92. memcpy((lpNtObject->NTValue).octetstring.pByte, pOctString->pByte,pOctString->dwSize);
  93. }
  94. RRETURN(S_OK);
  95. }
  96. HRESULT
  97. CopyDWORDToNTOBJECT(
  98. PDWORD pdwSrcValue,
  99. PNTOBJECT lpNtDestValue
  100. )
  101. {
  102. HRESULT hr = S_OK;
  103. if(!lpNtDestValue){
  104. RRETURN(E_POINTER);
  105. }
  106. lpNtDestValue->NTType = NT_SYNTAX_ID_DWORD;
  107. (lpNtDestValue->NTValue).dwValue = *pdwSrcValue;
  108. RRETURN(hr);
  109. }
  110. HRESULT
  111. CopyDATEToNTOBJECT(
  112. PDWORD pdwSrcValue,
  113. PNTOBJECT lpNtDestValue
  114. )
  115. {
  116. HRESULT hr = S_OK;
  117. if(!lpNtDestValue){
  118. RRETURN(E_POINTER);
  119. }
  120. lpNtDestValue->NTType = NT_SYNTAX_ID_DATE;
  121. (lpNtDestValue->NTValue).dwValue = *pdwSrcValue;
  122. RRETURN(hr);
  123. }
  124. HRESULT
  125. CopyBOOLToNTOBJECT(
  126. PBOOL pfSrcValue,
  127. PNTOBJECT lpNtObject
  128. )
  129. {
  130. if(!lpNtObject){
  131. RRETURN(E_POINTER);
  132. }
  133. lpNtObject->NTType = NT_SYNTAX_ID_BOOL;
  134. (lpNtObject->NTValue).fValue = *pfSrcValue;
  135. RRETURN(S_OK);
  136. }
  137. HRESULT
  138. CopySYSTEMTIMEToNTOBJECT(
  139. PSYSTEMTIME pSysTime,
  140. PNTOBJECT lpNtObject
  141. )
  142. {
  143. if(!lpNtObject){
  144. RRETURN(E_POINTER);
  145. }
  146. (lpNtObject->NTValue).stSystemTimeValue = *pSysTime;
  147. lpNtObject->NTType = NT_SYNTAX_ID_SYSTEMTIME;
  148. RRETURN(S_OK);
  149. }
  150. HRESULT
  151. CopyLPTSTRToNTOBJECT(
  152. LPTSTR pszSrcValue,
  153. PNTOBJECT lpNtObject
  154. )
  155. {
  156. if(!lpNtObject){
  157. RRETURN(E_POINTER);
  158. }
  159. lpNtObject->NTType = NT_SYNTAX_ID_LPTSTR;
  160. if(!pszSrcValue){
  161. (lpNtObject->NTValue). pszValue = NULL;
  162. } else {
  163. (lpNtObject->NTValue). pszValue = AllocADsStr(pszSrcValue);
  164. if (!((lpNtObject->NTValue). pszValue)){
  165. RRETURN(E_OUTOFMEMORY);
  166. }
  167. }
  168. RRETURN(S_OK);
  169. }
  170. HRESULT
  171. CopyDelimitedStringToNTOBJECT(
  172. LPTSTR pszSrcValue,
  173. PNTOBJECT lpNtObject,
  174. DWORD dwElements
  175. )
  176. {
  177. LPTSTR pszCurrPos = NULL;
  178. LONG i;
  179. WCHAR Delimiter = L',';
  180. HRESULT hr = S_OK;
  181. if(!pszSrcValue){
  182. RRETURN(E_POINTER);
  183. }
  184. if(!lpNtObject){
  185. RRETURN(E_POINTER);
  186. }
  187. //
  188. // scan string and put the appropriate pointers
  189. //
  190. pszCurrPos = pszSrcValue;
  191. //
  192. // Fill up the NtObject structure
  193. //
  194. lpNtObject[0].NTType = NT_SYNTAX_ID_DelimitedString;
  195. (lpNtObject[0].NTValue).pszValue = AllocADsStr(pszCurrPos);
  196. if (!(lpNtObject[0].NTValue). pszValue){
  197. hr = E_OUTOFMEMORY;
  198. goto error;
  199. }
  200. i = 1;
  201. while(i < (LONG)dwElements){
  202. if(*pszCurrPos == TEXT('\0')){
  203. lpNtObject[i].NTType = NT_SYNTAX_ID_DelimitedString;
  204. (lpNtObject[i].NTValue).pszValue = AllocADsStr(++pszCurrPos);
  205. if (!(lpNtObject[i].NTValue).pszValue){
  206. hr = E_OUTOFMEMORY;
  207. goto error;
  208. }
  209. i++;
  210. }
  211. pszCurrPos++;
  212. }
  213. error:
  214. RRETURN(hr);
  215. }
  216. HRESULT
  217. CopyNulledStringToNTOBJECT(
  218. LPTSTR pszSrcValue,
  219. PNTOBJECT lpNtObject,
  220. DWORD dwElements
  221. )
  222. {
  223. LPTSTR pszCurrPos = NULL;
  224. LONG i;
  225. HRESULT hr = S_OK;
  226. if(!pszSrcValue){
  227. RRETURN(E_POINTER);
  228. }
  229. if(!lpNtObject){
  230. RRETURN(E_POINTER);
  231. }
  232. //
  233. // scan string and put the appropriate pointers
  234. //
  235. pszCurrPos = pszSrcValue;
  236. //
  237. // Fill up the NtObject structure
  238. //
  239. lpNtObject[0].NTType = NT_SYNTAX_ID_NulledString;
  240. (lpNtObject[0].NTValue).pszValue = AllocADsStr(pszCurrPos);
  241. if (!(lpNtObject[0].NTValue). pszValue){
  242. hr = E_OUTOFMEMORY;
  243. goto error;
  244. }
  245. i = 1;
  246. while(i < (LONG)dwElements){
  247. if(*pszCurrPos == TEXT('\0')){
  248. lpNtObject[i].NTType = NT_SYNTAX_ID_NulledString;
  249. (lpNtObject[i].NTValue).pszValue = AllocADsStr(++pszCurrPos);
  250. if (!(lpNtObject[i].NTValue).pszValue){
  251. hr = E_OUTOFMEMORY;
  252. goto error;
  253. }
  254. i++;
  255. }
  256. pszCurrPos++;
  257. }
  258. error:
  259. RRETURN(hr);
  260. }
  261. HRESULT
  262. CopySeconds70ToNTOBJECT(
  263. PDWORD pdwSrcValue,
  264. PNTOBJECT lpNtDestValue
  265. )
  266. {
  267. HRESULT hr = S_OK;
  268. if(!lpNtDestValue){
  269. RRETURN(E_POINTER);
  270. }
  271. lpNtDestValue->NTType = NT_SYNTAX_ID_DATE_1970;
  272. (lpNtDestValue->NTValue).dwSeconds1970 = *pdwSrcValue;
  273. RRETURN(hr);
  274. }
  275. HRESULT
  276. CopyNTToNTSynId(
  277. DWORD dwSyntaxId,
  278. LPBYTE lpByte,
  279. PNTOBJECT lpNTObject,
  280. DWORD dwNumValues
  281. )
  282. {
  283. HRESULT hr = S_OK;
  284. switch (dwSyntaxId) {
  285. case NT_SYNTAX_ID_BOOL:
  286. hr = CopyBOOLToNTOBJECT(
  287. (PBOOL)lpByte,
  288. lpNTObject
  289. );
  290. break;
  291. case NT_SYNTAX_ID_SYSTEMTIME:
  292. hr = CopySYSTEMTIMEToNTOBJECT(
  293. (PSYSTEMTIME)lpByte,
  294. lpNTObject
  295. );
  296. break;
  297. case NT_SYNTAX_ID_DWORD:
  298. hr = CopyDWORDToNTOBJECT(
  299. (PDWORD)lpByte,
  300. lpNTObject
  301. );
  302. break;
  303. case NT_SYNTAX_ID_DelimitedString:
  304. hr = CopyDelimitedStringToNTOBJECT(
  305. (LPTSTR)lpByte,
  306. lpNTObject,
  307. dwNumValues
  308. );
  309. break;
  310. case NT_SYNTAX_ID_NulledString:
  311. hr = CopyNulledStringToNTOBJECT(
  312. (LPTSTR)lpByte,
  313. lpNTObject,
  314. dwNumValues
  315. );
  316. break;
  317. case NT_SYNTAX_ID_LPTSTR:
  318. hr = CopyLPTSTRToNTOBJECT(
  319. (LPTSTR)lpByte,
  320. lpNTObject
  321. );
  322. break;
  323. case NT_SYNTAX_ID_DATE_1970:
  324. hr = CopySeconds70ToNTOBJECT(
  325. (PDWORD)lpByte,
  326. lpNTObject
  327. );
  328. break;
  329. case NT_SYNTAX_ID_DATE :
  330. hr = CopyDATEToNTOBJECT(
  331. (PDWORD)lpByte,
  332. lpNTObject
  333. );
  334. break;
  335. case NT_SYNTAX_ID_OCTETSTRING:
  336. hr = CopyOctetToNTOBJECT(
  337. lpByte,
  338. lpNTObject
  339. );
  340. break;
  341. default:
  342. break;
  343. }
  344. RRETURN(hr);
  345. }
  346. //
  347. // NT is single-valued except for Delimited and Nulled Strings
  348. //
  349. HRESULT
  350. UnMarshallNTToNTSynId(
  351. DWORD dwSyntaxId,
  352. DWORD dwNumValues,
  353. LPBYTE lpValue,
  354. PNTOBJECT * ppNTObject
  355. )
  356. {
  357. LPBYTE lpByte = lpValue;
  358. DWORD i = 0;
  359. PNTOBJECT pNTObject = NULL;
  360. HRESULT hr = S_OK;
  361. pNTObject = (PNTOBJECT)AllocADsMem(
  362. sizeof(NTOBJECT)*dwNumValues
  363. );
  364. if (!pNTObject) {
  365. RRETURN(E_FAIL);
  366. }
  367. hr = CopyNTToNTSynId(
  368. dwSyntaxId,
  369. lpByte,
  370. pNTObject,
  371. dwNumValues
  372. );
  373. BAIL_ON_FAILURE(hr);
  374. *ppNTObject = pNTObject;
  375. RRETURN(hr);
  376. error:
  377. if(pNTObject){
  378. NTTypeFreeNTObjects(pNTObject, dwNumValues);
  379. }
  380. RRETURN(hr);
  381. }