| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Samba utility functions
|
|---|
| 4 |
|
|---|
| 5 | Copyright (C) Andrew Tridgell 1992-2001
|
|---|
| 6 | Copyright (C) Simo Sorce 2001-2002
|
|---|
| 7 | Copyright (C) Martin Pool 2003
|
|---|
| 8 | Copyright (C) James Peach 2006
|
|---|
| 9 |
|
|---|
| 10 | This program is free software; you can redistribute it and/or modify
|
|---|
| 11 | 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 | (at your option) any later version.
|
|---|
| 14 |
|
|---|
| 15 | This program is distributed in the hope that it will be useful,
|
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 18 | GNU General Public License for more details.
|
|---|
| 19 |
|
|---|
| 20 | 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.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include "includes.h"
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * @file
|
|---|
| 29 | * @brief String utilities.
|
|---|
| 30 | **/
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Internal function to get the next token from a string, return False if none
|
|---|
| 34 | * found. Handles double-quotes. This is the work horse function called by
|
|---|
| 35 | * next_token() and next_token_no_ltrim().
|
|---|
| 36 | *
|
|---|
| 37 | * Based on a routine by [email protected].
|
|---|
| 38 | * Extensively modified by [email protected]
|
|---|
| 39 | */
|
|---|
| 40 | static BOOL next_token_internal(const char **ptr,
|
|---|
| 41 | char *buff,
|
|---|
| 42 | const char *sep,
|
|---|
| 43 | size_t bufsize,
|
|---|
| 44 | BOOL ltrim)
|
|---|
| 45 | {
|
|---|
| 46 | char *s;
|
|---|
| 47 | char *pbuf;
|
|---|
| 48 | BOOL quoted;
|
|---|
| 49 | size_t len=1;
|
|---|
| 50 |
|
|---|
| 51 | if (!ptr)
|
|---|
| 52 | return(False);
|
|---|
| 53 |
|
|---|
| 54 | s = (char *)*ptr;
|
|---|
| 55 |
|
|---|
| 56 | /* default to simple separators */
|
|---|
| 57 | if (!sep)
|
|---|
| 58 | sep = " \t\n\r";
|
|---|
| 59 |
|
|---|
| 60 | /* find the first non sep char, if left-trimming is requested */
|
|---|
| 61 | if (ltrim) {
|
|---|
| 62 | while (*s && strchr_m(sep,*s))
|
|---|
| 63 | s++;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /* nothing left? */
|
|---|
| 67 | if (! *s)
|
|---|
| 68 | return(False);
|
|---|
| 69 |
|
|---|
| 70 | /* copy over the token */
|
|---|
| 71 | pbuf = buff;
|
|---|
| 72 | for (quoted = False; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
|
|---|
| 73 | if ( *s == '\"' ) {
|
|---|
| 74 | quoted = !quoted;
|
|---|
| 75 | } else {
|
|---|
| 76 | len++;
|
|---|
| 77 | *pbuf++ = *s;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | *ptr = (*s) ? s+1 : s;
|
|---|
| 82 | *pbuf = 0;
|
|---|
| 83 |
|
|---|
| 84 | return(True);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | * Get the next token from a string, return False if none found. Handles
|
|---|
| 89 | * double-quotes. This version trims leading separator characters before
|
|---|
| 90 | * looking for a token.
|
|---|
| 91 | */
|
|---|
| 92 | BOOL next_token(const char **ptr, char *buff, const char *sep, size_t bufsize)
|
|---|
| 93 | {
|
|---|
| 94 | return next_token_internal(ptr, buff, sep, bufsize, True);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * Get the next token from a string, return False if none found. Handles
|
|---|
| 99 | * double-quotes. This version does not trim leading separator characters
|
|---|
| 100 | * before looking for a token.
|
|---|
| 101 | */
|
|---|
| 102 | BOOL next_token_no_ltrim(const char **ptr,
|
|---|
| 103 | char *buff,
|
|---|
| 104 | const char *sep,
|
|---|
| 105 | size_t bufsize)
|
|---|
| 106 | {
|
|---|
| 107 | return next_token_internal(ptr, buff, sep, bufsize, False);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | This is like next_token but is not re-entrant and "remembers" the first
|
|---|
| 112 | parameter so you can pass NULL. This is useful for user interface code
|
|---|
| 113 | but beware the fact that it is not re-entrant!
|
|---|
| 114 | **/
|
|---|
| 115 |
|
|---|
| 116 | static const char *last_ptr=NULL;
|
|---|
| 117 |
|
|---|
| 118 | BOOL next_token_nr(const char **ptr,char *buff, const char *sep, size_t bufsize)
|
|---|
| 119 | {
|
|---|
| 120 | BOOL ret;
|
|---|
| 121 | if (!ptr)
|
|---|
| 122 | ptr = &last_ptr;
|
|---|
| 123 |
|
|---|
| 124 | ret = next_token(ptr, buff, sep, bufsize);
|
|---|
| 125 | last_ptr = *ptr;
|
|---|
| 126 | return ret;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static uint16 tmpbuf[sizeof(pstring)];
|
|---|
| 130 |
|
|---|
| 131 | void set_first_token(char *ptr)
|
|---|
| 132 | {
|
|---|
| 133 | last_ptr = ptr;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | Convert list of tokens to array; dependent on above routine.
|
|---|
| 138 | Uses last_ptr from above - bit of a hack.
|
|---|
| 139 | **/
|
|---|
| 140 |
|
|---|
| 141 | char **toktocliplist(int *ctok, const char *sep)
|
|---|
| 142 | {
|
|---|
| 143 | char *s=(char *)last_ptr;
|
|---|
| 144 | int ictok=0;
|
|---|
| 145 | char **ret, **iret;
|
|---|
| 146 |
|
|---|
| 147 | if (!sep)
|
|---|
| 148 | sep = " \t\n\r";
|
|---|
| 149 |
|
|---|
| 150 | while(*s && strchr_m(sep,*s))
|
|---|
| 151 | s++;
|
|---|
| 152 |
|
|---|
| 153 | /* nothing left? */
|
|---|
| 154 | if (!*s)
|
|---|
| 155 | return(NULL);
|
|---|
| 156 |
|
|---|
| 157 | do {
|
|---|
| 158 | ictok++;
|
|---|
| 159 | while(*s && (!strchr_m(sep,*s)))
|
|---|
| 160 | s++;
|
|---|
| 161 | while(*s && strchr_m(sep,*s))
|
|---|
| 162 | *s++=0;
|
|---|
| 163 | } while(*s);
|
|---|
| 164 |
|
|---|
| 165 | *ctok=ictok;
|
|---|
| 166 | s=(char *)last_ptr;
|
|---|
| 167 |
|
|---|
| 168 | if (!(ret=iret=SMB_MALLOC_ARRAY(char *,ictok+1)))
|
|---|
| 169 | return NULL;
|
|---|
| 170 |
|
|---|
| 171 | while(ictok--) {
|
|---|
| 172 | *iret++=s;
|
|---|
| 173 | if (ictok > 0) {
|
|---|
| 174 | while(*s++)
|
|---|
| 175 | ;
|
|---|
| 176 | while(!*s)
|
|---|
| 177 | s++;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | ret[*ctok] = NULL;
|
|---|
| 182 | return ret;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * Case insensitive string compararison.
|
|---|
| 187 | *
|
|---|
| 188 | * iconv does not directly give us a way to compare strings in
|
|---|
| 189 | * arbitrary unix character sets -- all we can is convert and then
|
|---|
| 190 | * compare. This is expensive.
|
|---|
| 191 | *
|
|---|
| 192 | * As an optimization, we do a first pass that considers only the
|
|---|
| 193 | * prefix of the strings that is entirely 7-bit. Within this, we
|
|---|
| 194 | * check whether they have the same value.
|
|---|
| 195 | *
|
|---|
| 196 | * Hopefully this will often give the answer without needing to copy.
|
|---|
| 197 | * In particular it should speed comparisons to literal ascii strings
|
|---|
| 198 | * or comparisons of strings that are "obviously" different.
|
|---|
| 199 | *
|
|---|
| 200 | * If we find a non-ascii character we fall back to converting via
|
|---|
| 201 | * iconv.
|
|---|
| 202 | *
|
|---|
| 203 | * This should never be slower than convering the whole thing, and
|
|---|
| 204 | * often faster.
|
|---|
| 205 | *
|
|---|
| 206 | * A different optimization would be to compare for bitwise equality
|
|---|
| 207 | * in the binary encoding. (It would be possible thought hairy to do
|
|---|
| 208 | * both simultaneously.) But in that case if they turn out to be
|
|---|
| 209 | * different, we'd need to restart the whole thing.
|
|---|
| 210 | *
|
|---|
| 211 | * Even better is to implement strcasecmp for each encoding and use a
|
|---|
| 212 | * function pointer.
|
|---|
| 213 | **/
|
|---|
| 214 | int StrCaseCmp(const char *s, const char *t)
|
|---|
| 215 | {
|
|---|
| 216 |
|
|---|
| 217 | const char *ps, *pt;
|
|---|
| 218 | size_t size;
|
|---|
| 219 | smb_ucs2_t *buffer_s, *buffer_t;
|
|---|
| 220 | int ret;
|
|---|
| 221 |
|
|---|
| 222 | for (ps = s, pt = t; ; ps++, pt++) {
|
|---|
| 223 | char us, ut;
|
|---|
| 224 |
|
|---|
| 225 | if (!*ps && !*pt)
|
|---|
| 226 | return 0; /* both ended */
|
|---|
| 227 | else if (!*ps)
|
|---|
| 228 | return -1; /* s is a prefix */
|
|---|
| 229 | else if (!*pt)
|
|---|
| 230 | return +1; /* t is a prefix */
|
|---|
| 231 | else if ((*ps & 0x80) || (*pt & 0x80))
|
|---|
| 232 | /* not ascii anymore, do it the hard way from here on in */
|
|---|
| 233 | break;
|
|---|
| 234 |
|
|---|
| 235 | us = toupper_ascii(*ps);
|
|---|
| 236 | ut = toupper_ascii(*pt);
|
|---|
| 237 | if (us == ut)
|
|---|
| 238 | continue;
|
|---|
| 239 | else if (us < ut)
|
|---|
| 240 | return -1;
|
|---|
| 241 | else if (us > ut)
|
|---|
| 242 | return +1;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | size = push_ucs2_allocate(&buffer_s, ps);
|
|---|
| 246 | if (size == (size_t)-1) {
|
|---|
| 247 | return strcmp(ps, pt);
|
|---|
| 248 | /* Not quite the right answer, but finding the right one
|
|---|
| 249 | under this failure case is expensive, and it's pretty close */
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | size = push_ucs2_allocate(&buffer_t, pt);
|
|---|
| 253 | if (size == (size_t)-1) {
|
|---|
| 254 | SAFE_FREE(buffer_s);
|
|---|
| 255 | return strcmp(ps, pt);
|
|---|
| 256 | /* Not quite the right answer, but finding the right one
|
|---|
| 257 | under this failure case is expensive, and it's pretty close */
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | ret = strcasecmp_w(buffer_s, buffer_t);
|
|---|
| 261 | SAFE_FREE(buffer_s);
|
|---|
| 262 | SAFE_FREE(buffer_t);
|
|---|
| 263 | return ret;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | Case insensitive string compararison, length limited.
|
|---|
| 269 | **/
|
|---|
| 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);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /**
|
|---|
| 279 | * Compare 2 strings.
|
|---|
| 280 | *
|
|---|
| 281 | * @note The comparison is case-insensitive.
|
|---|
| 282 | **/
|
|---|
| 283 | BOOL strequal(const char *s1, const char *s2)
|
|---|
| 284 | {
|
|---|
| 285 | if (s1 == s2)
|
|---|
| 286 | return(True);
|
|---|
| 287 | if (!s1 || !s2)
|
|---|
| 288 | return(False);
|
|---|
| 289 |
|
|---|
| 290 | return(StrCaseCmp(s1,s2)==0);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * Compare 2 strings up to and including the nth char.
|
|---|
| 295 | *
|
|---|
| 296 | * @note The comparison is case-insensitive.
|
|---|
| 297 | **/
|
|---|
| 298 | BOOL strnequal(const char *s1,const char *s2,size_t n)
|
|---|
| 299 | {
|
|---|
| 300 | if (s1 == s2)
|
|---|
| 301 | return(True);
|
|---|
| 302 | if (!s1 || !s2 || !n)
|
|---|
| 303 | return(False);
|
|---|
| 304 |
|
|---|
| 305 | return(StrnCaseCmp(s1,s2,n)==0);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | Compare 2 strings (case sensitive).
|
|---|
| 310 | **/
|
|---|
| 311 |
|
|---|
| 312 | BOOL strcsequal(const char *s1,const char *s2)
|
|---|
| 313 | {
|
|---|
| 314 | if (s1 == s2)
|
|---|
| 315 | return(True);
|
|---|
| 316 | if (!s1 || !s2)
|
|---|
| 317 | return(False);
|
|---|
| 318 |
|
|---|
| 319 | return(strcmp(s1,s2)==0);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /**
|
|---|
| 323 | Do a case-insensitive, whitespace-ignoring string compare.
|
|---|
| 324 | **/
|
|---|
| 325 |
|
|---|
| 326 | int strwicmp(const char *psz1, const char *psz2)
|
|---|
| 327 | {
|
|---|
| 328 | /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
|
|---|
| 329 | /* appropriate value. */
|
|---|
| 330 | if (psz1 == psz2)
|
|---|
| 331 | return (0);
|
|---|
| 332 | else if (psz1 == NULL)
|
|---|
| 333 | return (-1);
|
|---|
| 334 | else if (psz2 == NULL)
|
|---|
| 335 | return (1);
|
|---|
| 336 |
|
|---|
| 337 | /* sync the strings on first non-whitespace */
|
|---|
| 338 | while (1) {
|
|---|
| 339 | while (isspace((int)*psz1))
|
|---|
| 340 | psz1++;
|
|---|
| 341 | while (isspace((int)*psz2))
|
|---|
| 342 | psz2++;
|
|---|
| 343 | if (toupper_ascii(*psz1) != toupper_ascii(*psz2) || *psz1 == '\0'
|
|---|
| 344 | || *psz2 == '\0')
|
|---|
| 345 | break;
|
|---|
| 346 | psz1++;
|
|---|
| 347 | psz2++;
|
|---|
| 348 | }
|
|---|
| 349 | return (*psz1 - *psz2);
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 | /**
|
|---|
| 354 | Convert a string to upper case, but don't modify it.
|
|---|
| 355 | **/
|
|---|
| 356 |
|
|---|
| 357 | char *strupper_static(const char *s)
|
|---|
| 358 | {
|
|---|
| 359 | static pstring str;
|
|---|
| 360 |
|
|---|
| 361 | pstrcpy(str, s);
|
|---|
| 362 | strupper_m(str);
|
|---|
| 363 |
|
|---|
| 364 | return str;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /**
|
|---|
| 368 | Convert a string to "normal" form.
|
|---|
| 369 | **/
|
|---|
| 370 |
|
|---|
| 371 | void strnorm(char *s, int case_default)
|
|---|
| 372 | {
|
|---|
| 373 | if (case_default == CASE_UPPER)
|
|---|
| 374 | strupper_m(s);
|
|---|
| 375 | else
|
|---|
| 376 | strlower_m(s);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | /**
|
|---|
| 380 | Check if a string is in "normal" case.
|
|---|
| 381 | **/
|
|---|
| 382 |
|
|---|
| 383 | BOOL strisnormal(const char *s, int case_default)
|
|---|
| 384 | {
|
|---|
| 385 | if (case_default == CASE_UPPER)
|
|---|
| 386 | return(!strhaslower(s));
|
|---|
| 387 |
|
|---|
| 388 | return(!strhasupper(s));
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 | /**
|
|---|
| 393 | String replace.
|
|---|
| 394 | NOTE: oldc and newc must be 7 bit characters
|
|---|
| 395 | **/
|
|---|
| 396 |
|
|---|
| 397 | /**
|
|---|
| 398 | String replace.
|
|---|
| 399 | NOTE: oldc and newc must be 7 bit characters
|
|---|
| 400 | **/
|
|---|
| 401 | void string_replace( char *s, char oldc, char newc )
|
|---|
| 402 | {
|
|---|
| 403 | char *p;
|
|---|
| 404 |
|
|---|
| 405 | /* this is quite a common operation, so we want it to be
|
|---|
| 406 | fast. We optimise for the ascii case, knowing that all our
|
|---|
| 407 | supported multi-byte character sets are ascii-compatible
|
|---|
| 408 | (ie. they match for the first 128 chars) */
|
|---|
| 409 |
|
|---|
| 410 | for (p = s; *p; p++) {
|
|---|
| 411 | if (*p & 0x80) /* mb string - slow path. */
|
|---|
| 412 | break;
|
|---|
| 413 | if (*p == oldc) {
|
|---|
| 414 | *p = newc;
|
|---|
| 415 | }
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | if (!*p)
|
|---|
| 419 | return;
|
|---|
| 420 |
|
|---|
| 421 | /* Slow (mb) path. */
|
|---|
| 422 | #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
|
|---|
| 423 | /* With compose characters we must restart from the beginning. JRA. */
|
|---|
| 424 | p = s;
|
|---|
| 425 | #endif
|
|---|
| 426 |
|
|---|
| 427 | while (*p) {
|
|---|
| 428 | size_t c_size;
|
|---|
| 429 | next_codepoint(p, &c_size);
|
|---|
| 430 |
|
|---|
| 431 | if (c_size == 1) {
|
|---|
| 432 | if (*p == oldc) {
|
|---|
| 433 | *p = newc;
|
|---|
| 434 | }
|
|---|
| 435 | }
|
|---|
| 436 | p += c_size;
|
|---|
| 437 | }
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /**
|
|---|
| 441 | * Skip past some strings in a buffer - old version - no checks.
|
|---|
| 442 | * **/
|
|---|
| 443 |
|
|---|
| 444 | char *push_skip_string(char *buf)
|
|---|
| 445 | {
|
|---|
| 446 | buf += strlen(buf) + 1;
|
|---|
| 447 | return(buf);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | /**
|
|---|
| 451 | Skip past a string in a buffer. Buffer may not be
|
|---|
| 452 | null terminated. end_ptr points to the first byte after
|
|---|
| 453 | then end of the buffer.
|
|---|
| 454 | **/
|
|---|
| 455 |
|
|---|
| 456 | char *skip_string(const char *base, size_t len, char *buf)
|
|---|
| 457 | {
|
|---|
| 458 | const char *end_ptr = base + len;
|
|---|
| 459 |
|
|---|
| 460 | if (end_ptr < base || !base || !buf || buf >= end_ptr) {
|
|---|
| 461 | return NULL;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /* Skip the string */
|
|---|
| 465 | while (*buf) {
|
|---|
| 466 | buf++;
|
|---|
| 467 | if (buf >= end_ptr) {
|
|---|
| 468 | return NULL;
|
|---|
| 469 | }
|
|---|
| 470 | }
|
|---|
| 471 | /* Skip the '\0' */
|
|---|
| 472 | buf++;
|
|---|
| 473 | return buf;
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | /**
|
|---|
| 477 | Count the number of characters in a string. Normally this will
|
|---|
| 478 | be the same as the number of bytes in a string for single byte strings,
|
|---|
| 479 | but will be different for multibyte.
|
|---|
| 480 | **/
|
|---|
| 481 |
|
|---|
| 482 | size_t str_charnum(const char *s)
|
|---|
| 483 | {
|
|---|
| 484 | uint16 tmpbuf2[sizeof(pstring)];
|
|---|
| 485 | push_ucs2(NULL, tmpbuf2,s, sizeof(tmpbuf2), STR_TERMINATE);
|
|---|
| 486 | return strlen_w(tmpbuf2);
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /**
|
|---|
| 490 | Count the number of characters in a string. Normally this will
|
|---|
| 491 | be the same as the number of bytes in a string for single byte strings,
|
|---|
| 492 | but will be different for multibyte.
|
|---|
| 493 | **/
|
|---|
| 494 |
|
|---|
| 495 | size_t str_ascii_charnum(const char *s)
|
|---|
| 496 | {
|
|---|
| 497 | pstring tmpbuf2;
|
|---|
| 498 | push_ascii(tmpbuf2, s, sizeof(tmpbuf2), STR_TERMINATE);
|
|---|
| 499 | return strlen(tmpbuf2);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | BOOL trim_char(char *s,char cfront,char cback)
|
|---|
| 503 | {
|
|---|
| 504 | BOOL ret = False;
|
|---|
| 505 | char *ep;
|
|---|
| 506 | char *fp = s;
|
|---|
| 507 |
|
|---|
| 508 | /* Ignore null or empty strings. */
|
|---|
| 509 | if (!s || (s[0] == '\0'))
|
|---|
| 510 | return False;
|
|---|
| 511 |
|
|---|
| 512 | if (cfront) {
|
|---|
| 513 | while (*fp && *fp == cfront)
|
|---|
| 514 | fp++;
|
|---|
| 515 | if (!*fp) {
|
|---|
| 516 | /* We ate the string. */
|
|---|
| 517 | s[0] = '\0';
|
|---|
| 518 | return True;
|
|---|
| 519 | }
|
|---|
| 520 | if (fp != s)
|
|---|
| 521 | ret = True;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | ep = fp + strlen(fp) - 1;
|
|---|
| 525 | if (cback) {
|
|---|
| 526 | /* Attempt ascii only. Bail for mb strings. */
|
|---|
| 527 | while ((ep >= fp) && (*ep == cback)) {
|
|---|
| 528 | ret = True;
|
|---|
| 529 | if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
|
|---|
| 530 | /* Could be mb... bail back to tim_string. */
|
|---|
| 531 | char fs[2], bs[2];
|
|---|
| 532 | if (cfront) {
|
|---|
| 533 | fs[0] = cfront;
|
|---|
| 534 | fs[1] = '\0';
|
|---|
| 535 | }
|
|---|
| 536 | bs[0] = cback;
|
|---|
| 537 | bs[1] = '\0';
|
|---|
| 538 | return trim_string(s, cfront ? fs : NULL, bs);
|
|---|
| 539 | } else {
|
|---|
| 540 | ep--;
|
|---|
| 541 | }
|
|---|
| 542 | }
|
|---|
| 543 | if (ep < fp) {
|
|---|
| 544 | /* We ate the string. */
|
|---|
| 545 | s[0] = '\0';
|
|---|
| 546 | return True;
|
|---|
| 547 | }
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | ep[1] = '\0';
|
|---|
| 551 | memmove(s, fp, ep-fp+2);
|
|---|
| 552 | return ret;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | /**
|
|---|
| 556 | Trim the specified elements off the front and back of a string.
|
|---|
| 557 | **/
|
|---|
| 558 |
|
|---|
| 559 | BOOL trim_string(char *s,const char *front,const char *back)
|
|---|
| 560 | {
|
|---|
| 561 | BOOL ret = False;
|
|---|
| 562 | size_t front_len;
|
|---|
| 563 | size_t back_len;
|
|---|
| 564 | size_t len;
|
|---|
| 565 |
|
|---|
| 566 | /* Ignore null or empty strings. */
|
|---|
| 567 | if (!s || (s[0] == '\0'))
|
|---|
| 568 | return False;
|
|---|
| 569 |
|
|---|
| 570 | front_len = front? strlen(front) : 0;
|
|---|
| 571 | back_len = back? strlen(back) : 0;
|
|---|
| 572 |
|
|---|
| 573 | len = strlen(s);
|
|---|
| 574 |
|
|---|
| 575 | if (front_len) {
|
|---|
| 576 | while (len && strncmp(s, front, front_len)==0) {
|
|---|
| 577 | /* Must use memmove here as src & dest can
|
|---|
| 578 | * easily overlap. Found by valgrind. JRA. */
|
|---|
| 579 | memmove(s, s+front_len, (len-front_len)+1);
|
|---|
| 580 | len -= front_len;
|
|---|
| 581 | ret=True;
|
|---|
| 582 | }
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | if (back_len) {
|
|---|
| 586 | while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
|
|---|
| 587 | s[len-back_len]='\0';
|
|---|
| 588 | len -= back_len;
|
|---|
| 589 | ret=True;
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 | return ret;
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | /**
|
|---|
| 596 | Does a string have any uppercase chars in it?
|
|---|
| 597 | **/
|
|---|
| 598 |
|
|---|
| 599 | BOOL strhasupper(const char *s)
|
|---|
| 600 | {
|
|---|
| 601 | smb_ucs2_t *ptr;
|
|---|
| 602 | push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
|
|---|
| 603 | for(ptr=tmpbuf;*ptr;ptr++)
|
|---|
| 604 | if(isupper_w(*ptr))
|
|---|
| 605 | return True;
|
|---|
| 606 | return(False);
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | /**
|
|---|
| 610 | Does a string have any lowercase chars in it?
|
|---|
| 611 | **/
|
|---|
| 612 |
|
|---|
| 613 | BOOL strhaslower(const char *s)
|
|---|
| 614 | {
|
|---|
| 615 | smb_ucs2_t *ptr;
|
|---|
| 616 | push_ucs2(NULL, tmpbuf,s, sizeof(tmpbuf), STR_TERMINATE);
|
|---|
| 617 | for(ptr=tmpbuf;*ptr;ptr++)
|
|---|
| 618 | if(islower_w(*ptr))
|
|---|
| 619 | return True;
|
|---|
| 620 | return(False);
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | /**
|
|---|
| 624 | Find the number of 'c' chars in a string
|
|---|
| 625 | **/
|
|---|
| 626 |
|
|---|
| 627 | size_t count_chars(const char *s,char c)
|
|---|
| 628 | {
|
|---|
| 629 | smb_ucs2_t *ptr;
|
|---|
| 630 | int count;
|
|---|
| 631 | smb_ucs2_t *alloc_tmpbuf = NULL;
|
|---|
| 632 |
|
|---|
| 633 | if (push_ucs2_allocate(&alloc_tmpbuf, s) == (size_t)-1) {
|
|---|
| 634 | return 0;
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| 637 | for(count=0,ptr=alloc_tmpbuf;*ptr;ptr++)
|
|---|
| 638 | if(*ptr==UCS2_CHAR(c))
|
|---|
| 639 | count++;
|
|---|
| 640 |
|
|---|
| 641 | SAFE_FREE(alloc_tmpbuf);
|
|---|
| 642 | return(count);
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | /**
|
|---|
| 646 | Safe string copy into a known length string. maxlength does not
|
|---|
| 647 | include the terminating zero.
|
|---|
| 648 | **/
|
|---|
| 649 |
|
|---|
| 650 | char *safe_strcpy_fn(const char *fn, int line, char *dest,const char *src, size_t maxlength)
|
|---|
| 651 | {
|
|---|
| 652 | size_t len;
|
|---|
| 653 |
|
|---|
| 654 | if (!dest) {
|
|---|
| 655 | DEBUG(0,("ERROR: NULL dest in safe_strcpy, called from [%s][%d]\n", fn, line));
|
|---|
| 656 | return NULL;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 | #ifdef DEVELOPER
|
|---|
| 660 | clobber_region(fn,line,dest, maxlength+1);
|
|---|
| 661 | #endif
|
|---|
| 662 |
|
|---|
| 663 | if (!src) {
|
|---|
| 664 | *dest = 0;
|
|---|
| 665 | return dest;
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | len = strnlen(src, maxlength+1);
|
|---|
| 669 |
|
|---|
| 670 | if (len > maxlength) {
|
|---|
| 671 | DEBUG(0,("ERROR: string overflow by %lu (%lu - %lu) in safe_strcpy [%.50s]\n",
|
|---|
| 672 | (unsigned long)(len-maxlength), (unsigned long)len,
|
|---|
| 673 | (unsigned long)maxlength, src));
|
|---|
| 674 | len = maxlength;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | memmove(dest, src, len);
|
|---|
| 678 | dest[len] = 0;
|
|---|
| 679 | return dest;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | /**
|
|---|
| 683 | Safe string cat into a string. maxlength does not
|
|---|
| 684 | include the terminating zero.
|
|---|
| 685 | **/
|
|---|
| 686 | char *safe_strcat_fn(const char *fn, int line, char *dest, const char *src, size_t maxlength)
|
|---|
| 687 | {
|
|---|
| 688 | size_t src_len, dest_len;
|
|---|
| 689 |
|
|---|
| 690 | if (!dest) {
|
|---|
| 691 | DEBUG(0,("ERROR: NULL dest in safe_strcat, called from [%s][%d]\n", fn, line));
|
|---|
| 692 | return NULL;
|
|---|
| 693 | }
|
|---|
| 694 |
|
|---|
| 695 | if (!src)
|
|---|
| 696 | return dest;
|
|---|
| 697 |
|
|---|
| 698 | src_len = strnlen(src, maxlength + 1);
|
|---|
| 699 | dest_len = strnlen(dest, maxlength + 1);
|
|---|
| 700 |
|
|---|
| 701 | #ifdef DEVELOPER
|
|---|
| 702 | clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len);
|
|---|
| 703 | #endif
|
|---|
| 704 |
|
|---|
| 705 | if (src_len + dest_len > maxlength) {
|
|---|
| 706 | DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
|
|---|
| 707 | (int)(src_len + dest_len - maxlength), src));
|
|---|
| 708 | if (maxlength > dest_len) {
|
|---|
| 709 | memcpy(&dest[dest_len], src, maxlength - dest_len);
|
|---|
| 710 | }
|
|---|
| 711 | dest[maxlength] = 0;
|
|---|
| 712 | return NULL;
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | memcpy(&dest[dest_len], src, src_len);
|
|---|
| 716 | dest[dest_len + src_len] = 0;
|
|---|
| 717 | return dest;
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | /**
|
|---|
| 721 | Paranoid strcpy into a buffer of given length (includes terminating
|
|---|
| 722 | zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
|
|---|
| 723 | and replaces with '_'. Deliberately does *NOT* check for multibyte
|
|---|
| 724 | characters. Don't change it !
|
|---|
| 725 | **/
|
|---|
| 726 | char *alpha_strcpy_fn(const char *fn, int line, char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
|
|---|
| 727 | {
|
|---|
| 728 | size_t len, i;
|
|---|
| 729 |
|
|---|
| 730 | #ifdef DEVELOPER
|
|---|
| 731 | clobber_region(fn, line, dest, maxlength);
|
|---|
| 732 | #endif
|
|---|
| 733 |
|
|---|
| 734 | if (!dest) {
|
|---|
| 735 | DEBUG(0,("ERROR: NULL dest in alpha_strcpy, called from [%s][%d]\n", fn, line));
|
|---|
| 736 | return NULL;
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | if (!src) {
|
|---|
| 740 | *dest = 0;
|
|---|
| 741 | return dest;
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | len = strlen(src);
|
|---|
| 745 | if (len >= maxlength)
|
|---|
| 746 | len = maxlength - 1;
|
|---|
| 747 |
|
|---|
| 748 | if (!other_safe_chars)
|
|---|
| 749 | other_safe_chars = "";
|
|---|
| 750 |
|
|---|
| 751 | for(i = 0; i < len; i++) {
|
|---|
| 752 | int val = (src[i] & 0xff);
|
|---|
| 753 | if (isupper_ascii(val) || islower_ascii(val) || isdigit(val) || strchr_m(other_safe_chars, val))
|
|---|
| 754 | dest[i] = src[i];
|
|---|
| 755 | else
|
|---|
| 756 | dest[i] = '_';
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | dest[i] = '\0';
|
|---|
| 760 |
|
|---|
| 761 | return dest;
|
|---|
| 762 | }
|
|---|
| 763 |
|
|---|
| 764 | /**
|
|---|
| 765 | Like strncpy but always null terminates. Make sure there is room!
|
|---|
| 766 | The variable n should always be one less than the available size.
|
|---|
| 767 | **/
|
|---|
| 768 | char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n)
|
|---|
| 769 | {
|
|---|
| 770 | char *d = dest;
|
|---|
| 771 |
|
|---|
| 772 | #ifdef DEVELOPER
|
|---|
| 773 | clobber_region(fn, line, dest, n+1);
|
|---|
| 774 | #endif
|
|---|
| 775 |
|
|---|
| 776 | if (!dest) {
|
|---|
| 777 | DEBUG(0,("ERROR: NULL dest in StrnCpy, called from [%s][%d]\n", fn, line));
|
|---|
| 778 | return(NULL);
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | if (!src) {
|
|---|
| 782 | *dest = 0;
|
|---|
| 783 | return(dest);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | while (n-- && (*d = *src)) {
|
|---|
| 787 | d++;
|
|---|
| 788 | src++;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | *d = 0;
|
|---|
| 792 | return(dest);
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | #if 0
|
|---|
| 796 | /**
|
|---|
| 797 | Like strncpy but copies up to the character marker. always null terminates.
|
|---|
| 798 | returns a pointer to the character marker in the source string (src).
|
|---|
| 799 | **/
|
|---|
| 800 |
|
|---|
| 801 | static char *strncpyn(char *dest, const char *src, size_t n, char c)
|
|---|
| 802 | {
|
|---|
| 803 | char *p;
|
|---|
| 804 | size_t str_len;
|
|---|
| 805 |
|
|---|
| 806 | #ifdef DEVELOPER
|
|---|
| 807 | clobber_region(dest, n+1);
|
|---|
| 808 | #endif
|
|---|
| 809 | p = strchr_m(src, c);
|
|---|
| 810 | if (p == NULL) {
|
|---|
| 811 | DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
|
|---|
| 812 | return NULL;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | str_len = PTR_DIFF(p, src);
|
|---|
| 816 | strncpy(dest, src, MIN(n, str_len));
|
|---|
| 817 | dest[str_len] = '\0';
|
|---|
| 818 |
|
|---|
| 819 | return p;
|
|---|
| 820 | }
|
|---|
| 821 | #endif
|
|---|
| 822 |
|
|---|
| 823 | /**
|
|---|
| 824 | Routine to get hex characters and turn them into a 16 byte array.
|
|---|
| 825 | the array can be variable length, and any non-hex-numeric
|
|---|
| 826 | characters are skipped. "0xnn" or "0Xnn" is specially catered
|
|---|
| 827 | for.
|
|---|
| 828 |
|
|---|
| 829 | valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
|
|---|
| 830 |
|
|---|
| 831 | **/
|
|---|
| 832 |
|
|---|
| 833 | size_t strhex_to_str(char *p, size_t len, const char *strhex)
|
|---|
| 834 | {
|
|---|
| 835 | size_t i;
|
|---|
| 836 | size_t num_chars = 0;
|
|---|
| 837 | unsigned char lonybble, hinybble;
|
|---|
| 838 | const char *hexchars = "0123456789ABCDEF";
|
|---|
| 839 | char *p1 = NULL, *p2 = NULL;
|
|---|
| 840 |
|
|---|
| 841 | for (i = 0; i < len && strhex[i] != 0; i++) {
|
|---|
| 842 | if (strnequal(hexchars, "0x", 2)) {
|
|---|
| 843 | i++; /* skip two chars */
|
|---|
| 844 | continue;
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | if (!(p1 = strchr_m(hexchars, toupper_ascii(strhex[i]))))
|
|---|
| 848 | break;
|
|---|
| 849 |
|
|---|
| 850 | i++; /* next hex digit */
|
|---|
| 851 |
|
|---|
| 852 | if (!(p2 = strchr_m(hexchars, toupper_ascii(strhex[i]))))
|
|---|
| 853 | break;
|
|---|
| 854 |
|
|---|
| 855 | /* get the two nybbles */
|
|---|
| 856 | hinybble = PTR_DIFF(p1, hexchars);
|
|---|
| 857 | lonybble = PTR_DIFF(p2, hexchars);
|
|---|
| 858 |
|
|---|
| 859 | p[num_chars] = (hinybble << 4) | lonybble;
|
|---|
| 860 | num_chars++;
|
|---|
| 861 |
|
|---|
| 862 | p1 = NULL;
|
|---|
| 863 | p2 = NULL;
|
|---|
| 864 | }
|
|---|
| 865 | return num_chars;
|
|---|
| 866 | }
|
|---|
| 867 |
|
|---|
| 868 | DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex)
|
|---|
| 869 | {
|
|---|
| 870 | DATA_BLOB ret_blob;
|
|---|
| 871 |
|
|---|
| 872 | if (mem_ctx != NULL)
|
|---|
| 873 | ret_blob = data_blob_talloc(mem_ctx, NULL, strlen(strhex)/2+1);
|
|---|
| 874 | else
|
|---|
| 875 | ret_blob = data_blob(NULL, strlen(strhex)/2+1);
|
|---|
| 876 |
|
|---|
| 877 | ret_blob.length = strhex_to_str((char*)ret_blob.data,
|
|---|
| 878 | strlen(strhex),
|
|---|
| 879 | strhex);
|
|---|
| 880 |
|
|---|
| 881 | return ret_blob;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | /**
|
|---|
| 885 | * Routine to print a buffer as HEX digits, into an allocated string.
|
|---|
| 886 | */
|
|---|
| 887 |
|
|---|
| 888 | char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
|
|---|
| 889 | {
|
|---|
| 890 | int i;
|
|---|
| 891 | char *hex_buffer;
|
|---|
| 892 |
|
|---|
| 893 | hex_buffer = TALLOC_ARRAY(mem_ctx, char, (len*2)+1);
|
|---|
| 894 |
|
|---|
| 895 | for (i = 0; i < len; i++)
|
|---|
| 896 | slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
|
|---|
| 897 |
|
|---|
| 898 | return hex_buffer;
|
|---|
| 899 | }
|
|---|
| 900 |
|
|---|
| 901 | /**
|
|---|
| 902 | Check if a string is part of a list.
|
|---|
| 903 | **/
|
|---|
| 904 |
|
|---|
| 905 | BOOL in_list(const char *s, const char *list, BOOL casesensitive)
|
|---|
| 906 | {
|
|---|
| 907 | pstring tok;
|
|---|
| 908 | const char *p=list;
|
|---|
| 909 |
|
|---|
| 910 | if (!list)
|
|---|
| 911 | return(False);
|
|---|
| 912 |
|
|---|
| 913 | while (next_token(&p,tok,LIST_SEP,sizeof(tok))) {
|
|---|
| 914 | if (casesensitive) {
|
|---|
| 915 | if (strcmp(tok,s) == 0)
|
|---|
| 916 | return(True);
|
|---|
| 917 | } else {
|
|---|
| 918 | if (StrCaseCmp(tok,s) == 0)
|
|---|
| 919 | return(True);
|
|---|
| 920 | }
|
|---|
| 921 | }
|
|---|
| 922 | return(False);
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | /* this is used to prevent lots of mallocs of size 1 */
|
|---|
| 926 | static const char *null_string = "";
|
|---|
| 927 |
|
|---|
| 928 | /**
|
|---|
| 929 | Set a string value, allocing the space for the string
|
|---|
| 930 | **/
|
|---|
| 931 |
|
|---|
| 932 | static BOOL string_init(char **dest,const char *src)
|
|---|
| 933 | {
|
|---|
| 934 | size_t l;
|
|---|
| 935 |
|
|---|
| 936 | if (!src)
|
|---|
| 937 | src = "";
|
|---|
| 938 |
|
|---|
| 939 | l = strlen(src);
|
|---|
| 940 |
|
|---|
| 941 | if (l == 0) {
|
|---|
| 942 | *dest = CONST_DISCARD(char*, null_string);
|
|---|
| 943 | } else {
|
|---|
| 944 | (*dest) = SMB_STRDUP(src);
|
|---|
| 945 | if ((*dest) == NULL) {
|
|---|
| 946 | DEBUG(0,("Out of memory in string_init\n"));
|
|---|
| 947 | return False;
|
|---|
| 948 | }
|
|---|
| 949 | }
|
|---|
| 950 | return(True);
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | /**
|
|---|
| 954 | Free a string value.
|
|---|
| 955 | **/
|
|---|
| 956 |
|
|---|
| 957 | void string_free(char **s)
|
|---|
| 958 | {
|
|---|
| 959 | if (!s || !(*s))
|
|---|
| 960 | return;
|
|---|
| 961 | if (*s == null_string)
|
|---|
| 962 | *s = NULL;
|
|---|
| 963 | SAFE_FREE(*s);
|
|---|
| 964 | }
|
|---|
| 965 |
|
|---|
| 966 | /**
|
|---|
| 967 | Set a string value, deallocating any existing space, and allocing the space
|
|---|
| 968 | for the string
|
|---|
| 969 | **/
|
|---|
| 970 |
|
|---|
| 971 | BOOL string_set(char **dest,const char *src)
|
|---|
| 972 | {
|
|---|
| 973 | string_free(dest);
|
|---|
| 974 | return(string_init(dest,src));
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | /**
|
|---|
| 978 | Substitute a string for a pattern in another string. Make sure there is
|
|---|
| 979 | enough room!
|
|---|
| 980 |
|
|---|
| 981 | This routine looks for pattern in s and replaces it with
|
|---|
| 982 | insert. It may do multiple replacements or just one.
|
|---|
| 983 |
|
|---|
| 984 | Any of " ; ' $ or ` in the insert string are replaced with _
|
|---|
| 985 | if len==0 then the string cannot be extended. This is different from the old
|
|---|
| 986 | use of len==0 which was for no length checks to be done.
|
|---|
| 987 | **/
|
|---|
| 988 |
|
|---|
| 989 | void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
|
|---|
| 990 | BOOL remove_unsafe_characters, BOOL replace_once, BOOL allow_trailing_dollar)
|
|---|
| 991 | {
|
|---|
| 992 | char *p;
|
|---|
| 993 | ssize_t ls,lp,li, i;
|
|---|
| 994 |
|
|---|
| 995 | if (!insert || !pattern || !*pattern || !s)
|
|---|
| 996 | return;
|
|---|
| 997 |
|
|---|
| 998 | ls = (ssize_t)strlen(s);
|
|---|
| 999 | lp = (ssize_t)strlen(pattern);
|
|---|
| 1000 | li = (ssize_t)strlen(insert);
|
|---|
| 1001 |
|
|---|
| 1002 | if (len == 0)
|
|---|
| 1003 | len = ls + 1; /* len is number of *bytes* */
|
|---|
| 1004 |
|
|---|
| 1005 | while (lp <= ls && (p = strstr_m(s,pattern))) {
|
|---|
| 1006 | if (ls + (li-lp) >= len) {
|
|---|
| 1007 | DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
|
|---|
| 1008 | (int)(ls + (li-lp) - len),
|
|---|
| 1009 | pattern, (int)len));
|
|---|
| 1010 | break;
|
|---|
| 1011 | }
|
|---|
| 1012 | if (li != lp) {
|
|---|
| 1013 | memmove(p+li,p+lp,strlen(p+lp)+1);
|
|---|
| 1014 | }
|
|---|
| 1015 | for (i=0;i<li;i++) {
|
|---|
| 1016 | switch (insert[i]) {
|
|---|
| 1017 | case '`':
|
|---|
| 1018 | case '"':
|
|---|
| 1019 | case '\'':
|
|---|
| 1020 | case ';':
|
|---|
| 1021 | case '$':
|
|---|
| 1022 | /* allow a trailing $ (as in machine accounts) */
|
|---|
| 1023 | if (allow_trailing_dollar && (i == li - 1 )) {
|
|---|
| 1024 | p[i] = insert[i];
|
|---|
| 1025 | break;
|
|---|
| 1026 | }
|
|---|
| 1027 | case '%':
|
|---|
| 1028 | case '\r':
|
|---|
| 1029 | case '\n':
|
|---|
| 1030 | if ( remove_unsafe_characters ) {
|
|---|
| 1031 | p[i] = '_';
|
|---|
| 1032 | /* yes this break should be here since we want to
|
|---|
| 1033 | fall throw if not replacing unsafe chars */
|
|---|
| 1034 | break;
|
|---|
| 1035 | }
|
|---|
| 1036 | default:
|
|---|
| 1037 | p[i] = insert[i];
|
|---|
| 1038 | }
|
|---|
| 1039 | }
|
|---|
| 1040 | s = p + li;
|
|---|
| 1041 | ls += (li-lp);
|
|---|
| 1042 |
|
|---|
| 1043 | if (replace_once)
|
|---|
| 1044 | break;
|
|---|
| 1045 | }
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | void string_sub_once(char *s, const char *pattern, const char *insert, size_t len)
|
|---|
| 1049 | {
|
|---|
| 1050 | string_sub2( s, pattern, insert, len, True, True, False );
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | void string_sub(char *s,const char *pattern, const char *insert, size_t len)
|
|---|
| 1054 | {
|
|---|
| 1055 | string_sub2( s, pattern, insert, len, True, False, False );
|
|---|
| 1056 | }
|
|---|
| 1057 |
|
|---|
| 1058 | void fstring_sub(char *s,const char *pattern,const char *insert)
|
|---|
| 1059 | {
|
|---|
| 1060 | string_sub(s, pattern, insert, sizeof(fstring));
|
|---|
| 1061 | }
|
|---|
| 1062 |
|
|---|
| 1063 | void pstring_sub(char *s,const char *pattern,const char *insert)
|
|---|
| 1064 | {
|
|---|
| 1065 | string_sub(s, pattern, insert, sizeof(pstring));
|
|---|
| 1066 | }
|
|---|
| 1067 |
|
|---|
| 1068 | /**
|
|---|
| 1069 | Similar to string_sub, but it will accept only allocated strings
|
|---|
| 1070 | and may realloc them so pay attention at what you pass on no
|
|---|
| 1071 | pointers inside strings, no pstrings or const may be passed
|
|---|
| 1072 | as string.
|
|---|
| 1073 | **/
|
|---|
| 1074 |
|
|---|
| 1075 | char *realloc_string_sub(char *string, const char *pattern,
|
|---|
| 1076 | const char *insert)
|
|---|
| 1077 | {
|
|---|
| 1078 | char *p, *in;
|
|---|
| 1079 | char *s;
|
|---|
| 1080 | ssize_t ls,lp,li,ld, i;
|
|---|
| 1081 |
|
|---|
| 1082 | if (!insert || !pattern || !*pattern || !string || !*string)
|
|---|
| 1083 | return NULL;
|
|---|
| 1084 |
|
|---|
| 1085 | s = string;
|
|---|
| 1086 |
|
|---|
| 1087 | in = SMB_STRDUP(insert);
|
|---|
| 1088 | if (!in) {
|
|---|
| 1089 | DEBUG(0, ("realloc_string_sub: out of memory!\n"));
|
|---|
| 1090 | return NULL;
|
|---|
| 1091 | }
|
|---|
| 1092 | ls = (ssize_t)strlen(s);
|
|---|
| 1093 | lp = (ssize_t)strlen(pattern);
|
|---|
| 1094 | li = (ssize_t)strlen(insert);
|
|---|
| 1095 | ld = li - lp;
|
|---|
| 1096 | for (i=0;i<li;i++) {
|
|---|
| 1097 | switch (in[i]) {
|
|---|
| 1098 | case '`':
|
|---|
| 1099 | case '"':
|
|---|
| 1100 | case '\'':
|
|---|
| 1101 | case ';':
|
|---|
| 1102 | case '$':
|
|---|
| 1103 | case '%':
|
|---|
| 1104 | case '\r':
|
|---|
| 1105 | case '\n':
|
|---|
| 1106 | in[i] = '_';
|
|---|
| 1107 | default:
|
|---|
| 1108 | /* ok */
|
|---|
| 1109 | break;
|
|---|
| 1110 | }
|
|---|
| 1111 | }
|
|---|
| 1112 |
|
|---|
| 1113 | while ((p = strstr_m(s,pattern))) {
|
|---|
| 1114 | if (ld > 0) {
|
|---|
| 1115 | int offset = PTR_DIFF(s,string);
|
|---|
| 1116 | string = (char *)SMB_REALLOC(string, ls + ld + 1);
|
|---|
| 1117 | if (!string) {
|
|---|
| 1118 | DEBUG(0, ("realloc_string_sub: out of memory!\n"));
|
|---|
| 1119 | SAFE_FREE(in);
|
|---|
| 1120 | return NULL;
|
|---|
| 1121 | }
|
|---|
| 1122 | p = string + offset + (p - s);
|
|---|
| 1123 | }
|
|---|
| 1124 | if (li != lp) {
|
|---|
| 1125 | memmove(p+li,p+lp,strlen(p+lp)+1);
|
|---|
| 1126 | }
|
|---|
| 1127 | memcpy(p, in, li);
|
|---|
| 1128 | s = p + li;
|
|---|
| 1129 | ls += ld;
|
|---|
| 1130 | }
|
|---|
| 1131 | SAFE_FREE(in);
|
|---|
| 1132 | return string;
|
|---|
| 1133 | }
|
|---|
| 1134 |
|
|---|
| 1135 | /* Same as string_sub, but returns a talloc'ed string */
|
|---|
| 1136 |
|
|---|
| 1137 | char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
|
|---|
| 1138 | const char *pattern, const char *insert)
|
|---|
| 1139 | {
|
|---|
| 1140 | char *p, *in;
|
|---|
| 1141 | char *s;
|
|---|
| 1142 | char *string;
|
|---|
| 1143 | ssize_t ls,lp,li,ld, i;
|
|---|
| 1144 |
|
|---|
| 1145 | if (!insert || !pattern || !*pattern || !src || !*src)
|
|---|
| 1146 | return NULL;
|
|---|
| 1147 |
|
|---|
| 1148 | string = talloc_strdup(mem_ctx, src);
|
|---|
| 1149 | if (string == NULL) {
|
|---|
| 1150 | DEBUG(0, ("talloc_strdup failed\n"));
|
|---|
| 1151 | return NULL;
|
|---|
| 1152 | }
|
|---|
| 1153 |
|
|---|
| 1154 | s = string;
|
|---|
| 1155 |
|
|---|
| 1156 | in = SMB_STRDUP(insert);
|
|---|
| 1157 | if (!in) {
|
|---|
| 1158 | DEBUG(0, ("talloc_string_sub: out of memory!\n"));
|
|---|
| 1159 | return NULL;
|
|---|
| 1160 | }
|
|---|
| 1161 | ls = (ssize_t)strlen(s);
|
|---|
| 1162 | lp = (ssize_t)strlen(pattern);
|
|---|
| 1163 | li = (ssize_t)strlen(insert);
|
|---|
| 1164 | ld = li - lp;
|
|---|
| 1165 | for (i=0;i<li;i++) {
|
|---|
| 1166 | switch (in[i]) {
|
|---|
| 1167 | case '`':
|
|---|
| 1168 | case '"':
|
|---|
| 1169 | case '\'':
|
|---|
| 1170 | case ';':
|
|---|
| 1171 | case '$':
|
|---|
| 1172 | case '%':
|
|---|
| 1173 | case '\r':
|
|---|
| 1174 | case '\n':
|
|---|
| 1175 | in[i] = '_';
|
|---|
| 1176 | default:
|
|---|
| 1177 | /* ok */
|
|---|
| 1178 | break;
|
|---|
| 1179 | }
|
|---|
| 1180 | }
|
|---|
| 1181 |
|
|---|
| 1182 | while ((p = strstr_m(s,pattern))) {
|
|---|
| 1183 | if (ld > 0) {
|
|---|
| 1184 | int offset = PTR_DIFF(s,string);
|
|---|
| 1185 | string = (char *)TALLOC_REALLOC(mem_ctx, string,
|
|---|
| 1186 | ls + ld + 1);
|
|---|
| 1187 | if (!string) {
|
|---|
| 1188 | DEBUG(0, ("talloc_string_sub: out of "
|
|---|
| 1189 | "memory!\n"));
|
|---|
| 1190 | SAFE_FREE(in);
|
|---|
| 1191 | return NULL;
|
|---|
| 1192 | }
|
|---|
| 1193 | p = string + offset + (p - s);
|
|---|
| 1194 | }
|
|---|
| 1195 | if (li != lp) {
|
|---|
| 1196 | memmove(p+li,p+lp,strlen(p+lp)+1);
|
|---|
| 1197 | }
|
|---|
| 1198 | memcpy(p, in, li);
|
|---|
| 1199 | s = p + li;
|
|---|
| 1200 | ls += ld;
|
|---|
| 1201 | }
|
|---|
| 1202 | SAFE_FREE(in);
|
|---|
| 1203 | return string;
|
|---|
| 1204 | }
|
|---|
| 1205 |
|
|---|
| 1206 | /**
|
|---|
| 1207 | Similar to string_sub() but allows for any character to be substituted.
|
|---|
| 1208 | Use with caution!
|
|---|
| 1209 | if len==0 then the string cannot be extended. This is different from the old
|
|---|
| 1210 | use of len==0 which was for no length checks to be done.
|
|---|
| 1211 | **/
|
|---|
| 1212 |
|
|---|
| 1213 | void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
|
|---|
| 1214 | {
|
|---|
| 1215 | char *p;
|
|---|
| 1216 | ssize_t ls,lp,li;
|
|---|
| 1217 |
|
|---|
| 1218 | if (!insert || !pattern || !s)
|
|---|
| 1219 | return;
|
|---|
| 1220 |
|
|---|
| 1221 | ls = (ssize_t)strlen(s);
|
|---|
| 1222 | lp = (ssize_t)strlen(pattern);
|
|---|
| 1223 | li = (ssize_t)strlen(insert);
|
|---|
| 1224 |
|
|---|
| 1225 | if (!*pattern)
|
|---|
| 1226 | return;
|
|---|
| 1227 |
|
|---|
| 1228 | if (len == 0)
|
|---|
| 1229 | len = ls + 1; /* len is number of *bytes* */
|
|---|
| 1230 |
|
|---|
| 1231 | while (lp <= ls && (p = strstr_m(s,pattern))) {
|
|---|
| 1232 | if (ls + (li-lp) >= len) {
|
|---|
| 1233 | DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
|
|---|
| 1234 | (int)(ls + (li-lp) - len),
|
|---|
| 1235 | pattern, (int)len));
|
|---|
| 1236 | break;
|
|---|
| 1237 | }
|
|---|
| 1238 | if (li != lp) {
|
|---|
| 1239 | memmove(p+li,p+lp,strlen(p+lp)+1);
|
|---|
| 1240 | }
|
|---|
| 1241 | memcpy(p, insert, li);
|
|---|
| 1242 | s = p + li;
|
|---|
| 1243 | ls += (li-lp);
|
|---|
| 1244 | }
|
|---|
| 1245 | }
|
|---|
| 1246 |
|
|---|
| 1247 | /**
|
|---|
| 1248 | Similar to all_string_sub but for unicode strings.
|
|---|
| 1249 | Return a new allocated unicode string.
|
|---|
| 1250 | similar to string_sub() but allows for any character to be substituted.
|
|---|
| 1251 | Use with caution!
|
|---|
| 1252 | **/
|
|---|
| 1253 |
|
|---|
| 1254 | static smb_ucs2_t *all_string_sub_w(const smb_ucs2_t *s, const smb_ucs2_t *pattern,
|
|---|
| 1255 | const smb_ucs2_t *insert)
|
|---|
| 1256 | {
|
|---|
| 1257 | smb_ucs2_t *r, *rp;
|
|---|
| 1258 | const smb_ucs2_t *sp;
|
|---|
| 1259 | size_t lr, lp, li, lt;
|
|---|
| 1260 |
|
|---|
| 1261 | if (!insert || !pattern || !*pattern || !s)
|
|---|
| 1262 | return NULL;
|
|---|
| 1263 |
|
|---|
| 1264 | lt = (size_t)strlen_w(s);
|
|---|
| 1265 | lp = (size_t)strlen_w(pattern);
|
|---|
| 1266 | li = (size_t)strlen_w(insert);
|
|---|
| 1267 |
|
|---|
| 1268 | if (li > lp) {
|
|---|
| 1269 | const smb_ucs2_t *st = s;
|
|---|
| 1270 | int ld = li - lp;
|
|---|
| 1271 | while ((sp = strstr_w(st, pattern))) {
|
|---|
| 1272 | st = sp + lp;
|
|---|
| 1273 | lt += ld;
|
|---|
| 1274 | }
|
|---|
| 1275 | }
|
|---|
| 1276 |
|
|---|
| 1277 | r = rp = SMB_MALLOC_ARRAY(smb_ucs2_t, lt + 1);
|
|---|
| 1278 | if (!r) {
|
|---|
| 1279 | DEBUG(0, ("all_string_sub_w: out of memory!\n"));
|
|---|
| 1280 | return NULL;
|
|---|
| 1281 | }
|
|---|
| 1282 |
|
|---|
| 1283 | while ((sp = strstr_w(s, pattern))) {
|
|---|
| 1284 | memcpy(rp, s, (sp - s));
|
|---|
| 1285 | rp += ((sp - s) / sizeof(smb_ucs2_t));
|
|---|
| 1286 | memcpy(rp, insert, (li * sizeof(smb_ucs2_t)));
|
|---|
| 1287 | s = sp + lp;
|
|---|
| 1288 | rp += li;
|
|---|
| 1289 | }
|
|---|
| 1290 | lr = ((rp - r) / sizeof(smb_ucs2_t));
|
|---|
| 1291 | if (lr < lt) {
|
|---|
| 1292 | memcpy(rp, s, ((lt - lr) * sizeof(smb_ucs2_t)));
|
|---|
| 1293 | rp += (lt - lr);
|
|---|
| 1294 | }
|
|---|
| 1295 | *rp = 0;
|
|---|
| 1296 |
|
|---|
| 1297 | return r;
|
|---|
| 1298 | }
|
|---|
| 1299 |
|
|---|
| 1300 | smb_ucs2_t *all_string_sub_wa(smb_ucs2_t *s, const char *pattern,
|
|---|
| 1301 | const char *insert)
|
|---|
| 1302 | {
|
|---|
| 1303 | wpstring p, i;
|
|---|
| 1304 |
|
|---|
| 1305 | if (!insert || !pattern || !s)
|
|---|
| 1306 | return NULL;
|
|---|
| 1307 | push_ucs2(NULL, p, pattern, sizeof(wpstring) - 1, STR_TERMINATE);
|
|---|
| 1308 | push_ucs2(NULL, i, insert, sizeof(wpstring) - 1, STR_TERMINATE);
|
|---|
| 1309 | return all_string_sub_w(s, p, i);
|
|---|
| 1310 | }
|
|---|
| 1311 |
|
|---|
| 1312 | #if 0
|
|---|
| 1313 | /**
|
|---|
| 1314 | Splits out the front and back at a separator.
|
|---|
| 1315 | **/
|
|---|
| 1316 |
|
|---|
| 1317 | static void split_at_last_component(char *path, char *front, char sep, char *back)
|
|---|
| 1318 | {
|
|---|
| 1319 | char *p = strrchr_m(path, sep);
|
|---|
| 1320 |
|
|---|
| 1321 | if (p != NULL)
|
|---|
| 1322 | *p = 0;
|
|---|
| 1323 |
|
|---|
| 1324 | if (front != NULL)
|
|---|
| 1325 | pstrcpy(front, path);
|
|---|
| 1326 |
|
|---|
| 1327 | if (p != NULL) {
|
|---|
| 1328 | if (back != NULL)
|
|---|
| 1329 | pstrcpy(back, p+1);
|
|---|
| 1330 | *p = '\\';
|
|---|
| 1331 | } else {
|
|---|
| 1332 | if (back != NULL)
|
|---|
| 1333 | back[0] = 0;
|
|---|
| 1334 | }
|
|---|
| 1335 | }
|
|---|
| 1336 | #endif
|
|---|
| 1337 |
|
|---|
| 1338 | /**
|
|---|
| 1339 | Write an octal as a string.
|
|---|
| 1340 | **/
|
|---|
| 1341 |
|
|---|
| 1342 | const char *octal_string(int i)
|
|---|
| 1343 | {
|
|---|
| 1344 | static char ret[64];
|
|---|
| 1345 | if (i == -1)
|
|---|
| 1346 | return "-1";
|
|---|
| 1347 | slprintf(ret, sizeof(ret)-1, "0%o", i);
|
|---|
| 1348 | return ret;
|
|---|
| 1349 | }
|
|---|
| 1350 |
|
|---|
| 1351 |
|
|---|
| 1352 | /**
|
|---|
| 1353 | Truncate a string at a specified length.
|
|---|
| 1354 | **/
|
|---|
| 1355 |
|
|---|
| 1356 | char *string_truncate(char *s, unsigned int length)
|
|---|
| 1357 | {
|
|---|
| 1358 | if (s && strlen(s) > length)
|
|---|
| 1359 | s[length] = 0;
|
|---|
| 1360 | return s;
|
|---|
| 1361 | }
|
|---|
| 1362 |
|
|---|
| 1363 | /**
|
|---|
| 1364 | Strchr and strrchr_m are very hard to do on general multi-byte strings.
|
|---|
| 1365 | We convert via ucs2 for now.
|
|---|
| 1366 | **/
|
|---|
| 1367 |
|
|---|
| 1368 | char *strchr_m(const char *src, char c)
|
|---|
| 1369 | {
|
|---|
| 1370 | wpstring ws;
|
|---|
| 1371 | pstring s2;
|
|---|
| 1372 | smb_ucs2_t *p;
|
|---|
| 1373 | const char *s;
|
|---|
| 1374 |
|
|---|
| 1375 | /* characters below 0x3F are guaranteed to not appear in
|
|---|
| 1376 | non-initial position in multi-byte charsets */
|
|---|
| 1377 | if ((c & 0xC0) == 0) {
|
|---|
| 1378 | return strchr(src, c);
|
|---|
| 1379 | }
|
|---|
| 1380 |
|
|---|
| 1381 | /* this is quite a common operation, so we want it to be
|
|---|
| 1382 | fast. We optimise for the ascii case, knowing that all our
|
|---|
| 1383 | supported multi-byte character sets are ascii-compatible
|
|---|
| 1384 | (ie. they match for the first 128 chars) */
|
|---|
| 1385 |
|
|---|
| 1386 | for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
|
|---|
| 1387 | if (*s == c)
|
|---|
| 1388 | return (char *)s;
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | if (!*s)
|
|---|
| 1392 | return NULL;
|
|---|
| 1393 |
|
|---|
| 1394 | #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
|
|---|
| 1395 | /* With compose characters we must restart from the beginning. JRA. */
|
|---|
| 1396 | s = src;
|
|---|
| 1397 | #endif
|
|---|
| 1398 |
|
|---|
| 1399 | push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
|
|---|
| 1400 | p = strchr_w(ws, UCS2_CHAR(c));
|
|---|
| 1401 | if (!p)
|
|---|
| 1402 | return NULL;
|
|---|
| 1403 | *p = 0;
|
|---|
| 1404 | pull_ucs2_pstring(s2, ws);
|
|---|
| 1405 | return (char *)(s+strlen(s2));
|
|---|
| 1406 | }
|
|---|
| 1407 |
|
|---|
| 1408 | char *strrchr_m(const char *s, char c)
|
|---|
| 1409 | {
|
|---|
| 1410 | /* characters below 0x3F are guaranteed to not appear in
|
|---|
| 1411 | non-initial position in multi-byte charsets */
|
|---|
| 1412 | if ((c & 0xC0) == 0) {
|
|---|
| 1413 | return strrchr(s, c);
|
|---|
| 1414 | }
|
|---|
| 1415 |
|
|---|
| 1416 | /* this is quite a common operation, so we want it to be
|
|---|
| 1417 | fast. We optimise for the ascii case, knowing that all our
|
|---|
| 1418 | supported multi-byte character sets are ascii-compatible
|
|---|
| 1419 | (ie. they match for the first 128 chars). Also, in Samba
|
|---|
| 1420 | we only search for ascii characters in 'c' and that
|
|---|
| 1421 | in all mb character sets with a compound character
|
|---|
| 1422 | containing c, if 'c' is not a match at position
|
|---|
| 1423 | p, then p[-1] > 0x7f. JRA. */
|
|---|
| 1424 |
|
|---|
| 1425 | {
|
|---|
| 1426 | size_t len = strlen(s);
|
|---|
| 1427 | const char *cp = s;
|
|---|
| 1428 | BOOL got_mb = False;
|
|---|
| 1429 |
|
|---|
| 1430 | if (len == 0)
|
|---|
| 1431 | return NULL;
|
|---|
| 1432 | cp += (len - 1);
|
|---|
| 1433 | do {
|
|---|
| 1434 | if (c == *cp) {
|
|---|
| 1435 | /* Could be a match. Part of a multibyte ? */
|
|---|
| 1436 | if ((cp > s) && (((unsigned char)cp[-1]) & 0x80)) {
|
|---|
| 1437 | /* Yep - go slow :-( */
|
|---|
| 1438 | got_mb = True;
|
|---|
| 1439 | break;
|
|---|
| 1440 | }
|
|---|
| 1441 | /* No - we have a match ! */
|
|---|
| 1442 | return (char *)cp;
|
|---|
| 1443 | }
|
|---|
| 1444 | } while (cp-- != s);
|
|---|
| 1445 | if (!got_mb)
|
|---|
| 1446 | return NULL;
|
|---|
| 1447 | }
|
|---|
| 1448 |
|
|---|
| 1449 | /* String contained a non-ascii char. Slow path. */
|
|---|
| 1450 | {
|
|---|
| 1451 | wpstring ws;
|
|---|
| 1452 | pstring s2;
|
|---|
| 1453 | smb_ucs2_t *p;
|
|---|
| 1454 |
|
|---|
| 1455 | push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
|
|---|
| 1456 | p = strrchr_w(ws, UCS2_CHAR(c));
|
|---|
| 1457 | if (!p)
|
|---|
| 1458 | return NULL;
|
|---|
| 1459 | *p = 0;
|
|---|
| 1460 | pull_ucs2_pstring(s2, ws);
|
|---|
| 1461 | return (char *)(s+strlen(s2));
|
|---|
| 1462 | }
|
|---|
| 1463 | }
|
|---|
| 1464 |
|
|---|
| 1465 | /***********************************************************************
|
|---|
| 1466 | Return the equivalent of doing strrchr 'n' times - always going
|
|---|
| 1467 | backwards.
|
|---|
| 1468 | ***********************************************************************/
|
|---|
| 1469 |
|
|---|
| 1470 | char *strnrchr_m(const char *s, char c, unsigned int n)
|
|---|
| 1471 | {
|
|---|
| 1472 | wpstring ws;
|
|---|
| 1473 | pstring s2;
|
|---|
| 1474 | smb_ucs2_t *p;
|
|---|
| 1475 |
|
|---|
| 1476 | push_ucs2(NULL, ws, s, sizeof(ws), STR_TERMINATE);
|
|---|
| 1477 | p = strnrchr_w(ws, UCS2_CHAR(c), n);
|
|---|
| 1478 | if (!p)
|
|---|
| 1479 | return NULL;
|
|---|
| 1480 | *p = 0;
|
|---|
| 1481 | pull_ucs2_pstring(s2, ws);
|
|---|
| 1482 | return (char *)(s+strlen(s2));
|
|---|
| 1483 | }
|
|---|
| 1484 |
|
|---|
| 1485 | /***********************************************************************
|
|---|
| 1486 | strstr_m - We convert via ucs2 for now.
|
|---|
| 1487 | ***********************************************************************/
|
|---|
| 1488 |
|
|---|
| 1489 | char *strstr_m(const char *src, const char *findstr)
|
|---|
| 1490 | {
|
|---|
| 1491 | smb_ucs2_t *p;
|
|---|
| 1492 | smb_ucs2_t *src_w, *find_w;
|
|---|
| 1493 | const char *s;
|
|---|
| 1494 | char *s2;
|
|---|
| 1495 | char *retp;
|
|---|
| 1496 |
|
|---|
| 1497 | size_t findstr_len = 0;
|
|---|
| 1498 |
|
|---|
| 1499 | /* for correctness */
|
|---|
| 1500 | if (!findstr[0]) {
|
|---|
| 1501 | return (char*)src;
|
|---|
| 1502 | }
|
|---|
| 1503 |
|
|---|
| 1504 | /* Samba does single character findstr calls a *lot*. */
|
|---|
| 1505 | if (findstr[1] == '\0')
|
|---|
| 1506 | return strchr_m(src, *findstr);
|
|---|
| 1507 |
|
|---|
| 1508 | /* We optimise for the ascii case, knowing that all our
|
|---|
| 1509 | supported multi-byte character sets are ascii-compatible
|
|---|
| 1510 | (ie. they match for the first 128 chars) */
|
|---|
| 1511 |
|
|---|
| 1512 | for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
|
|---|
| 1513 | if (*s == *findstr) {
|
|---|
| 1514 | if (!findstr_len)
|
|---|
| 1515 | findstr_len = strlen(findstr);
|
|---|
| 1516 |
|
|---|
| 1517 | if (strncmp(s, findstr, findstr_len) == 0) {
|
|---|
| 1518 | return (char *)s;
|
|---|
| 1519 | }
|
|---|
| 1520 | }
|
|---|
| 1521 | }
|
|---|
| 1522 |
|
|---|
| 1523 | if (!*s)
|
|---|
| 1524 | return NULL;
|
|---|
| 1525 |
|
|---|
| 1526 | #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
|
|---|
| 1527 | /* 'make check' fails unless we do this */
|
|---|
| 1528 |
|
|---|
| 1529 | /* With compose characters we must restart from the beginning. JRA. */
|
|---|
| 1530 | s = src;
|
|---|
| 1531 | #endif
|
|---|
| 1532 |
|
|---|
| 1533 | if (push_ucs2_allocate(&src_w, src) == (size_t)-1) {
|
|---|
| 1534 | DEBUG(0,("strstr_m: src malloc fail\n"));
|
|---|
| 1535 | return NULL;
|
|---|
| 1536 | }
|
|---|
| 1537 |
|
|---|
| 1538 | if (push_ucs2_allocate(&find_w, findstr) == (size_t)-1) {
|
|---|
| 1539 | SAFE_FREE(src_w);
|
|---|
| 1540 | DEBUG(0,("strstr_m: find malloc fail\n"));
|
|---|
| 1541 | return NULL;
|
|---|
| 1542 | }
|
|---|
| 1543 |
|
|---|
| 1544 | p = strstr_w(src_w, find_w);
|
|---|
| 1545 |
|
|---|
| 1546 | if (!p) {
|
|---|
| 1547 | SAFE_FREE(src_w);
|
|---|
| 1548 | SAFE_FREE(find_w);
|
|---|
| 1549 | return NULL;
|
|---|
| 1550 | }
|
|---|
| 1551 |
|
|---|
| 1552 | *p = 0;
|
|---|
| 1553 | if (pull_ucs2_allocate(&s2, src_w) == (size_t)-1) {
|
|---|
| 1554 | SAFE_FREE(src_w);
|
|---|
| 1555 | SAFE_FREE(find_w);
|
|---|
| 1556 | DEBUG(0,("strstr_m: dest malloc fail\n"));
|
|---|
| 1557 | return NULL;
|
|---|
| 1558 | }
|
|---|
| 1559 | retp = (char *)(s+strlen(s2));
|
|---|
| 1560 | SAFE_FREE(src_w);
|
|---|
| 1561 | SAFE_FREE(find_w);
|
|---|
| 1562 | SAFE_FREE(s2);
|
|---|
| 1563 | return retp;
|
|---|
| 1564 | }
|
|---|
| 1565 |
|
|---|
| 1566 | /**
|
|---|
| 1567 | Convert a string to lower case.
|
|---|
| 1568 | **/
|
|---|
| 1569 |
|
|---|
| 1570 | void strlower_m(char *s)
|
|---|
| 1571 | {
|
|---|
| 1572 | size_t len;
|
|---|
| 1573 | int errno_save;
|
|---|
| 1574 |
|
|---|
| 1575 | /* this is quite a common operation, so we want it to be
|
|---|
| 1576 | fast. We optimise for the ascii case, knowing that all our
|
|---|
| 1577 | supported multi-byte character sets are ascii-compatible
|
|---|
| 1578 | (ie. they match for the first 128 chars) */
|
|---|
| 1579 |
|
|---|
| 1580 | while (*s && !(((unsigned char)s[0]) & 0x80)) {
|
|---|
| 1581 | *s = tolower_ascii((unsigned char)*s);
|
|---|
| 1582 | s++;
|
|---|
| 1583 | }
|
|---|
| 1584 |
|
|---|
| 1585 | if (!*s)
|
|---|
| 1586 | return;
|
|---|
| 1587 |
|
|---|
| 1588 | /* I assume that lowercased string takes the same number of bytes
|
|---|
| 1589 | * as source string even in UTF-8 encoding. (VIV) */
|
|---|
| 1590 | len = strlen(s) + 1;
|
|---|
| 1591 | errno_save = errno;
|
|---|
| 1592 | errno = 0;
|
|---|
| 1593 | unix_strlower(s,len,s,len);
|
|---|
| 1594 | /* Catch mb conversion errors that may not terminate. */
|
|---|
| 1595 | if (errno)
|
|---|
| 1596 | s[len-1] = '\0';
|
|---|
| 1597 | errno = errno_save;
|
|---|
| 1598 | }
|
|---|
| 1599 |
|
|---|
| 1600 | /**
|
|---|
| 1601 | Convert a string to upper case.
|
|---|
| 1602 | **/
|
|---|
| 1603 |
|
|---|
| 1604 | void strupper_m(char *s)
|
|---|
| 1605 | {
|
|---|
| 1606 | size_t len;
|
|---|
| 1607 | int errno_save;
|
|---|
| 1608 |
|
|---|
| 1609 | /* this is quite a common operation, so we want it to be
|
|---|
| 1610 | fast. We optimise for the ascii case, knowing that all our
|
|---|
| 1611 | supported multi-byte character sets are ascii-compatible
|
|---|
| 1612 | (ie. they match for the first 128 chars) */
|
|---|
| 1613 |
|
|---|
| 1614 | while (*s && !(((unsigned char)s[0]) & 0x80)) {
|
|---|
| 1615 | *s = toupper_ascii((unsigned char)*s);
|
|---|
| 1616 | s++;
|
|---|
| 1617 | }
|
|---|
| 1618 |
|
|---|
| 1619 | if (!*s)
|
|---|
| 1620 | return;
|
|---|
| 1621 |
|
|---|
| 1622 | /* I assume that lowercased string takes the same number of bytes
|
|---|
| 1623 | * as source string even in multibyte encoding. (VIV) */
|
|---|
| 1624 | len = strlen(s) + 1;
|
|---|
| 1625 | errno_save = errno;
|
|---|
| 1626 | errno = 0;
|
|---|
| 1627 | unix_strupper(s,len,s,len);
|
|---|
| 1628 | /* Catch mb conversion errors that may not terminate. */
|
|---|
| 1629 | if (errno)
|
|---|
| 1630 | s[len-1] = '\0';
|
|---|
| 1631 | errno = errno_save;
|
|---|
| 1632 | }
|
|---|
| 1633 |
|
|---|
| 1634 | /**
|
|---|
| 1635 | Count the number of UCS2 characters in a string. Normally this will
|
|---|
| 1636 | be the same as the number of bytes in a string for single byte strings,
|
|---|
| 1637 | but will be different for multibyte.
|
|---|
| 1638 | **/
|
|---|
| 1639 |
|
|---|
| 1640 | size_t strlen_m(const char *s)
|
|---|
| 1641 | {
|
|---|
| 1642 | size_t count = 0;
|
|---|
| 1643 |
|
|---|
| 1644 | if (!s) {
|
|---|
| 1645 | return 0;
|
|---|
| 1646 | }
|
|---|
| 1647 |
|
|---|
| 1648 | while (*s && !(((uint8_t)*s) & 0x80)) {
|
|---|
| 1649 | s++;
|
|---|
| 1650 | count++;
|
|---|
| 1651 | }
|
|---|
| 1652 |
|
|---|
| 1653 | if (!*s) {
|
|---|
| 1654 | return count;
|
|---|
| 1655 | }
|
|---|
| 1656 |
|
|---|
| 1657 | while (*s) {
|
|---|
| 1658 | size_t c_size;
|
|---|
| 1659 | codepoint_t c = next_codepoint(s, &c_size);
|
|---|
| 1660 | if (c < 0x10000) {
|
|---|
| 1661 | /* Unicode char fits into 16 bits. */
|
|---|
| 1662 | count += 1;
|
|---|
| 1663 | } else {
|
|---|
| 1664 | /* Double-width unicode char - 32 bits. */
|
|---|
| 1665 | count += 2;
|
|---|
| 1666 | }
|
|---|
| 1667 | s += c_size;
|
|---|
| 1668 | }
|
|---|
| 1669 |
|
|---|
| 1670 | return count;
|
|---|
| 1671 | }
|
|---|
| 1672 |
|
|---|
| 1673 | /**
|
|---|
| 1674 | Count the number of UCS2 characters in a string including the null
|
|---|
| 1675 | terminator.
|
|---|
| 1676 | **/
|
|---|
| 1677 |
|
|---|
| 1678 | size_t strlen_m_term(const char *s)
|
|---|
| 1679 | {
|
|---|
| 1680 | if (!s) {
|
|---|
| 1681 | return 0;
|
|---|
| 1682 | }
|
|---|
| 1683 | return strlen_m(s) + 1;
|
|---|
| 1684 | }
|
|---|
| 1685 |
|
|---|
| 1686 | /*
|
|---|
| 1687 | * Weird helper routine for the winreg pipe: If nothing is around, return 0,
|
|---|
| 1688 | * if a string is there, include the terminator.
|
|---|
| 1689 | */
|
|---|
| 1690 |
|
|---|
| 1691 | size_t strlen_m_term_null(const char *s)
|
|---|
| 1692 | {
|
|---|
| 1693 | size_t len;
|
|---|
| 1694 | if (!s) {
|
|---|
| 1695 | return 0;
|
|---|
| 1696 | }
|
|---|
| 1697 | len = strlen_m(s);
|
|---|
| 1698 | if (len == 0) {
|
|---|
| 1699 | return 0;
|
|---|
| 1700 | }
|
|---|
| 1701 |
|
|---|
| 1702 | return len+1;
|
|---|
| 1703 | }
|
|---|
| 1704 | /**
|
|---|
| 1705 | Return a RFC2254 binary string representation of a buffer.
|
|---|
| 1706 | Used in LDAP filters.
|
|---|
| 1707 | Caller must free.
|
|---|
| 1708 | **/
|
|---|
| 1709 |
|
|---|
| 1710 | char *binary_string_rfc2254(char *buf, int len)
|
|---|
| 1711 | {
|
|---|
| 1712 | char *s;
|
|---|
| 1713 | int i, j;
|
|---|
| 1714 | const char *hex = "0123456789ABCDEF";
|
|---|
| 1715 | s = (char *)SMB_MALLOC(len * 3 + 1);
|
|---|
| 1716 | if (!s)
|
|---|
| 1717 | return NULL;
|
|---|
| 1718 | for (j=i=0;i<len;i++) {
|
|---|
| 1719 | s[j] = '\\';
|
|---|
| 1720 | s[j+1] = hex[((unsigned char)buf[i]) >> 4];
|
|---|
| 1721 | s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
|
|---|
| 1722 | j += 3;
|
|---|
| 1723 | }
|
|---|
| 1724 | s[j] = 0;
|
|---|
| 1725 | return s;
|
|---|
| 1726 | }
|
|---|
| 1727 |
|
|---|
| 1728 | char *binary_string(char *buf, int len)
|
|---|
| 1729 | {
|
|---|
| 1730 | char *s;
|
|---|
| 1731 | int i, j;
|
|---|
| 1732 | const char *hex = "0123456789ABCDEF";
|
|---|
| 1733 | s = (char *)SMB_MALLOC(len * 2 + 1);
|
|---|
| 1734 | if (!s)
|
|---|
| 1735 | return NULL;
|
|---|
| 1736 | for (j=i=0;i<len;i++) {
|
|---|
| 1737 | s[j] = hex[((unsigned char)buf[i]) >> 4];
|
|---|
| 1738 | s[j+1] = hex[((unsigned char)buf[i]) & 0xF];
|
|---|
| 1739 | j += 2;
|
|---|
| 1740 | }
|
|---|
| 1741 | s[j] = 0;
|
|---|
| 1742 | return s;
|
|---|
| 1743 | }
|
|---|
| 1744 | /**
|
|---|
| 1745 | Just a typesafety wrapper for snprintf into a pstring.
|
|---|
| 1746 | **/
|
|---|
| 1747 |
|
|---|
| 1748 | int pstr_sprintf(pstring s, const char *fmt, ...)
|
|---|
| 1749 | {
|
|---|
| 1750 | va_list ap;
|
|---|
| 1751 | int ret;
|
|---|
| 1752 |
|
|---|
| 1753 | va_start(ap, fmt);
|
|---|
| 1754 | ret = vsnprintf(s, PSTRING_LEN, fmt, ap);
|
|---|
| 1755 | va_end(ap);
|
|---|
| 1756 | return ret;
|
|---|
| 1757 | }
|
|---|
| 1758 |
|
|---|
| 1759 |
|
|---|
| 1760 | /**
|
|---|
| 1761 | Just a typesafety wrapper for snprintf into a fstring.
|
|---|
| 1762 | **/
|
|---|
| 1763 |
|
|---|
| 1764 | int fstr_sprintf(fstring s, const char *fmt, ...)
|
|---|
| 1765 | {
|
|---|
| 1766 | va_list ap;
|
|---|
| 1767 | int ret;
|
|---|
| 1768 |
|
|---|
| 1769 | va_start(ap, fmt);
|
|---|
| 1770 | ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
|
|---|
| 1771 | va_end(ap);
|
|---|
| 1772 | return ret;
|
|---|
| 1773 | }
|
|---|
| 1774 |
|
|---|
| 1775 | /**
|
|---|
| 1776 | List of Strings manipulation functions
|
|---|
| 1777 | **/
|
|---|
| 1778 |
|
|---|
| 1779 | #define S_LIST_ABS 16 /* List Allocation Block Size */
|
|---|
| 1780 |
|
|---|
| 1781 | static char **str_list_make_internal(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
|
|---|
| 1782 | {
|
|---|
| 1783 | char **list, **rlist;
|
|---|
| 1784 | const char *str;
|
|---|
| 1785 | char *s;
|
|---|
| 1786 | int num, lsize;
|
|---|
| 1787 | pstring tok;
|
|---|
| 1788 |
|
|---|
| 1789 | if (!string || !*string)
|
|---|
| 1790 | return NULL;
|
|---|
| 1791 | if (mem_ctx) {
|
|---|
| 1792 | s = talloc_strdup(mem_ctx, string);
|
|---|
| 1793 | } else {
|
|---|
| 1794 | s = SMB_STRDUP(string);
|
|---|
| 1795 | }
|
|---|
| 1796 | if (!s) {
|
|---|
| 1797 | DEBUG(0,("str_list_make: Unable to allocate memory"));
|
|---|
| 1798 | return NULL;
|
|---|
| 1799 | }
|
|---|
| 1800 | if (!sep) sep = LIST_SEP;
|
|---|
| 1801 |
|
|---|
| 1802 | num = lsize = 0;
|
|---|
|
|---|