2 Unix SMB/Netbios implementation.
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.
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
;
42 if (initialised
) return;
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
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
;
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
;
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))
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
];
147 char *lbuf
= lbufs
[nexti
];
149 pull_ucs2(NULL
, lbuf
, src
, MAXUNI
-3, len
*2, STR_NOALIGN
);
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
)
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);
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
)
228 for(len
= 0; *src
++; len
++) ;
233 /*******************************************************************
235 ********************************************************************/
236 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
239 if (c
== *s
) return (smb_ucs2_t
*)s
;
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
;
257 if (c
== *p
) return (smb_ucs2_t
*)p
;
262 /*******************************************************************
264 ********************************************************************/
265 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
270 if (!s
|| !*s
|| !ins
|| !*ins
) return NULL
;
272 inslen
= strlen_w(ins
);
274 while (r
= strchr_w(r
, *ins
)) {
275 if (strncmp_w(r
, ins
, inslen
) == 0) return r
;
281 smb_ucs2_t
*strstr_wa(const smb_ucs2_t
*s
, const char *ins
)
286 if (!s
|| !*s
|| !ins
|| !*ins
) return NULL
;
288 inslen
= strlen(ins
);
290 while (r
= strchr_w(r
, UCS2_CHAR(*ins
))) {
291 if (strncmp_wa(r
, ins
, inslen
) == 0) return r
;
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
)
305 smb_ucs2_t v
= tolower_w(*s
);
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
)
323 smb_ucs2_t v
= toupper_w(*s
);
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
)
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
)
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 /*******************************************************************
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 /*******************************************************************
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
)
399 if (!len
) len
= strlen_w(src
);
400 dest
= (smb_ucs2_t
*)malloc((len
+ 1) * sizeof(smb_ucs2_t
));
402 DEBUG(0,("strdup_w: out of memory!\n"));
406 memcpy(dest
, src
, len
* sizeof(smb_ucs2_t
));
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
)
420 if (!dest
|| !src
) return NULL
;
422 for (len
= 0; (src
[len
] != 0) && (len
< max
); len
++)
423 dest
[len
] = src
[len
];
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
)
440 if (!dest
|| !src
) return NULL
;
442 start
= strlen_w(dest
);
444 if (len
> max
) len
= max
;
446 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
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
)
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
++; }
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
480 int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
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
)
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
)
504 for (i
=0; p
[i
] && *s
!= UCS2_CHAR(p
[i
]); i
++)
506 if (p
[i
]) return (smb_ucs2_t
*)s
;
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
));
524 DEBUG(0,("strncpy_wa: out of memory!\n"));
527 push_ucs2(NULL
, ucs2_src
, src
, -1, STR_TERMINATE
|STR_NOALIGN
);
529 strncpy_w(dest
, ucs2_src
, max
);
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
));
546 DEBUG(0,("strncat_wa: out of memory!\n"));
549 push_ucs2(NULL
, ucs2_src
, src
, -1, STR_TERMINATE
|STR_NOALIGN
);
551 strncat_w(dest
, ucs2_src
, max
);
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
)
563 if(*s
==oldc
) *s
=newc
;
567 /*******************************************************************
569 ********************************************************************/
571 BOOL
trim_string_w(smb_ucs2_t
*s
, const smb_ucs2_t
*front
,
572 const smb_ucs2_t
*back
)
575 size_t len
, lw
, front_len
, flw
, back_len
, blw
;
577 if (!s
|| !*s
) return False
;
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
);
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;
608 BOOL
trim_string_wa(smb_ucs2_t
*s
, const char *front
,
613 if (front
) push_ucs2(NULL
, f
, front
, sizeof(wpstring
) - 1, STR_TERMINATE
);
615 if (back
) push_ucs2(NULL
, b
, back
, sizeof(wpstring
) - 1, STR_TERMINATE
);
617 return trim_string_w(s
, f
, b
);