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.

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