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.

1409 lines
29 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. WsbPort.cpp
  5. Abstract:
  6. Macros, functions, and classes to support portability.
  7. Author:
  8. Ron White [ronw] 19-Dec-1996
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "wsbport.h"
  13. HRESULT
  14. WsbConvertFromBytes(
  15. UCHAR* pBytes,
  16. BOOL* pValue,
  17. ULONG* pSize
  18. )
  19. /*++
  20. Routine Description:
  21. Convert a BOOL value from a string of bytes. Useful
  22. for stream portability and creating WsbDbKey values.
  23. Arguments:
  24. pBytes - The source byte array.
  25. pValue - Pointer to the returned value.
  26. pSize - Returns the number of bytes used. Can be NULL.
  27. Return Value:
  28. S_OK - Success
  29. E_POINTER - pBytes or pValue was NULL.
  30. --*/
  31. {
  32. HRESULT hr = S_OK;
  33. WsbTraceIn(OLESTR("WsbConvertFromBytes(BOOL)"), OLESTR(""));
  34. try {
  35. WsbAssert(0 != pBytes, E_POINTER);
  36. WsbAssert(0 != pValue, E_POINTER);
  37. WsbAssert(1 == WSB_BYTE_SIZE_BOOL, E_UNEXPECTED);
  38. if (*pBytes) {
  39. *pValue = TRUE;
  40. } else {
  41. *pValue = FALSE;
  42. }
  43. if (pSize) {
  44. *pSize = WSB_BYTE_SIZE_BOOL;
  45. }
  46. } WsbCatch(hr);
  47. WsbTraceOut(OLESTR("WsbConvertFromBytes(BOOL)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  48. return(hr);
  49. }
  50. HRESULT
  51. WsbConvertFromBytes(
  52. UCHAR* pBytes,
  53. GUID* pValue,
  54. ULONG* pSize
  55. )
  56. /*++
  57. Routine Description:
  58. Convert a GUID value from a string of bytes. Useful
  59. for stream portability and creating WsbDbKey values.
  60. Arguments:
  61. pBytes - The source byte array (must at least 16 bytes long).
  62. pValue - Pointer to the returned value.
  63. pSize - Returns the number of bytes used. Can be NULL.
  64. Return Value:
  65. S_OK - Success
  66. E_POINTER - pBytes or pValue was NULL.
  67. --*/
  68. {
  69. HRESULT hr = S_OK;
  70. WsbTraceIn(OLESTR("WsbConvertFromBytes(GUID)"), OLESTR(""));
  71. try {
  72. ULONG lsize;
  73. ULONG tsize;
  74. WsbAssert(0 != pBytes, E_POINTER);
  75. WsbAssert(0 != pValue, E_POINTER);
  76. WsbAffirmHr(WsbConvertFromBytes(pBytes, &pValue->Data1, &lsize));
  77. tsize = lsize;
  78. WsbAffirmHr(WsbConvertFromBytes(pBytes + tsize, &pValue->Data2, &lsize));
  79. tsize += lsize;
  80. WsbAffirmHr(WsbConvertFromBytes(pBytes + tsize, &pValue->Data3, &lsize));
  81. tsize += lsize;
  82. memcpy(pValue->Data4, pBytes + tsize, 8);
  83. tsize += 8;
  84. if (pSize) {
  85. *pSize = tsize;
  86. }
  87. } WsbCatch(hr);
  88. WsbTraceOut(OLESTR("WsbConvertFromBytes(GUID)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  89. return(hr);
  90. }
  91. HRESULT
  92. WsbConvertFromBytes(
  93. UCHAR* pBytes,
  94. LONG* pValue,
  95. ULONG* pSize
  96. )
  97. /*++
  98. Routine Description:
  99. Convert a LONG value from a string of bytes. Useful
  100. for stream portability and creating WsbDbKey values.
  101. Arguments:
  102. pBytes - The source byte array (must at least 4 bytes long).
  103. pValue - Pointer to the returned value.
  104. pSize - Returns the number of bytes used. Can be NULL.
  105. Return Value:
  106. S_OK - Success
  107. E_POINTER - pBytes or pValue was NULL.
  108. --*/
  109. {
  110. HRESULT hr = S_OK;
  111. WsbTraceIn(OLESTR("WsbConvertFromBytes(LONG)"), OLESTR(""));
  112. try {
  113. WsbAssert(0 != pBytes, E_POINTER);
  114. WsbAssert(0 != pValue, E_POINTER);
  115. WsbAssert(4 == WSB_BYTE_SIZE_LONG, E_UNEXPECTED);
  116. *pValue = (pBytes[0] << 24) | (pBytes[1] << 16) |
  117. (pBytes[2] << 8) | pBytes[3];
  118. if (pSize) {
  119. *pSize = WSB_BYTE_SIZE_LONG;
  120. }
  121. } WsbCatch(hr);
  122. WsbTraceOut(OLESTR("WsbConvertFromBytes(LONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  123. return(hr);
  124. }
  125. HRESULT
  126. WsbConvertFromBytes(
  127. UCHAR* pBytes,
  128. LONGLONG* pValue,
  129. ULONG* pSize
  130. )
  131. /*++
  132. Routine Description:
  133. Convert a LONGLONG value from a string of bytes. Useful
  134. for stream portability and creating WsbDbKey values.
  135. Arguments:
  136. pBytes - The source byte array (must at least 8 bytes long).
  137. pValue - Pointer to the returned value.
  138. pSize - Returns the number of bytes used. Can be NULL.
  139. Return Value:
  140. S_OK - Success
  141. E_POINTER - pBytes or pValue was NULL.
  142. --*/
  143. {
  144. HRESULT hr = S_OK;
  145. WsbTraceIn(OLESTR("WsbConvertFromBytes(LONGLONG)"), OLESTR(""));
  146. try {
  147. ULONG size;
  148. ULONG total = 0;
  149. ULONG ul;
  150. LONGLONG ll;
  151. WsbAssert(0 != pBytes, E_POINTER);
  152. WsbAssert(0 != pValue, E_POINTER);
  153. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  154. total += size;
  155. pBytes += size;
  156. ll = (LONGLONG) ul;
  157. *pValue = ll << 32;
  158. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  159. total += size;
  160. ll = (LONGLONG) ul;
  161. *pValue |= ll;
  162. if (pSize) {
  163. *pSize = total;
  164. }
  165. } WsbCatch(hr);
  166. WsbTraceOut(OLESTR("WsbConvertFromBytes(LONGLONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  167. return(hr);
  168. }
  169. HRESULT
  170. WsbConvertFromBytes(
  171. UCHAR* pBytes,
  172. ULONGLONG* pValue,
  173. ULONG* pSize
  174. )
  175. /*++
  176. Routine Description:
  177. Convert a ULONGLONG value from a string of bytes. Useful
  178. for stream portability and creating WsbDbKey values.
  179. Arguments:
  180. pBytes - The source byte array (must at least 8 bytes long).
  181. pValue - Pointer to the returned value.
  182. pSize - Returns the number of bytes used. Can be NULL.
  183. Return Value:
  184. S_OK - Success
  185. E_POINTER - pBytes or pValue was NULL.
  186. --*/
  187. {
  188. HRESULT hr = S_OK;
  189. WsbTraceIn(OLESTR("WsbConvertFromBytes(ULONGLONG)"), OLESTR(""));
  190. try {
  191. ULONG size;
  192. ULONG total = 0;
  193. ULONG ul;
  194. LONGLONG ll;
  195. WsbAssert(0 != pBytes, E_POINTER);
  196. WsbAssert(0 != pValue, E_POINTER);
  197. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  198. total += size;
  199. pBytes += size;
  200. ll = (ULONGLONG) ul;
  201. *pValue = ll << 32;
  202. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  203. total += size;
  204. ll = (ULONGLONG) ul;
  205. *pValue |= ll;
  206. if (pSize) {
  207. *pSize = total;
  208. }
  209. } WsbCatch(hr);
  210. WsbTraceOut(OLESTR("WsbConvertFromBytes(ULONGLONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  211. return(hr);
  212. }
  213. HRESULT
  214. WsbConvertFromBytes(
  215. UCHAR* pBytes,
  216. DATE* pValue,
  217. ULONG* pSize
  218. )
  219. /*++
  220. Routine Description:
  221. Convert a DATE value from a string of bytes. Useful
  222. for stream portability and creating WsbDbKey values.
  223. Arguments:
  224. pBytes - The source byte array (must at least 8 bytes long).
  225. pValue - Pointer to the returned value.
  226. pSize - Returns the number of bytes used. Can be NULL.
  227. Return Value:
  228. S_OK - Success
  229. E_POINTER - pBytes or pValue was NULL.
  230. --*/
  231. {
  232. HRESULT hr = S_OK;
  233. WsbTraceIn(OLESTR("WsbConvertFromBytes(DATE)"), OLESTR(""));
  234. try {
  235. LONGLONG ll;
  236. WsbAssert(0 != pBytes, E_POINTER);
  237. WsbAssert(0 != pValue, E_POINTER);
  238. WsbAssert(WSB_BYTE_SIZE_DATE == WSB_BYTE_SIZE_LONGLONG, E_UNEXPECTED);
  239. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ll, NULL));
  240. *pValue = (DATE) ll;
  241. if (pSize) {
  242. *pSize = WSB_BYTE_SIZE_DATE;
  243. }
  244. } WsbCatch(hr);
  245. WsbTraceOut(OLESTR("WsbConvertFromBytes(DATE)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  246. return(hr);
  247. }
  248. HRESULT
  249. WsbConvertFromBytes(
  250. UCHAR* pBytes,
  251. FILETIME* pValue,
  252. ULONG* pSize
  253. )
  254. /*++
  255. Routine Description:
  256. Convert a FILETIME value from a string of bytes. Useful
  257. for stream portability and creating WsbDbKey values.
  258. Arguments:
  259. pBytes - The source byte array (must at least 8 bytes long).
  260. pValue - Pointer to the returned value.
  261. pSize - Returns the number of bytes used. Can be NULL.
  262. Return Value:
  263. S_OK - Success
  264. E_POINTER - pBytes or pValue was NULL.
  265. --*/
  266. {
  267. HRESULT hr = S_OK;
  268. WsbTraceIn(OLESTR("WsbConvertFromBytes(FILETIME)"), OLESTR(""));
  269. try {
  270. ULONG size;
  271. ULONG total = 0;
  272. ULONG ul;
  273. WsbAssert(0 != pBytes, E_POINTER);
  274. WsbAssert(0 != pValue, E_POINTER);
  275. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  276. total += size;
  277. pBytes += size;
  278. pValue->dwHighDateTime = ul;
  279. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  280. total += size;
  281. pValue->dwLowDateTime = ul;
  282. if (pSize) {
  283. *pSize = total;
  284. }
  285. } WsbCatch(hr);
  286. WsbTraceOut(OLESTR("WsbConvertFromBytes(FILETIME)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  287. return(hr);
  288. }
  289. HRESULT
  290. WsbConvertFromBytes(
  291. UCHAR* pBytes,
  292. SHORT* pValue,
  293. ULONG* pSize
  294. )
  295. /*++
  296. Routine Description:
  297. Convert a SHORT value from a string of bytes. Useful
  298. for stream portability and creating WsbDbKey values.
  299. Arguments:
  300. pBytes - The source byte array (must at least 2 bytes long).
  301. pValue - Pointer to the returned value.
  302. pSize - Returns the number of bytes used. Can be NULL.
  303. Return Value:
  304. S_OK - Success
  305. E_POINTER - pBytes or pValue was NULL.
  306. --*/
  307. {
  308. HRESULT hr = S_OK;
  309. WsbTraceIn(OLESTR("WsbConvertFromBytes(SHORT)"), OLESTR(""));
  310. try {
  311. WsbAssert(0 != pBytes, E_POINTER);
  312. WsbAssert(0 != pValue, E_POINTER);
  313. WsbAssert(2 == WSB_BYTE_SIZE_SHORT, E_UNEXPECTED);
  314. *pValue = (SHORT)( (pBytes[0] << 8) | pBytes[1] );
  315. if (pSize) {
  316. *pSize = WSB_BYTE_SIZE_SHORT;
  317. }
  318. } WsbCatch(hr);
  319. WsbTraceOut(OLESTR("WsbConvertFromBytes(SHORT)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  320. return(hr);
  321. }
  322. HRESULT
  323. WsbConvertFromBytes(
  324. UCHAR* pBytes,
  325. ULARGE_INTEGER* pValue,
  326. ULONG* pSize
  327. )
  328. /*++
  329. Routine Description:
  330. Convert a ULARGE_INTEGER value from a string of bytes. Useful
  331. for stream portability and creating WsbDbKey values.
  332. Arguments:
  333. pBytes - The source byte array (must at least 8 bytes long).
  334. pValue - Pointer to the returned value.
  335. pSize - Returns the number of bytes used. Can be NULL.
  336. Return Value:
  337. S_OK - Success
  338. E_POINTER - pBytes or pValue was NULL.
  339. --*/
  340. {
  341. HRESULT hr = S_OK;
  342. WsbTraceIn(OLESTR("WsbConvertFromBytes(ULARGE_INTEGER)"), OLESTR(""));
  343. try {
  344. ULONG size;
  345. ULONG total = 0;
  346. ULONG ul;
  347. WsbAssert(0 != pBytes, E_POINTER);
  348. WsbAssert(0 != pValue, E_POINTER);
  349. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  350. total += size;
  351. pBytes += size;
  352. pValue->HighPart = ul;
  353. WsbAffirmHr(WsbConvertFromBytes(pBytes, &ul, &size));
  354. total += size;
  355. pValue->LowPart = ul;
  356. if (pSize) {
  357. *pSize = total;
  358. }
  359. } WsbCatch(hr);
  360. WsbTraceOut(OLESTR("WsbConvertFromBytes(ULARGE_INTEGER)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  361. return(hr);
  362. }
  363. HRESULT
  364. WsbConvertFromBytes(
  365. UCHAR* pBytes,
  366. ULONG* pValue,
  367. ULONG* pSize
  368. )
  369. /*++
  370. Routine Description:
  371. Convert a ULONG value from a string of bytes. Useful
  372. for stream portability and creating WsbDbKey values.
  373. Arguments:
  374. pBytes - The source byte array (must at least 4 bytes long).
  375. pValue - Pointer to the returned value.
  376. pSize - Returns the number of bytes used. Can be NULL.
  377. Return Value:
  378. S_OK - Success
  379. E_POINTER - pBytes or pValue was NULL.
  380. --*/
  381. {
  382. HRESULT hr = S_OK;
  383. WsbTraceIn(OLESTR("WsbConvertFromBytes(ULONG)"), OLESTR(""));
  384. try {
  385. WsbAssert(0 != pBytes, E_POINTER);
  386. WsbAssert(0 != pValue, E_POINTER);
  387. WsbAssert(4 == WSB_BYTE_SIZE_ULONG, E_UNEXPECTED);
  388. *pValue = (pBytes[0] << 24) | (pBytes[1] << 16) |
  389. (pBytes[2] << 8) | pBytes[3];
  390. if (pSize) {
  391. *pSize = WSB_BYTE_SIZE_ULONG;
  392. }
  393. } WsbCatch(hr);
  394. WsbTraceOut(OLESTR("WsbConvertFromBytes(ULONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  395. return(hr);
  396. }
  397. HRESULT
  398. WsbConvertFromBytes(
  399. UCHAR* pBytes,
  400. USHORT* pValue,
  401. ULONG* pSize
  402. )
  403. /*++
  404. Routine Description:
  405. Convert a USHORT value from a string of bytes. Useful
  406. for stream portability and creating WsbDbKey values.
  407. Arguments:
  408. pBytes - The source byte array (must at least 2 bytes long).
  409. pValue - Pointer to the returned value.
  410. pSize - Returns the number of bytes used. Can be NULL.
  411. Return Value:
  412. S_OK - Success
  413. E_POINTER - pBytes or pValue was NULL.
  414. --*/
  415. {
  416. HRESULT hr = S_OK;
  417. WsbTraceIn(OLESTR("WsbConvertFromBytes(USHORT)"), OLESTR(""));
  418. try {
  419. WsbAssert(0 != pBytes, E_POINTER);
  420. WsbAssert(0 != pValue, E_POINTER);
  421. WsbAssert(2 == WSB_BYTE_SIZE_USHORT, E_UNEXPECTED);
  422. *pValue = (USHORT)( ( pBytes[0] << 8 ) | pBytes[1] );
  423. if (pSize) {
  424. *pSize = WSB_BYTE_SIZE_USHORT;
  425. }
  426. } WsbCatch(hr);
  427. WsbTraceOut(OLESTR("WsbConvertFromBytes(USHORT)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  428. return(hr);
  429. }
  430. HRESULT
  431. WsbConvertToBytes(
  432. UCHAR* pBytes,
  433. BOOL value,
  434. ULONG* pSize
  435. )
  436. /*++
  437. Routine Description:
  438. Convert a BOOL value to a string of bytes. Useful
  439. for stream portability and creating WsbDbKey values.
  440. Arguments:
  441. pBytes - The target byte array.
  442. value - The BOOL value to convert.
  443. pSize - Returns the number of bytes used. Can be NULL.
  444. Return Value:
  445. S_OK - Success
  446. E_POINTER - pBytes was NULL.
  447. --*/
  448. {
  449. HRESULT hr = S_OK;
  450. WsbTraceIn(OLESTR("WsbConvertToBytes(BOOL)"), OLESTR("value = <%s>"),
  451. WsbBoolAsString(value));
  452. try {
  453. WsbAssert(0 != pBytes, E_POINTER);
  454. WsbAssert(1 == WSB_BYTE_SIZE_BOOL, E_UNEXPECTED);
  455. if (value) {
  456. *pBytes = 1;
  457. } else {
  458. *pBytes = 0;
  459. }
  460. if (pSize) {
  461. *pSize = WSB_BYTE_SIZE_BOOL;
  462. }
  463. } WsbCatch(hr);
  464. WsbTraceOut(OLESTR("WsbConvertToBytes(BOOL)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  465. return(hr);
  466. }
  467. HRESULT
  468. WsbConvertToBytes(
  469. UCHAR* pBytes,
  470. GUID value,
  471. ULONG* pSize
  472. )
  473. /*++
  474. Routine Description:
  475. Convert a GUID value to a string of bytes. Useful
  476. for stream portability and creating WsbDbKey values.
  477. Arguments:
  478. pBytes - The target byte array (must at least 16 bytes long).
  479. value - The GUID value to convert.
  480. pSize - Returns the number of bytes used. Can be NULL.
  481. Return Value:
  482. S_OK - Success
  483. E_POINTER - pBytes was NULL.
  484. --*/
  485. {
  486. HRESULT hr = S_OK;
  487. WsbTraceIn(OLESTR("WsbConvertToBytes(GUID)"), OLESTR("value = <%s>"),
  488. WsbGuidAsString(value));
  489. try {
  490. ULONG lsize;
  491. ULONG tsize;
  492. WsbAssert(0 != pBytes, E_POINTER);
  493. WsbAffirmHr(WsbConvertToBytes(pBytes, value.Data1, &lsize));
  494. tsize = lsize;
  495. WsbAffirmHr(WsbConvertToBytes(pBytes + tsize, value.Data2, &lsize));
  496. tsize += lsize;
  497. WsbAffirmHr(WsbConvertToBytes(pBytes + tsize, value.Data3, &lsize));
  498. tsize += lsize;
  499. memcpy(pBytes + tsize, value.Data4, 8);
  500. tsize += 8;
  501. if (pSize) {
  502. *pSize = tsize;
  503. }
  504. } WsbCatch(hr);
  505. WsbTraceOut(OLESTR("WsbConvertToBytes(GUID)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  506. return(hr);
  507. }
  508. HRESULT
  509. WsbConvertToBytes(
  510. UCHAR* pBytes,
  511. LONG value,
  512. ULONG* pSize
  513. )
  514. /*++
  515. Routine Description:
  516. Convert a LONG value to a string of bytes. Useful
  517. for stream portability and creating WsbDbKey values.
  518. Arguments:
  519. pBytes - The target byte array (must at least 4 bytes long).
  520. value - The LONG value to convert.
  521. pSize - Returns the number of bytes used. Can be NULL.
  522. Return Value:
  523. S_OK - Success
  524. E_POINTER - pBytes was NULL.
  525. --*/
  526. {
  527. HRESULT hr = S_OK;
  528. WsbTraceIn(OLESTR("WsbConvertToBytes(LONG)"), OLESTR("value = <%d>"), value);
  529. try {
  530. WsbAssert(0 != pBytes, E_POINTER);
  531. WsbAssert(4 == WSB_BYTE_SIZE_LONG, E_UNEXPECTED);
  532. pBytes[0] = ((UCHAR)(value >> 24));
  533. pBytes[1] = ((UCHAR)((value >> 16) & 0xFF));
  534. pBytes[2] = ((UCHAR)((value >> 8) & 0xFF));
  535. pBytes[3] = ((UCHAR)(value & 0xFF));
  536. if (pSize) {
  537. *pSize = WSB_BYTE_SIZE_LONG;
  538. }
  539. } WsbCatch(hr);
  540. WsbTraceOut(OLESTR("WsbConvertToBytes(LONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  541. return(hr);
  542. }
  543. HRESULT
  544. WsbConvertToBytes(
  545. UCHAR* pBytes,
  546. LONGLONG value,
  547. ULONG* pSize
  548. )
  549. /*++
  550. Routine Description:
  551. Convert a LONGLONG value to a string of bytes. Useful
  552. for stream portability and creating WsbDbKey values.
  553. Arguments:
  554. pBytes - The target byte array (must at least 8 bytes long).
  555. value - The LONGLONG value to convert.
  556. pSize - Returns the number of bytes used. Can be NULL.
  557. Return Value:
  558. S_OK - Success
  559. E_POINTER - pBytes was NULL.
  560. --*/
  561. {
  562. HRESULT hr = S_OK;
  563. WsbTraceIn(OLESTR("WsbConvertToBytes(LONGLONG)"), OLESTR("value = <%d>"), value);
  564. try {
  565. ULONG size;
  566. ULONG total = 0;
  567. ULONG ul;
  568. WsbAssert(0 != pBytes, E_POINTER);
  569. ul = (ULONG)(value >> 32);
  570. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  571. total += size;
  572. pBytes += size;
  573. ul = (ULONG)(value & 0xFFFFFFFF);
  574. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  575. total += size;
  576. if (pSize) {
  577. *pSize = total;
  578. }
  579. } WsbCatch(hr);
  580. WsbTraceOut(OLESTR("WsbConvertToBytes(LONGLONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  581. return(hr);
  582. }
  583. HRESULT
  584. WsbConvertToBytes(
  585. UCHAR* pBytes,
  586. ULONGLONG value,
  587. ULONG* pSize
  588. )
  589. /*++
  590. Routine Description:
  591. Convert a ULONGLONG value to a string of bytes. Useful
  592. for stream portability and creating WsbDbKey values.
  593. Arguments:
  594. pBytes - The target byte array (must at least 8 bytes long).
  595. value - The LONGLONG value to convert.
  596. pSize - Returns the number of bytes used. Can be NULL.
  597. Return Value:
  598. S_OK - Success
  599. E_POINTER - pBytes was NULL.
  600. --*/
  601. {
  602. HRESULT hr = S_OK;
  603. WsbTraceIn(OLESTR("WsbConvertToBytes(ULONGLONG)"), OLESTR("value = <%d>"), value);
  604. try {
  605. ULONG size;
  606. ULONG total = 0;
  607. ULONG ul;
  608. WsbAssert(0 != pBytes, E_POINTER);
  609. ul = (ULONG)(value >> 32);
  610. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  611. total += size;
  612. pBytes += size;
  613. ul = (ULONG)(value & 0xFFFFFFFF);
  614. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  615. total += size;
  616. if (pSize) {
  617. *pSize = total;
  618. }
  619. } WsbCatch(hr);
  620. WsbTraceOut(OLESTR("WsbConvertToBytes(ULONGLONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  621. return(hr);
  622. }
  623. HRESULT
  624. WsbConvertToBytes(
  625. UCHAR* pBytes,
  626. DATE value,
  627. ULONG* pSize
  628. )
  629. /*++
  630. Routine Description:
  631. Convert a DATE value to a string of bytes. Useful
  632. for stream portability and creating WsbDbKey values.
  633. Arguments:
  634. pBytes - The target byte array (must at least 8 bytes long).
  635. value - The DATE value to convert.
  636. pSize - Returns the number of bytes used. Can be NULL.
  637. Return Value:
  638. S_OK - Success
  639. E_POINTER - pBytes was NULL.
  640. --*/
  641. {
  642. HRESULT hr = S_OK;
  643. WsbTraceIn(OLESTR("WsbConvertToBytes(DATE)"), OLESTR("value = <%d>"), value);
  644. try {
  645. WsbAssert(0 != pBytes, E_POINTER);
  646. WsbAssert(WSB_BYTE_SIZE_DATE == WSB_BYTE_SIZE_LONGLONG, E_UNEXPECTED);
  647. // Needs to modified after WsbDate functions.
  648. WsbAffirmHr(WsbConvertToBytes(pBytes, (LONGLONG) value, NULL));
  649. if (pSize) {
  650. *pSize = WSB_BYTE_SIZE_DATE;
  651. }
  652. } WsbCatch(hr);
  653. WsbTraceOut(OLESTR("WsbConvertToBytes(DATE)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  654. return(hr);
  655. }
  656. HRESULT
  657. WsbConvertToBytes(
  658. UCHAR* pBytes,
  659. FILETIME value,
  660. ULONG* pSize
  661. )
  662. /*++
  663. Routine Description:
  664. Convert a FILETIME value to a string of bytes. Useful
  665. for stream portability and creating WsbDbKey values.
  666. Arguments:
  667. pBytes - The target byte array (must at least 8 bytes long).
  668. value - The FILETIME value to convert.
  669. pSize - Returns the number of bytes used. Can be NULL.
  670. Return Value:
  671. S_OK - Success
  672. E_POINTER - pBytes was NULL.
  673. --*/
  674. {
  675. HRESULT hr = S_OK;
  676. WsbTraceIn(OLESTR("WsbConvertToBytes(FILETIME)"), OLESTR("value = <%d>"), value);
  677. try {
  678. ULONG size;
  679. ULONG total = 0;
  680. WsbAssert(0 != pBytes, E_POINTER);
  681. WsbAffirmHr(WsbConvertToBytes(pBytes, value.dwHighDateTime, &size));
  682. total += size;
  683. pBytes += size;
  684. WsbAffirmHr(WsbConvertToBytes(pBytes, value.dwLowDateTime, &size));
  685. total += size;
  686. if (pSize) {
  687. *pSize = total;
  688. }
  689. } WsbCatch(hr);
  690. WsbTraceOut(OLESTR("WsbConvertToBytes(FILETIME)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  691. return(hr);
  692. }
  693. HRESULT
  694. WsbConvertToBytes(
  695. UCHAR* pBytes,
  696. SHORT value,
  697. ULONG* pSize
  698. )
  699. /*++
  700. Routine Description:
  701. Convert a SHORT value to a string of bytes. Useful
  702. for stream portability and creating WsbDbKey values.
  703. Arguments:
  704. pBytes - The target byte array (must at least 2 bytes long).
  705. value - The SHORT value to convert.
  706. pSize - Returns the number of bytes used. Can be NULL.
  707. Return Value:
  708. S_OK - Success
  709. E_POINTER - pBytes was NULL.
  710. --*/
  711. {
  712. HRESULT hr = S_OK;
  713. WsbTraceIn(OLESTR("WsbConvertToBytes(SHORT)"), OLESTR("value = <%d>"), value);
  714. try {
  715. WsbAssert(0 != pBytes, E_POINTER);
  716. WsbAssert(2 == WSB_BYTE_SIZE_SHORT, E_UNEXPECTED);
  717. pBytes[0] = (UCHAR)( (value >> 8) & 0xFF);
  718. pBytes[1] = (UCHAR)( value & 0xFF );
  719. if (pSize) {
  720. *pSize = WSB_BYTE_SIZE_SHORT;
  721. }
  722. } WsbCatch(hr);
  723. WsbTraceOut(OLESTR("WsbConvertToBytes(SHORT)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  724. return(hr);
  725. }
  726. HRESULT
  727. WsbConvertToBytes(
  728. UCHAR* pBytes,
  729. ULARGE_INTEGER value,
  730. ULONG* pSize
  731. )
  732. /*++
  733. Routine Description:
  734. Convert a ULARGE_INTEGER value to a string of bytes. Useful
  735. for stream portability and creating WsbDbKey values.
  736. Arguments:
  737. pBytes - The target byte array (must at least 8 bytes long).
  738. value - The ULARGE_INTEGER value to convert.
  739. pSize - Returns the number of bytes used. Can be NULL.
  740. Return Value:
  741. S_OK - Success
  742. E_POINTER - pBytes was NULL.
  743. --*/
  744. {
  745. HRESULT hr = S_OK;
  746. WsbTraceIn(OLESTR("WsbConvertToBytes(ULARGE_INTEGER)"), OLESTR("value = <%d>"), value);
  747. try {
  748. ULONG size;
  749. ULONG total = 0;
  750. ULONG ul;
  751. WsbAssert(0 != pBytes, E_POINTER);
  752. ul = (ULONG)(value.QuadPart >> 32);
  753. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  754. total += size;
  755. pBytes += size;
  756. ul = (ULONG)(value.QuadPart & 0xFFFFFFFF);
  757. WsbAffirmHr(WsbConvertToBytes(pBytes, ul, &size));
  758. total += size;
  759. if (pSize) {
  760. *pSize = total;
  761. }
  762. } WsbCatch(hr);
  763. WsbTraceOut(OLESTR("WsbConvertToBytes(ULARGE_INTEGER)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  764. return(hr);
  765. }
  766. HRESULT
  767. WsbConvertToBytes(
  768. UCHAR* pBytes,
  769. ULONG value,
  770. ULONG* pSize
  771. )
  772. /*++
  773. Routine Description:
  774. Convert a ULONG value to a string of bytes. Useful
  775. for stream portability and creating WsbDbKey values.
  776. Arguments:
  777. pBytes - The target byte array (must at least 4 bytes long).
  778. value - The ULONG value to convert.
  779. pSize - Returns the number of bytes used. Can be NULL.
  780. Return Value:
  781. S_OK - Success
  782. E_POINTER - pBytes was NULL.
  783. --*/
  784. {
  785. HRESULT hr = S_OK;
  786. WsbTraceIn(OLESTR("WsbConvertToBytes(ULONG)"), OLESTR("value = <%d>"), value);
  787. try {
  788. WsbAssert(0 != pBytes, E_POINTER);
  789. WsbAssert(4 == WSB_BYTE_SIZE_ULONG, E_UNEXPECTED);
  790. pBytes[0] = ((UCHAR)(value >> 24));
  791. pBytes[1] = ((UCHAR)((value >> 16) & 0xFF));
  792. pBytes[2] = ((UCHAR)((value >> 8) & 0xFF));
  793. pBytes[3] = ((UCHAR)(value & 0xFF));
  794. if (pSize) {
  795. *pSize = WSB_BYTE_SIZE_ULONG;
  796. }
  797. } WsbCatch(hr);
  798. WsbTraceOut(OLESTR("WsbConvertToBytes(ULONG)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  799. return(hr);
  800. }
  801. HRESULT
  802. WsbConvertToBytes(
  803. UCHAR* pBytes,
  804. USHORT value,
  805. ULONG* pSize
  806. )
  807. /*++
  808. Routine Description:
  809. Convert a USHORT value to a string of bytes. Useful
  810. for stream portability and creating WsbDbKey values.
  811. Arguments:
  812. pBytes - The target byte array (must at least 2 bytes long).
  813. value - The USHORT value to convert.
  814. pSize - Returns the number of bytes used. Can be NULL.
  815. Return Value:
  816. S_OK - Success
  817. E_POINTER - pBytes was NULL.
  818. --*/
  819. {
  820. HRESULT hr = S_OK;
  821. WsbTraceIn(OLESTR("WsbConvertToBytes(USHORT)"), OLESTR("value = <%d>"), value);
  822. try {
  823. WsbAssert(0 != pBytes, E_POINTER);
  824. WsbAssert(2 == WSB_BYTE_SIZE_USHORT, E_UNEXPECTED);
  825. pBytes[0] = (UCHAR)( ( value >> 8 ) & 0xFF );
  826. pBytes[1] = (UCHAR)( value & 0xFF );
  827. if (pSize) {
  828. *pSize = WSB_BYTE_SIZE_USHORT;
  829. }
  830. } WsbCatch(hr);
  831. WsbTraceOut(OLESTR("WsbConvertToBytes(USHORT)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  832. return(hr);
  833. }
  834. HRESULT
  835. WsbOlestrFromBytes(
  836. UCHAR* pBytes,
  837. OLECHAR* pValue,
  838. ULONG* pSize
  839. )
  840. /*++
  841. Routine Description:
  842. Convert a OLECHAR string from a string of bytes. Useful
  843. for stream portability and creating WsbDbKey values.
  844. Arguments:
  845. pBytes - The source byte array.
  846. pValue - Pointer to the returned string.
  847. pSize - Returns the number of bytes used. Can be NULL.
  848. Return Value:
  849. S_OK - Success
  850. E_POINTER - pBytes or pValue was NULL.
  851. --*/
  852. {
  853. HRESULT hr = S_OK;
  854. WsbTraceIn(OLESTR("WsbOlestrFromBytes(OLECHAR)"), OLESTR(""));
  855. try {
  856. ULONG size = 0;
  857. WsbAssert(0 != pBytes, E_POINTER);
  858. WsbAssert(0 != pValue, E_POINTER);
  859. WsbAssert(sizeof(OLECHAR) == 2, E_FAIL);
  860. while (TRUE) {
  861. OLECHAR wc;
  862. wc = (OLECHAR)( (*pBytes++) << 8 );
  863. wc |= *pBytes++;
  864. size += 2;
  865. *pValue++ = wc;
  866. if (wc == 0) break;
  867. }
  868. if (pSize) {
  869. *pSize = size;
  870. }
  871. } WsbCatch(hr);
  872. WsbTraceOut(OLESTR("WsbOlestrFromBytes(OLECHAR)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  873. return(hr);
  874. }
  875. HRESULT
  876. WsbOlestrToBytes(
  877. UCHAR* pBytes,
  878. OLECHAR* pValue,
  879. ULONG* pSize
  880. )
  881. /*++
  882. Routine Description:
  883. Convert a OLECHAR sring to a string of bytes. Useful
  884. for stream portability and creating WsbDbKey values.
  885. Arguments:
  886. pBytes - The target byte array (must long enough).
  887. pValue - The OLECHAR string to convert.
  888. pSize - Returns the number of bytes used. Can be NULL.
  889. Return Value:
  890. S_OK - Success
  891. E_POINTER - pBytes was NULL.
  892. --*/
  893. {
  894. HRESULT hr = S_OK;
  895. WsbTraceIn(OLESTR("WsbOlestrToBytes(OLECHAR)"), OLESTR("value = <%S>"), pValue);
  896. try {
  897. ULONG size = 0;
  898. WsbAssert(0 != pBytes, E_POINTER);
  899. WsbAssert(0 != pValue, E_POINTER);
  900. WsbAssert(sizeof(OLECHAR) == 2, E_FAIL);
  901. while (TRUE) {
  902. OLECHAR wc;
  903. wc = *pValue++;
  904. *pBytes++ = (UCHAR)( ( wc >> 8 ) & 0xFF );
  905. *pBytes++ = (UCHAR)( wc & 0xFF );
  906. size += 2;
  907. if (wc == 0) break;
  908. }
  909. if (pSize) {
  910. *pSize = size;
  911. }
  912. } WsbCatch(hr);
  913. WsbTraceOut(OLESTR("WsbOlestrToBytes(OLECHAR)"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  914. return(hr);
  915. }