Changeset 77 for trunk/samba/source/lib/util_str.c
- Timestamp:
- Sep 30, 2007, 3:42:50 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/samba/source/lib/util_str.c
r60 r77 7 7 Copyright (C) Martin Pool 2003 8 8 Copyright (C) James Peach 2006 9 Copyright (C) Jeremy Allison 1992-2007 9 10 10 11 This program is free software; you can redistribute it and/or modify 11 12 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2of the License, or13 the Free Software Foundation; either version 3 of the License, or 13 14 (at your option) any later version. 14 15 … … 19 20 20 21 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 along with this program. If not, see <http://www.gnu.org/licenses/>. 23 23 */ 24 24 … … 126 126 return ret; 127 127 } 128 129 static uint16 tmpbuf[sizeof(pstring)];130 128 131 129 void set_first_token(char *ptr) … … 268 266 Case insensitive string compararison, length limited. 269 267 **/ 270 int StrnCaseCmp(const char *s, const char *t, size_t n) 271 { 272 pstring buf1, buf2; 273 unix_strupper(s, strlen(s)+1, buf1, sizeof(buf1)); 274 unix_strupper(t, strlen(t)+1, buf2, sizeof(buf2)); 275 return strncmp(buf1,buf2,n); 268 int StrnCaseCmp(const char *s, const char *t, size_t len) 269 { 270 size_t n = 0; 271 const char *ps, *pt; 272 size_t size; 273 smb_ucs2_t *buffer_s, *buffer_t; 274 int ret; 275 276 for (ps = s, pt = t; n < len ; ps++, pt++, n++) { 277 char us, ut; 278 279 if (!*ps && !*pt) 280 return 0; /* both ended */ 281 else if (!*ps) 282 return -1; /* s is a prefix */ 283 else if (!*pt) 284 return +1; /* t is a prefix */ 285 else if ((*ps & 0x80) || (*pt & 0x80)) 286 /* not ascii anymore, do it the 287 * hard way from here on in */ 288 break; 289 290 us = toupper_ascii(*ps); 291 ut = toupper_ascii(*pt); 292 if (us == ut) 293 continue; 294 else if (us < ut) 295 return -1; 296 else if (us > ut) 297 return +1; 298 } 299 300 if (n == len) { 301 return 0; 302 } 303 304 size = push_ucs2_allocate(&buffer_s, ps); 305 if (size == (size_t)-1) { 306 return strncmp(ps, pt, len-n); 307 /* Not quite the right answer, but finding the right one 308 under this failure case is expensive, 309 and it's pretty close */ 310 } 311 312 size = push_ucs2_allocate(&buffer_t, pt); 313 if (size == (size_t)-1) { 314 SAFE_FREE(buffer_s); 315 return strncmp(ps, pt, len-n); 316 /* Not quite the right answer, but finding the right one 317 under this failure case is expensive, 318 and it's pretty close */ 319 } 320 321 ret = strncasecmp_w(buffer_s, buffer_t, len-n); 322 SAFE_FREE(buffer_s); 323 SAFE_FREE(buffer_t); 324 return ret; 276 325 } 277 326 … … 287 336 if (!s1 || !s2) 288 337 return(False); 289 338 290 339 return(StrCaseCmp(s1,s2)==0); 291 340 } … … 302 351 if (!s1 || !s2 || !n) 303 352 return(False); 304 353 305 354 return(StrnCaseCmp(s1,s2,n)==0); 306 355 } … … 357 406 char *strupper_static(const char *s) 358 407 { 359 static pstring str; 360 361 pstrcpy(str, s); 408 static char *str = NULL; 409 410 if (str) { 411 SAFE_FREE(str); 412 } 413 str = SMB_STRDUP(s); 414 if (!str) { 415 return CONST_DISCARD(char *,s); 416 } 362 417 strupper_m(str); 363 364 418 return str; 365 419 } … … 385 439 if (case_default == CASE_UPPER) 386 440 return(!strhaslower(s)); 387 441 388 442 return(!strhasupper(s)); 389 443 } … … 394 448 NOTE: oldc and newc must be 7 bit characters 395 449 **/ 396 397 void string_replace( pstring s, char oldc, char newc ) 450 void string_replace( char *s, char oldc, char newc ) 398 451 { 399 452 char *p; … … 407 460 if (*p & 0x80) /* mb string - slow path. */ 408 461 break; 409 if (*p == oldc) 462 if (*p == oldc) { 410 463 *p = newc; 464 } 411 465 } 412 466 … … 419 473 p = s; 420 474 #endif 421 push_ucs2(NULL, tmpbuf, p, sizeof(tmpbuf), STR_TERMINATE); 422 string_replace_w(tmpbuf, UCS2_CHAR(oldc), UCS2_CHAR(newc)); 423 pull_ucs2(NULL, p, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE); 475 476 while (*p) { 477 size_t c_size; 478 next_codepoint(p, &c_size); 479 480 if (c_size == 1) { 481 if (*p == oldc) { 482 *p = newc; 483 } 484 } 485 p += c_size; 486 } 424 487 } 425 488 … … 468 531 size_t str_charnum(const char *s) 469 532 { 470 uint16 tmpbuf2[sizeof(pstring)]; 471 push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE); 472 return strlen_w(tmpbuf2); 533 size_t ret; 534 smb_ucs2_t *tmpbuf2 = NULL; 535 if (push_ucs2_allocate(&tmpbuf2, s) == (size_t)-1) { 536 return 0; 537 } 538 ret = strlen_w(tmpbuf2); 539 SAFE_FREE(tmpbuf2); 540 return ret; 473 541 } 474 542 … … 481 549 size_t str_ascii_charnum(const char *s) 482 550 { 483 pstring tmpbuf2; 484 push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE); 485 return strlen(tmpbuf2); 551 size_t ret; 552 char *tmpbuf2 = NULL; 553 if (push_ascii_allocate(&tmpbuf2, s) == (size_t)-1) { 554 return 0; 555 } 556 ret = strlen(tmpbuf2); 557 SAFE_FREE(tmpbuf2); 558 return ret; 486 559 } 487 560 … … 585 658 BOOL strhasupper(const char *s) 586 659 { 587 smb_ucs2_t *ptr; 588 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); 589 for(ptr=tmpbuf;*ptr;ptr++) 590 if(isupper_w(*ptr)) 591 return True; 592 return(False); 660 smb_ucs2_t *tmp, *p; 661 BOOL ret; 662 663 if (push_ucs2_allocate(&tmp, s) == -1) { 664 return False; 665 } 666 667 for(p = tmp; *p != 0; p++) { 668 if(isupper_w(*p)) { 669 break; 670 } 671 } 672 673 ret = (*p != 0); 674 SAFE_FREE(tmp); 675 return ret; 593 676 } 594 677 … … 599 682 BOOL strhaslower(const char *s) 600 683 { 601 smb_ucs2_t *ptr; 602 push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE); 603 for(ptr=tmpbuf;*ptr;ptr++) 604 if(islower_w(*ptr)) 605 return True; 606 return(False); 684 smb_ucs2_t *tmp, *p; 685 BOOL ret; 686 687 if (push_ucs2_allocate(&tmp, s) == -1) { 688 return False; 689 } 690 691 for(p = tmp; *p != 0; p++) { 692 if(islower_w(*p)) { 693 break; 694 } 695 } 696 697 ret = (*p != 0); 698 SAFE_FREE(tmp); 699 return ret; 607 700 } 608 701 … … 891 984 BOOL in_list(const char *s, const char *list, BOOL casesensitive) 892 985 { 893 pstringtok;986 char *tok; 894 987 const char *p=list; 988 size_t bufsize = strlen(list); 989 BOOL ret = False; 895 990 896 991 if (!list) 897 992 return(False); 898 993 899 while (next_token(&p,tok,LIST_SEP,sizeof(tok))) { 994 /* We know a token can't be larger 995 * than the entire list. */ 996 997 tok = SMB_MALLOC_ARRAY(char, bufsize+1); 998 if (!tok) { 999 return False; 1000 } 1001 1002 while (next_token(&p,tok,LIST_SEP,bufsize+1)) { 900 1003 if (casesensitive) { 901 if (strcmp(tok,s) == 0) 902 return(True); 1004 if (strcmp(tok,s) == 0) { 1005 ret = True; 1006 break; 1007 } 903 1008 } else { 904 if (StrCaseCmp(tok,s) == 0) 905 return(True); 906 } 907 } 908 return(False); 1009 if (StrCaseCmp(tok,s) == 0) { 1010 ret = True; 1011 break; 1012 } 1013 } 1014 } 1015 1016 SAFE_FREE(tok); 1017 return ret; 909 1018 } 910 1019 … … 1354 1463 char *strchr_m(const char *src, char c) 1355 1464 { 1356 wpstring ws;1357 pstring s2;1465 smb_ucs2_t *ws = NULL; 1466 char *s2 = NULL; 1358 1467 smb_ucs2_t *p; 1359 1468 const char *s; 1469 char *ret; 1360 1470 1361 1471 /* characters below 0x3F are guaranteed to not appear in … … 1383 1493 #endif 1384 1494 1385 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); 1495 if (push_ucs2_allocate(&ws, s)==(size_t)-1) { 1496 /* Wrong answer, but what can we do... */ 1497 return strchr(src, c); 1498 } 1386 1499 p = strchr_w(ws, UCS2_CHAR(c)); 1387 if (!p) 1388 return NULL; 1500 if (!p) { 1501 SAFE_FREE(ws); 1502 return NULL; 1503 } 1389 1504 *p = 0; 1390 pull_ucs2_pstring(s2, ws); 1391 return (char *)(s+strlen(s2)); 1505 if (pull_ucs2_allocate(&s2, ws)==(size_t)-1) { 1506 SAFE_FREE(ws); 1507 /* Wrong answer, but what can we do... */ 1508 return strchr(src, c); 1509 } 1510 ret = (char *)(s+strlen(s2)); 1511 SAFE_FREE(ws); 1512 SAFE_FREE(s2); 1513 return ret; 1392 1514 } 1393 1515 … … 1435 1557 /* String contained a non-ascii char. Slow path. */ 1436 1558 { 1437 wpstring ws;1438 pstring s2;1559 smb_ucs2_t *ws = NULL; 1560 char *s2 = NULL; 1439 1561 smb_ucs2_t *p; 1440 1441 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); 1562 char *ret; 1563 1564 if (push_ucs2_allocate(&ws,s)==(size_t)-1) { 1565 /* Wrong answer, but what can we do. */ 1566 return strrchr(s, c); 1567 } 1442 1568 p = strrchr_w(ws, UCS2_CHAR(c)); 1443 if (!p) 1569 if (!p) { 1570 SAFE_FREE(ws); 1444 1571 return NULL; 1572 } 1445 1573 *p = 0; 1446 pull_ucs2_pstring(s2, ws); 1447 return (char *)(s+strlen(s2)); 1574 if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { 1575 SAFE_FREE(ws); 1576 /* Wrong answer, but what can we do. */ 1577 return strrchr(s, c); 1578 } 1579 ret = (char *)(s+strlen(s2)); 1580 SAFE_FREE(ws); 1581 SAFE_FREE(s2); 1582 return ret; 1448 1583 } 1449 1584 } … … 1456 1591 char *strnrchr_m(const char *s, char c, unsigned int n) 1457 1592 { 1458 wpstring ws;1459 pstring s2;1593 smb_ucs2_t *ws = NULL; 1594 char *s2 = NULL; 1460 1595 smb_ucs2_t *p; 1461 1462 push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE); 1596 char *ret; 1597 1598 if (push_ucs2_allocate(&ws,s)==(size_t)-1) { 1599 /* Too hard to try and get right. */ 1600 return NULL; 1601 } 1463 1602 p = strnrchr_w(ws, UCS2_CHAR(c), n); 1464 if (!p) 1465 return NULL; 1603 if (!p) { 1604 SAFE_FREE(ws); 1605 return NULL; 1606 } 1466 1607 *p = 0; 1467 pull_ucs2_pstring(s2, ws); 1468 return (char *)(s+strlen(s2)); 1608 if (pull_ucs2_allocate(&s2,ws)==(size_t)-1) { 1609 SAFE_FREE(ws); 1610 /* Too hard to try and get right. */ 1611 return NULL; 1612 } 1613 ret = (char *)(s+strlen(s2)); 1614 SAFE_FREE(ws); 1615 SAFE_FREE(s2); 1616 return ret; 1469 1617 } 1470 1618 … … 1772 1920 int num, lsize; 1773 1921 pstring tok; 1774 1922 1775 1923 if (!string || !*string) 1776 1924 return NULL; … … 1785 1933 } 1786 1934 if (!sep) sep = LIST_SEP; 1787 1935 1788 1936 num = lsize = 0; 1789 1937 list = NULL; 1790 1938 1791 1939 str = s; 1792 while (next_token(&str, tok, sep, sizeof(tok))) { 1940 while (next_token(&str, tok, sep, sizeof(tok))) { 1793 1941 if (num == lsize) { 1794 1942 lsize += S_LIST_ABS; … … 1820 1968 list[num] = SMB_STRDUP(tok); 1821 1969 } 1822 1970 1823 1971 if (!list[num]) { 1824 1972 DEBUG(0,("str_list_make: Unable to allocate memory")); … … 2629 2777 { 2630 2778 size_t srclen = strlen(src); 2631 char *ret = SMB_MALLOC ((srclen * 2) + 1);2779 char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1); 2632 2780 char *dest = ret; 2633 2781 BOOL in_s_quote = False;
Note:
See TracChangeset
for help on using the changeset viewer.