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.

459 lines
8.5 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: uwbase64.cpp
  7. //
  8. // Contents: obsolete base64 encode/decode implementation
  9. // for backward compatibility
  10. //
  11. // History: 3-2000 xtan created
  12. //
  13. //---------------------------------------------------------------------------
  14. #include <pch.cpp>
  15. #pragma hdrstop
  16. #include <certsrv.h>
  17. #include "csdisp.h"
  18. HRESULT
  19. ObsoleteDecodeFileA(
  20. IN CHAR const *pszfn,
  21. OUT BYTE **ppbOut,
  22. OUT DWORD *pcbOut,
  23. IN DWORD Flags)
  24. {
  25. HRESULT hr;
  26. WCHAR *pwszfn = NULL;
  27. if (!myConvertSzToWsz(&pwszfn, pszfn, -1))
  28. {
  29. hr = E_OUTOFMEMORY;
  30. _JumpError(hr, error, "myConvertSzToWsz");
  31. }
  32. hr = DecodeFileW(pwszfn, ppbOut, pcbOut, Flags);
  33. error:
  34. if (NULL != pwszfn)
  35. {
  36. LocalFree(pwszfn);
  37. }
  38. return hr;
  39. }
  40. HRESULT
  41. ObsoleteEncodeToFileA(
  42. IN CHAR const *pszfn,
  43. IN BYTE const *pbIn,
  44. IN DWORD cbIn,
  45. IN DWORD Flags)
  46. {
  47. HRESULT hr;
  48. WCHAR *pwszfn = NULL;
  49. if (!myConvertSzToWsz(&pwszfn, pszfn, -1))
  50. {
  51. hr = E_OUTOFMEMORY;
  52. _JumpError(hr, error, "myConvertSzToWsz");
  53. }
  54. hr = EncodeToFileW(pwszfn, pbIn, cbIn, Flags);
  55. error:
  56. if (NULL != pwszfn)
  57. {
  58. LocalFree(pwszfn);
  59. }
  60. return hr;
  61. }
  62. HRESULT
  63. ObsoleteDecodeCertX(
  64. IN BOOL fUnicode,
  65. IN void const *pchIn,
  66. IN DWORD cchIn,
  67. IN DWORD Flags,
  68. OUT BYTE **ppbOut,
  69. OUT DWORD *pcbOut)
  70. {
  71. HRESULT hr;
  72. BYTE *pbOut = NULL;
  73. DWORD cbOut;
  74. BOOL fRet;
  75. //init
  76. *ppbOut = NULL;
  77. *pcbOut = 0;
  78. while (TRUE)
  79. {
  80. if (fUnicode)
  81. fRet = CryptStringToBinaryW((LPCWSTR)pchIn, cchIn, Flags, pbOut, &cbOut, NULL, NULL);
  82. else
  83. fRet = CryptStringToBinaryA((LPCSTR)pchIn, cchIn, Flags, pbOut, &cbOut, NULL, NULL);
  84. if (!fRet)
  85. {
  86. hr = myHLastError();
  87. _JumpError(hr, error, "CryptStringToBinary");
  88. }
  89. if (NULL != pbOut)
  90. {
  91. break; //done
  92. }
  93. pbOut = (BYTE*)LocalAlloc(LMEM_FIXED, cbOut);
  94. if (NULL == pbOut)
  95. {
  96. hr = E_OUTOFMEMORY;
  97. _JumpError(hr, error, "LocalALloc");
  98. }
  99. }
  100. *ppbOut = pbOut;
  101. pbOut = NULL;
  102. *pcbOut = cbOut;
  103. hr = S_OK;
  104. error:
  105. if (NULL != pbOut)
  106. {
  107. LocalFree(pbOut);
  108. }
  109. return hr;
  110. }
  111. HRESULT
  112. ObsoleteDecodeCertA(
  113. IN CHAR const *pchIn,
  114. IN DWORD cchIn,
  115. IN DWORD Flags,
  116. OUT BYTE **ppbOut,
  117. OUT DWORD *pcbOut)
  118. {
  119. return ObsoleteDecodeCertX(
  120. FALSE, //ansi
  121. (void const *)pchIn,
  122. cchIn,
  123. Flags,
  124. ppbOut,
  125. pcbOut);
  126. }
  127. HRESULT
  128. ObsoleteDecodeCertW(
  129. IN CHAR const *pchIn,
  130. IN DWORD cchIn,
  131. IN DWORD Flags,
  132. OUT BYTE **ppbOut,
  133. OUT DWORD *pcbOut)
  134. {
  135. return ObsoleteDecodeCertX(
  136. TRUE, //unicode
  137. (void const *)pchIn,
  138. cchIn,
  139. Flags,
  140. ppbOut,
  141. pcbOut);
  142. }
  143. HRESULT
  144. ObsoleteEncodeCertX(
  145. IN BOOL fUnicode,
  146. IN BYTE const *pbIn,
  147. IN DWORD cbIn,
  148. IN DWORD Flags,
  149. OUT void **ppchOut,
  150. OUT DWORD *pcchOut)
  151. {
  152. HRESULT hr;
  153. BOOL fRet;
  154. void *pOut = NULL;
  155. DWORD ccOut = 0;
  156. //init
  157. *ppchOut = NULL;
  158. *pcchOut = 0;
  159. while (TRUE)
  160. {
  161. if (fUnicode)
  162. fRet = CryptBinaryToStringW(
  163. pbIn,
  164. cbIn,
  165. Flags,
  166. (WCHAR*)pOut,
  167. &ccOut);
  168. else
  169. fRet = CryptBinaryToStringA(
  170. pbIn,
  171. cbIn,
  172. Flags,
  173. (CHAR*)pOut,
  174. &ccOut);
  175. if (!fRet)
  176. {
  177. hr = myHLastError();
  178. _JumpError(hr, error, "CryptBinaryToString");
  179. }
  180. if (NULL != pOut)
  181. {
  182. break; //done
  183. }
  184. pOut = LocalAlloc(LMEM_FIXED,
  185. ccOut * (fUnicode ? sizeof(WCHAR) : sizeof(CHAR)));
  186. if (NULL == pOut)
  187. {
  188. hr = E_OUTOFMEMORY;
  189. _JumpError(hr, error, "LocalAlloc");
  190. }
  191. }
  192. *ppchOut = pOut;
  193. pOut = NULL;
  194. *pcchOut = ccOut;
  195. hr = S_OK;
  196. error:
  197. if (NULL != pOut)
  198. {
  199. LocalFree(pOut);
  200. }
  201. return hr;
  202. }
  203. HRESULT
  204. ObsoleteEncodeCertA(
  205. IN BYTE const *pbIn,
  206. IN DWORD cbIn,
  207. IN DWORD Flags,
  208. OUT CHAR **ppchOut,
  209. OUT DWORD *pcchOut)
  210. {
  211. return ObsoleteEncodeCertX(
  212. FALSE, //ansi
  213. pbIn,
  214. cbIn,
  215. Flags,
  216. (void**)ppchOut,
  217. pcchOut);
  218. }
  219. HRESULT
  220. ObsoleteEncodeCertW(
  221. IN BYTE const *pbIn,
  222. IN DWORD cbIn,
  223. IN DWORD Flags,
  224. OUT WCHAR **ppchOut,
  225. OUT DWORD *pcchOut)
  226. {
  227. return ObsoleteEncodeCertX(
  228. TRUE, //unicode
  229. pbIn,
  230. cbIn,
  231. Flags,
  232. (void**)ppchOut,
  233. pcchOut);
  234. }
  235. DWORD // cchHeader
  236. ObsoleteSizeBase64HeaderA(
  237. IN char const *pchIn,
  238. IN DWORD cchIn,
  239. IN BOOL fBegin,
  240. OUT DWORD *pcchSkip)
  241. {
  242. DWORD cchHeader = 0;
  243. DWORD cb;
  244. //??? fBegin and pcchSkip are ignored???
  245. if (!CryptStringToBinaryA(
  246. pchIn,
  247. cchIn,
  248. CRYPT_STRING_ANY,
  249. NULL,
  250. &cb,
  251. &cchHeader,
  252. NULL))
  253. {
  254. _PrintError(myHLastError(), "CryptStringToBinaryA");
  255. cchHeader = MAXDWORD;
  256. }
  257. return cchHeader;
  258. }
  259. DWORD // cchHeader
  260. ObsoleteSizeBase64HeaderW(
  261. IN WCHAR const *pchIn,
  262. IN DWORD cchIn,
  263. IN BOOL fBegin,
  264. OUT DWORD *pcchSkip)
  265. {
  266. DWORD cchHeader = 0;
  267. DWORD cb;
  268. //??? fBegin and pcchSkip are ignored???
  269. if (!CryptStringToBinaryW(
  270. pchIn,
  271. cchIn,
  272. CRYPT_STRING_ANY,
  273. NULL,
  274. &cb,
  275. &cchHeader,
  276. NULL))
  277. {
  278. _PrintError(myHLastError(), "CryptStringToBinaryW");
  279. cchHeader = MAXDWORD;
  280. }
  281. return cchHeader;
  282. }
  283. HRESULT
  284. ObsoleteHexDecodeA(
  285. IN CHAR const *pchIn,
  286. IN DWORD cchIn,
  287. IN DWORD Flags,
  288. OPTIONAL OUT BYTE *pbOut,
  289. IN OUT DWORD *pcbOut)
  290. {
  291. HRESULT hr = S_OK;
  292. if (!CryptStringToBinaryA(pchIn, cchIn, Flags, pbOut, pcbOut, NULL, NULL))
  293. {
  294. hr = myHLastError();
  295. }
  296. return(hr);
  297. }
  298. HRESULT
  299. ObsoleteHexDecodeW(
  300. IN WCHAR const *pchIn,
  301. IN DWORD cchIn,
  302. IN DWORD Flags,
  303. OPTIONAL OUT BYTE *pbOut,
  304. IN OUT DWORD *pcbOut)
  305. {
  306. HRESULT hr = S_OK;
  307. if (!CryptStringToBinaryW(pchIn, cchIn, Flags, pbOut, pcbOut, NULL, NULL))
  308. {
  309. hr = myHLastError();
  310. }
  311. return(hr);
  312. }
  313. HRESULT
  314. ObsoleteHexEncodeA(
  315. IN BYTE const *pbIn,
  316. IN DWORD cbIn,
  317. IN DWORD Flags,
  318. OPTIONAL OUT CHAR *pchOut,
  319. IN OUT DWORD *pcchOut)
  320. {
  321. HRESULT hr = S_OK;
  322. if (!CryptBinaryToStringA(pbIn, cbIn, Flags, pchOut, pcchOut))
  323. {
  324. hr = myHLastError();
  325. }
  326. return(hr);
  327. }
  328. HRESULT
  329. ObsoleteHexEncodeW(
  330. IN BYTE const *pbIn,
  331. IN DWORD cbIn,
  332. IN DWORD Flags,
  333. OPTIONAL OUT WCHAR *pchOut,
  334. IN OUT DWORD *pcchOut)
  335. {
  336. HRESULT hr = S_OK;
  337. if (!CryptBinaryToStringW(pbIn, cbIn, Flags, pchOut, pcchOut))
  338. {
  339. hr = myHLastError();
  340. }
  341. return(hr);
  342. }
  343. HRESULT
  344. ObsoleteBase64DecodeA(
  345. IN CHAR const *pchIn,
  346. IN DWORD cchIn,
  347. OPTIONAL OUT BYTE *pbOut,
  348. IN OUT DWORD *pcbOut)
  349. {
  350. HRESULT hr = S_OK;
  351. if (!CryptStringToBinaryA(
  352. pchIn,
  353. cchIn,
  354. CRYPT_STRING_BASE64,
  355. pbOut,
  356. pcbOut,
  357. NULL,
  358. NULL))
  359. {
  360. hr = myHLastError();
  361. }
  362. return(hr);
  363. }
  364. HRESULT
  365. ObsoleteBase64DecodeW(
  366. IN WCHAR const *pchIn,
  367. IN DWORD cchIn,
  368. OPTIONAL OUT BYTE *pbOut,
  369. IN OUT DWORD *pcbOut)
  370. {
  371. HRESULT hr = S_OK;
  372. if (!CryptStringToBinaryW(
  373. pchIn,
  374. cchIn,
  375. CRYPT_STRING_BASE64,
  376. pbOut,
  377. pcbOut,
  378. NULL,
  379. NULL))
  380. {
  381. hr = myHLastError();
  382. }
  383. return(hr);
  384. }
  385. HRESULT
  386. ObsoleteBase64EncodeA(
  387. IN BYTE const *pbIn,
  388. IN DWORD cbIn,
  389. OPTIONAL OUT CHAR *pchOut,
  390. IN OUT DWORD *pcchOut)
  391. {
  392. HRESULT hr = S_OK;
  393. if (!CryptBinaryToStringA(pbIn, cbIn, CRYPT_STRING_BASE64, pchOut, pcchOut))
  394. {
  395. hr = myHLastError();
  396. }
  397. return(hr);
  398. }
  399. HRESULT
  400. ObsoleteBase64EncodeW(
  401. IN BYTE const *pbIn,
  402. IN DWORD cbIn,
  403. OPTIONAL OUT WCHAR *pchOut,
  404. IN OUT DWORD *pcchOut)
  405. {
  406. HRESULT hr = S_OK;
  407. if (!CryptBinaryToStringW(pbIn, cbIn, CRYPT_STRING_BASE64, pchOut, pcchOut))
  408. {
  409. hr = myHLastError();
  410. }
  411. return(hr);
  412. }