Ignore:
Timestamp:
Sep 30, 2007, 3:42:50 AM (18 years ago)
Author:
Paul Smedley
Message:

Update trunk to 3.2.0pre1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/samba/source/lib/util_str.c

    r60 r77  
    77   Copyright (C) Martin Pool     2003
    88   Copyright (C) James Peach     2006
     9   Copyright (C) Jeremy Allison  1992-2007
    910   
    1011   This program is free software; you can redistribute it and/or modify
    1112   it under the terms of the GNU General Public License as published by
    12    the Free Software Foundation; either version 2 of the License, or
     13   the Free Software Foundation; either version 3 of the License, or
    1314   (at your option) any later version.
    1415   
     
    1920   
    2021   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/>.
    2323*/
    2424
     
    126126        return ret;     
    127127}
    128 
    129 static uint16 tmpbuf[sizeof(pstring)];
    130128
    131129void set_first_token(char *ptr)
     
    268266 Case insensitive string compararison, length limited.
    269267**/
    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);
     268int 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;
    276325}
    277326
     
    287336        if (!s1 || !s2)
    288337                return(False);
    289  
     338
    290339        return(StrCaseCmp(s1,s2)==0);
    291340}
     
    302351        if (!s1 || !s2 || !n)
    303352                return(False);
    304  
     353
    305354        return(StrnCaseCmp(s1,s2,n)==0);
    306355}
     
    357406char *strupper_static(const char *s)
    358407{
    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        }
    362417        strupper_m(str);
    363 
    364418        return str;
    365419}
     
    385439        if (case_default == CASE_UPPER)
    386440                return(!strhaslower(s));
    387        
     441
    388442        return(!strhasupper(s));
    389443}
     
    394448 NOTE: oldc and newc must be 7 bit characters
    395449**/
    396 
    397 void string_replace( pstring s, char oldc, char newc )
     450void string_replace( char *s, char oldc, char newc )
    398451{
    399452        char *p;
     
    407460                if (*p & 0x80) /* mb string - slow path. */
    408461                        break;
    409                 if (*p == oldc)
     462                if (*p == oldc) {
    410463                        *p = newc;
     464                }
    411465        }
    412466
     
    419473        p = s;
    420474#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        }
    424487}
    425488
     
    468531size_t str_charnum(const char *s)
    469532{
    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;
    473541}
    474542
     
    481549size_t str_ascii_charnum(const char *s)
    482550{
    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;
    486559}
    487560
     
    585658BOOL strhasupper(const char *s)
    586659{
    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;
    593676}
    594677
     
    599682BOOL strhaslower(const char *s)
    600683{
    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;
    607700}
    608701
     
    891984BOOL in_list(const char *s, const char *list, BOOL casesensitive)
    892985{
    893         pstring tok;
     986        char *tok;
    894987        const char *p=list;
     988        size_t bufsize = strlen(list);
     989        BOOL ret = False;
    895990
    896991        if (!list)
    897992                return(False);
    898993
    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)) {
    9001003                if (casesensitive) {
    901                         if (strcmp(tok,s) == 0)
    902                                 return(True);
     1004                        if (strcmp(tok,s) == 0) {
     1005                                ret = True;
     1006                                break;
     1007                        }
    9031008                } 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;
    9091018}
    9101019
     
    13541463char *strchr_m(const char *src, char c)
    13551464{
    1356         wpstring ws;
    1357         pstring s2;
     1465        smb_ucs2_t *ws = NULL;
     1466        char *s2 = NULL;
    13581467        smb_ucs2_t *p;
    13591468        const char *s;
     1469        char *ret;
    13601470
    13611471        /* characters below 0x3F are guaranteed to not appear in
     
    13831493#endif
    13841494
    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        }
    13861499        p = strchr_w(ws, UCS2_CHAR(c));
    1387         if (!p)
    1388                 return NULL;
     1500        if (!p) {
     1501                SAFE_FREE(ws);
     1502                return NULL;
     1503        }
    13891504        *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;
    13921514}
    13931515
     
    14351557        /* String contained a non-ascii char. Slow path. */
    14361558        {
    1437                 wpstring ws;
    1438                 pstring s2;
     1559                smb_ucs2_t *ws = NULL;
     1560                char *s2 = NULL;
    14391561                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                }
    14421568                p = strrchr_w(ws, UCS2_CHAR(c));
    1443                 if (!p)
     1569                if (!p) {
     1570                        SAFE_FREE(ws);
    14441571                        return NULL;
     1572                }
    14451573                *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;
    14481583        }
    14491584}
     
    14561591char *strnrchr_m(const char *s, char c, unsigned int n)
    14571592{
    1458         wpstring ws;
    1459         pstring s2;
     1593        smb_ucs2_t *ws = NULL;
     1594        char *s2 = NULL;
    14601595        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        }
    14631602        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        }
    14661607        *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;
    14691617}
    14701618
     
    17721920        int num, lsize;
    17731921        pstring tok;
    1774        
     1922
    17751923        if (!string || !*string)
    17761924                return NULL;
     
    17851933        }
    17861934        if (!sep) sep = LIST_SEP;
    1787        
     1935
    17881936        num = lsize = 0;
    17891937        list = NULL;
    1790        
     1938
    17911939        str = s;
    1792         while (next_token(&str, tok, sep, sizeof(tok))) {               
     1940        while (next_token(&str, tok, sep, sizeof(tok))) {
    17931941                if (num == lsize) {
    17941942                        lsize += S_LIST_ABS;
     
    18201968                        list[num] = SMB_STRDUP(tok);
    18211969                }
    1822                
     1970
    18231971                if (!list[num]) {
    18241972                        DEBUG(0,("str_list_make: Unable to allocate memory"));
     
    26292777{
    26302778        size_t srclen = strlen(src);
    2631         char *ret = SMB_MALLOC((srclen * 2) + 1);
     2779        char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1);
    26322780        char *dest = ret;
    26332781        BOOL in_s_quote = False;
Note: See TracChangeset for help on using the changeset viewer.