Work around for deref issues (least impact solution taken, even if issues remain).
[wine/multimedia.git] / memory / string.c
blobcdd22bd5bcf5e87da24fa9b10dac441bdc601836
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "wine/exception.h"
15 #include "wine/unicode.h"
16 #include "winerror.h"
17 #include "winnls.h"
18 #include "ldt.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(string);
23 /* filter for page-fault exceptions */
24 static WINE_EXCEPTION_FILTER(page_fault)
26 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
27 return EXCEPTION_EXECUTE_HANDLER;
28 return EXCEPTION_CONTINUE_SEARCH;
32 /***********************************************************************
33 * hmemcpy16 (KERNEL.348)
35 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
37 memcpy( dst, src, count );
41 /***********************************************************************
42 * lstrcat16 (KERNEL.89)
44 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
46 /* Windows does not check for NULL pointers here, so we don't either */
47 strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
48 return dst;
52 /***********************************************************************
53 * lstrcatA (KERNEL32.599)
55 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
57 __TRY
59 strcat( dst, src );
61 __EXCEPT(page_fault)
63 SetLastError( ERROR_INVALID_PARAMETER );
64 return NULL;
66 __ENDTRY
67 return dst;
71 /***********************************************************************
72 * lstrcatW (KERNEL32.600)
74 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
76 __TRY
78 strcatW( dst, src );
80 __EXCEPT(page_fault)
82 SetLastError( ERROR_INVALID_PARAMETER );
83 return NULL;
85 __ENDTRY
86 return dst;
90 /***********************************************************************
91 * lstrcatn16 (KERNEL.352)
93 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
95 LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
97 while (*p) p++;
98 if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
99 lstrcpynA( p, src, n );
100 return dst;
104 /***********************************************************************
105 * lstrcmpA (KERNEL.602)
107 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
109 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
113 /***********************************************************************
114 * lstrcmpW (KERNEL.603)
115 * FIXME : should call CompareStringW, when it is implemented.
116 * This implementation is not "word sort", as it should.
118 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
120 TRACE("%s and %s\n",
121 debugstr_w (str1), debugstr_w (str2));
122 if (!str1 || !str2) {
123 SetLastError(ERROR_INVALID_PARAMETER);
124 return 0;
126 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
127 return (INT)(*str1 - *str2);
131 /***********************************************************************
132 * lstrcmpiA (KERNEL32.605)
134 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
135 { TRACE("strcmpi %s and %s\n",
136 debugstr_a (str1), debugstr_a (str2));
137 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
141 /***********************************************************************
142 * lstrcmpiW (KERNEL32.606)
144 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
146 if (!str1 || !str2) {
147 SetLastError(ERROR_INVALID_PARAMETER);
148 return 0;
150 return strcmpiW( str1, str2 );
154 /***********************************************************************
155 * lstrcpy16 (KERNEL.88)
157 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
159 if (!lstrcpyA( PTR_SEG_TO_LIN(dst), src )) dst = 0;
160 return dst;
164 /***********************************************************************
165 * lstrcpyA (KERNEL32.608)
167 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
169 __TRY
171 /* this is how Windows does it */
172 memmove( dst, src, strlen(src)+1 );
174 __EXCEPT(page_fault)
176 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
177 SetLastError( ERROR_INVALID_PARAMETER );
178 return NULL;
180 __ENDTRY
181 return dst;
185 /***********************************************************************
186 * lstrcpyW (KERNEL32.609)
188 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
190 __TRY
192 strcpyW( dst, src );
194 __EXCEPT(page_fault)
196 SetLastError( ERROR_INVALID_PARAMETER );
197 return NULL;
199 __ENDTRY
200 return dst;
204 /***********************************************************************
205 * lstrcpyn16 (KERNEL.353)
207 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
209 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
210 return dst;
214 /***********************************************************************
215 * lstrcpynA (KERNEL32.611)
216 * Note: this function differs from the UNIX strncpy, it _always_ writes
217 * a terminating \0
219 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
221 LPSTR p = dst;
222 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
223 /* In real windows the whole function is protected by an exception handler
224 * that returns ERROR_INVALID_PARAMETER on faulty parameters
225 * We currently just check for NULL.
227 if (!dst || !src) {
228 SetLastError(ERROR_INVALID_PARAMETER);
229 return 0;
231 while ((n-- > 1) && *src) *p++ = *src++;
232 if (n >= 0) *p = 0;
233 return dst;
237 /***********************************************************************
238 * lstrcpynW (KERNEL32.612)
239 * Note: this function differs from the UNIX strncpy, it _always_ writes
240 * a terminating \0
242 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
244 LPWSTR p = dst;
245 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
246 /* In real windows the whole function is protected by an exception handler
247 * that returns ERROR_INVALID_PARAMETER on faulty parameters
248 * We currently just check for NULL.
250 if (!dst || !src) {
251 SetLastError(ERROR_INVALID_PARAMETER);
252 return 0;
254 while ((n-- > 1) && *src) *p++ = *src++;
255 if (n >= 0) *p = 0;
256 return dst;
260 /***********************************************************************
261 * lstrlen16 (KERNEL.90)
263 INT16 WINAPI lstrlen16( LPCSTR str )
265 return (INT16)lstrlenA( str );
269 /***********************************************************************
270 * lstrlenA (KERNEL32.614)
272 INT WINAPI lstrlenA( LPCSTR str )
274 INT ret;
275 __TRY
277 ret = strlen(str);
279 __EXCEPT(page_fault)
281 SetLastError( ERROR_INVALID_PARAMETER );
282 return 0;
284 __ENDTRY
285 return ret;
289 /***********************************************************************
290 * lstrlenW (KERNEL32.615)
292 INT WINAPI lstrlenW( LPCWSTR str )
294 INT ret;
295 __TRY
297 ret = strlenW(str);
299 __EXCEPT(page_fault)
301 SetLastError( ERROR_INVALID_PARAMETER );
302 return 0;
304 __ENDTRY
305 return ret;
309 /***********************************************************************
310 * UnicodeToAnsi (KERNEL.434)
312 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
314 if ( codepage == -1 )
315 codepage = CP_ACP;
317 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );