source: trunk/src/kernel32/heapstring.cpp@ 9975

Last change on this file since 9975 was 9975, checked in by sandervl, 22 years ago

YD: Changes for header updates

File size: 19.3 KB
Line 
1/* $Id: heapstring.cpp,v 1.52 2003-04-02 12:58:29 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 <wcstr.h>
28#include "heap.h"
29#include <wine\unicode.h>
30#include "misc.h"
31#include "codepage.h"
32
33#define DBG_LOCALLOG DBG_heapstring
34#include "dbglocal.h"
35
36
37/*****************************************************************************
38 * Defines *
39 *****************************************************************************/
40
41ODINDEBUGCHANNEL(KERNEL32-HEAPSTRING)
42
43
44/*****************************************************************************
45 * Imported variables *
46 *****************************************************************************/
47
48// This macro could be mapped to GetProcessHeap() if there
49// is any change in functionality
50#define GetProcessHeap() Heap_ProcessHeap
51
52extern HANDLE Heap_ProcessHeap;
53
54
55/*****************************************************************************
56 * Name :
57 * Purpose :
58 * Parameters:
59 * Variables :
60 * Result :
61 * Remark :
62 * Status :
63 *
64 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
65 *****************************************************************************/
66
67int WIN32API lstrlenA(LPCSTR arg1)
68{
69 dprintf2(("KERNEL32: lstrlenA(%s)\n",
70 arg1));
71
72 if(arg1 == NULL) {
73 SetLastError(ERROR_INVALID_PARAMETER);
74 return 0;
75 }
76 return strlen(arg1);
77}
78
79
80/*****************************************************************************
81 * Name :
82 * Purpose :
83 * Parameters:
84 * Variables :
85 * Result :
86 * Remark :
87 * Status :
88 *
89 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
90 *****************************************************************************/
91
92int WIN32API lstrlenW(LPCWSTR str)
93{
94 if(str == NULL) {
95 SetLastError( ERROR_INVALID_PARAMETER );
96 return 0;
97 }
98
99 const WCHAR *s = str;
100 while (*s) s++;
101 dprintf2(("KERNEL32: lstrlenW(%08xh) returns %d\n",
102 str, s - str));
103
104 return s - str;
105}
106
107
108/*****************************************************************************
109 * Name :
110 * Purpose :
111 * Parameters:
112 * Variables :
113 * Result :
114 * Remark :
115 * Status :
116 *
117 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
118 *****************************************************************************/
119
120LPSTR WIN32API lstrcatA(LPSTR arg1, LPCSTR arg2)
121{
122 dprintf2(("KERNEL32: lstrcat(%s,%s)\n",
123 arg1,
124 arg2));
125
126 if(arg2 == NULL)
127 return arg1;
128 strcat(arg1, arg2);
129 return arg1;
130}
131
132
133/*****************************************************************************
134 * Name :
135 * Purpose :
136 * Parameters:
137 * Variables :
138 * Result :
139 * Remark :
140 * Status :
141 *
142 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
143 *****************************************************************************/
144
145LPWSTR WIN32API lstrcatW(LPWSTR dst, LPCWSTR src)
146{
147 dprintf2(("KERNEL32: lstrcatW(%08xh,%08xh)\n",
148 dst, src));
149
150 if(src == NULL)
151 return dst;
152
153 strcpyW( dst + strlenW(dst), src );
154 return dst;
155}
156
157
158/*****************************************************************************
159 * Name :
160 * Purpose :
161 * Parameters:
162 * Variables :
163 * Result :
164 * Remark :
165 * Status :
166 *
167 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
168 *****************************************************************************/
169
170int WIN32API lstrcmpA(LPCSTR arg1, LPCSTR arg2)
171{
172 dprintf2(("KERNEL32: lstrcmpA(%s,%s)\n",
173 arg1,
174 arg2));
175
176 if(arg1 == NULL)
177 return -1;
178 if(arg2 == NULL)
179 return 1;
180
181 return strcmp(arg1, arg2);
182}
183
184
185/*****************************************************************************
186 * Name :
187 * Purpose :
188 * Parameters:
189 * Variables :
190 * Result :
191 * Remark :
192 * Status :
193 *
194 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
195 *****************************************************************************/
196
197int WIN32API lstrncmpA(LPCSTR arg1, LPCSTR arg2, int l)
198{
199 dprintf2(("KERNEL32: lstrncmpA(%s,%s,%d)\n",
200 arg1,
201 arg2,
202 l));
203
204 return strncmp(arg1, arg2, l);
205}
206
207/*****************************************************************************
208 * Name : lstrncmpiA
209 * Purpose :
210 * Parameters:
211 * Variables :
212 * Result :
213 * Remark :
214 * Status :
215 *
216 * Author : Przemyslaw Dobrowolski
217 *****************************************************************************/
218int WIN32API lstrncmpiA(LPCSTR str1, LPCSTR str2, INT n )
219{
220 INT firstch,lastch;
221 INT result = 0;
222
223 if (n)
224 {
225 do
226 {
227 firstch = toupper(*str1);
228 lastch = toupper(*str2);
229 str1++;
230 str2++;
231 } while (--n && *str1 && *str2 && firstch == lastch);
232
233 result = firstch - lastch;
234 }
235
236 return(result);
237}
238//TODO: Don't know if this is completely correct
239int WIN32API lstrncmpiW(LPCWSTR str1, LPCWSTR str2, int n)
240{
241 INT firstch,lastch;
242 INT result = 0;
243
244 if (n)
245 {
246 do
247 {
248 firstch = toupperW(*str1);
249 lastch = toupperW(*str2);
250 str1++;
251 str2++;
252 } while (--n && *str1 && *str2 && firstch == lastch);
253
254 result = firstch - lastch;
255 }
256
257 return(result);
258}
259
260/*****************************************************************************
261 * Name :
262 * Purpose :
263 * Parameters:
264 * Variables :
265 * Result :
266 * Remark :
267 * Status :
268 *
269 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
270 *****************************************************************************/
271
272int WIN32API lstrcmpW(LPCWSTR arg1, LPCWSTR arg2)
273{
274 dprintf2(("KERNEL32: lstrcmpW (%08xh, %08xh)\n",
275 arg1,
276 arg2));
277
278 if(arg1 == NULL)
279 return -1;
280 if(arg2 == NULL)
281 return 1;
282
283 return strcmpW( arg1, arg2 );
284}
285
286
287/*****************************************************************************
288 * Name :
289 * Purpose :
290 * Parameters:
291 * Variables :
292 * Result :
293 * Remark :
294 * Status :
295 *
296 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
297 *****************************************************************************/
298
299int WIN32API lstrncmpW(LPCWSTR arg1, LPCWSTR arg2, int l)
300{
301 dprintf2(("KERNEL32: lstrncmpW(%08xh,%08xh,%d)\n",
302 arg1,
303 arg2,
304 l));
305
306 return wcsncmp((wchar_t*)arg1,
307 (wchar_t*)arg2,
308 l);
309}
310
311/*****************************************************************************
312 * Name :
313 * Purpose :
314 * Parameters:
315 * Variables :
316 * Result :
317 * Remark :
318 * Status :
319 *
320 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
321 *****************************************************************************/
322
323LPSTR WIN32API lstrcpyA(LPSTR dest, LPCSTR src)
324{
325 if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
326 return NULL;
327
328 return strcpy(dest, src);
329}
330
331
332/*****************************************************************************
333 * Name :
334 * Purpose :
335 * Parameters:
336 * Variables :
337 * Result :
338 * Remark :
339 * Status :
340 *
341 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
342 *****************************************************************************/
343
344LPWSTR WIN32API lstrcpyW(LPWSTR dest, LPCWSTR src)
345{
346 if ( (src == NULL) || (dest == NULL) ) // stupid parameter checking
347 return NULL;
348
349 WCHAR *p = dest;
350 while ((*p++ = *src++));
351
352 return dest;
353}
354
355
356/*****************************************************************************
357 * Name :
358 * Purpose :
359 * Parameters:
360 * Variables :
361 * Result :
362 * Remark :
363 * Status :
364 *
365 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
366 *****************************************************************************/
367
368LPSTR WIN32API lstrcpynA(LPSTR arg1, LPCSTR arg2, int arg3)
369{
370 register LPSTR p1 = arg1;
371 register LPSTR p2 = (LPSTR)arg2;
372
373 if(arg3 == 0) {
374 return NULL;
375 }
376
377 //PH: looks like either \0 or arg3 terminate the copy
378 //return strncpy(arg1, arg2, arg3);
379 arg3--; // pre-decrement to avoid exceeding buffer length
380 // results in better code than (arg1 > 1)
381
382 for (;*p2 && arg3; arg3--)
383 *p1++ = *p2++;
384
385 *p1 = 0; //CB: copy arg-1, set end 0
386
387 return arg1;
388}
389
390
391/*****************************************************************************
392 * Name :
393 * Purpose :
394 * Parameters:
395 * Variables :
396 * Result :
397 * Remark :
398 * Status :
399 *
400 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
401 *****************************************************************************/
402
403LPWSTR WIN32API lstrcpynW(LPWSTR dst, LPCWSTR src, int n)
404{
405 LPWSTR p = dst;
406
407 /* In real windows the whole function is protected by an exception handler
408 * that returns ERROR_INVALID_PARAMETER on faulty parameters
409 * We currently just check for NULL.
410 */
411 if (!dst || !src) {
412 SetLastError(ERROR_INVALID_PARAMETER);
413 return 0;
414 }
415 while ((n-- > 1) && *src) *p++ = *src++;
416 if (n >= 0) *p = 0;
417 return dst;
418}
419
420
421/*****************************************************************************
422 * Name :
423 * Purpose :
424 * Parameters:
425 * Variables :
426 * Result :
427 * Remark :
428 * Status :
429 *
430 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
431 *****************************************************************************/
432
433int WIN32API lstrcmpiA(LPCSTR arg1, LPCSTR arg2)
434{
435 dprintf2(("KERNEL32: lstrcmpiA(%s,%s)\n",
436 arg1,
437 arg2));
438
439 if(arg1 == NULL)
440 return -1;
441
442 if(arg2 == NULL)
443 return 1;
444
445 return strcmpi(arg1, arg2);
446}
447
448
449/*****************************************************************************
450 * Name :
451 * Purpose :
452 * Parameters:
453 * Variables :
454 * Result :
455 * Remark :
456 * Status :
457 *
458 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
459 *****************************************************************************/
460
461int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
462{
463 if (!str1 || !str2) {
464
465 SetLastError(ERROR_INVALID_PARAMETER);
466 return 0;
467 }
468 return strcmpiW( str1, str2 );
469}
470
471//*****************************************************************************
472//lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
473//because Wine code depends on this behaviour (i.e. comdlg32)
474//*****************************************************************************
475int WIN32API lstrcpynWtoA(LPSTR astring, LPCWSTR ustring, int length)
476{
477 int ret;
478
479 ret = WideCharToMultiByte(CP_ACP, 0, ustring, -1, astring, length, 0, NULL);
480 if(ret == 0) {
481 SetLastError(ERROR_SUCCESS); //WideCharToMultiByte sets it to ERROR_INSUFFICIENT_BUFFER
482 ret = length;
483 }
484 //Must not always set the last character to 0; some apps send the wrong
485 //string size to apis that use this function (i.e. GetMenuStringW (Notes))
486 //-> overwrites stack
487 if(ret == length) {
488 astring[length-1] = 0;
489 }
490 else astring[ret] = 0;
491
492 return ret;
493}
494
495//lstrcpynWtoA and lstrcpynAtoW must zero-terminate the string
496//because Wine code depends on this behaviour (i.e. comdlg32)
497int WIN32API lstrcpynAtoW(LPWSTR unicode, LPCSTR ascii, int asciilen)
498{
499 int ret;
500
501 ret = MultiByteToWideChar(CP_ACP, 0, ascii, -1, unicode, asciilen);
502 if(ret == 0) {
503 SetLastError(ERROR_SUCCESS); //MultiByteToWideChar sets it to ERROR_INSUFFICIENT_BUFFER
504 ret = asciilen;
505 }
506
507 //Must not always set the last character to 0; some apps send the wrong
508 //string size to apis that use this function (i.e. GetMenuStringW (Notes))
509 //-> overwrites stack
510 if(ret == asciilen) {
511 unicode[asciilen-1] = 0;
512 }
513 else unicode[ret] = 0;
514 return ret;
515
516
517}
518
519/*****************************************************************************
520 * Name :
521 * Purpose : Converts unicode string to ascii string
522 * Parameters:
523 * Variables :
524 * Result : returns length of ascii string
525 * Remark :
526 * Status :
527 *
528 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
529 *****************************************************************************/
530
531LPSTR WIN32API lstrcpyWtoA(LPSTR ascii, LPCWSTR unicode)
532{
533 //@@@PH huh? wuz dat?
534 if (unicode == NULL)
535 {
536 DebugInt3();
537 if (ascii != NULL) ((LPWSTR)ascii)[0] = 0; //CB: set at least end
538 return NULL;
539 }
540
541 if (ascii == NULL) {
542 DebugInt3();
543 return NULL; /* garbage in, garbage out ! */
544 }
545
546 /* forward to function with len parameter */
547 lstrcpynWtoA(ascii,
548 unicode,
549 lstrlenW(unicode)+1); //end included
550
551 return ascii;
552}
553
554
555/*****************************************************************************
556 * Name :
557 * Purpose : Copies the full string from ascii to unicode
558 * Parameters:
559 * Variables :
560 * Result :
561 * Remark :
562 * Status :
563 *
564 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
565 *****************************************************************************/
566
567LPWSTR WIN32API lstrcpyAtoW(LPWSTR unicode, LPCSTR ascii)
568{
569 /* achimha for security, strlen might trap if garbage in */
570 /* @@@PH 98/06/07 */
571 if (ascii == NULL)
572 {
573 DebugInt3();
574 if (unicode != NULL) unicode[0] = 0; //CB: set at least end
575 return NULL;
576 }
577
578 if (unicode == NULL) {
579 DebugInt3();
580 return NULL; /* garbage in, garbage out ! */
581 }
582
583 /* forward to call with length parameter */
584 lstrcpynAtoW(unicode, ascii, strlen(ascii)+1); //end included
585 return (unicode);
586}
587
588
589
590
591/*****************************************************************************
592 * Name :
593 * Purpose :
594 * Parameters:
595 * Variables :
596 * Result :
597 * Remark :
598 * Status :
599 *
600 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
601 *****************************************************************************/
602
603LPVOID WIN32API HEAP_xalloc(HANDLE heap, DWORD flags, DWORD size )
604{
605 LPVOID p = HeapAlloc( heap, flags, size );
606 if (!p)
607 {
608 dprintf2(("KERNEL32: HEAP_xalloc(%08xh,%08xh,%08xh) out of memory.\n",
609 heap,
610 flags,
611 size));
612 }
613 return p;
614}
615
616
617/*****************************************************************************
618 * Name :
619 * Purpose :
620 * Parameters:
621 * Variables :
622 * Result :
623 * Remark :
624 * Status :
625 *
626 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
627 *****************************************************************************/
628
629LPVOID WIN32API HEAP_xrealloc(HANDLE heap, DWORD flags, LPVOID lpMem,
630 DWORD size )
631{
632 LPVOID p = HeapReAlloc( heap, flags, lpMem, size );
633 if (!p)
634 {
635 dprintf2(("KERNEL32: HEAP_xrealloc(%08xh,%08xh,%08xh,%08xh) out of memory.\n",
636 heap,
637 flags,
638 lpMem,
639 size));
640 }
641 return p;
642}
643
644
645/*****************************************************************************
646 * Name :
647 * Purpose :
648 * Parameters:
649 * Variables :
650 * Result :
651 * Remark :
652 * Status :
653 *
654 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
655 *****************************************************************************/
656
657LPVOID WIN32API HEAP_malloc(DWORD size )
658{
659 LPVOID p = HeapAlloc( GetProcessHeap(), 0, size );
660 if (!p)
661 {
662 dprintf2(("KERNEL32: HEAP_malloc(%08xh) out of memory.\n",
663 size));
664 }
665 return p;
666}
667
668
669/*****************************************************************************
670 * Name :
671 * Purpose :
672 * Parameters:
673 * Variables :
674 * Result :
675 * Remark :
676 * Status :
677 *
678 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
679 *****************************************************************************/
680
681DWORD WIN32API HEAP_size(LPVOID lpMem)
682{
683 return HeapSize( GetProcessHeap(), 0, lpMem );
684}
685
686
687/*****************************************************************************
688 * Name :
689 * Purpose :
690 * Parameters:
691 * Variables :
692 * Result :
693 * Remark :
694 * Status :
695 *
696 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
697 *****************************************************************************/
698
699LPVOID WIN32API HEAP_realloc(LPVOID lpMem, DWORD size )
700{
701 LPVOID p = HeapReAlloc( GetProcessHeap(), 0, lpMem, size );
702 if (!p)
703 {
704 dprintf2(("KERNEL32: HEAP_realloc(%08xh,%08xh) out of memory.\n",
705 lpMem,
706 size));
707 }
708 return p;
709}
710
711
712/*****************************************************************************
713 * Name :
714 * Purpose :
715 * Parameters:
716 * Variables :
717 * Result :
718 * Remark :
719 * Status :
720 *
721 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
722 *****************************************************************************/
723
724BOOL WIN32API HEAP_free(LPVOID lpMem)
725{
726 return HeapFree( GetProcessHeap(), 0, lpMem);
727}
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
742LPSTR WIN32API HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR str )
743{
744 int iLength = lstrlenA(str) + 1;
745 LPSTR p = (LPSTR)HEAP_xalloc( heap, flags, iLength );
746 memcpy( p, str, iLength );
747 return p;
748}
749
750
751/*****************************************************************************
752 * Name :
753 * Purpose :
754 * Parameters:
755 * Variables :
756 * Result :
757 * Remark :
758 * Status :
759 *
760 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
761 *****************************************************************************/
762
763LPWSTR WIN32API HEAP_strdupW(HANDLE heap, DWORD flags, LPCWSTR str )
764{
765 INT len = lstrlenW(str) + 1;
766 LPWSTR p = (LPWSTR)HEAP_xalloc( heap, flags, len * sizeof(WCHAR) );
767 memcpy( p, str, len );
768 return p;
769}
770
771
772/*****************************************************************************
773 * Name :
774 * Purpose :
775 * Parameters:
776 * Variables :
777 * Result :
778 * Remark :
779 * Status :
780 *
781 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
782 *****************************************************************************/
783
784LPWSTR WIN32API HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR str )
785{
786 LPWSTR ret;
787 int len;
788
789 if (!str) return NULL;
790
791 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0);
792 ret = (LPWSTR)HEAP_xalloc( heap, flags, len*sizeof(WCHAR));
793 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
794
795 return ret;
796}
797
798
799/*****************************************************************************
800 * Name :
801 * Purpose :
802 * Parameters:
803 * Variables :
804 * Result :
805 * Remark :
806 * Status :
807 *
808 * Author : Patrick Haller [Thu, 1999/08/05 20:46]
809 *****************************************************************************/
810
811LPSTR WIN32API HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR str )
812{
813 LPSTR ret;
814 int len;
815
816 if (!str) return NULL;
817
818 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, 0, NULL);
819 ret = (LPSTR)HEAP_xalloc( heap, flags, len);
820 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, len, 0, NULL );
821 return ret;
822}
823
824
825
826
Note: See TracBrowser for help on using the repository browser.