1 | /* $Id: heapstring.cpp,v 1.55 2004-03-18 11:13:41 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
4 | *
|
---|
5 | * Win32 compatibility string functions for OS/2
|
---|
6 | *
|
---|
7 | * NOTE: lstrcpyn* always appends a terminating 0 (unlike strncpy)!
|
---|
8 | *
|
---|
9 | * Copyright 1999 Patrick Haller
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*****************************************************************************
|
---|
13 | * Includes *
|
---|
14 | *****************************************************************************/
|
---|
15 |
|
---|
16 | #include <odin.h>
|
---|
17 | #include <odinwrap.h>
|
---|
18 | #include <os2sel.h>
|
---|
19 |
|
---|
20 | #include <os2win.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <winnls.h>
|
---|
25 | #include <unicode.h>
|
---|
26 | #include <ctype.h>
|
---|
27 | #include <wchar.h>
|
---|
28 | #ifndef __GNUC__
|
---|
29 | #include <wcstr.h>
|
---|
30 | #endif
|
---|
31 | #include "heap.h"
|
---|
32 | #include <wine/unicode.h>
|
---|
33 | #include "misc.h"
|
---|
34 | #include "codepage.h"
|
---|
35 |
|
---|
36 | #define DBG_LOCALLOG DBG_heapstring
|
---|
37 | #include "dbglocal.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*****************************************************************************
|
---|
41 | * Defines *
|
---|
42 | *****************************************************************************/
|
---|
43 |
|
---|
44 | ODINDEBUGCHANNEL(KERNEL32-HEAPSTRING)
|
---|
45 |
|
---|
46 |
|
---|
47 | /*****************************************************************************
|
---|
48 | * Imported variables *
|
---|
49 | *****************************************************************************/
|
---|
50 |
|
---|
51 | // This macro could be mapped to GetProcessHeap() if there
|
---|
52 | // is any change in functionality
|
---|
53 | #define GetProcessHeap() Heap_ProcessHeap
|
---|
54 |
|
---|
55 | extern HANDLE Heap_ProcessHeap;
|
---|
56 |
|
---|
57 |
|
---|
58 | /*****************************************************************************
|
---|
59 | * Name :
|
---|
60 | * Purpose :
|
---|
61 | * Parameters:
|
---|
62 | * Variables :
|
---|
63 | * Result :
|
---|
64 | * Remark :
|
---|
65 | * Status :
|
---|
66 | *
|
---|
67 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
68 | *****************************************************************************/
|
---|
69 |
|
---|
70 | int WIN32API lstrlenA(LPCSTR arg1)
|
---|
71 | {
|
---|
72 | dprintf2(("KERNEL32: lstrlenA(%s)\n",
|
---|
73 | arg1));
|
---|
74 |
|
---|
75 | if(arg1 == NULL) {
|
---|
76 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
77 | return 0;
|
---|
78 | }
|
---|
79 | return strlen(arg1);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /*****************************************************************************
|
---|
84 | * Name :
|
---|
85 | * Purpose :
|
---|
86 | * Parameters:
|
---|
87 | * Variables :
|
---|
88 | * Result :
|
---|
89 | * Remark :
|
---|
90 | * Status :
|
---|
91 | *
|
---|
92 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
93 | *****************************************************************************/
|
---|
94 |
|
---|
95 | int WIN32API lstrlenW(LPCWSTR str)
|
---|
96 | {
|
---|
97 | if(str == NULL) {
|
---|
98 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 | const WCHAR *s = str;
|
---|
103 | while (*s) s++;
|
---|
104 | dprintf2(("KERNEL32: lstrlenW(%08xh) returns %d\n",
|
---|
105 | str, s - str));
|
---|
106 |
|
---|
107 | return s - str;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /*****************************************************************************
|
---|
112 | * Name :
|
---|
113 | * Purpose :
|
---|
114 | * Parameters:
|
---|
115 | * Variables :
|
---|
116 | * Result :
|
---|
117 | * Remark :
|
---|
118 | * Status :
|
---|
119 | *
|
---|
120 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
121 | *****************************************************************************/
|
---|
122 |
|
---|
123 | LPSTR WIN32API lstrcatA(LPSTR arg1, LPCSTR arg2)
|
---|
124 | {
|
---|
125 | dprintf2(("KERNEL32: lstrcat(%s,%s)\n",
|
---|
126 | arg1,
|
---|
127 | arg2));
|
---|
128 |
|
---|
129 | if(arg2 == NULL)
|
---|
130 | return arg1;
|
---|
131 | strcat(arg1, arg2);
|
---|
132 | return arg1;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /*****************************************************************************
|
---|
137 | * Name :
|
---|
138 | * Purpose :
|
---|
139 | * Parameters:
|
---|
140 | * Variables :
|
---|
141 | * Result :
|
---|
142 | * Remark :
|
---|
143 | * Status :
|
---|
144 | *
|
---|
145 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
146 | *****************************************************************************/
|
---|
147 |
|
---|
148 | LPWSTR WIN32API lstrcatW(LPWSTR dst, LPCWSTR src)
|
---|
149 | {
|
---|
150 | dprintf2(("KERNEL32: lstrcatW(%08xh,%08xh)\n",
|
---|
151 | dst, src));
|
---|
152 |
|
---|
153 | if(src == NULL)
|
---|
154 | return dst;
|
---|
155 |
|
---|
156 | strcpyW( dst + strlenW(dst), src );
|
---|
157 | return dst;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /*****************************************************************************
|
---|
162 | * Name :
|
---|
163 | * Purpose :
|
---|
164 | * Parameters:
|
---|
165 | * Variables :
|
---|
166 | * Result :
|
---|
167 | * Remark :
|
---|
168 | * Status :
|
---|
169 | *
|
---|
170 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
171 | *****************************************************************************/
|
---|
172 |
|
---|
173 | int WIN32API lstrcmpA(LPCSTR arg1, LPCSTR arg2)
|
---|
174 | {
|
---|
175 | dprintf2(("KERNEL32: lstrcmpA(%s,%s)\n",
|
---|
176 | arg1,
|
---|
177 | arg2));
|
---|
178 |
|
---|
179 | if(arg1 == NULL)
|
---|
180 | return -1;
|
---|
181 | if(arg2 == NULL)
|
---|
182 | return 1;
|
---|
183 |
|
---|
184 | return strcmp(arg1, arg2);
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /*****************************************************************************
|
---|
189 | * Name :
|
---|
190 | * Purpose :
|
---|
191 | * Parameters:
|
---|
192 | * Variables :
|
---|
193 | * Result :
|
---|
194 | * Remark :
|
---|
195 | * Status :
|
---|
196 | *
|
---|
197 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
198 | *****************************************************************************/
|
---|
199 |
|
---|
200 | int WIN32API lstrncmpA(LPCSTR arg1, LPCSTR arg2, int l)
|
---|
201 | {
|
---|
202 | dprintf2(("KERNEL32: lstrncmpA(%s,%s,%d)\n",
|
---|
203 | arg1,
|
---|
204 | arg2,
|
---|
205 | l));
|
---|
206 |
|
---|
207 | return strncmp(arg1, arg2, l);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /*****************************************************************************
|
---|
211 | * Name : lstrncmpiA
|
---|
212 | * Purpose :
|
---|
213 | * Parameters:
|
---|
214 | * Variables :
|
---|
215 | * Result :
|
---|
216 | * Remark :
|
---|
217 | * Status :
|
---|
218 | *
|
---|
219 | * Author : Przemyslaw Dobrowolski
|
---|
220 | *****************************************************************************/
|
---|
221 | int WIN32API lstrncmpiA(LPCSTR str1, LPCSTR str2, INT n )
|
---|
222 | {
|
---|
223 | INT firstch,lastch;
|
---|
224 | INT result = 0;
|
---|
225 |
|
---|
226 | if (n)
|
---|
227 | {
|
---|
228 | do
|
---|
229 | {
|
---|
230 | firstch = toupper(*str1);
|
---|
231 | lastch = toupper(*str2);
|
---|
232 | str1++;
|
---|
233 | str2++;
|
---|
234 | } while (--n && *str1 && *str2 && firstch == lastch);
|
---|
235 |
|
---|
236 | result = firstch - lastch;
|
---|
237 | }
|
---|
238 |
|
---|
239 | return(result);
|
---|
240 | }
|
---|
241 | //TODO: Don't know if this is completely correct
|
---|
242 | int WIN32API lstrncmpiW(LPCWSTR str1, LPCWSTR str2, int n)
|
---|
243 | {
|
---|
244 | INT firstch,lastch;
|
---|
245 | INT result = 0;
|
---|
246 |
|
---|
247 | if (n)
|
---|
248 | {
|
---|
249 | do
|
---|
250 | {
|
---|
251 | firstch = toupperW(*str1);
|
---|
252 | lastch = toupperW(*str2);
|
---|
253 | str1++;
|
---|
254 | str2++;
|
---|
255 | } while (--n && *str1 && *str2 && firstch == lastch);
|
---|
256 |
|
---|
257 | result = firstch - lastch;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return(result);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /*****************************************************************************
|
---|
264 | * Name :
|
---|
265 | * Purpose :
|
---|
266 | * Parameters:
|
---|
267 | * Variables :
|
---|
268 | * Result :
|
---|
269 | * Remark :
|
---|
270 | * Status :
|
---|
271 | *
|
---|
272 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
273 | *****************************************************************************/
|
---|
274 |
|
---|
275 | int WIN32API lstrcmpW(LPCWSTR arg1, LPCWSTR arg2)
|
---|
276 | {
|
---|
277 | dprintf2(("KERNEL32: lstrcmpW (%08xh, %08xh)\n",
|
---|
278 | arg1,
|
---|
279 | arg2));
|
---|
280 |
|
---|
281 | if(arg1 == NULL)
|
---|
282 | return -1;
|
---|
283 | if(arg2 == NULL)
|
---|
284 | return 1;
|
---|
285 |
|
---|
286 | return strcmpW( arg1, arg2 );
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | /*****************************************************************************
|
---|
291 | * Name :
|
---|
292 | * Purpose :
|
---|
293 | * Parameters:
|
---|
294 | * Variables :
|
---|
295 | * Result :
|
---|
296 | * Remark :
|
---|
297 | * Status :
|
---|
298 | *
|
---|
299 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
300 | *****************************************************************************/
|
---|
301 |
|
---|
302 | int WIN32API lstrncmpW(LPCWSTR arg1, LPCWSTR arg2, int l)
|
---|
303 | {
|
---|
304 | dprintf2(("KERNEL32: lstrncmpW(%08xh,%08xh,%d)\n",
|
---|
305 | arg1,
|
---|
306 | arg2,
|
---|
307 | l));
|
---|
308 |
|
---|
309 | return wcsncmp((wchar_t*)arg1,
|
---|
310 | (wchar_t*)arg2,
|
---|
311 | l);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*****************************************************************************
|
---|
315 | * Name :
|
---|
316 | * Purpose :
|
---|
317 | * Parameters:
|
---|
318 | * Variables :
|
---|
319 | * Result :
|
---|
320 | * Remark :
|
---|
321 | * Status :
|
---|
322 | *
|
---|
323 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
324 | *****************************************************************************/
|
---|
325 |
|
---|
326 | LPSTR WIN32API lstrcpyA(LPSTR dest, LPCSTR src)
|
---|
327 | {
|
---|
328 | if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
|
---|
329 | return NULL;
|
---|
330 |
|
---|
331 | return strcpy(dest, src);
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | /*****************************************************************************
|
---|
336 | * Name :
|
---|
337 | * Purpose :
|
---|
338 | * Parameters:
|
---|
339 | * Variables :
|
---|
340 | * Result :
|
---|
341 | * Remark :
|
---|
342 | * Status :
|
---|
343 | *
|
---|
344 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
345 | *****************************************************************************/
|
---|
346 |
|
---|
347 | LPWSTR WIN32API lstrcpyW(LPWSTR dest, LPCWSTR src)
|
---|
348 | {
|
---|
349 | if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
|
---|
350 | return NULL;
|
---|
351 |
|
---|
352 | WCHAR *p = dest;
|
---|
353 | while ((*p++ = *src++) != 0);
|
---|
354 |
|
---|
355 | return dest;
|
---|
356 | }
|
---|
357 |
|
---|
358 |
|
---|
359 | /*****************************************************************************
|
---|
360 | * Name :
|
---|
361 | * Purpose :
|
---|
362 | * Parameters:
|
---|
363 | * Variables :
|
---|
364 | * Result :
|
---|
365 | * Remark :
|
---|
366 | * Status :
|
---|
367 | *
|
---|
368 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
369 | *****************************************************************************/
|
---|
370 |
|
---|
371 | LPSTR WIN32API lstrcpynA(LPSTR arg1, LPCSTR arg2, int arg3)
|
---|
372 | {
|
---|
373 | register LPSTR p1 = arg1;
|
---|
374 | register LPSTR p2 = (LPSTR)arg2;
|
---|
375 |
|
---|
376 | if(arg3 == 0) {
|
---|
377 | return NULL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | //PH: looks like either \0 or arg3 terminate the copy
|
---|
381 | //return strncpy(arg1, arg2, arg3);
|
---|
382 | arg3--; // pre-decrement to avoid exceeding buffer length
|
---|
383 | // results in better code than (arg1 > 1)
|
---|
384 |
|
---|
385 | for (;*p2 && arg3; arg3--)
|
---|
386 | *p1++ = *p2++;
|
---|
387 |
|
---|
388 | *p1 = 0; //CB: copy arg-1, set end 0
|
---|
389 |
|
---|
390 | return arg1;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /*****************************************************************************
|
---|
395 | * Name :
|
---|
396 | * Purpose :
|
---|
397 | * Parameters:
|
---|
398 | * Variables :
|
---|
399 | * Result :
|
---|
400 | * Remark :
|
---|
401 | * Status :
|
---|
402 | *
|
---|
403 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
404 | *****************************************************************************/
|
---|
405 |
|
---|
406 | LPWSTR WIN32API lstrcpynW(LPWSTR dst, LPCWSTR src, int n)
|
---|
407 | {
|
---|
408 | LPWSTR p = dst;
|
---|
409 |
|
---|
410 | /* In real windows the whole function is protected by an exception handler
|
---|
411 | * that returns ERROR_INVALID_PARAMETER on faulty parameters
|
---|
412 | * We currently just check for NULL.
|
---|
413 | */
|
---|
414 | if (!dst || !src) {
|
---|
415 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
416 | return 0;
|
---|
417 | }
|
---|
418 | while ((n-- > 1) && *src) *p++ = *src++;
|
---|
419 | if (n >= 0) *p = 0;
|
---|
420 | return dst;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | /*****************************************************************************
|
---|
425 | * Name :
|
---|
426 | * Purpose :
|
---|
427 | * Parameters:
|
---|
428 | * Variables :
|
---|
429 | * Result :
|
---|
430 | * Remark :
|
---|
431 | * Status :
|
---|
432 | *
|
---|
433 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
434 | *****************************************************************************/
|
---|
435 |
|
---|
436 | int WIN32API lstrcmpiA(LPCSTR arg1, LPCSTR arg2)
|
---|
437 | {
|
---|
438 | dprintf2(("KERNEL32: lstrcmpiA(%s,%s)\n",
|
---|
439 | arg1,
|
---|
440 | arg2));
|
---|
441 |
|
---|
442 | if(arg1 == NULL)
|
---|
443 | return -1;
|
---|
444 |
|
---|
445 | if(arg2 == NULL)
|
---|
446 | return 1;
|
---|
447 |
|
---|
448 | return stricmp(arg1, arg2);
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | /*****************************************************************************
|
---|
453 | * Name :
|
---|
454 | * Purpose :
|
---|
455 | * Parameters:
|
---|
456 | * Variables :
|
---|
457 | * Result :
|
---|
458 | * Remark :
|
---|
459 | * Status :
|
---|
460 | *
|
---|
461 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
462 | *****************************************************************************/
|
---|
463 |
|
---|
464 | int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
|
---|
465 | {
|
---|
466 | if (!str1 || !str2) {
|
---|
467 |
|
---|
468 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
469 | return 0;
|
---|
470 | }
|
---|
471 | return strcmpiW( str1, str2 );
|
---|
472 | }
|
---|
473 |
|
---|
474 | //*****************************************************************************
|
---|
475 | //lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
|
---|
476 | //because Wine code depends on this behaviour (i.e. comdlg32)
|
---|
477 | //*****************************************************************************
|
---|
478 | int WIN32API lstrcpynWtoA(LPSTR astring, LPCWSTR ustring, int length)
|
---|
479 | {
|
---|
480 | int ret;
|
---|
481 |
|
---|
482 | ret = WideCharToMultiByte(CP_ACP, 0, ustring, -1, astring, length, 0, NULL);
|
---|
483 | if(ret == 0) {
|
---|
484 | SetLastError(ERROR_SUCCESS); //WideCharToMultiByte sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
485 | ret = length;
|
---|
486 | }
|
---|
487 | //Must not always set the last character to 0; some apps send the wrong
|
---|
488 | //string size to apis that use this function (i.e. GetMenuStringW (Notes))
|
---|
489 | //-> overwrites stack
|
---|
490 | if(ret == length) {
|
---|
491 | #if 1
|
---|
492 | lstrtrunc( astring, length );
|
---|
493 | #else
|
---|
494 | astring[length-1] = 0;
|
---|
495 | #endif
|
---|
496 | }
|
---|
497 | else astring[ret] = 0;
|
---|
498 |
|
---|
499 | return ret;
|
---|
500 | }
|
---|
501 |
|
---|
502 | //lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
|
---|
503 | //because Wine code depends on this behaviour (i.e. comdlg32)
|
---|
504 | int WIN32API lstrcpynAtoW(LPWSTR unicode, LPCSTR ascii, int unilen)
|
---|
505 | {
|
---|
506 | int ret;
|
---|
507 |
|
---|
508 | ret = MultiByteToWideChar(CP_ACP, 0, ascii, -1, unicode, unilen );
|
---|
509 | if(ret == 0) {
|
---|
510 | SetLastError(ERROR_SUCCESS); //MultiByteToWideChar sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
511 | ret = unilen;
|
---|
512 | }
|
---|
513 |
|
---|
514 | //Must not always set the last character to 0; some apps send the wrong
|
---|
515 | //string size to apis that use this function (i.e. GetMenuStringW (Notes))
|
---|
516 | //-> overwrites stack
|
---|
517 | if(ret == unilen) {
|
---|
518 | unicode[unilen-1] = 0;
|
---|
519 | }
|
---|
520 | else unicode[ret] = 0;
|
---|
521 | return ret;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*****************************************************************************
|
---|
525 | * Name :
|
---|
526 | * Purpose : Converts unicode string to ascii string
|
---|
527 | * Parameters:
|
---|
528 | * Variables :
|
---|
529 | * Result : returns length of ascii string
|
---|
530 | * Remark :
|
---|
531 | * Status :
|
---|
532 | *
|
---|
533 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
534 | *****************************************************************************/
|
---|
535 |
|
---|
536 | LPSTR WIN32API lstrcpyWtoA(LPSTR ascii, LPCWSTR unicode)
|
---|
537 | {
|
---|
538 | //@@@PH huh? wuz dat?
|
---|
539 | if (unicode == NULL)
|
---|
540 | {
|
---|
541 | DebugInt3();
|
---|
542 | if (ascii != NULL) ((LPWSTR)ascii)[0] = 0; //CB: set at least end
|
---|
543 | return NULL;
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (ascii == NULL) {
|
---|
547 | DebugInt3();
|
---|
548 | return NULL; /* garbage in, garbage out ! */
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* forward to function with len parameter */
|
---|
552 | lstrcpynWtoA(ascii,
|
---|
553 | unicode,
|
---|
554 | WideCharToMultiByte( CP_ACP, 0, unicode, -1, 0, 0, 0, 0 )); //end included
|
---|
555 |
|
---|
556 | return ascii;
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | /*****************************************************************************
|
---|
561 | * Name :
|
---|
562 | * Purpose : Copies the full string from ascii to unicode
|
---|
563 | * Parameters:
|
---|
564 | * Variables :
|
---|
565 | * Result :
|
---|
566 | * Remark :
|
---|
567 | * Status :
|
---|
568 | *
|
---|
569 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
570 | *****************************************************************************/
|
---|
571 |
|
---|
572 | LPWSTR WIN32API lstrcpyAtoW(LPWSTR unicode, LPCSTR ascii)
|
---|
573 | {
|
---|
574 | /* achimha for security, strlen might trap if garbage in */
|
---|
575 | /* @@@PH 98/06/07 */
|
---|
576 | if (ascii == NULL)
|
---|
577 | {
|
---|
578 | //DebugInt3();
|
---|
579 | if (unicode != NULL) unicode[0] = 0; //CB: set at least end
|
---|
580 | return NULL;
|
---|
581 | }
|
---|
582 |
|
---|
583 | if (unicode == NULL) {
|
---|
584 | //DebugInt3();
|
---|
585 | return NULL; /* garbage in, garbage out ! */
|
---|
586 | }
|
---|
587 |
|
---|
588 | /* forward to call with length parameter */
|
---|
589 | lstrcpynAtoW(unicode, ascii, MultiByteToWideChar( CP_ACP, 0, ascii, -1, 0, 0 )); //end included
|
---|
590 | return (unicode);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*****************************************************************************
|
---|
594 | * NAME
|
---|
595 | * lstrlenWtoA
|
---|
596 | *
|
---|
597 | * PURPOSE
|
---|
598 | * calculate the length of string when unicode string was converted to
|
---|
599 | * ansi string.
|
---|
600 | *
|
---|
601 | * PARAMETERS
|
---|
602 | * ustring - unicode string
|
---|
603 | * ulen - length of ustring
|
---|
604 | *
|
---|
605 | * RESULT
|
---|
606 | * Success
|
---|
607 | * if ulen < 0, length of null-terminated unicode string NOT including
|
---|
608 | * null-terminator.
|
---|
609 | * otherwise, length of string when first ulen characters of ustring
|
---|
610 | * converted to ansi string.
|
---|
611 | *
|
---|
612 | * Failure
|
---|
613 | * return 0.
|
---|
614 | *
|
---|
615 | * REMARK
|
---|
616 | * this function is not Win32 API but helper for convenient.
|
---|
617 | *
|
---|
618 | * AUTHOR : KO Myung-Hun
|
---|
619 | *****************************************************************************/
|
---|
620 |
|
---|
621 | int WIN32API lstrlenWtoA( LPCWSTR ustring, int ulen )
|
---|
622 | {
|
---|
623 | int ret;
|
---|
624 |
|
---|
625 | if( ulen < 0 )
|
---|
626 | ulen = lstrlenW( ustring );
|
---|
627 |
|
---|
628 | if( !IsDBCSEnv() ) return ulen;
|
---|
629 |
|
---|
630 | ret = WideCharToMultiByte( CP_ACP, 0, ustring, ulen, 0, 0, 0, 0 );
|
---|
631 | if(ret == 0) {
|
---|
632 | SetLastError(ERROR_SUCCESS); //WideCharToMultiByte sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
633 | return 0;
|
---|
634 | }
|
---|
635 |
|
---|
636 | return ret;
|
---|
637 | }
|
---|
638 |
|
---|
639 | /*****************************************************************************
|
---|
640 | * NAME
|
---|
641 | * lstrlenAtoW
|
---|
642 | *
|
---|
643 | * PURPOSE
|
---|
644 | * return the length of string when ansi string was converted to
|
---|
645 | * unicode string.
|
---|
646 | *
|
---|
647 | * PARAMETERS
|
---|
648 | * astring - ansi string
|
---|
649 | * alen - length of astring
|
---|
650 | *
|
---|
651 | * RESULT
|
---|
652 | * Success
|
---|
653 | * if alen < 0, length of null-terminated ansi string NOT including
|
---|
654 | * null-terminator.
|
---|
655 | * otherwise, length of string when first alen characters of astring
|
---|
656 | * converted to unicode string.
|
---|
657 | *
|
---|
658 | * Failure
|
---|
659 | * return 0.
|
---|
660 | *
|
---|
661 | * REMARK
|
---|
662 | * this function is not Win32 API but helper for convenient.
|
---|
663 | *
|
---|
664 | * AUTHOR : KO Myung-Hun
|
---|
665 | *****************************************************************************/
|
---|
666 |
|
---|
667 | int WIN32API lstrlenAtoW( LPCSTR astring, int alen )
|
---|
668 | {
|
---|
669 | int ret;
|
---|
670 |
|
---|
671 | if( alen < 0 )
|
---|
672 | alen = strlen( astring );
|
---|
673 |
|
---|
674 | if( !IsDBCSEnv() ) return alen;
|
---|
675 |
|
---|
676 | ret = MultiByteToWideChar( CP_ACP, 0, astring, alen, 0, 0 );
|
---|
677 | if(ret == 0) {
|
---|
678 | SetLastError(ERROR_SUCCESS); //MultiByteToWideChar sets it to ERROR_INSUFFICIENT_BUFFER
|
---|
679 | return 0;
|
---|
680 | }
|
---|
681 |
|
---|
682 | return ret;
|
---|
683 | }
|
---|
684 |
|
---|
685 | /*****************************************************************************
|
---|
686 | * NAME
|
---|
687 | * lstrtrunc
|
---|
688 | *
|
---|
689 | * PURPOSE
|
---|
690 | * truncate ansi string
|
---|
691 | *
|
---|
692 | * PARAMETERS
|
---|
693 | * max - max length of truncated string including null-terminator
|
---|
694 | *
|
---|
695 | * RESULT
|
---|
696 | * Success
|
---|
697 | * return the length of truncated string not including null-terminator
|
---|
698 | *
|
---|
699 | * Failure
|
---|
700 | * return 0.
|
---|
701 | *
|
---|
702 | * REMARK
|
---|
703 | * this function is not Win32 API but helper for convenient.
|
---|
704 | *
|
---|
705 | * AUTHOR : KO Myung-Hun
|
---|
706 | *****************************************************************************/
|
---|
707 |
|
---|
708 | int WIN32API lstrtrunc( LPSTR astring, int max )
|
---|
709 | {
|
---|
710 | int i;
|
---|
711 |
|
---|
712 | if( !astring || ( max < 1 ))
|
---|
713 | return 0;
|
---|
714 |
|
---|
715 | astring[ max - 1 ] = 0;
|
---|
716 |
|
---|
717 | if( !IsDBCSEnv() ) return max;
|
---|
718 |
|
---|
719 | max = strlen( astring );
|
---|
720 | for( i = 0; i < max; i++ )
|
---|
721 | if( IsDBCSLeadByte( astring[ i ]))
|
---|
722 | i++;
|
---|
723 |
|
---|
724 | if( i > max ) // broken DBCS lead byte ?
|
---|
725 | astring[ --max ] = 0; // then remove DBCS lead byte
|
---|
726 |
|
---|
727 | return max;
|
---|
728 | }
|
---|
729 |
|
---|
730 | /*****************************************************************************
|
---|
731 | * Name :
|
---|
732 | * Purpose :
|
---|
733 | * Parameters:
|
---|
734 | * Variables :
|
---|
735 | * Result :
|
---|
736 | * Remark :
|
---|
737 | * Status :
|
---|
738 | *
|
---|
739 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
740 | *****************************************************************************/
|
---|
741 |
|
---|
742 | LPVOID WIN32API HEAP_xalloc(HANDLE heap, DWORD flags, DWORD size )
|
---|
743 | {
|
---|
744 | LPVOID p = HeapAlloc( heap, flags, size );
|
---|
745 | if (!p)
|
---|
746 | {
|
---|
747 | dprintf2(("KERNEL32: HEAP_xalloc(%08xh,%08xh,%08xh) out of memory.\n",
|
---|
748 | heap,
|
---|
749 | flags,
|
---|
750 | size));
|
---|
751 | }
|
---|
752 | return p;
|
---|
753 | }
|
---|
754 |
|
---|
755 |
|
---|
756 | /*****************************************************************************
|
---|
757 | * Name :
|
---|
758 | * Purpose :
|
---|
759 | * Parameters:
|
---|
760 | * Variables :
|
---|
761 | * Result :
|
---|
762 | * Remark :
|
---|
763 | * Status :
|
---|
764 | *
|
---|
765 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
766 | *****************************************************************************/
|
---|
767 |
|
---|
768 | LPVOID WIN32API HEAP_xrealloc(HANDLE heap, DWORD flags, LPVOID lpMem,
|
---|
769 | DWORD size )
|
---|
770 | {
|
---|
771 | LPVOID p = HeapReAlloc( heap, flags, lpMem, size );
|
---|
772 | if (!p)
|
---|
773 | {
|
---|
774 | dprintf2(("KERNEL32: HEAP_xrealloc(%08xh,%08xh,%08xh,%08xh) out of memory.\n",
|
---|
775 | heap,
|
---|
776 | flags,
|
---|
777 | lpMem,
|
---|
778 | size));
|
---|
779 | }
|
---|
780 | return p;
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | /*****************************************************************************
|
---|
785 | * Name :
|
---|
786 | * Purpose :
|
---|
787 | * Parameters:
|
---|
788 | * Variables :
|
---|
789 | * Result :
|
---|
790 | * Remark :
|
---|
791 | * Status :
|
---|
792 | *
|
---|
793 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
794 | *****************************************************************************/
|
---|
795 |
|
---|
796 | LPVOID WIN32API HEAP_malloc(DWORD size )
|
---|
797 | {
|
---|
798 | LPVOID p = HeapAlloc( GetProcessHeap(), 0, size );
|
---|
799 | if (!p)
|
---|
800 | {
|
---|
801 | dprintf2(("KERNEL32: HEAP_malloc(%08xh) out of memory.\n",
|
---|
802 | size));
|
---|
803 | }
|
---|
804 | return p;
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | /*****************************************************************************
|
---|
809 | * Name :
|
---|
810 | * Purpose :
|
---|
811 | * Parameters:
|
---|
812 | * Variables :
|
---|
813 | * Result :
|
---|
814 | * Remark :
|
---|
815 | * Status :
|
---|
816 | *
|
---|
817 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
818 | *****************************************************************************/
|
---|
819 |
|
---|
820 | DWORD WIN32API HEAP_size(LPVOID lpMem)
|
---|
821 | {
|
---|
822 | return HeapSize( GetProcessHeap(), 0, lpMem );
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | /*****************************************************************************
|
---|
827 | * Name :
|
---|
828 | * Purpose :
|
---|
829 | * Parameters:
|
---|
830 | * Variables :
|
---|
831 | * Result :
|
---|
832 | * Remark :
|
---|
833 | * Status :
|
---|
834 | *
|
---|
835 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
836 | *****************************************************************************/
|
---|
837 |
|
---|
838 | LPVOID WIN32API HEAP_realloc(LPVOID lpMem, DWORD size )
|
---|
839 | {
|
---|
840 | LPVOID p = HeapReAlloc( GetProcessHeap(), 0, lpMem, size );
|
---|
841 | if (!p)
|
---|
842 | {
|
---|
843 | dprintf2(("KERNEL32: HEAP_realloc(%08xh,%08xh) out of memory.\n",
|
---|
844 | lpMem,
|
---|
845 | size));
|
---|
846 | }
|
---|
847 | return p;
|
---|
848 | }
|
---|
849 |
|
---|
850 |
|
---|
851 | /*****************************************************************************
|
---|
852 | * Name :
|
---|
853 | * Purpose :
|
---|
854 | * Parameters:
|
---|
855 | * Variables :
|
---|
856 | * Result :
|
---|
857 | * Remark :
|
---|
858 | * Status :
|
---|
859 | *
|
---|
860 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
861 | *****************************************************************************/
|
---|
862 |
|
---|
863 | BOOL WIN32API HEAP_free(LPVOID lpMem)
|
---|
864 | {
|
---|
865 | return HeapFree( GetProcessHeap(), 0, lpMem);
|
---|
866 | }
|
---|
867 |
|
---|
868 |
|
---|
869 | /*****************************************************************************
|
---|
870 | * Name :
|
---|
871 | * Purpose :
|
---|
872 | * Parameters:
|
---|
873 | * Variables :
|
---|
874 | * Result :
|
---|
875 | * Remark :
|
---|
876 | * Status :
|
---|
877 | *
|
---|
878 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
879 | *****************************************************************************/
|
---|
880 |
|
---|
881 | LPSTR WIN32API HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR str )
|
---|
882 | {
|
---|
883 | int iLength = lstrlenA(str) + 1;
|
---|
884 | LPSTR p = (LPSTR)HEAP_xalloc( heap, flags, iLength );
|
---|
885 | memcpy( p, str, iLength );
|
---|
886 | return p;
|
---|
887 | }
|
---|
888 |
|
---|
889 |
|
---|
890 | /*****************************************************************************
|
---|
891 | * Name :
|
---|
892 | * Purpose :
|
---|
893 | * Parameters:
|
---|
894 | * Variables :
|
---|
895 | * Result :
|
---|
896 | * Remark :
|
---|
897 | * Status :
|
---|
898 | *
|
---|
899 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
900 | *****************************************************************************/
|
---|
901 |
|
---|
902 | LPWSTR WIN32API HEAP_strdupW(HANDLE heap, DWORD flags, LPCWSTR str )
|
---|
903 | {
|
---|
904 | INT len = lstrlenW(str) + 1;
|
---|
905 | LPWSTR p = (LPWSTR)HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
|
---|
906 | memcpy( p, str, len * sizeof(WCHAR) );
|
---|
907 | return p;
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | /*****************************************************************************
|
---|
912 | * Name :
|
---|
913 | * Purpose :
|
---|
914 | * Parameters:
|
---|
915 | * Variables :
|
---|
916 | * Result :
|
---|
917 | * Remark :
|
---|
918 | * Status :
|
---|
919 | *
|
---|
920 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
921 | *****************************************************************************/
|
---|
922 |
|
---|
923 | LPWSTR WIN32API HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR str )
|
---|
924 | {
|
---|
925 | LPWSTR ret;
|
---|
926 | int len;
|
---|
927 |
|
---|
928 | if (!str) return NULL;
|
---|
929 |
|
---|
930 | len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0);
|
---|
931 | ret = (LPWSTR)HEAP_xalloc( heap, flags, len*sizeof(WCHAR));
|
---|
932 | MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
|
---|
933 |
|
---|
934 | return ret;
|
---|
935 | }
|
---|
936 |
|
---|
937 |
|
---|
938 | /*****************************************************************************
|
---|
939 | * Name :
|
---|
940 | * Purpose :
|
---|
941 | * Parameters:
|
---|
942 | * Variables :
|
---|
943 | * Result :
|
---|
944 | * Remark :
|
---|
945 | * Status :
|
---|
946 | *
|
---|
947 | * Author : Patrick Haller [Thu, 1999/08/05 20:46]
|
---|
948 | *****************************************************************************/
|
---|
949 |
|
---|
950 | LPSTR WIN32API HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR str )
|
---|
951 | {
|
---|
952 | LPSTR ret;
|
---|
953 | int len;
|
---|
954 |
|
---|
955 | if (!str) return NULL;
|
---|
956 |
|
---|
957 | len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, 0, NULL);
|
---|
958 | ret = (LPSTR)HEAP_xalloc( heap, flags, len);
|
---|
959 | WideCharToMultiByte(CP_ACP, 0, str, -1, ret, len, 0, NULL );
|
---|
960 | return ret;
|
---|
961 | }
|
---|
962 |
|
---|
963 |
|
---|
964 |
|
---|
965 |
|
---|