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.

1018 lines
20 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1999 - 1999
  3. Module Name:
  4. Conversion
  5. Abstract:
  6. This module contains simple conversion routines.
  7. Author:
  8. Doug Barlow (dbarlow) 6/20/1999
  9. Notes:
  10. ?Notes?
  11. --*/
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include "stdafx.h"
  16. #include "ByteBuffer.h"
  17. #include "Conversion.h"
  18. static BOOL
  19. GuidFromString(
  20. LPCTSTR szGuid,
  21. LPGUID pGuid);
  22. /*++
  23. ConstructRequest:
  24. This routine builds an APDU Request.
  25. Arguments:
  26. bCla supplies the Class byte
  27. cIns supplies the Instance byte
  28. bP1 supplies P1
  29. bP2 supplies P2
  30. bfData supplies the data
  31. wLe supplies the expected return length
  32. dwFlags supplies any special processing flags:
  33. APDU_EXTENDED_LC - Force an extended value for Lc
  34. APDU_EXTENDED_LE - Force an externded value for Le
  35. APDU_MAXIMUM_LE - Request the maximum Le value
  36. bfApdu receives the constructed APDU
  37. Return Value:
  38. None
  39. Throws:
  40. Errors are thrown as HRESULT status codes
  41. Remarks:
  42. ?Remarks?
  43. Author:
  44. Doug Barlow (dbarlow) 6/26/1999
  45. --*/
  46. #undef __SUBROUTINE__
  47. #define __SUBROUTINE__ TEXT("ConstructRequest")
  48. void
  49. ConstructRequest(
  50. IN BYTE bCla,
  51. IN BYTE bIns,
  52. IN BYTE bP1,
  53. IN BYTE bP2,
  54. IN CBuffer &bfData,
  55. IN WORD wLe,
  56. IN DWORD dwFlags,
  57. OUT CBuffer &bfApdu)
  58. {
  59. WORD wLc;
  60. BOOL fExtended;
  61. BYTE b, rgLen[2];
  62. //
  63. // Quick prep work
  64. //
  65. if (0xffff < bfData.Length())
  66. throw (HRESULT)E_INVALIDARG;
  67. wLc = (WORD)bfData.Length();
  68. bfApdu.Presize(4 + 3 + 3 + wLc); // Worst case
  69. fExtended = (0 != (dwFlags & APDU_EXTENDED_LENGTH))
  70. || (0xff < wLe)
  71. || (0xff < wLc);
  72. //
  73. // Fill in the buffer with the easy stuff.
  74. //
  75. bfApdu.Set(&bCla, 1);
  76. bfApdu.Append(&bIns, 1);
  77. bfApdu.Append(&bP1, 1);
  78. bfApdu.Append(&bP2, 1);
  79. //
  80. // Is there data to be sent?
  81. //
  82. if (0 != wLc)
  83. {
  84. if (fExtended)
  85. {
  86. LocalToNet(rgLen, wLc);
  87. bfApdu.Append((LPCBYTE)"", 1); // Append a zero byte
  88. bfApdu.Append(rgLen, 2);
  89. }
  90. else
  91. {
  92. b = LeastSignificantByte(wLc);
  93. bfApdu.Append(&b, 1);
  94. }
  95. bfApdu.Append(bfData.Access(), wLc);
  96. }
  97. //
  98. // Do we expect data back?
  99. //
  100. if ((0 != wLe) || (0 != (dwFlags & APDU_MAXIMUM_LE)))
  101. {
  102. if (fExtended)
  103. {
  104. if (0 == wLc)
  105. bfApdu.Append((LPCBYTE)"", 1); // Append a zero byte
  106. LocalToNet(rgLen, wLe);
  107. bfApdu.Append(rgLen, 2);
  108. }
  109. else
  110. {
  111. b = LeastSignificantByte(wLe);
  112. bfApdu.Append(&b, 1);
  113. }
  114. }
  115. }
  116. /*++
  117. ParseRequest:
  118. This routine parses an APDU into it's components.
  119. Arguments:
  120. bfApdu supplies the APDU to be parsed.
  121. pbCla receives the Class
  122. pbIns receives the Instance
  123. pbP1 receives P1
  124. pbP2 receives P2
  125. pbfData receives the data
  126. pwLc receives the supplied data length
  127. pwLe receives the expected length
  128. pdwFlags receives the construction flags
  129. APDU_EXTENDED_LC - There was an extended value for Lc
  130. APDU_EXTENDED_LE - There was an externded value for Le
  131. APDU_MAXIMUM_LE - There was a maximum Le value
  132. Return Value:
  133. None
  134. Throws:
  135. Errors are thrown as an HRESULT status code.
  136. Remarks:
  137. ?Remarks?
  138. Author:
  139. Doug Barlow (dbarlow) 6/26/1999
  140. --*/
  141. #undef __SUBROUTINE__
  142. #define __SUBROUTINE__ TEXT("ParseRequest")
  143. void
  144. ParseRequest(
  145. IN LPCBYTE pbApdu,
  146. IN DWORD cbApdu,
  147. OUT LPBYTE pbCla,
  148. OUT LPBYTE pbIns,
  149. OUT LPBYTE pbP1,
  150. OUT LPBYTE pbP2,
  151. OUT LPCBYTE *ppbData,
  152. OUT LPWORD pwLc,
  153. OUT LPWORD pwLe,
  154. OUT LPDWORD pdwFlags)
  155. {
  156. DWORD dwLen = cbApdu;
  157. DWORD dwFlags = 0;
  158. WORD wLen, wLe, wLc;
  159. //
  160. // Easy stuff.
  161. //
  162. if (4 > dwLen)
  163. throw (HRESULT)E_INVALIDARG;
  164. if (NULL != pbCla)
  165. *pbCla = pbApdu[0];
  166. if (NULL != pbIns)
  167. *pbIns = pbApdu[1];
  168. if (NULL != pbP1)
  169. *pbP1 = pbApdu[2];
  170. if (NULL != pbP2)
  171. *pbP2 = pbApdu[3];
  172. //
  173. // Harder stuff.
  174. //
  175. if (NULL != ppbData)
  176. *ppbData = NULL;
  177. if (4 == dwLen)
  178. {
  179. // Type 1
  180. wLc = 0;
  181. wLe = 0;
  182. }
  183. else if ((0 != pbApdu[4]) || (5 == dwLen))
  184. {
  185. // Short length
  186. wLen = pbApdu[4];
  187. if (5 == dwLen)
  188. {
  189. // Type 2S
  190. wLc = 0;
  191. wLe = wLen;
  192. if (0 == wLen)
  193. dwFlags |= APDU_MAXIMUM_LE;
  194. }
  195. else if (5 == dwLen - wLen)
  196. {
  197. // Type 3S
  198. if (NULL != ppbData)
  199. *ppbData = &pbApdu[5];
  200. wLc = wLen;
  201. wLe = 0;
  202. }
  203. else if (6 == dwLen - wLen)
  204. {
  205. // Type 4S
  206. if (NULL != ppbData)
  207. *ppbData = &pbApdu[5];
  208. wLc = wLen;
  209. wLe = pbApdu[dwLen - 1];
  210. if (0 == wLe)
  211. dwFlags |= APDU_MAXIMUM_LE;
  212. }
  213. else
  214. throw (HRESULT)E_INVALIDARG;
  215. }
  216. else if (7 <= dwLen)
  217. {
  218. // Extended length
  219. dwFlags |= APDU_EXTENDED_LENGTH;
  220. wLen = NetToLocal(&pbApdu[5]);
  221. if (7 == dwLen)
  222. {
  223. // Type 2E
  224. wLe = wLen;
  225. if (0 == wLen)
  226. dwFlags |= APDU_MAXIMUM_LE;
  227. }
  228. else if (7 == dwLen - wLen)
  229. {
  230. // Type 3E
  231. if (NULL != ppbData)
  232. *ppbData = &pbApdu[6];
  233. wLc = wLen;
  234. wLe = 0;
  235. }
  236. else if (9 == dwLen - wLen)
  237. {
  238. // Type 4E
  239. if (NULL != ppbData)
  240. *ppbData = &pbApdu[6];
  241. wLc = wLen;
  242. wLe = NetToLocal(&pbApdu[dwLen - 2]);
  243. if (0 == wLe)
  244. dwFlags |= APDU_MAXIMUM_LE;
  245. }
  246. else
  247. throw (HRESULT)E_INVALIDARG;
  248. }
  249. else
  250. throw (HRESULT)E_INVALIDARG;
  251. if (NULL != pwLc)
  252. *pwLc = wLc;
  253. if (NULL != pwLe)
  254. *pwLe = wLe;
  255. if (NULL != pdwFlags)
  256. *pdwFlags = dwFlags;
  257. }
  258. /*++
  259. ParseReply:
  260. This routine parses an APDU reply.
  261. Arguments:
  262. bfApdu supplies the APDU reply to be parsed.
  263. pbSW1 receives SW1
  264. pbSW2 receives SW2
  265. Return Value:
  266. None
  267. Throws:
  268. Errors are thrown as HRESULT status codes.
  269. Remarks:
  270. ?Remarks?
  271. Author:
  272. Doug Barlow (dbarlow) 6/26/1999
  273. --*/
  274. #undef __SUBROUTINE__
  275. #define __SUBROUTINE__ TEXT("ParseReply")
  276. void
  277. ParseReply(
  278. IN CBuffer &bfApdu,
  279. OUT LPBYTE pbSW1,
  280. OUT LPBYTE pbSW2)
  281. {
  282. DWORD dwLen = bfApdu.Length();
  283. if (2 > dwLen)
  284. throw (HRESULT)E_INVALIDARG;
  285. if (NULL != pbSW1)
  286. *pbSW1 = bfApdu[dwLen - 2];
  287. if (NULL != pbSW2)
  288. *pbSW2 = bfApdu[dwLen - 1];
  289. }
  290. /*++
  291. MultiStringToSafeArray:
  292. This function converts a Calais Multistring to a SafeArray Structure.
  293. Arguments:
  294. msz supplies the multistring to be converted.
  295. pprgsz supplies and/or receives the SafeArray.
  296. Return Value:
  297. None
  298. Throws:
  299. Errors are thrown as HRESULT error codes.
  300. Remarks:
  301. ?Remarks?
  302. Author:
  303. Doug Barlow (dbarlow) 6/20/1999
  304. --*/
  305. #undef __SUBROUTINE__
  306. #define __SUBROUTINE__ TEXT("MultiStringToSafeArray")
  307. void
  308. MultiStringToSafeArray(
  309. IN LPCTSTR msz,
  310. IN OUT LPSAFEARRAY *pprgsz)
  311. {
  312. LONG ni = 0;
  313. DWORD csz = MStringCount(msz);
  314. VARTYPE vt;
  315. HRESULT hr;
  316. LPCTSTR sz;
  317. CTextString tz;
  318. LPSAFEARRAY pDelArray = NULL;
  319. try
  320. {
  321. if (NULL == *pprgsz)
  322. {
  323. vt = VT_BSTR;
  324. pDelArray = SafeArrayCreateVector(vt, 0, csz);
  325. if (NULL == pDelArray)
  326. throw (HRESULT)E_OUTOFMEMORY;
  327. *pprgsz= pDelArray;
  328. }
  329. else
  330. {
  331. SAFEARRAYBOUND bound;
  332. if (1 != SafeArrayGetDim(*pprgsz))
  333. throw (HRESULT)E_INVALIDARG;
  334. bound.cElements = csz;
  335. bound.lLbound = 0;
  336. hr = SafeArrayRedim(*pprgsz, &bound);
  337. if (FAILED(hr))
  338. throw hr;
  339. hr = SafeArrayGetVartype(*pprgsz, &vt);
  340. if (FAILED(hr))
  341. throw hr;
  342. }
  343. for (sz = FirstString(msz); NULL != sz; sz = NextString(sz))
  344. {
  345. tz = sz;
  346. switch (vt)
  347. {
  348. case VT_LPSTR:
  349. hr = SafeArrayPutElement(*pprgsz, &ni, (LPVOID)((LPCSTR)tz));
  350. break;
  351. case VT_LPWSTR:
  352. hr = SafeArrayPutElement(*pprgsz, &ni, (LPVOID)((LPCWSTR)tz));
  353. break;
  354. case VT_BSTR:
  355. hr = SafeArrayPutElement(*pprgsz, &ni, (LPVOID)((LPCWSTR)tz));
  356. break;
  357. default:
  358. hr = E_INVALIDARG;
  359. }
  360. if (FAILED(hr))
  361. throw hr;
  362. ni += 1;
  363. }
  364. }
  365. catch (...)
  366. {
  367. if (NULL != pDelArray)
  368. {
  369. try { *pprgsz = NULL; } catch (...) {}
  370. SafeArrayDestroy(pDelArray);
  371. throw;
  372. }
  373. }
  374. }
  375. /*++
  376. GuidArrayToSafeArray:
  377. This function converts a vector of GUIDs into its SafeArray form.
  378. Arguments:
  379. pGuids supplies the list of GUIDs
  380. cguids supplies the number of GUIDs in the list
  381. pprgguids supplies a safe array to receive the GUIDs, or if NULL, receives
  382. a new safe array of GUIDs.
  383. Return Value:
  384. None
  385. Throws:
  386. Errors are thrown as HRESULT error codes.
  387. Remarks:
  388. ?Remarks?
  389. Author:
  390. Doug Barlow (dbarlow) 6/25/1999
  391. --*/
  392. #undef __SUBROUTINE__
  393. #define __SUBROUTINE__ TEXT("GuidArrayToSafeArray")
  394. void
  395. GuidArrayToSafeArray(
  396. IN LPCGUID pGuids,
  397. IN DWORD cguids,
  398. IN OUT LPSAFEARRAY *pprgguids)
  399. {
  400. LONG ni = 0;
  401. VARTYPE vt;
  402. HRESULT hr;
  403. LPSAFEARRAY pDelArray = NULL;
  404. CTextString tz;
  405. try
  406. {
  407. if (NULL == *pprgguids)
  408. {
  409. vt = VT_CLSID;
  410. pDelArray = SafeArrayCreateVector(vt, 0, cguids);
  411. if (NULL == pDelArray)
  412. throw (HRESULT)E_OUTOFMEMORY;
  413. *pprgguids = pDelArray;
  414. }
  415. else
  416. {
  417. SAFEARRAYBOUND bound;
  418. if (1 != SafeArrayGetDim(*pprgguids))
  419. throw (HRESULT)E_INVALIDARG;
  420. bound.cElements = cguids;
  421. bound.lLbound = 0;
  422. hr = SafeArrayRedim(*pprgguids, &bound);
  423. if (FAILED(hr))
  424. throw hr;
  425. hr = SafeArrayGetVartype(*pprgguids, &vt);
  426. if (FAILED(hr))
  427. throw hr;
  428. }
  429. for (ni = 0; (DWORD)ni < cguids; ni += 1)
  430. {
  431. TCHAR szGuid[40];
  432. StringFromGuid(&pGuids[ni], szGuid);
  433. tz = szGuid;
  434. switch (vt)
  435. {
  436. case VT_LPSTR:
  437. hr = SafeArrayPutElement(
  438. *pprgguids,
  439. &ni,
  440. (LPVOID)((LPCSTR)tz));
  441. break;
  442. case VT_LPWSTR:
  443. hr = SafeArrayPutElement(
  444. *pprgguids,
  445. &ni,
  446. (LPVOID)((LPCWSTR)tz));
  447. break;
  448. case VT_BSTR:
  449. hr = SafeArrayPutElement(
  450. *pprgguids,
  451. &ni,
  452. (LPVOID)((LPCWSTR)tz));
  453. break;
  454. case VT_CLSID:
  455. hr = SafeArrayPutElement(
  456. *pprgguids,
  457. &ni,
  458. (LPVOID)(&pGuids[ni]));
  459. break;
  460. default:
  461. hr = E_INVALIDARG;
  462. }
  463. if (FAILED(hr))
  464. throw hr;
  465. }
  466. }
  467. catch (...)
  468. {
  469. if (NULL != pDelArray)
  470. {
  471. try { *pprgguids = NULL; } catch (...) {}
  472. SafeArrayDestroy(pDelArray);
  473. }
  474. throw;
  475. }
  476. }
  477. /*++
  478. SafeArrayToGuidArray:
  479. This routine converts a given SafeArray object into a list of GUIDs.
  480. Arguments:
  481. prgGuids supplies the SafeArray containing the GUIDs.
  482. bfGuids receives a block of memory containing binary GUIDs.
  483. pcGuids receives the number of GUIDs in the array.
  484. Return Value:
  485. None
  486. Throws:
  487. Errors are thrown as HRESULT status codes.
  488. Remarks:
  489. ?Remarks?
  490. Author:
  491. Doug Barlow (dbarlow) 6/25/1999
  492. --*/
  493. #undef __SUBROUTINE__
  494. #define __SUBROUTINE__ TEXT("SafeArrayToGuidArray")
  495. void
  496. SafeArrayToGuidArray(
  497. IN LPSAFEARRAY prgGuids,
  498. OUT CBuffer &bfGuids,
  499. OUT LPDWORD pcGuids)
  500. {
  501. VARTYPE vt;
  502. HRESULT hr;
  503. LONG lLBound, lUBound, lIndex;
  504. LPVOID pVoid;
  505. CTextString tz;
  506. LPGUID pguid;
  507. LONG lOne = 1;
  508. if (1 != SafeArrayGetDim(prgGuids))
  509. throw (HRESULT)E_INVALIDARG;
  510. hr = SafeArrayGetLBound(prgGuids, 1, &lLBound);
  511. if (FAILED(hr))
  512. throw hr;
  513. hr = SafeArrayGetUBound(prgGuids, 1, &lUBound);
  514. if (FAILED(hr))
  515. throw hr;
  516. hr = SafeArrayGetVartype(prgGuids, &vt);
  517. if (FAILED(hr))
  518. throw hr;
  519. lIndex = lUBound - lLBound;
  520. pguid = (LPGUID)bfGuids.Resize(lIndex * sizeof(GUID));
  521. if (NULL != pcGuids)
  522. *pcGuids = (DWORD)lIndex;
  523. for (lIndex = lLBound; lIndex <= lUBound; lIndex += 1)
  524. {
  525. hr = SafeArrayGetElement(prgGuids, &lOne, &pVoid);
  526. if (FAILED(hr))
  527. throw hr;
  528. switch (vt)
  529. {
  530. case VT_LPSTR:
  531. tz = (LPCSTR)pVoid;
  532. if (!GuidFromString(tz, &pguid[lIndex - lLBound]))
  533. hr = E_INVALIDARG;
  534. break;
  535. case VT_LPWSTR:
  536. tz = (LPCWSTR)pVoid;
  537. if (!GuidFromString(tz, &pguid[lIndex - lLBound]))
  538. hr = E_INVALIDARG;
  539. break;
  540. case VT_BSTR:
  541. tz = (BSTR)pVoid;
  542. if (!GuidFromString(tz, &pguid[lIndex - lLBound]))
  543. hr = E_INVALIDARG;
  544. break;
  545. case VT_CLSID:
  546. CopyMemory(&pguid[lIndex - lLBound], pVoid, sizeof(GUID));
  547. break;
  548. default:
  549. hr = E_INVALIDARG;
  550. }
  551. if (FAILED(hr))
  552. throw hr;
  553. }
  554. }
  555. /*++
  556. SafeArrayToMultiString:
  557. This routine converts a SafeArray into a multiString.
  558. Arguments:
  559. prgsz supplies the SafeArray
  560. msz receives the MultiString
  561. Return Value:
  562. None
  563. Throws:
  564. Errors are thrown as HRESULT status codes
  565. Remarks:
  566. ?Remarks?
  567. Author:
  568. Doug Barlow (dbarlow) 6/25/1999
  569. --*/
  570. #undef __SUBROUTINE__
  571. #define __SUBROUTINE__ TEXT("SafeArrayToMultiString")
  572. void
  573. SafeArrayToMultiString(
  574. IN LPSAFEARRAY prgsz,
  575. IN OUT CTextMultistring &msz)
  576. {
  577. VARTYPE vt;
  578. HRESULT hr;
  579. LONG lLBound, lUBound, lIndex;
  580. LPVOID pVoid;
  581. CBuffer bf;
  582. if (1 != SafeArrayGetDim(prgsz))
  583. throw (HRESULT)E_INVALIDARG;
  584. hr = SafeArrayGetLBound(prgsz, 1, &lLBound);
  585. if (FAILED(hr))
  586. throw hr;
  587. hr = SafeArrayGetUBound(prgsz, 1, &lUBound);
  588. if (FAILED(hr))
  589. throw hr;
  590. hr = SafeArrayGetVartype(prgsz, &vt);
  591. if (FAILED(hr))
  592. throw hr;
  593. for (lIndex = lLBound; lIndex <= lUBound; lIndex += 1)
  594. {
  595. hr = SafeArrayGetElement(prgsz, &lIndex, &pVoid);
  596. if (FAILED(hr))
  597. throw hr;
  598. switch (vt)
  599. {
  600. case VT_LPSTR:
  601. MStrAdd(bf, (LPCSTR)pVoid);
  602. break;
  603. case VT_LPWSTR:
  604. case VT_BSTR:
  605. MStrAdd(bf, (LPCWSTR)pVoid);
  606. break;
  607. default:
  608. hr = E_INVALIDARG;
  609. }
  610. if (FAILED(hr))
  611. throw hr;
  612. }
  613. msz = (LPCTSTR)bf.Access();
  614. }
  615. /*++
  616. GuidFromString:
  617. This routine converts a string GUID into a binary GUID.
  618. Arguments:
  619. szGuid supplies the GUID in the string format.
  620. pGuid receives the converted GUID.
  621. Return Value:
  622. TRUE - Successful conversion
  623. FALSE - Parsing Error
  624. Remarks:
  625. ?Remarks?
  626. Author:
  627. Doug Barlow (dbarlow) 6/25/1999
  628. --*/
  629. #undef __SUBROUTINE__
  630. #define __SUBROUTINE__ TEXT("GuidFromString")
  631. static BOOL
  632. GuidFromString(
  633. LPCTSTR szGuid,
  634. LPGUID pGuid)
  635. {
  636. //
  637. // The following placement assumes Little Endianness.
  638. // 1D92589A-91E4-11d1-93AA-00C04FD91402
  639. // 012345678901234567890123456789012345
  640. // 1 2 3
  641. //
  642. static const BYTE rgbPlace[sizeof(GUID)]
  643. = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
  644. static const DWORD rgdwPunct[]
  645. = { 8, 13, 18, 23 };
  646. LPCTSTR pch = szGuid;
  647. BYTE bVal;
  648. DWORD dwI, dwJ, dwPunct = 0;
  649. szGuid += _tcsspn(szGuid, TEXT("{[("));
  650. pch = szGuid;
  651. for (dwI = 0; dwI < sizeof(GUID); dwI += 1)
  652. {
  653. if ((BYTE)(pch - szGuid) == rgdwPunct[dwPunct])
  654. {
  655. if (TEXT('-') != *pch)
  656. goto ErrorExit;
  657. dwPunct += 1;
  658. pch += 1;
  659. }
  660. bVal = 0;
  661. for (dwJ = 0; dwJ < 2; dwJ += 1)
  662. {
  663. bVal <<= 4;
  664. if ((TEXT('0') <= *pch) && (TEXT('9') >= *pch))
  665. bVal += *pch - TEXT('0');
  666. else if ((TEXT('A') <= *pch) && (TEXT('F') >= *pch))
  667. bVal += 10 + *pch - TEXT('A');
  668. else if ((TEXT('f') <= *pch) && (TEXT('f') >= *pch))
  669. bVal += 10 + *pch - TEXT('a');
  670. else
  671. goto ErrorExit;
  672. pch += 1;
  673. }
  674. ((LPBYTE)pGuid)[rgbPlace[dwI]] = bVal;
  675. }
  676. return TRUE;
  677. ErrorExit:
  678. return FALSE;
  679. }
  680. /*++
  681. ByteBufferToBuffer:
  682. This routine extracts the contents of a ByteBuffer object into a CBuffer
  683. for easy access.
  684. Arguments:
  685. pby supplies the ByteBuffer to be read.
  686. bf receives the contents of pby.
  687. Return Value:
  688. Number of bytes read from the stream.
  689. Throws:
  690. Errors are thrown as HRESULT status codes.
  691. Remarks:
  692. ?Remarks?
  693. Author:
  694. Doug Barlow (dbarlow) 6/29/1999
  695. --*/
  696. #undef __SUBROUTINE__
  697. #define __SUBROUTINE__ TEXT("ByteBufferToBuffer")
  698. LONG
  699. ByteBufferToBuffer(
  700. IN LPBYTEBUFFER pby,
  701. OUT CBuffer &bf)
  702. {
  703. HRESULT hr;
  704. LONG nLen = 0;
  705. if (NULL != pby)
  706. {
  707. hr = pby->Seek(0, STREAM_SEEK_END, &nLen);
  708. if (FAILED(hr))
  709. throw hr;
  710. hr = pby->Seek(0, STREAM_SEEK_SET, NULL);
  711. if (FAILED(hr))
  712. throw hr;
  713. hr = pby->Read(
  714. bf.Presize((DWORD)nLen),
  715. nLen,
  716. &nLen);
  717. if (FAILED(hr))
  718. throw hr;
  719. bf.Resize((DWORD)nLen, TRUE);
  720. }
  721. else
  722. bf.Reset();
  723. return nLen;
  724. }
  725. /*++
  726. BufferToByteBuffer:
  727. This routine writes the contents of the supplied CBuffer object into the
  728. supplied IByteBuffer object, replacing any existing contents.
  729. Arguments:
  730. bf supplies the data to be written into pby.
  731. ppby receives the contents of bf.
  732. Return Value:
  733. Number of bytes written to the stream.
  734. Throws:
  735. Errors are thrown as HRESULT status codes.
  736. Remarks:
  737. ?Remarks?
  738. Author:
  739. Doug Barlow (dbarlow) 6/29/1999
  740. --*/
  741. #undef __SUBROUTINE__
  742. #define __SUBROUTINE__ TEXT("BufferToByteBuffer")
  743. LONG
  744. BufferToByteBuffer(
  745. IN CBuffer &bf,
  746. OUT LPBYTEBUFFER *ppby)
  747. {
  748. HRESULT hr;
  749. LONG lLen = 0;
  750. if (NULL == *ppby)
  751. {
  752. *ppby = NewByteBuffer();
  753. if (NULL == *ppby)
  754. throw (HRESULT)E_OUTOFMEMORY;
  755. }
  756. hr = (*ppby)->Initialize();
  757. if (FAILED(hr))
  758. throw hr;
  759. hr = (*ppby)->Write(bf.Access(), bf.Length(), &lLen);
  760. if (FAILED(hr))
  761. throw hr;
  762. hr = (*ppby)->Seek(0, STREAM_SEEK_SET, NULL);
  763. if (FAILED(hr))
  764. throw hr;
  765. return lLen;
  766. }