1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Samba utility functions
|
---|
4 | Copyright (C) Andrew Tridgell 1992-2001
|
---|
5 | Copyright (C) Simo Sorce 2001
|
---|
6 | Copyright (C) Jeremy Allison 2005
|
---|
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 3 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, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 |
|
---|
24 | #ifndef MAXUNI
|
---|
25 | #define MAXUNI 1024
|
---|
26 | #endif
|
---|
27 |
|
---|
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;
|
---|
33 | static bool upcase_table_use_unmap;
|
---|
34 | static bool lowcase_table_use_unmap;
|
---|
35 | static bool valid_table_use_unmap;
|
---|
36 | static bool initialized;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Destroy global objects allocated by load_case_tables()
|
---|
40 | **/
|
---|
41 | void gfree_case_tables(void)
|
---|
42 | {
|
---|
43 | if ( upcase_table ) {
|
---|
44 | if ( upcase_table_use_unmap )
|
---|
45 | unmap_file(upcase_table, 0x20000);
|
---|
46 | else
|
---|
47 | SAFE_FREE(upcase_table);
|
---|
48 | upcase_table = NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if ( lowcase_table ) {
|
---|
52 | if ( lowcase_table_use_unmap )
|
---|
53 | unmap_file(lowcase_table, 0x20000);
|
---|
54 | else
|
---|
55 | SAFE_FREE(lowcase_table);
|
---|
56 | lowcase_table = NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if ( valid_table ) {
|
---|
60 | if ( valid_table_use_unmap )
|
---|
61 | unmap_file(valid_table, 0x10000);
|
---|
62 | else
|
---|
63 | SAFE_FREE(valid_table);
|
---|
64 | valid_table = NULL;
|
---|
65 | }
|
---|
66 | initialized = false;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Load or generate the case handling tables.
|
---|
71 | *
|
---|
72 | * The case tables are defined in UCS2 and don't depend on any
|
---|
73 | * configured parameters, so they never need to be reloaded.
|
---|
74 | **/
|
---|
75 |
|
---|
76 | void load_case_tables(void)
|
---|
77 | {
|
---|
78 | char *old_locale = NULL, *saved_locale = NULL;
|
---|
79 | int i;
|
---|
80 | TALLOC_CTX *frame = NULL;
|
---|
81 |
|
---|
82 | if (initialized) {
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | initialized = true;
|
---|
86 |
|
---|
87 | frame = talloc_stackframe();
|
---|
88 |
|
---|
89 | upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),
|
---|
90 | 0x20000);
|
---|
91 | upcase_table_use_unmap = ( upcase_table != NULL );
|
---|
92 |
|
---|
93 | lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),
|
---|
94 | 0x20000);
|
---|
95 | lowcase_table_use_unmap = ( lowcase_table != NULL );
|
---|
96 |
|
---|
97 | #ifdef HAVE_SETLOCALE
|
---|
98 | /* Get the name of the current locale. */
|
---|
99 | old_locale = setlocale(LC_ALL, NULL);
|
---|
100 |
|
---|
101 | if (old_locale) {
|
---|
102 | /* Save it as it is in static storage. */
|
---|
103 | saved_locale = SMB_STRDUP(old_locale);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
|
---|
107 | setlocale(LC_ALL, "C");
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | /* we would like Samba to limp along even if these tables are
|
---|
111 | not available */
|
---|
112 | if (!upcase_table) {
|
---|
113 | DEBUG(1,("creating lame upcase table\n"));
|
---|
114 | upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
|
---|
115 | for (i=0;i<0x10000;i++) {
|
---|
116 | smb_ucs2_t v;
|
---|
117 | SSVAL(&v, 0, i);
|
---|
118 | upcase_table[v] = i;
|
---|
119 | }
|
---|
120 | for (i=0;i<256;i++) {
|
---|
121 | smb_ucs2_t v;
|
---|
122 | SSVAL(&v, 0, UCS2_CHAR(i));
|
---|
123 | upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (!lowcase_table) {
|
---|
128 | DEBUG(1,("creating lame lowcase table\n"));
|
---|
129 | lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
|
---|
130 | for (i=0;i<0x10000;i++) {
|
---|
131 | smb_ucs2_t v;
|
---|
132 | SSVAL(&v, 0, i);
|
---|
133 | lowcase_table[v] = i;
|
---|
134 | }
|
---|
135 | for (i=0;i<256;i++) {
|
---|
136 | smb_ucs2_t v;
|
---|
137 | SSVAL(&v, 0, UCS2_CHAR(i));
|
---|
138 | lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | #ifdef HAVE_SETLOCALE
|
---|
143 | /* Restore the old locale. */
|
---|
144 | if (saved_locale) {
|
---|
145 | setlocale (LC_ALL, saved_locale);
|
---|
146 | SAFE_FREE(saved_locale);
|
---|
147 | }
|
---|
148 | #endif
|
---|
149 | TALLOC_FREE(frame);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static int check_dos_char_slowly(smb_ucs2_t c)
|
---|
153 | {
|
---|
154 | char buf[10];
|
---|
155 | smb_ucs2_t c2 = 0;
|
---|
156 | int len1, len2;
|
---|
157 |
|
---|
158 | len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
|
---|
159 | if (len1 == 0) {
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 | len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
|
---|
163 | if (len2 != 2) {
|
---|
164 | return 0;
|
---|
165 | }
|
---|
166 | return (c == c2);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Load the valid character map table from <tt>valid.dat</tt> or
|
---|
171 | * create from the configured codepage.
|
---|
172 | *
|
---|
173 | * This function is called whenever the configuration is reloaded.
|
---|
174 | * However, the valid character table is not changed if it's loaded
|
---|
175 | * from a file, because we can't unmap files.
|
---|
176 | **/
|
---|
177 |
|
---|
178 | void init_valid_table(void)
|
---|
179 | {
|
---|
180 | static int mapped_file;
|
---|
181 | int i;
|
---|
182 | const char *allowed = ".!#$%&'()_-@^`~";
|
---|
183 | uint8 *valid_file;
|
---|
184 |
|
---|
185 | if (mapped_file) {
|
---|
186 | /* Can't unmap files, so stick with what we have */
|
---|
187 | return;
|
---|
188 | }
|
---|
189 |
|
---|
190 | valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000);
|
---|
191 | if (valid_file) {
|
---|
192 | valid_table = valid_file;
|
---|
193 | mapped_file = 1;
|
---|
194 | valid_table_use_unmap = True;
|
---|
195 | return;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /* Otherwise, we're using a dynamically created valid_table.
|
---|
199 | * It might need to be regenerated if the code page changed.
|
---|
200 | * We know that we're not using a mapped file, so we can
|
---|
201 | * free() the old one. */
|
---|
202 | SAFE_FREE(valid_table);
|
---|
203 |
|
---|
204 | /* use free rather than unmap */
|
---|
205 | valid_table_use_unmap = False;
|
---|
206 |
|
---|
207 | DEBUG(2,("creating default valid table\n"));
|
---|
208 | valid_table = (uint8 *)SMB_MALLOC(0x10000);
|
---|
209 | SMB_ASSERT(valid_table != NULL);
|
---|
210 | for (i=0;i<128;i++) {
|
---|
211 | valid_table[i] = isalnum(i) || strchr(allowed,i);
|
---|
212 | }
|
---|
213 |
|
---|
214 | lazy_initialize_conv();
|
---|
215 |
|
---|
216 | for (;i<0x10000;i++) {
|
---|
217 | smb_ucs2_t c;
|
---|
218 | SSVAL(&c, 0, i);
|
---|
219 | valid_table[i] = check_dos_char_slowly(c);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | /*******************************************************************
|
---|
224 | Write a string in (little-endian) unicode format. src is in
|
---|
225 | the current DOS codepage. len is the length in bytes of the
|
---|
226 | string pointed to by dst.
|
---|
227 |
|
---|
228 | if null_terminate is True then null terminate the packet (adds 2 bytes)
|
---|
229 |
|
---|
230 | the return value is the length in bytes consumed by the string, including the
|
---|
231 | null termination if applied
|
---|
232 | ********************************************************************/
|
---|
233 |
|
---|
234 | size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
|
---|
235 | {
|
---|
236 | int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
|
---|
237 | : STR_UNICODE|STR_NOALIGN;
|
---|
238 | return push_ucs2(NULL, dst, src, len, flags);
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | /*******************************************************************
|
---|
243 | Skip past a unicode string, but not more than len. Always move
|
---|
244 | past a terminating zero if found.
|
---|
245 | ********************************************************************/
|
---|
246 |
|
---|
247 | char *skip_unibuf(char *src, size_t len)
|
---|
248 | {
|
---|
249 | char *srcend = src + len;
|
---|
250 |
|
---|
251 | while (src < srcend && SVAL(src,0)) {
|
---|
252 | src += 2;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if(!SVAL(src,0)) {
|
---|
256 | src += 2;
|
---|
257 | }
|
---|
258 |
|
---|
259 | return src;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /* Converts a string from internal samba format to unicode
|
---|
263 | */
|
---|
264 |
|
---|
265 | int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
|
---|
266 | {
|
---|
267 | return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* Converts a string from internal samba format to unicode. Always terminates.
|
---|
271 | * Actually just a wrapper round push_ucs2_talloc().
|
---|
272 | */
|
---|
273 |
|
---|
274 | int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
|
---|
275 | {
|
---|
276 | size_t size;
|
---|
277 | if (push_ucs2_talloc(ctx, dest, src, &size))
|
---|
278 | return size;
|
---|
279 | else
|
---|
280 | return -1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /*******************************************************************
|
---|
284 | Convert a wchar to upper case.
|
---|
285 | ********************************************************************/
|
---|
286 |
|
---|
287 | smb_ucs2_t toupper_w(smb_ucs2_t val)
|
---|
288 | {
|
---|
289 | return upcase_table[SVAL(&val,0)];
|
---|
290 | }
|
---|
291 |
|
---|
292 | /*******************************************************************
|
---|
293 | Convert a wchar to lower case.
|
---|
294 | ********************************************************************/
|
---|
295 |
|
---|
296 | smb_ucs2_t tolower_w( smb_ucs2_t val )
|
---|
297 | {
|
---|
298 | return lowcase_table[SVAL(&val,0)];
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*******************************************************************
|
---|
302 | Determine if a character is lowercase.
|
---|
303 | ********************************************************************/
|
---|
304 |
|
---|
305 | bool islower_w(smb_ucs2_t c)
|
---|
306 | {
|
---|
307 | return upcase_table[SVAL(&c,0)] != c;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /*******************************************************************
|
---|
311 | Determine if a character is uppercase.
|
---|
312 | ********************************************************************/
|
---|
313 |
|
---|
314 | bool isupper_w(smb_ucs2_t c)
|
---|
315 | {
|
---|
316 | return lowcase_table[SVAL(&c,0)] != c;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*******************************************************************
|
---|
320 | Determine if a character is valid in a 8.3 name.
|
---|
321 | ********************************************************************/
|
---|
322 |
|
---|
323 | bool isvalid83_w(smb_ucs2_t c)
|
---|
324 | {
|
---|
325 | return valid_table[SVAL(&c,0)] != 0;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /*******************************************************************
|
---|
329 | Count the number of characters in a smb_ucs2_t string.
|
---|
330 | ********************************************************************/
|
---|
331 |
|
---|
332 | size_t strlen_w(const smb_ucs2_t *src)
|
---|
333 | {
|
---|
334 | size_t len;
|
---|
335 | smb_ucs2_t c;
|
---|
336 |
|
---|
337 | for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
|
---|
338 | ;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return len;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*******************************************************************
|
---|
345 | Count up to max number of characters in a smb_ucs2_t string.
|
---|
346 | ********************************************************************/
|
---|
347 |
|
---|
348 | size_t strnlen_w(const smb_ucs2_t *src, size_t max)
|
---|
349 | {
|
---|
350 | size_t len;
|
---|
351 | smb_ucs2_t c;
|
---|
352 |
|
---|
353 | for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
|
---|
354 | ;
|
---|
355 | }
|
---|
356 |
|
---|
357 | return len;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /*******************************************************************
|
---|
361 | Wide strchr().
|
---|
362 | ********************************************************************/
|
---|
363 |
|
---|
364 | smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
|
---|
365 | {
|
---|
366 | smb_ucs2_t cp;
|
---|
367 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
---|
368 | if (c == cp) {
|
---|
369 | return (smb_ucs2_t *)s;
|
---|
370 | }
|
---|
371 | s++;
|
---|
372 | }
|
---|
373 | if (c == cp) {
|
---|
374 | return (smb_ucs2_t *)s;
|
---|
375 | }
|
---|
376 |
|
---|
377 | return NULL;
|
---|
378 | }
|
---|
379 |
|
---|
380 | smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
|
---|
381 | {
|
---|
382 | return strchr_w(s, UCS2_CHAR(c));
|
---|
383 | }
|
---|
384 |
|
---|
385 | /*******************************************************************
|
---|
386 | Wide strrchr().
|
---|
387 | ********************************************************************/
|
---|
388 |
|
---|
389 | smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
|
---|
390 | {
|
---|
391 | smb_ucs2_t cp;
|
---|
392 | const smb_ucs2_t *p = s;
|
---|
393 | int len = strlen_w(s);
|
---|
394 |
|
---|
395 | if (len == 0) {
|
---|
396 | return NULL;
|
---|
397 | }
|
---|
398 | p += (len - 1);
|
---|
399 | do {
|
---|
400 | if (c == *(COPY_UCS2_CHAR(&cp,p))) {
|
---|
401 | return (smb_ucs2_t *)p;
|
---|
402 | }
|
---|
403 | } while (p-- != s);
|
---|
404 | return NULL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /*******************************************************************
|
---|
408 | Wide version of strrchr that returns after doing strrchr 'n' times.
|
---|
409 | ********************************************************************/
|
---|
410 |
|
---|
411 | smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
|
---|
412 | {
|
---|
413 | smb_ucs2_t cp;
|
---|
414 | const smb_ucs2_t *p = s;
|
---|
415 | int len = strlen_w(s);
|
---|
416 |
|
---|
417 | if (len == 0 || !n) {
|
---|
418 | return NULL;
|
---|
419 | }
|
---|
420 | p += (len - 1);
|
---|
421 | do {
|
---|
422 | if (c == *(COPY_UCS2_CHAR(&cp,p))) {
|
---|
423 | n--;
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (!n) {
|
---|
427 | return (smb_ucs2_t *)p;
|
---|
428 | }
|
---|
429 | } while (p-- != s);
|
---|
430 | return NULL;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /*******************************************************************
|
---|
434 | Wide strstr().
|
---|
435 | ********************************************************************/
|
---|
436 |
|
---|
437 | smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
|
---|
438 | {
|
---|
439 | smb_ucs2_t *r;
|
---|
440 | size_t inslen;
|
---|
441 |
|
---|
442 | if (!s || !*s || !ins || !*ins) {
|
---|
443 | return NULL;
|
---|
444 | }
|
---|
445 |
|
---|
446 | inslen = strlen_w(ins);
|
---|
447 | r = (smb_ucs2_t *)s;
|
---|
448 |
|
---|
449 | while ((r = strchr_w(r, *ins))) {
|
---|
450 | if (strncmp_w(r, ins, inslen) == 0) {
|
---|
451 | return r;
|
---|
452 | }
|
---|
453 | r++;
|
---|
454 | }
|
---|
455 |
|
---|
456 | return NULL;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /*******************************************************************
|
---|
460 | Convert a string to lower case.
|
---|
461 | return True if any char is converted
|
---|
462 | ********************************************************************/
|
---|
463 |
|
---|
464 | bool strlower_w(smb_ucs2_t *s)
|
---|
465 | {
|
---|
466 | smb_ucs2_t cp;
|
---|
467 | bool ret = False;
|
---|
468 |
|
---|
469 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
---|
470 | smb_ucs2_t v = tolower_w(cp);
|
---|
471 | if (v != cp) {
|
---|
472 | COPY_UCS2_CHAR(s,&v);
|
---|
473 | ret = True;
|
---|
474 | }
|
---|
475 | s++;
|
---|
476 | }
|
---|
477 | return ret;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*******************************************************************
|
---|
481 | Convert a string to upper case.
|
---|
482 | return True if any char is converted
|
---|
483 | ********************************************************************/
|
---|
484 |
|
---|
485 | bool strupper_w(smb_ucs2_t *s)
|
---|
486 | {
|
---|
487 | smb_ucs2_t cp;
|
---|
488 | bool ret = False;
|
---|
489 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
---|
490 | smb_ucs2_t v = toupper_w(cp);
|
---|
491 | if (v != cp) {
|
---|
492 | COPY_UCS2_CHAR(s,&v);
|
---|
493 | ret = True;
|
---|
494 | }
|
---|
495 | s++;
|
---|
496 | }
|
---|
497 | return ret;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /*******************************************************************
|
---|
501 | Convert a string to "normal" form.
|
---|
502 | ********************************************************************/
|
---|
503 |
|
---|
504 | void strnorm_w(smb_ucs2_t *s, int case_default)
|
---|
505 | {
|
---|
506 | if (case_default == CASE_UPPER) {
|
---|
507 | strupper_w(s);
|
---|
508 | } else {
|
---|
509 | strlower_w(s);
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
|
---|
514 | {
|
---|
515 | smb_ucs2_t cpa, cpb;
|
---|
516 |
|
---|
517 | while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
|
---|
518 | a++;
|
---|
519 | b++;
|
---|
520 | }
|
---|
521 | return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
|
---|
522 | /* warning: if *a != *b and both are not 0 we return a random
|
---|
523 | greater or lesser than 0 number not realted to which
|
---|
524 | string is longer */
|
---|
525 | }
|
---|
526 |
|
---|
527 | int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
|
---|
528 | {
|
---|
529 | smb_ucs2_t cpa, cpb;
|
---|
530 | size_t n = 0;
|
---|
531 |
|
---|
532 | while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
|
---|
533 | a++;
|
---|
534 | b++;
|
---|
535 | n++;
|
---|
536 | }
|
---|
537 | return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /*******************************************************************
|
---|
541 | Case insensitive string comparison.
|
---|
542 | ********************************************************************/
|
---|
543 |
|
---|
544 | int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
|
---|
545 | {
|
---|
546 | smb_ucs2_t cpa, cpb;
|
---|
547 |
|
---|
548 | while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
|
---|
549 | a++;
|
---|
550 | b++;
|
---|
551 | }
|
---|
552 | return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
|
---|
553 | }
|
---|
554 |
|
---|
555 | /*******************************************************************
|
---|
556 | Case insensitive string comparison, length limited.
|
---|
557 | ********************************************************************/
|
---|
558 |
|
---|
559 | int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
|
---|
560 | {
|
---|
561 | smb_ucs2_t cpa, cpb;
|
---|
562 | size_t n = 0;
|
---|
563 |
|
---|
564 | while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
|
---|
565 | a++;
|
---|
566 | b++;
|
---|
567 | n++;
|
---|
568 | }
|
---|
569 | return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /*******************************************************************
|
---|
573 | Compare 2 strings.
|
---|
574 | ********************************************************************/
|
---|
575 |
|
---|
576 | bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
|
---|
577 | {
|
---|
578 | if (s1 == s2) {
|
---|
579 | return(True);
|
---|
580 | }
|
---|
581 | if (!s1 || !s2) {
|
---|
582 | return(False);
|
---|
583 | }
|
---|
584 |
|
---|
585 | return(strcasecmp_w(s1,s2)==0);
|
---|
586 | }
|
---|
587 |
|
---|
588 | /*******************************************************************
|
---|
589 | Compare 2 strings up to and including the nth char.
|
---|
590 | ******************************************************************/
|
---|
591 |
|
---|
592 | bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
|
---|
593 | {
|
---|
594 | if (s1 == s2) {
|
---|
595 | return(True);
|
---|
596 | }
|
---|
597 | if (!s1 || !s2 || !n) {
|
---|
598 | return(False);
|
---|
599 | }
|
---|
600 |
|
---|
601 | return(strncasecmp_w(s1,s2,n)==0);
|
---|
602 | }
|
---|
603 |
|
---|
604 | /*******************************************************************
|
---|
605 | Duplicate string.
|
---|
606 | ********************************************************************/
|
---|
607 |
|
---|
608 | smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
|
---|
609 | {
|
---|
610 | return strndup_w(src, 0);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* if len == 0 then duplicate the whole string */
|
---|
614 |
|
---|
615 | smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
|
---|
616 | {
|
---|
617 | smb_ucs2_t *dest;
|
---|
618 |
|
---|
619 | if (!len) {
|
---|
620 | len = strlen_w(src);
|
---|
621 | }
|
---|
622 | dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
|
---|
623 | if (!dest) {
|
---|
624 | DEBUG(0,("strdup_w: out of memory!\n"));
|
---|
625 | return NULL;
|
---|
626 | }
|
---|
627 |
|
---|
628 | memcpy(dest, src, len * sizeof(smb_ucs2_t));
|
---|
629 | dest[len] = 0;
|
---|
630 | return dest;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /*******************************************************************
|
---|
634 | Copy a string with max len.
|
---|
635 | ********************************************************************/
|
---|
636 |
|
---|
637 | smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
|
---|
638 | {
|
---|
639 | smb_ucs2_t cp;
|
---|
640 | size_t len;
|
---|
641 |
|
---|
642 | if (!dest || !src) {
|
---|
643 | return NULL;
|
---|
644 | }
|
---|
645 |
|
---|
646 | for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
|
---|
647 | cp = *COPY_UCS2_CHAR(dest+len,src+len);
|
---|
648 | }
|
---|
649 | cp = 0;
|
---|
650 | for ( /*nothing*/ ; len < max; len++ ) {
|
---|
651 | cp = *COPY_UCS2_CHAR(dest+len,&cp);
|
---|
652 | }
|
---|
653 |
|
---|
654 | return dest;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*******************************************************************
|
---|
658 | Append a string of len bytes and add a terminator.
|
---|
659 | ********************************************************************/
|
---|
660 |
|
---|
661 | smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
|
---|
662 | {
|
---|
663 | size_t start;
|
---|
664 | size_t len;
|
---|
665 | smb_ucs2_t z = 0;
|
---|
666 |
|
---|
667 | if (!dest || !src) {
|
---|
668 | return NULL;
|
---|
669 | }
|
---|
670 |
|
---|
671 | start = strlen_w(dest);
|
---|
672 | len = strnlen_w(src, max);
|
---|
673 |
|
---|
674 | memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
|
---|
675 | z = *COPY_UCS2_CHAR(dest+start+len,&z);
|
---|
676 |
|
---|
677 | return dest;
|
---|
678 | }
|
---|
679 |
|
---|
680 | smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
|
---|
681 | {
|
---|
682 | size_t start;
|
---|
683 | size_t len;
|
---|
684 | smb_ucs2_t z = 0;
|
---|
685 |
|
---|
686 | if (!dest || !src) {
|
---|
687 | return NULL;
|
---|
688 | }
|
---|
689 |
|
---|
690 | start = strlen_w(dest);
|
---|
691 | len = strlen_w(src);
|
---|
692 |
|
---|
693 | memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
|
---|
694 | z = *COPY_UCS2_CHAR(dest+start+len,&z);
|
---|
695 |
|
---|
696 | return dest;
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | /*******************************************************************
|
---|
701 | Replace any occurence of oldc with newc in unicode string.
|
---|
702 | ********************************************************************/
|
---|
703 |
|
---|
704 | void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
|
---|
705 | {
|
---|
706 | smb_ucs2_t cp;
|
---|
707 |
|
---|
708 | for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
|
---|
709 | if(cp==oldc) {
|
---|
710 | COPY_UCS2_CHAR(s,&newc);
|
---|
711 | }
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | /*******************************************************************
|
---|
716 | Trim unicode string.
|
---|
717 | ********************************************************************/
|
---|
718 |
|
---|
719 | bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
|
---|
720 | const smb_ucs2_t *back)
|
---|
721 | {
|
---|
722 | bool ret = False;
|
---|
723 | size_t len, front_len, back_len;
|
---|
724 |
|
---|
725 | if (!s) {
|
---|
726 | return False;
|
---|
727 | }
|
---|
728 |
|
---|
729 | len = strlen_w(s);
|
---|
730 |
|
---|
731 | if (front && *front) {
|
---|
732 | front_len = strlen_w(front);
|
---|
733 | while (len && strncmp_w(s, front, front_len) == 0) {
|
---|
734 | memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
|
---|
735 | len -= front_len;
|
---|
736 | ret = True;
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | if (back && *back) {
|
---|
741 | back_len = strlen_w(back);
|
---|
742 | while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
|
---|
743 | s[len - back_len] = 0;
|
---|
744 | len -= back_len;
|
---|
745 | ret = True;
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | return ret;
|
---|
750 | }
|
---|
751 |
|
---|
752 | /*
|
---|
753 | The *_wa() functions take a combination of 7 bit ascii
|
---|
754 | and wide characters They are used so that you can use string
|
---|
755 | functions combining C string constants with ucs2 strings
|
---|
756 |
|
---|
757 | The char* arguments must NOT be multibyte - to be completely sure
|
---|
758 | of this only pass string constants */
|
---|
759 |
|
---|
760 | int strcmp_wa(const smb_ucs2_t *a, const char *b)
|
---|
761 | {
|
---|
762 | smb_ucs2_t cp = 0;
|
---|
763 |
|
---|
764 | while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
|
---|
765 | a++;
|
---|
766 | b++;
|
---|
767 | }
|
---|
768 | return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
|
---|
769 | }
|
---|
770 |
|
---|
771 | int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
|
---|
772 | {
|
---|
773 | smb_ucs2_t cp = 0;
|
---|
774 | size_t n = 0;
|
---|
775 |
|
---|
776 | while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
|
---|
777 | a++;
|
---|
778 | b++;
|
---|
779 | n++;
|
---|
780 | }
|
---|
781 | return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
|
---|
782 | }
|
---|
783 |
|
---|
784 | smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
|
---|
785 | {
|
---|
786 | smb_ucs2_t cp;
|
---|
787 |
|
---|
788 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
---|
789 | int i;
|
---|
790 | for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
|
---|
791 | ;
|
---|
792 | if (p[i]) {
|
---|
793 | return (smb_ucs2_t *)s;
|
---|
794 | }
|
---|
795 | s++;
|
---|
796 | }
|
---|
797 | return NULL;
|
---|
798 | }
|
---|
799 |
|
---|
800 | smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
|
---|
801 | {
|
---|
802 | smb_ucs2_t *r;
|
---|
803 | size_t inslen;
|
---|
804 |
|
---|
805 | if (!s || !ins) {
|
---|
806 | return NULL;
|
---|
807 | }
|
---|
808 |
|
---|
809 | inslen = strlen(ins);
|
---|
810 | r = (smb_ucs2_t *)s;
|
---|
811 |
|
---|
812 | while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
|
---|
813 | if (strncmp_wa(r, ins, inslen) == 0)
|
---|
814 | return r;
|
---|
815 | r++;
|
---|
816 | }
|
---|
817 |
|
---|
818 | return NULL;
|
---|
819 | }
|
---|
820 |
|
---|
821 | /*************************************************************
|
---|
822 | ascii only toupper - saves the need for smbd to be in C locale.
|
---|
823 | *************************************************************/
|
---|
824 |
|
---|
825 | int toupper_ascii(int c)
|
---|
826 | {
|
---|
827 | smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
|
---|
828 | return UCS2_TO_CHAR(uc);
|
---|
829 | }
|
---|
830 |
|
---|
831 | /*************************************************************
|
---|
832 | ascii only tolower - saves the need for smbd to be in C locale.
|
---|
833 | *************************************************************/
|
---|
834 |
|
---|
835 | int tolower_ascii(int c)
|
---|
836 | {
|
---|
837 | smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
|
---|
838 | return UCS2_TO_CHAR(uc);
|
---|
839 | }
|
---|
840 |
|
---|
841 | /*************************************************************
|
---|
842 | ascii only isupper - saves the need for smbd to be in C locale.
|
---|
843 | *************************************************************/
|
---|
844 |
|
---|
845 | int isupper_ascii(int c)
|
---|
846 | {
|
---|
847 | return isupper_w(UCS2_CHAR(c));
|
---|
848 | }
|
---|
849 |
|
---|
850 | /*************************************************************
|
---|
851 | ascii only islower - saves the need for smbd to be in C locale.
|
---|
852 | *************************************************************/
|
---|
853 |
|
---|
854 | int islower_ascii(int c)
|
---|
855 | {
|
---|
856 | return islower_w(UCS2_CHAR(c));
|
---|
857 | }
|
---|