fixed warnings on irix and crash bug on big endian machines
[tprouty/samba.git] / source / lib / util_unistr.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-2001
6    Copyright (C) Simo Sorce 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #ifndef MAXUNI
26 #define MAXUNI 1024
27 #endif
28
29 /* these 3 tables define the unicode case handling.  They are loaded
30    at startup either via mmap() or read() from the lib directory */
31 static smb_ucs2_t *upcase_table;
32 static smb_ucs2_t *lowcase_table;
33 static uint8 *valid_table;
34
35
36 /*******************************************************************
37 load the case handling tables
38 ********************************************************************/
39 void load_case_tables(void)
40 {
41         static int initialised;
42         int i;
43
44         if (initialised) return;
45         initialised = 1;
46
47         upcase_table = map_file(lib_path("upcase.dat"), 0x20000);
48         lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);
49
50         /* we would like Samba to limp along even if these tables are
51            not available */
52         if (!upcase_table) {
53                 DEBUG(1,("creating lame upcase table\n"));
54                 upcase_table = malloc(0x20000);
55                 for (i=0;i<0x10000;i++) upcase_table[i] = i;
56                 for (i=0;i<256;i++) upcase_table[UCS2_CHAR(i)] = UCS2_CHAR(islower(i)?toupper(i):i);
57         }
58
59         if (!lowcase_table) {
60                 DEBUG(1,("creating lame lowcase table\n"));
61                 lowcase_table = malloc(0x20000);
62                 for (i=0;i<0x10000;i++) lowcase_table[i] = i;
63                 for (i=0;i<256;i++) lowcase_table[UCS2_CHAR(i)] = UCS2_CHAR(isupper(i)?tolower(i):i);
64         }
65 }
66
67 /*
68   see if a ucs2 character can be mapped correctly to a dos character
69   and mapped back to the same character in ucs2
70 */
71 static int check_dos_char(smb_ucs2_t c)
72 {
73         char buf[10];
74         smb_ucs2_t c2 = 0;
75         int len1, len2;
76         len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf));
77         if (len1 == 0) return 0;
78         len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2);
79         if (len2 != 2) return 0;
80         return (c == c2);
81 }
82
83 /*******************************************************************
84 load the valid character map table
85 ********************************************************************/
86 void init_valid_table(void)
87 {
88         static int initialised;
89         static int mapped_file;
90         int i;
91         const char *allowed = ".!#$%&'()_-@^`~";
92
93         if (initialised && mapped_file) return;
94         initialised = 1;
95
96         valid_table = map_file(lib_path("valid.dat"), 0x10000);
97         if (valid_table) {
98                 mapped_file = 1;
99                 return;
100         }
101
102         if (valid_table) free(valid_table);
103
104         DEBUG(2,("creating default valid table\n"));
105         valid_table = malloc(0x10000);
106         for (i=0;i<128;i++) valid_table[UCS2_CHAR(i)] = isalnum(i) || 
107                                     strchr(allowed,i);
108         for (;i<0x10000;i++) {
109                 smb_ucs2_t c;
110                 SSVAL(&c, 0, i);
111                 valid_table[c] = check_dos_char(c);
112         }
113 }
114
115
116 /*******************************************************************
117  Write a string in (little-endian) unicode format. src is in
118  the current DOS codepage. len is the length in bytes of the
119  string pointed to by dst.
120
121  if null_terminate is True then null terminate the packet (adds 2 bytes)
122
123  the return value is the length in bytes consumed by the string, including the
124  null termination if applied
125 ********************************************************************/
126
127 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
128 {
129         return push_ucs2(NULL, dst, src, len, 
130                          STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
131 }
132
133
134 /*******************************************************************
135  Skip past a unicode string, but not more than len. Always move
136  past a terminating zero if found.
137 ********************************************************************/
138
139 char *skip_unibuf(char *src, size_t len)
140 {
141     char *srcend = src + len;
142
143     while (src < srcend && SVAL(src,0))
144         src += 2;
145
146     if(!SVAL(src,0))
147         src += 2;
148
149     return src;
150 }
151
152 /* Copy a string from little-endian or big-endian unicode source (depending
153  * on flags) to internal samba format destination
154  */ 
155 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
156 {
157         if(dest_len==-1) dest_len=MAXUNI-3;
158         return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
159 }
160
161 /* Copy a string from a unistr2 source to internal samba format
162    destination.  Use this instead of direct calls to rpcstr_pull() to avoid
163    having to determine whether the source string is null terminated. */
164
165 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
166 {
167         return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
168                          src->uni_str_len * 2, 0);
169 }
170
171 /* Converts a string from internal samba format to unicode
172  */ 
173 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
174 {
175         return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
176 }
177
178 /*******************************************************************
179  Return a DOS codepage version of a little-endian unicode string.
180  len is the filename length (ignoring any terminating zero) in uin16
181  units. Always null terminates.
182  Hack alert: uses fixed buffer(s).
183 ********************************************************************/
184 char *dos_unistrn2(const uint16 *src, int len)
185 {
186         static char lbufs[8][MAXUNI];
187         static int nexti;
188         char *lbuf = lbufs[nexti];
189         nexti = (nexti+1)%8;
190         pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
191         return lbuf;
192 }
193
194 /*******************************************************************
195  Convert a (little-endian) UNISTR2 structure to an ASCII string
196 ********************************************************************/
197 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
198 {
199         if (str == NULL) {
200                 *dest='\0';
201                 return;
202         }
203         pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
204 }
205
206
207 /*******************************************************************
208  duplicate a UNISTR2 string into a null terminated char*
209  using a talloc context
210 ********************************************************************/
211 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
212 {
213         char *s;
214         int maxlen = (str->uni_str_len+1)*4;
215         if (!str->buffer) return NULL;
216         s = (char *)talloc(ctx, maxlen); /* convervative */
217         if (!s) return NULL;
218         pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, 
219                   STR_NOALIGN);
220         return s;
221 }
222
223
224 /*******************************************************************
225 Return a number stored in a buffer
226 ********************************************************************/
227
228 uint32 buffer2_to_uint32(BUFFER2 *str)
229 {
230         if (str->buf_len == 4)
231                 return IVAL(str->buffer, 0);
232         else
233                 return 0;
234 }
235
236 /*******************************************************************
237  Convert a wchar to upper case.
238 ********************************************************************/
239
240 smb_ucs2_t toupper_w(smb_ucs2_t val)
241 {
242         return upcase_table[val];
243 }
244
245 /*******************************************************************
246  Convert a wchar to lower case.
247 ********************************************************************/
248
249 smb_ucs2_t tolower_w( smb_ucs2_t val )
250 {
251         return lowcase_table[val];
252 }
253
254 /*******************************************************************
255 determine if a character is lowercase
256 ********************************************************************/
257 BOOL islower_w(smb_ucs2_t c)
258 {
259         return upcase_table[c] != c;
260 }
261
262 /*******************************************************************
263 determine if a character is uppercase
264 ********************************************************************/
265 BOOL isupper_w(smb_ucs2_t c)
266 {
267         return lowcase_table[c] != c;
268 }
269
270
271 /*******************************************************************
272 determine if a character is valid in a 8.3 name
273 ********************************************************************/
274 BOOL isvalid83_w(smb_ucs2_t c)
275 {
276         return valid_table[c] != 0;
277 }
278
279 /*******************************************************************
280  Count the number of characters in a smb_ucs2_t string.
281 ********************************************************************/
282 size_t strlen_w(const smb_ucs2_t *src)
283 {
284         size_t len;
285
286         for(len = 0; *src++; len++) ;
287
288         return len;
289 }
290
291 /*******************************************************************
292  Count up to max number of characters in a smb_ucs2_t string.
293 ********************************************************************/
294 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
295 {
296         size_t len;
297
298         for(len = 0; *src++ && (len < max); len++) ;
299
300         return len;
301 }
302
303 /*******************************************************************
304 wide strchr()
305 ********************************************************************/
306 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
307 {
308         while (*s != 0) {
309                 if (c == *s) return (smb_ucs2_t *)s;
310                 s++;
311         }
312         return NULL;
313 }
314
315 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
316 {
317         return strchr_w(s, UCS2_CHAR(c));
318 }
319
320 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
321 {
322         const smb_ucs2_t *p = s;
323         int len = strlen_w(s);
324         if (len == 0) return NULL;
325         p += (len - 1);
326         do {
327                 if (c == *p) return (smb_ucs2_t *)p;
328         } while (p-- != s);
329         return NULL;
330 }
331
332 /*******************************************************************
333 wide strstr()
334 ********************************************************************/
335 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
336 {
337         smb_ucs2_t *r;
338         size_t slen, inslen;
339
340         if (!s || !*s || !ins || !*ins) return NULL;
341         slen = strlen_w(s);
342         inslen = strlen_w(ins);
343         r = (smb_ucs2_t *)s;
344         while ((r = strchr_w(r, *ins))) {
345                 if (strncmp_w(r, ins, inslen) == 0) return r;
346                 r++;
347         }
348         return NULL;
349 }
350
351 /*******************************************************************
352  Convert a string to lower case.
353  return True if any char is converted
354 ********************************************************************/
355 BOOL strlower_w(smb_ucs2_t *s)
356 {
357         BOOL ret = False;
358         while (*s) {
359                 smb_ucs2_t v = tolower_w(*s);
360                 if (v != *s) {
361                         *s = v;
362                         ret = True;
363                 }
364                 s++;
365         }
366         return ret;
367 }
368
369 /*******************************************************************
370  Convert a string to upper case.
371  return True if any char is converted
372 ********************************************************************/
373 BOOL strupper_w(smb_ucs2_t *s)
374 {
375         BOOL ret = False;
376         while (*s) {
377                 smb_ucs2_t v = toupper_w(*s);
378                 if (v != *s) {
379                         *s = v;
380                         ret = True;
381                 }
382                 s++;
383         }
384         return ret;
385 }
386
387 /*******************************************************************
388   convert a string to "normal" form
389 ********************************************************************/
390 void strnorm_w(smb_ucs2_t *s)
391 {
392   extern int case_default;
393   if (case_default == CASE_UPPER)
394     strupper_w(s);
395   else
396     strlower_w(s);
397 }
398
399 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
400 {
401         while (*b && *a == *b) { a++; b++; }
402         return (*a - *b);
403         /* warning: if *a != *b and both are not 0 we retrun a random
404                 greater or lesser than 0 number not realted to which
405                 string is longer */
406 }
407
408 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
409 {
410         size_t n = 0;
411         while ((n < len) && *b && *a == *b) { a++; b++; n++;}
412         return (len - n)?(*a - *b):0;   
413 }
414
415 /*******************************************************************
416 case insensitive string comparison
417 ********************************************************************/
418 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
419 {
420         while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
421         return (tolower_w(*a) - tolower_w(*b));
422 }
423
424 /*******************************************************************
425 case insensitive string comparison, lenght limited
426 ********************************************************************/
427 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
428 {
429         size_t n = 0;
430         while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
431         return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
432 }
433
434 /*******************************************************************
435   compare 2 strings 
436 ********************************************************************/
437 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
438 {
439         if (s1 == s2) return(True);
440         if (!s1 || !s2) return(False);
441   
442         return(strcasecmp_w(s1,s2)==0);
443 }
444
445 /*******************************************************************
446   compare 2 strings up to and including the nth char.
447   ******************************************************************/
448 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
449 {
450   if (s1 == s2) return(True);
451   if (!s1 || !s2 || !n) return(False);
452   
453   return(strncasecmp_w(s1,s2,n)==0);
454 }
455
456 /*******************************************************************
457 duplicate string
458 ********************************************************************/
459 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
460 {
461         return strndup_w(src, 0);
462 }
463
464 /* if len == 0 then duplicate the whole string */
465 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
466 {
467         smb_ucs2_t *dest;
468         
469         if (!len) len = strlen_w(src);
470         dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
471         if (!dest) {
472                 DEBUG(0,("strdup_w: out of memory!\n"));
473                 return NULL;
474         }
475
476         memcpy(dest, src, len * sizeof(smb_ucs2_t));
477         dest[len] = 0;
478         
479         return dest;
480 }
481
482 /*******************************************************************
483 copy a string with max len
484 ********************************************************************/
485
486 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
487 {
488         size_t len;
489         
490         if (!dest || !src) return NULL;
491         
492         for (len = 0; (src[len] != 0) && (len < max); len++)
493                 dest[len] = src[len];
494         while (len < max)
495                 dest[len++] = 0;
496         
497         return dest;
498 }
499
500
501 /*******************************************************************
502 append a string of len bytes and add a terminator
503 ********************************************************************/
504
505 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
506 {       
507         size_t start;
508         size_t len;     
509         
510         if (!dest || !src) return NULL;
511         
512         start = strlen_w(dest);
513         len = strnlen_w(src, max);
514
515         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
516         dest[start+len] = 0;
517         
518         return dest;
519 }
520
521 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
522 {       
523         size_t start;
524         size_t len;     
525         
526         if (!dest || !src) return NULL;
527         
528         start = strlen_w(dest);
529         len = strlen_w(src);
530
531         memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));                      
532         dest[start+len] = 0;
533         
534         return dest;
535 }
536
537
538 /*******************************************************************
539 replace any occurence of oldc with newc in unicode string
540 ********************************************************************/
541
542 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
543 {
544         for(;*s;s++) {
545                 if(*s==oldc) *s=newc;
546         }
547 }
548
549 /*******************************************************************
550 trim unicode string
551 ********************************************************************/
552
553 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
554                                   const smb_ucs2_t *back)
555 {
556         BOOL ret = False;
557         size_t len, front_len, back_len;
558
559         if (!s || !*s) return False;
560
561         len = strlen_w(s);
562
563         if (front && *front) {
564                 front_len = strlen_w(front);
565                 while (len && strncmp_w(s, front, front_len) == 0) {
566                         memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
567                         len -= front_len;
568                         ret = True;
569                 }
570         }
571         
572         if (back && *back) {
573                 back_len = strlen_w(back);
574                 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
575                         s[len - back_len] = 0;
576                         len -= back_len;
577                         ret = True;
578                 }
579         }
580
581         return ret;
582 }
583
584 /*
585   The *_wa() functions take a combination of 7 bit ascii
586   and wide characters They are used so that you can use string
587   functions combining C string constants with ucs2 strings
588
589   The char* arguments must NOT be multibyte - to be completely sure
590   of this only pass string constants */
591
592
593 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
594 {
595         int i;
596         for (i=0;i<PSTRING_LEN;i++) {
597                 dest[i] = UCS2_CHAR(src[i]);
598                 if (src[i] == 0) return;
599         }
600 }
601
602 int strcmp_wa(const smb_ucs2_t *a, const char *b)
603 {
604         while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
605         return (*a - UCS2_CHAR(*b));
606 }
607
608 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
609 {
610         size_t n = 0;
611         while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
612         return (len - n)?(*a - UCS2_CHAR(*b)):0;
613 }
614
615 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
616 {
617         while (*s != 0) {
618                 int i;
619                 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) 
620                         ;
621                 if (p[i]) return (smb_ucs2_t *)s;
622                 s++;
623         }
624         return NULL;
625 }
626
627 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
628 {
629         smb_ucs2_t *r;
630         size_t slen, inslen;
631
632         if (!s || !*s || !ins || !*ins) return NULL;
633         slen = strlen_w(s);
634         inslen = strlen(ins);
635         r = (smb_ucs2_t *)s;
636         while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
637                 if (strncmp_wa(r, ins, inslen) == 0) return r;
638                 r++;
639         }
640         return NULL;
641 }
642
643 /*******************************************************************
644 copy a string with max len
645 ********************************************************************/
646
647 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
648 {
649         smb_ucs2_t *ucs2_src;
650
651         if (!dest || !src) return NULL;
652         if (!(ucs2_src = acnv_uxu2(src)))
653                 return NULL;
654         
655         strncpy_w(dest, ucs2_src, max);
656         SAFE_FREE(ucs2_src);
657         return dest;
658 }
659
660 /*******************************************************************
661 convert and duplicate an ascii string
662 ********************************************************************/
663 smb_ucs2_t *strdup_wa(const char *src)
664 {
665         return strndup_wa(src, 0);
666 }
667
668 /* if len == 0 then duplicate the whole string */
669 smb_ucs2_t *strndup_wa(const char *src, size_t len)
670 {
671         smb_ucs2_t *dest, *s;
672
673         s = acnv_dosu2(src);    
674         if (!len) len = strlen_w(s);
675         dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
676         if (!dest) {
677                 DEBUG(0,("strdup_w: out of memory!\n"));
678                 SAFE_FREE(s);
679                 return NULL;
680         }
681
682         memcpy(dest, src, len * sizeof(smb_ucs2_t));
683         dest[len] = 0;
684
685         SAFE_FREE(s);
686         return dest;
687 }
688
689 /*******************************************************************
690 append a string of len bytes and add a terminator
691 ********************************************************************/
692
693 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
694 {
695         smb_ucs2_t *ucs2_src;
696
697         if (!dest || !src) return NULL;
698         if (!(ucs2_src = acnv_uxu2(src)))
699                 return NULL;
700         
701         strncat_w(dest, ucs2_src, max);
702         SAFE_FREE(ucs2_src);
703         return dest;
704 }
705
706 smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src)
707 {       
708         smb_ucs2_t *ucs2_src;
709         
710         if (!dest || !src) return NULL;
711         if (!(ucs2_src = acnv_uxu2(src)))
712                 return NULL;
713         
714         strcat_w(dest, ucs2_src);
715         SAFE_FREE(ucs2_src);
716         return dest;
717 }
718
719 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
720                                   const char *back)
721 {
722         wpstring f, b;
723
724         if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
725         else *f = 0;
726         if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
727         else *b = 0;
728         return trim_string_w(s, f, b);
729 }