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.

474 lines
13 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: ascii.cxx
  7. //
  8. // Contents: char to WCHAR conversion layer
  9. //
  10. // Notes: Most of the functions that provide the conversions are
  11. // here. Note that is layer will not be present for _UNICODE
  12. // builds
  13. //
  14. //---------------------------------------------------------------
  15. #ifndef _UNICODE // If UNICODE is defined, none of this is necessary
  16. #include "exphead.cxx"
  17. #include "expdf.hxx"
  18. #include "expiter.hxx"
  19. #include "expst.hxx"
  20. #include "ascii.hxx"
  21. extern "C" {
  22. #include <string.h>
  23. };
  24. //+--------------------------------------------------------------
  25. //
  26. // Function: ValidateSNBA, private
  27. //
  28. // Synopsis: Validates a char SNB
  29. //
  30. // Arguments: [snb] - SNB
  31. //
  32. // Returns: Appropriate status code
  33. //
  34. //---------------------------------------------------------------
  35. static SCODE ValidateSNBA(SNB snb)
  36. {
  37. SCODE sc;
  38. for (;;)
  39. {
  40. olChk(ValidatePtrBuffer(snb));
  41. if (*snb == NULL)
  42. break;
  43. olChk(ValidateNameA(*snb, CBMAXPATHCOMPLEN));
  44. snb++;
  45. }
  46. return S_OK;
  47. EH_Err:
  48. return sc;
  49. }
  50. //+--------------------------------------------------------------
  51. //
  52. // Function: SNBToSNBW, private
  53. //
  54. // Synopsis: Converts a char SNB to a WCHAR SNB
  55. //
  56. // Arguments: [snbIn] - char SNB
  57. //
  58. // Returns: WCHAR SNB or NULL
  59. //
  60. //---------------------------------------------------------------
  61. static SNBW SNBToSNBW(SNB snbIn)
  62. {
  63. ULONG cbStrings = 0;
  64. SNB snb;
  65. ULONG cItems = 0;
  66. SNBW snbw, snbwOut;
  67. WCHAR *pwcs;
  68. BYTE *pb;
  69. for (snb = snbIn; *snb; snb++, cItems++)
  70. cbStrings += (strlen(*snb)+1)*sizeof(WCHAR);
  71. cItems++;
  72. pb = new BYTE[(size_t)(cbStrings+sizeof(WCHAR *)*cItems)];
  73. if (pb == NULL)
  74. return NULL;
  75. snbwOut = (SNBW)pb;
  76. pwcs = (WCHAR *)(pb+sizeof(WCHAR *)*cItems);
  77. for (snb = snbIn, snbw = snbwOut; *snb; snb++, snbw++)
  78. {
  79. *snbw = pwcs;
  80. _tbstowcs(*snbw, *snb, strlen(*snb)+1);
  81. pwcs += wcslen(*snbw)+1;
  82. }
  83. *snbw = NULL;
  84. return snbwOut;
  85. }
  86. //+--------------------------------------------------------------
  87. //
  88. // Function: CheckAName, public
  89. //
  90. // Synopsis: Checks name for illegal characters and length
  91. //
  92. // Arguments: [pwcsName] - Name
  93. //
  94. // Returns: Appropriate status code
  95. //
  96. //---------------------------------------------------------------
  97. const char INVALIDCHARS[] = "\\/:!";
  98. SCODE CheckAName(char const *pwcsName)
  99. {
  100. SCODE sc;
  101. olDebugOut((DEB_ITRACE, "In CheckAName(%s)\n", pwcsName));
  102. if (FAILED(sc = ValidateNameA(pwcsName, CBMAXPATHCOMPLEN)))
  103. return sc;
  104. // >= is used because the max len includes the null terminator
  105. if (strlen(pwcsName) >= CWCMAXPATHCOMPLEN)
  106. return STG_E_INVALIDNAME;
  107. for (; *pwcsName; pwcsName++)
  108. if (strchr(INVALIDCHARS, (int)*pwcsName))
  109. return STG_E_INVALIDNAME;
  110. olDebugOut((DEB_ITRACE, "Out CheckAName\n"));
  111. return S_OK;
  112. }
  113. //+--------------------------------------------------------------
  114. //
  115. // Member: CExposedIterator::Next, public
  116. //
  117. // Synopsis: char version
  118. //
  119. //---------------------------------------------------------------
  120. STDMETHODIMP CExposedIterator::Next(ULONG celt,
  121. STATSTG FAR *rgelt,
  122. ULONG *pceltFetched)
  123. {
  124. SCODE sc;
  125. ULONG i;
  126. ULONG cnt;
  127. olAssert(sizeof(STATSTG) == sizeof(STATSTGW));
  128. olChk(sc = Next(celt, (STATSTGW *)rgelt, &cnt));
  129. for (i = 0; i<cnt; i++)
  130. if (rgelt[i].pwcsName)
  131. _wcstotbs(rgelt[i].pwcsName, (WCHAR *)rgelt[i].pwcsName,
  132. CWCSTORAGENAME);
  133. if (pceltFetched)
  134. *pceltFetched = cnt;
  135. EH_Err:
  136. return ResultFromScode(sc);
  137. }
  138. //+--------------------------------------------------------------
  139. //
  140. // Member: CExposedStream::Stat, public
  141. //
  142. // Synopsis: char version
  143. //
  144. //---------------------------------------------------------------
  145. STDMETHODIMP CExposedStream::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
  146. {
  147. SCODE sc;
  148. olAssert(sizeof(STATSTG) == sizeof(STATSTGW));
  149. olChk(sc = Stat((STATSTGW *)pstatstg, grfStatFlag));
  150. if (pstatstg->pwcsName)
  151. _wcstotbs(pstatstg->pwcsName, (WCHAR *)pstatstg->pwcsName,
  152. CWCSTORAGENAME);
  153. EH_Err:
  154. return ResultFromScode(sc);
  155. }
  156. //+--------------------------------------------------------------
  157. //
  158. // Member: CExposedDocFile::Stat, public
  159. //
  160. // Synopsis: char version
  161. //
  162. //---------------------------------------------------------------
  163. STDMETHODIMP CExposedDocFile::Stat(STATSTG *pstatstg, DWORD grfStatFlag)
  164. {
  165. SCODE sc;
  166. olAssert(sizeof(STATSTG) == sizeof(STATSTGW));
  167. // call the virtual (wide char) function
  168. olChk(sc = this->Stat((STATSTGW *)pstatstg, grfStatFlag));
  169. if (pstatstg->pwcsName)
  170. _wcstotbs(pstatstg->pwcsName, (WCHAR *)pstatstg->pwcsName,
  171. _MAX_PATH);
  172. EH_Err:
  173. return ResultFromScode(sc);
  174. }
  175. //+--------------------------------------------------------------
  176. //
  177. // Member: CExposedDocFile::CreateStream, public
  178. //
  179. // Synopsis: char version
  180. //
  181. //---------------------------------------------------------------
  182. STDMETHODIMP CExposedDocFile::CreateStream(char const *pszName,
  183. DWORD grfMode,
  184. DWORD reserved1,
  185. DWORD reserved2,
  186. IStream **ppstm)
  187. {
  188. SCODE sc;
  189. WCHAR wcsName[CWCSTORAGENAME];
  190. olChk(CheckAName(pszName));
  191. _tbstowcs(wcsName, pszName, CWCSTORAGENAME);
  192. sc = CreateStream(wcsName, grfMode, reserved1, reserved2, ppstm);
  193. // Fall through
  194. EH_Err:
  195. return ResultFromScode(sc);
  196. }
  197. //+--------------------------------------------------------------
  198. //
  199. // Member: CExposedDocFile::OpenStream, public
  200. //
  201. // Synopsis: char version
  202. //
  203. //---------------------------------------------------------------
  204. STDMETHODIMP CExposedDocFile::OpenStream(char const *pszName,
  205. void *reserved1,
  206. DWORD grfMode,
  207. DWORD reserved2,
  208. IStream **ppstm)
  209. {
  210. SCODE sc;
  211. WCHAR wcsName[CWCSTORAGENAME];
  212. olChk(CheckAName(pszName));
  213. _tbstowcs(wcsName, pszName, CWCSTORAGENAME);
  214. sc = OpenStream(wcsName, reserved1, grfMode, reserved2, ppstm);
  215. // Fall through
  216. EH_Err:
  217. return ResultFromScode(sc);
  218. }
  219. //+--------------------------------------------------------------
  220. //
  221. // Member: CExposedDocFile::CreateStorage, public
  222. //
  223. // Synopsis: char version
  224. //
  225. //---------------------------------------------------------------
  226. STDMETHODIMP CExposedDocFile::CreateStorage(char const *pszName,
  227. DWORD grfMode,
  228. DWORD reserved1,
  229. DWORD reserved2,
  230. IStorage **ppstg)
  231. {
  232. SCODE sc;
  233. WCHAR wcsName[CWCSTORAGENAME];
  234. olChk(CheckAName(pszName));
  235. _tbstowcs(wcsName, pszName, CWCSTORAGENAME);
  236. sc = CreateStorage(wcsName, grfMode, reserved1, reserved2, ppstg);
  237. // Fall through
  238. EH_Err:
  239. return ResultFromScode(sc);
  240. }
  241. //+--------------------------------------------------------------
  242. //
  243. // Member: CExposedDocFile::OpenStorage, public
  244. //
  245. // Synopsis: char version
  246. //
  247. //---------------------------------------------------------------
  248. STDMETHODIMP CExposedDocFile::OpenStorage(char const *pszName,
  249. IStorage *pstgPriority,
  250. DWORD grfMode,
  251. SNB snbExclude,
  252. DWORD reserved,
  253. IStorage **ppstg)
  254. {
  255. SNBW snbw;
  256. SCODE sc;
  257. WCHAR wcsName[CWCSTORAGENAME];
  258. olChk(CheckAName(pszName));
  259. _tbstowcs(wcsName, pszName, CWCSTORAGENAME);
  260. if (snbExclude)
  261. olChk(STG_E_INVALIDFUNCTION);
  262. else
  263. snbw = NULL;
  264. sc = OpenStorage(wcsName, pstgPriority, grfMode, snbw,
  265. reserved, ppstg);
  266. delete snbw;
  267. EH_Err:
  268. return ResultFromScode(sc);
  269. }
  270. //+--------------------------------------------------------------
  271. //
  272. // Member: CExposedDocFile::DestroyElement, public
  273. //
  274. // Synopsis: char version
  275. //
  276. //---------------------------------------------------------------
  277. STDMETHODIMP CExposedDocFile::DestroyElement(char const *pszName)
  278. {
  279. SCODE sc;
  280. WCHAR wcsName[CWCSTORAGENAME];
  281. olChk(CheckAName(pszName));
  282. _tbstowcs(wcsName, pszName, CWCSTORAGENAME);
  283. sc = DestroyElement(wcsName);
  284. // Fall through
  285. EH_Err:
  286. return ResultFromScode(sc);
  287. }
  288. //+--------------------------------------------------------------
  289. //
  290. // Member: CExposedDocFile::RenameElement, public
  291. //
  292. // Synopsis: char version
  293. //
  294. //---------------------------------------------------------------
  295. STDMETHODIMP CExposedDocFile::RenameElement(char const *pszOldName,
  296. char const *pszNewName)
  297. {
  298. SCODE sc;
  299. WCHAR wcsOldName[CWCSTORAGENAME], wcsNewName[CWCSTORAGENAME];
  300. olChk(CheckAName(pszOldName));
  301. olChk(CheckAName(pszNewName));
  302. _tbstowcs(wcsOldName, pszOldName, CWCSTORAGENAME);
  303. _tbstowcs(wcsNewName, pszNewName, CWCSTORAGENAME);
  304. sc = RenameElement(wcsOldName, wcsNewName);
  305. // Fall through
  306. EH_Err:
  307. return ResultFromScode(sc);
  308. }
  309. //+--------------------------------------------------------------
  310. //
  311. // Member: CExposedDocFile::CopyTo, public
  312. //
  313. // Synopsis: ANSI version
  314. //
  315. //---------------------------------------------------------------
  316. STDMETHODIMP CExposedDocFile::CopyTo(DWORD ciidExclude,
  317. IID const *rgiidExclude,
  318. SNB snbExclude,
  319. IStorage *pstgDest)
  320. {
  321. SNBW snbw;
  322. SCODE sc;
  323. if (snbExclude)
  324. {
  325. olChk(ValidateSNBA(snbExclude));
  326. olMem(snbw = SNBToSNBW(snbExclude));
  327. }
  328. else
  329. snbw = NULL;
  330. sc = CopyTo(ciidExclude, rgiidExclude, snbw, pstgDest);
  331. delete snbw;
  332. EH_Err:
  333. return ResultFromScode(sc);
  334. }
  335. //+--------------------------------------------------------------
  336. //
  337. // Member: CExposedDocFile::MoveElementTo, public
  338. //
  339. // Synopsis: ANSI version
  340. //
  341. //---------------------------------------------------------------
  342. STDMETHODIMP CExposedDocFile::MoveElementTo(TCHAR const *lpszName,
  343. IStorage *pstgDest,
  344. TCHAR const *lpszNewName,
  345. DWORD grfFlags)
  346. {
  347. SCODE sc;
  348. WCHAR wcsName[CWCSTORAGENAME];
  349. olChk(CheckAName(lpszName));
  350. _tbstowcs(wcsName, lpszName, CWCSTORAGENAME);
  351. sc = MoveElementTo(wcsName, pstgDest, lpszNewName, grfFlags);
  352. EH_Err:
  353. return ResultFromScode(sc);
  354. }
  355. //+--------------------------------------------------------------
  356. //
  357. // Member: CExposedDocFile::SetElementTimes, public
  358. //
  359. // Synopsis: ANSI version
  360. //
  361. //---------------------------------------------------------------
  362. STDMETHODIMP CExposedDocFile::SetElementTimes(TCHAR const *lpszName,
  363. FILETIME const *pctime,
  364. FILETIME const *patime,
  365. FILETIME const *pmtime)
  366. {
  367. SCODE sc;
  368. WCHAR wcsName[CWCSTORAGENAME];
  369. olChk(CheckAName(lpszName));
  370. _tbstowcs(wcsName, lpszName, CWCSTORAGENAME);
  371. sc = SetElementTimes(wcsName, pctime, patime, pmtime);
  372. // Fall through
  373. EH_Err:
  374. return ResultFromScode(sc);
  375. }
  376. //+--------------------------------------------------------------
  377. //
  378. // Function: DfOpenStorageOnILockBytes, public
  379. //
  380. // Synopsis: char version
  381. //
  382. //---------------------------------------------------------------
  383. HRESULT DfOpenStorageOnILockBytes(ILockBytes *plkbyt,
  384. IStorage *pstgPriority,
  385. DWORD grfMode,
  386. SNB snbExclude,
  387. DWORD reserved,
  388. IStorage **ppstgOpen,
  389. CLSID *pcid)
  390. {
  391. SNBW snbw;
  392. SCODE sc;
  393. olChk(ValidatePtrBuffer(ppstgOpen));
  394. *ppstgOpen = NULL;
  395. if (snbExclude)
  396. {
  397. olChk(ValidateSNBA(snbExclude));
  398. olMem(snbw = SNBToSNBW(snbExclude));
  399. }
  400. else
  401. snbw = NULL;
  402. sc = DfOpenStorageOnILockBytesW(plkbyt, pstgPriority, grfMode,
  403. snbw, reserved, ppstgOpen, pcid);
  404. delete snbw;
  405. EH_Err:
  406. return ResultFromScode(sc);
  407. }
  408. #endif // ifndef _UNICODE