Added missing strchr_wa.
[Samba/ekacnet.git] / source / lib / util_unistr.c
blob8a7253a71884765172e227cd45281a0d827be5f9
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 #ifndef MAXUNI
25 #define MAXUNI 1024
26 #endif
28 /* these 3 tables define the unicode case handling. They are loaded
29 at startup either via mmap() or read() from the lib directory */
30 static smb_ucs2_t *upcase_table;
31 static smb_ucs2_t *lowcase_table;
32 static uint8 *valid_table;
34 /*******************************************************************
35 load the case handling tables
36 ********************************************************************/
37 void load_case_tables(void)
39 static int initialised;
40 int i;
42 if (initialised) return;
43 initialised = 1;
45 upcase_table = map_file(lib_path("upcase.dat"), 0x20000);
46 lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);
47 valid_table = map_file(lib_path("valid.dat"), 0x10000);
49 /* we would like Samba to limp along even if these tables are
50 not available */
51 if (!upcase_table) {
52 DEBUG(1,("creating lame upcase table\n"));
53 upcase_table = malloc(0x20000);
54 for (i=0;i<256;i++) upcase_table[i] = islower(i)?toupper(i):i;
55 for (;i<0x10000;i++) upcase_table[i] = i;
58 if (!lowcase_table) {
59 DEBUG(1,("creating lame lowcase table\n"));
60 lowcase_table = malloc(0x20000);
61 for (i=0;i<256;i++) lowcase_table[i] = isupper(i)?tolower(i):i;
62 for (;i<0x10000;i++) lowcase_table[i] = i;
65 if (!valid_table) {
66 const char *allowed = "!#$%&'()_-@^`~";
67 DEBUG(1,("creating lame valid table\n"));
68 valid_table = malloc(0x10000);
69 for (i=0;i<256;i++) valid_table[i] = isalnum(i) || strchr(allowed,i);
70 for (;i<0x10000;i++) valid_table[i] = 0;
75 /*******************************************************************
76 Write a string in (little-endian) unicode format. src is in
77 the current DOS codepage. len is the length in bytes of the
78 string pointed to by dst.
80 if null_terminate is True then null terminate the packet (adds 2 bytes)
82 the return value is the length in bytes consumed by the string, including the
83 null termination if applied
84 ********************************************************************/
86 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
88 return push_ucs2(NULL, dst, src, len,
89 STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
93 /*******************************************************************
94 Skip past a unicode string, but not more than len. Always move
95 past a terminating zero if found.
96 ********************************************************************/
98 char *skip_unibuf(char *src, size_t len)
100 char *srcend = src + len;
102 while (src < srcend && SVAL(src,0))
103 src += 2;
105 if(!SVAL(src,0))
106 src += 2;
108 return src;
111 /* Copy a string from little-endian or big-endian unicode source (depending
112 * on flags) to internal samba format destination
114 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
116 if(dest_len==-1) dest_len=MAXUNI-3;
117 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
120 /* Copy a string from a unistr2 source to internal samba format
121 destination. Use this instead of direct calls to rpcstr_pull() to avoid
122 having to determine whether the source string is null terminated. */
124 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
126 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
127 src->uni_str_len * 2, 0);
130 /* Converts a string from internal samba format to unicode
132 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
134 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
137 /*******************************************************************
138 Return a DOS codepage version of a little-endian unicode string.
139 len is the filename length (ignoring any terminating zero) in uin16
140 units. Always null terminates.
141 Hack alert: uses fixed buffer(s).
142 ********************************************************************/
143 char *dos_unistrn2(const uint16 *src, int len)
145 static char lbufs[8][MAXUNI];
146 static int nexti;
147 char *lbuf = lbufs[nexti];
148 nexti = (nexti+1)%8;
149 pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
150 return lbuf;
153 /*******************************************************************
154 Convert a (little-endian) UNISTR2 structure to an ASCII string
155 ********************************************************************/
156 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
158 if (str == NULL) {
159 *dest='\0';
160 return;
162 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
166 /*******************************************************************
167 Return a number stored in a buffer
168 ********************************************************************/
170 uint32 buffer2_to_uint32(BUFFER2 *str)
172 if (str->buf_len == 4)
173 return IVAL(str->buffer, 0);
174 else
175 return 0;
178 /*******************************************************************
179 Convert a wchar to upper case.
180 ********************************************************************/
182 smb_ucs2_t toupper_w(smb_ucs2_t val)
184 return upcase_table[SVAL(&val,0)];
187 /*******************************************************************
188 Convert a wchar to lower case.
189 ********************************************************************/
191 smb_ucs2_t tolower_w( smb_ucs2_t val )
193 return lowcase_table[SVAL(&val,0)];
196 /*******************************************************************
197 determine if a character is lowercase
198 ********************************************************************/
199 BOOL islower_w(smb_ucs2_t c)
201 return upcase_table[SVAL(&c,0)] != c;
204 /*******************************************************************
205 determine if a character is uppercase
206 ********************************************************************/
207 BOOL isupper_w(smb_ucs2_t c)
209 return lowcase_table[SVAL(&c,0)] != c;
213 /*******************************************************************
214 determine if a character is valid in a 8.3 name
215 ********************************************************************/
216 BOOL isvalid83_w(smb_ucs2_t c)
218 return valid_table[SVAL(&c,0)] != 0;
221 /*******************************************************************
222 Count the number of characters in a smb_ucs2_t string.
223 ********************************************************************/
224 size_t strlen_w(const smb_ucs2_t *src)
226 size_t len;
228 for(len = 0; *src++; len++) ;
230 return len;
233 /*******************************************************************
234 wide strchr()
235 ********************************************************************/
236 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
238 while (*s != 0) {
239 if (c == *s) return (smb_ucs2_t *)s;
240 s++;
242 return NULL;
245 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
247 return strchr_w(s, UCS2_CHAR(c));
250 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
252 const smb_ucs2_t *p = s;
253 int len = strlen_w(s);
254 if (len == 0) return NULL;
255 p += (len - 1);
256 do {
257 if (c == *p) return (smb_ucs2_t *)p;
258 } while (p-- != s);
259 return NULL;
262 /*******************************************************************
263 wide strstr()
264 ********************************************************************/
265 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
267 smb_ucs2_t *r;
268 size_t slen, inslen;
270 if (!s || !*s || !ins || !*ins) return NULL;
271 slen = strlen_w(s);
272 inslen = strlen_w(ins);
273 r = (smb_ucs2_t *)s;
274 while (r = strchr_w(r, *ins)) {
275 if (strncmp_w(r, ins, inslen) == 0) return r;
276 r++;
278 return NULL;
281 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
283 smb_ucs2_t *r;
284 size_t slen, inslen;
286 if (!s || !*s || !ins || !*ins) return NULL;
287 slen = strlen_w(s);
288 inslen = strlen(ins);
289 r = (smb_ucs2_t *)s;
290 while (r = strchr_w(r, UCS2_CHAR(*ins))) {
291 if (strncmp_wa(r, ins, inslen) == 0) return r;
292 r++;
294 return NULL;
297 /*******************************************************************
298 Convert a string to lower case.
299 return True if any char is converted
300 ********************************************************************/
301 BOOL strlower_w(smb_ucs2_t *s)
303 BOOL ret = False;
304 while (*s) {
305 smb_ucs2_t v = tolower_w(*s);
306 if (v != *s) {
307 *s = v;
308 ret = True;
310 s++;
312 return ret;
315 /*******************************************************************
316 Convert a string to upper case.
317 return True if any char is converted
318 ********************************************************************/
319 BOOL strupper_w(smb_ucs2_t *s)
321 BOOL ret = False;
322 while (*s) {
323 smb_ucs2_t v = toupper_w(*s);
324 if (v != *s) {
325 *s = v;
326 ret = True;
328 s++;
330 return ret;
333 /*******************************************************************
334 convert a string to "normal" form
335 ********************************************************************/
336 void strnorm_w(smb_ucs2_t *s)
338 extern int case_default;
339 if (case_default == CASE_UPPER)
340 strupper_w(s);
341 else
342 strlower_w(s);
345 /*******************************************************************
346 case insensitive string comparison
347 ********************************************************************/
348 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
350 while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
351 return (tolower_w(*a) - tolower_w(*b));
354 /*******************************************************************
355 case insensitive string comparison, lenght limited
356 ********************************************************************/
357 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
359 size_t n = 0;
360 while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
361 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
364 /*******************************************************************
365 compare 2 strings
366 ********************************************************************/
367 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
369 if (s1 == s2) return(True);
370 if (!s1 || !s2) return(False);
372 return(strcasecmp_w(s1,s2)==0);
375 /*******************************************************************
376 compare 2 strings up to and including the nth char.
377 ******************************************************************/
378 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
380 if (s1 == s2) return(True);
381 if (!s1 || !s2 || !n) return(False);
383 return(strncasecmp_w(s1,s2,n)==0);
386 /*******************************************************************
387 duplicate string
388 ********************************************************************/
389 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
391 return strndup_w(src, 0);
394 /* if len == 0 then duplicate the whole string */
395 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
397 smb_ucs2_t *dest;
399 if (!len) len = strlen_w(src);
400 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
401 if (!dest) {
402 DEBUG(0,("strdup_w: out of memory!\n"));
403 return NULL;
406 memcpy(dest, src, len * sizeof(smb_ucs2_t));
407 dest[len] = 0;
409 return dest;
412 /*******************************************************************
413 copy a string with max len
414 ********************************************************************/
416 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
418 size_t len;
420 if (!dest || !src) return NULL;
422 for (len = 0; (src[len] != 0) && (len < max); len++)
423 dest[len] = src[len];
424 while (len < max)
425 dest[len++] = 0;
427 return dest;
431 /*******************************************************************
432 append a string of len bytes and add a terminator
433 ********************************************************************/
435 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
437 size_t start;
438 size_t len;
440 if (!dest || !src) return NULL;
442 start = strlen_w(dest);
443 len = strlen_w(src);
444 if (len > max) len = max;
446 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
447 dest[start+len] = 0;
449 return dest;
454 The *_wa() functions take a combination of 7 bit ascii
455 and wide characters They are used so that you can use string
456 functions combining C string constants with ucs2 strings
458 The char* arguments must NOT be multibyte - to be completely sure
459 of this only pass string constants */
462 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
464 int i;
465 for (i=0;i<PSTRING_LEN;i++) {
466 dest[i] = UCS2_CHAR(src[i]);
467 if (src[i] == 0) return;
471 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
473 while (*b && *a == *b) { a++; b++; }
474 return (*a - *b);
475 /* warning: if *a != *b and both are not 0 we retrun a random
476 greater or lesser than 0 number not realted to which
477 string is longer */
480 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
482 size_t n = 0;
483 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
484 return (len - n)?(*a - *b):0;
487 int strcmp_wa(const smb_ucs2_t *a, const char *b)
489 while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
490 return (*a - UCS2_CHAR(*b));
493 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
495 size_t n = 0;
496 while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
497 return (len - n)?(*a - UCS2_CHAR(*b)):0;
500 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
502 while (*s != 0) {
503 int i;
504 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++)
506 if (p[i]) return (smb_ucs2_t *)s;
507 s++;
509 return NULL;
513 /*******************************************************************
514 copy a string with max len
515 ********************************************************************/
517 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
519 smb_ucs2_t *ucs2_src;
521 if (!dest || !src) return NULL;
522 ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t));
523 if (!ucs2_src) {
524 DEBUG(0,("strncpy_wa: out of memory!\n"));
525 return NULL;
527 push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN);
529 strncpy_w(dest, ucs2_src, max);
530 SAFE_FREE(ucs2_src);
531 return dest;
535 /*******************************************************************
536 append a string of len bytes and add a terminator
537 ********************************************************************/
539 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
541 smb_ucs2_t *ucs2_src;
543 if (!dest || !src) return NULL;
544 ucs2_src = (smb_ucs2_t *)malloc((strlen(src)+1)*sizeof(smb_ucs2_t));
545 if (!ucs2_src) {
546 DEBUG(0,("strncat_wa: out of memory!\n"));
547 return NULL;
549 push_ucs2(NULL, ucs2_src, src, -1, STR_TERMINATE|STR_NOALIGN);
551 strncat_w(dest, ucs2_src, max);
552 SAFE_FREE(ucs2_src);
553 return dest;
556 /*******************************************************************
557 replace any occurence of oldc with newc in unicode string
558 ********************************************************************/
560 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
562 for(;*s;s++) {
563 if(*s==oldc) *s=newc;
567 /*******************************************************************
568 trim unicode string
569 ********************************************************************/
571 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
572 const smb_ucs2_t *back)
574 BOOL ret = False;
575 size_t len, lw, front_len, flw, back_len, blw;
577 if (!s || !*s) return False;
579 len = strlen_w(s);
581 if (front && *front) {
582 front_len = strlen_w(front);
583 flw = front_len * sizeof(smb_ucs2_t);
584 lw = (len + 1) * sizeof(smb_ucs2_t);
585 while (len && strncmp_w(s, front, front_len) == 0) {
586 memcpy(s, s + flw, lw - flw);
587 len -= front_len;
588 lw -= flw;
589 ret = True;
593 if (back && *back) {
594 back_len = strlen_w(back);
595 blw = back_len * sizeof(smb_ucs2_t);
596 lw = len * sizeof(smb_ucs2_t);
597 while (len && strncmp_w(s + lw - blw, back, back_len) == 0) {
598 s[len - back_len] = 0;
599 len -= back_len;
600 lw -= blw;
601 ret = True;
605 return ret;
608 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
609 const char *back)
611 wpstring f, b;
613 if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
614 else *f = 0;
615 if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
616 else *b = 0;
617 return trim_string_w(s, f, b);