ReactOS  0.4.15-dev-1074-gffc96d2
shellstring.c
Go to the documentation of this file.
1 /*
2  * Copyright 2000 Juergen Schmied
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 
24 #include <windef.h>
25 #include <winbase.h>
26 #include <shlobj.h>
27 #include <shlwapi.h>
28 #include <wine/unicode.h>
29 #include <wine/debug.h>
30 
31 #include "shell32_main.h"
32 #include "undocshell.h"
33 #include "shlwapi_undoc.h"
34 
36 
37 /************************* STRRET functions ****************************/
38 
39 static const char *debugstr_strret(STRRET *s)
40 {
41  switch (s->uType)
42  {
43  case STRRET_WSTR:
44  return "STRRET_WSTR";
45  case STRRET_CSTR:
46  return "STRRET_CSTR";
47  case STRRET_OFFSET:
48  return "STRRET_OFFSET";
49  default:
50  return "STRRET_???";
51  }
52 }
53 
55 {
56  TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
57 
58  if (!dest)
59  return FALSE;
60 
61  switch (src->uType)
62  {
63  case STRRET_WSTR:
64  WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
65  CoTaskMemFree(src->u.pOleStr);
66  break;
67  case STRRET_CSTR:
68  lstrcpynA(dest, src->u.cStr, len);
69  break;
70  case STRRET_OFFSET:
71  lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
72  break;
73  default:
74  FIXME("unknown type %u!\n", src->uType);
75  if (len)
76  *dest = '\0';
77  return FALSE;
78  }
79  TRACE("-- %s\n", debugstr_a(dest) );
80  return TRUE;
81 }
82 
83 /************************************************************************/
84 
86 {
87  TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
88 
89  if (!dest)
90  return FALSE;
91 
92  switch (src->uType)
93  {
94  case STRRET_WSTR:
95  lstrcpynW(dest, src->u.pOleStr, len);
96  CoTaskMemFree(src->u.pOleStr);
97  break;
98  case STRRET_CSTR:
99  if (!MultiByteToWideChar(CP_ACP, 0, src->u.cStr, -1, dest, len) && len)
100  dest[len-1] = 0;
101  break;
102  case STRRET_OFFSET:
103  if (!MultiByteToWideChar(CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len)
104  && len)
105  dest[len-1] = 0;
106  break;
107  default:
108  FIXME("unknown type %u!\n", src->uType);
109  if (len)
110  *dest = '\0';
111  return FALSE;
112  }
113  return TRUE;
114 }
115 
116 
117 /*************************************************************************
118  * StrRetToStrN [SHELL32.96]
119  *
120  * converts a STRRET to a normal string
121  *
122  * NOTES
123  * the pidl is for STRRET OFFSET
124  */
126 {
127  if(SHELL_OsIsUnicode())
128  return StrRetToStrNW(dest, len, src, pidl);
129  else
130  return StrRetToStrNA(dest, len, src, pidl);
131 }
132 
133 /************************* OLESTR functions ****************************/
134 
135 /************************************************************************
136  * StrToOleStr [SHELL32.163]
137  *
138  */
139 static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
140 {
141  TRACE("(%p, %p %s)\n",
142  lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
143 
144  return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
145 
146 }
147 static int StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
148 {
149  TRACE("(%p, %p %s)\n",
150  lpWideCharStr, lpWString, debugstr_w(lpWString));
151 
152  strcpyW (lpWideCharStr, lpWString );
153  return strlenW(lpWideCharStr);
154 }
155 
156 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
157 {
158  if (SHELL_OsIsUnicode())
159  return StrToOleStrW (lpWideCharStr, lpString);
160  return StrToOleStrA (lpWideCharStr, lpString);
161 }
162 
163 /*************************************************************************
164  * StrToOleStrN [SHELL32.79]
165  * lpMulti, nMulti, nWide [IN]
166  * lpWide [OUT]
167  */
168 static BOOL StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
169 {
170  TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
171  return MultiByteToWideChar (CP_ACP, 0, lpStrA, nStr, lpWide, nWide);
172 }
173 static BOOL StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
174 {
175  TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
176 
177  if (lstrcpynW (lpWide, lpStrW, nWide))
178  { return lstrlenW (lpWide);
179  }
180  return FALSE;
181 }
182 
183 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
184 {
185  if (SHELL_OsIsUnicode())
186  return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
187  return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
188 }
189 
190 /*************************************************************************
191  * OleStrToStrN [SHELL32.78]
192  */
193 static BOOL OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
194 {
195  TRACE("(%p, %x, %s, %x)\n", lpStr, nStr, debugstr_wn(lpOle,nOle), nOle);
196  return WideCharToMultiByte (CP_ACP, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
197 }
198 
199 static BOOL OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
200 {
201  TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
202 
203  if (lstrcpynW ( lpwStr, lpOle, nwStr))
204  { return lstrlenW (lpwStr);
205  }
206  return FALSE;
207 }
208 
209 BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
210 {
211  if (SHELL_OsIsUnicode())
212  return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
213  return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
214 }
215 
216 
217 /*************************************************************************
218  * CheckEscapesA [SHELL32.@]
219  *
220  * Checks a string for special characters which are not allowed in a path
221  * and encloses it in quotes if that is the case.
222  *
223  * PARAMS
224  * string [I/O] string to check and on return eventually quoted
225  * len [I] length of string
226  */
228  LPSTR string, /* [I/O] string to check ??*/
229  DWORD len) /* [I] is 0 */
230 {
231  LPWSTR wString;
232  TRACE("(%s %d)\n", debugstr_a(string), len);
233 
234  if (!string || !string[0])
235  return;
236 
237  wString = LocalAlloc(LPTR, len * sizeof(WCHAR));
238  if (!wString)
239  return;
240 
241  SHAnsiToUnicode(string, wString, len);
242  CheckEscapesW(wString, len);
243  SHUnicodeToAnsi(wString, string, len);
244  LocalFree(wString);
245 }
246 
247 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
248 
249 /*************************************************************************
250  * CheckEscapesW [SHELL32.@]
251  *
252  * See CheckEscapesA.
253  */
255  LPWSTR string,
256  DWORD len)
257 {
258  DWORD size = lstrlenW(string);
259  LPWSTR s, d;
260 
261  TRACE("(%s %d) stub\n", debugstr_w(string), len);
262 
263  if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
264  {
265  s = &string[size - 1];
266  d = &string[size + 2];
267  *d-- = 0;
268  *d-- = '"';
269  for (;d > string;)
270  *d-- = *s--;
271  *d = '"';
272  }
273 }
LPWSTR WINAPI StrPBrkW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
Definition: string.c:1276
VOID WINAPI CoTaskMemFree(LPVOID ptr)
Definition: ifs.c:442
WINE_UNICODE_INLINE unsigned int strlenW(const WCHAR *str)
Definition: unicode.h:212
#define WideCharToMultiByte
Definition: compat.h:111
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define TRUE
Definition: types.h:120
#define CP_ACP
Definition: compat.h:109
static BOOL OleStrToStrNA(LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
Definition: shellstring.c:193
static __inline BOOL SHELL_OsIsUnicode(void)
Definition: shell32_main.h:130
char * LPSTR
Definition: xmlstorage.h:182
VOID WINAPI CheckEscapesW(LPWSTR string, DWORD len)
Definition: shellstring.c:254
#define lstrlenW
Definition: compat.h:498
int32_t INT
Definition: typedefs.h:58
#define lstrcpynW
Definition: compat.h:486
static int StrToOleStrA(LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
Definition: shellstring.c:139
static const WCHAR strEscapedChars[]
Definition: shellstring.c:247
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL StrToOleStrNW(LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
Definition: shellstring.c:173
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:111
smooth NULL
Definition: ftsmooth.c:416
static BOOL StrToOleStrNA(LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
Definition: shellstring.c:168
const char * LPCSTR
Definition: xmlstorage.h:183
VOID WINAPI CheckEscapesA(LPSTR string, DWORD len)
Definition: shellstring.c:227
#define TRACE(s)
Definition: solgame.cpp:4
GLsizeiptr size
Definition: glext.h:5919
#define d
Definition: ke_i.h:81
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define debugstr_a
Definition: kernel32.h:31
WINE_DEFAULT_DEBUG_CHANNEL(shell)
#define MAX_PATH
Definition: compat.h:34
#define WINAPI
Definition: msvc.h:6
DWORD WINAPI SHAnsiToUnicode(LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
Definition: string.c:2659
unsigned long DWORD
Definition: ntddk_ex.h:95
static __inline const char * debugstr_an(const char *s, int n)
Definition: compat.h:55
GLenum GLsizei len
Definition: glext.h:6722
INT WINAPI SHUnicodeToAnsi(LPCWSTR lpSrcStr, LPSTR lpDstStr, INT iLen)
Definition: string.c:2783
GLdouble s
Definition: gl.h:2039
GLenum src
Definition: glext.h:6340
#define debugstr_wn
Definition: kernel32.h:33
char string[160]
Definition: util.h:11
WINE_UNICODE_INLINE WCHAR * strcpyW(WCHAR *dst, const WCHAR *src)
Definition: unicode.h:219
static const char * debugstr_strret(STRRET *s)
Definition: shellstring.c:39
void shell(int argc, const char *argv[])
Definition: cmds.c:1231
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define lstrcpynA
Definition: compat.h:499
#define MultiByteToWideChar
Definition: compat.h:110
CONST void * LPCVOID
Definition: windef.h:191
static int StrToOleStrW(LPWSTR lpWideCharStr, LPCWSTR lpWString)
Definition: shellstring.c:147
#define LPTR
Definition: winbase.h:362
static BOOL OleStrToStrNW(LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
Definition: shellstring.c:199
static char * dest
Definition: rtl.c:135
SHITEMID mkid
Definition: shtypes.idl:34
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL WINAPI StrToOleStrNAW(LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
Definition: shellstring.c:183
BOOL WINAPI StrRetToStrNAW(LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:125
BOOL WINAPI StrToOleStrAW(LPWSTR lpWideCharStr, LPCVOID lpString)
Definition: shellstring.c:156
BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:54
BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:85
BOOL WINAPI OleStrToStrNAW(LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
Definition: shellstring.c:209