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