Merge branch 'master' of ssh://git.samba.org/data/git/samba into crypt
[samba.git] / source3 / lib / util_str.c
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    Copyright (C) Jeremy Allison  1992-2007
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26
27 char toupper_ascii_fast_table[128] = {
28         0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
29         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
30         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
31         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
32         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
33         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
34         0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
35         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
36 };
37
38 /**
39  * @file
40  * @brief String utilities.
41  **/
42
43 static bool next_token_internal_talloc(TALLOC_CTX *ctx,
44                                 const char **ptr,
45                                 char **pp_buff,
46                                 const char *sep,
47                                 bool ltrim)
48 {
49         char *s;
50         char *saved_s;
51         char *pbuf;
52         bool quoted;
53         size_t len=1;
54
55         *pp_buff = NULL;
56         if (!ptr) {
57                 return(false);
58         }
59
60         s = (char *)*ptr;
61
62         /* default to simple separators */
63         if (!sep) {
64                 sep = " \t\n\r";
65         }
66
67         /* find the first non sep char, if left-trimming is requested */
68         if (ltrim) {
69                 while (*s && strchr_m(sep,*s)) {
70                         s++;
71                 }
72         }
73
74         /* nothing left? */
75         if (!*s) {
76                 return false;
77         }
78
79         /* When restarting we need to go from here. */
80         saved_s = s;
81
82         /* Work out the length needed. */
83         for (quoted = false; *s &&
84                         (quoted || !strchr_m(sep,*s)); s++) {
85                 if (*s == '\"') {
86                         quoted = !quoted;
87                 } else {
88                         len++;
89                 }
90         }
91
92         /* We started with len = 1 so we have space for the nul. */
93         *pp_buff = TALLOC_ARRAY(ctx, char, len);
94         if (!*pp_buff) {
95                 return false;
96         }
97
98         /* copy over the token */
99         pbuf = *pp_buff;
100         s = saved_s;
101         for (quoted = false; *s &&
102                         (quoted || !strchr_m(sep,*s)); s++) {
103                 if ( *s == '\"' ) {
104                         quoted = !quoted;
105                 } else {
106                         *pbuf++ = *s;
107                 }
108         }
109
110         *ptr = (*s) ? s+1 : s;
111         *pbuf = 0;
112
113         return true;
114 }
115
116 #if 0
117 /*
118  * Get the next token from a string, return false if none found.  Handles
119  * double-quotes.  This version trims leading separator characters before
120  * looking for a token.
121  */
122 bool next_token(const char **ptr, char *buff, const char *sep, size_t bufsize)
123 {
124         return next_token_internal(ptr, buff, sep, bufsize, true);
125 }
126 #endif
127
128 bool next_token_talloc(TALLOC_CTX *ctx,
129                         const char **ptr,
130                         char **pp_buff,
131                         const char *sep)
132 {
133         return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
134 }
135
136 /*
137  * Get the next token from a string, return false if none found.  Handles
138  * double-quotes.  This version does not trim leading separator characters
139  * before looking for a token.
140  */
141
142 bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
143                         const char **ptr,
144                         char **pp_buff,
145                         const char *sep)
146 {
147         return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
148 }
149
150 /**
151  * Case insensitive string compararison.
152  *
153  * iconv does not directly give us a way to compare strings in
154  * arbitrary unix character sets -- all we can is convert and then
155  * compare.  This is expensive.
156  *
157  * As an optimization, we do a first pass that considers only the
158  * prefix of the strings that is entirely 7-bit.  Within this, we
159  * check whether they have the same value.
160  *
161  * Hopefully this will often give the answer without needing to copy.
162  * In particular it should speed comparisons to literal ascii strings
163  * or comparisons of strings that are "obviously" different.
164  *
165  * If we find a non-ascii character we fall back to converting via
166  * iconv.
167  *
168  * This should never be slower than convering the whole thing, and
169  * often faster.
170  *
171  * A different optimization would be to compare for bitwise equality
172  * in the binary encoding.  (It would be possible thought hairy to do
173  * both simultaneously.)  But in that case if they turn out to be
174  * different, we'd need to restart the whole thing.
175  *
176  * Even better is to implement strcasecmp for each encoding and use a
177  * function pointer.
178  **/
179 int StrCaseCmp(const char *s, const char *t)
180 {
181
182         const char *ps, *pt;
183         size_t size;
184         smb_ucs2_t *buffer_s, *buffer_t;
185         int ret;
186
187         for (ps = s, pt = t; ; ps++, pt++) {
188                 char us, ut;
189
190                 if (!*ps && !*pt)
191                         return 0; /* both ended */
192                 else if (!*ps)
193                         return -1; /* s is a prefix */
194                 else if (!*pt)
195                         return +1; /* t is a prefix */
196                 else if ((*ps & 0x80) || (*pt & 0x80))
197                         /* not ascii anymore, do it the hard way
198                          * from here on in */
199                         break;
200
201                 us = toupper_ascii_fast(*ps);
202                 ut = toupper_ascii_fast(*pt);
203                 if (us == ut)
204                         continue;
205                 else if (us < ut)
206                         return -1;
207                 else if (us > ut)
208                         return +1;
209         }
210
211         if (!push_ucs2_allocate(&buffer_s, ps, &size)) {
212                 return strcmp(ps, pt);
213                 /* Not quite the right answer, but finding the right one
214                    under this failure case is expensive, and it's pretty
215                    close */
216         }
217
218         if (!push_ucs2_allocate(&buffer_t, pt, &size)) {
219                 SAFE_FREE(buffer_s);
220                 return strcmp(ps, pt);
221                 /* Not quite the right answer, but finding the right one
222                    under this failure case is expensive, and it's pretty
223                    close */
224         }
225
226         ret = strcasecmp_w(buffer_s, buffer_t);
227         SAFE_FREE(buffer_s);
228         SAFE_FREE(buffer_t);
229         return ret;
230 }
231
232
233 /**
234  Case insensitive string compararison, length limited.
235 **/
236 int StrnCaseCmp(const char *s, const char *t, size_t len)
237 {
238         size_t n = 0;
239         const char *ps, *pt;
240         size_t size;
241         smb_ucs2_t *buffer_s, *buffer_t;
242         int ret;
243
244         for (ps = s, pt = t; n < len ; ps++, pt++, n++) {
245                 char us, ut;
246
247                 if (!*ps && !*pt)
248                         return 0; /* both ended */
249                 else if (!*ps)
250                         return -1; /* s is a prefix */
251                 else if (!*pt)
252                         return +1; /* t is a prefix */
253                 else if ((*ps & 0x80) || (*pt & 0x80))
254                         /* not ascii anymore, do it the
255                          * hard way from here on in */
256                         break;
257
258                 us = toupper_ascii_fast(*ps);
259                 ut = toupper_ascii_fast(*pt);
260                 if (us == ut)
261                         continue;
262                 else if (us < ut)
263                         return -1;
264                 else if (us > ut)
265                         return +1;
266         }
267
268         if (n == len) {
269                 return 0;
270         }
271
272         if (!push_ucs2_allocate(&buffer_s, ps, &size)) {
273                 return strncmp(ps, pt, len-n);
274                 /* Not quite the right answer, but finding the right one
275                    under this failure case is expensive,
276                    and it's pretty close */
277         }
278
279         if (!push_ucs2_allocate(&buffer_t, pt, &size)) {
280                 SAFE_FREE(buffer_s);
281                 return strncmp(ps, pt, len-n);
282                 /* Not quite the right answer, but finding the right one
283                    under this failure case is expensive,
284                    and it's pretty close */
285         }
286
287         ret = strncasecmp_w(buffer_s, buffer_t, len-n);
288         SAFE_FREE(buffer_s);
289         SAFE_FREE(buffer_t);
290         return ret;
291 }
292
293 /**
294  * Compare 2 strings.
295  *
296  * @note The comparison is case-insensitive.
297  **/
298 bool strequal(const char *s1, const char *s2)
299 {
300         if (s1 == s2)
301                 return(true);
302         if (!s1 || !s2)
303                 return(false);
304
305         return(StrCaseCmp(s1,s2)==0);
306 }
307
308 /**
309  * Compare 2 strings up to and including the nth char.
310  *
311  * @note The comparison is case-insensitive.
312  **/
313 bool strnequal(const char *s1,const char *s2,size_t n)
314 {
315         if (s1 == s2)
316                 return(true);
317         if (!s1 || !s2 || !n)
318                 return(false);
319
320         return(StrnCaseCmp(s1,s2,n)==0);
321 }
322
323 /**
324  Compare 2 strings (case sensitive).
325 **/
326
327 bool strcsequal(const char *s1,const char *s2)
328 {
329         if (s1 == s2)
330                 return(true);
331         if (!s1 || !s2)
332                 return(false);
333
334         return(strcmp(s1,s2)==0);
335 }
336
337 /**
338 Do a case-insensitive, whitespace-ignoring string compare.
339 **/
340
341 int strwicmp(const char *psz1, const char *psz2)
342 {
343         /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
344         /* appropriate value. */
345         if (psz1 == psz2)
346                 return (0);
347         else if (psz1 == NULL)
348                 return (-1);
349         else if (psz2 == NULL)
350                 return (1);
351
352         /* sync the strings on first non-whitespace */
353         while (1) {
354                 while (isspace((int)*psz1))
355                         psz1++;
356                 while (isspace((int)*psz2))
357                         psz2++;
358                 if (toupper_ascii(*psz1) != toupper_ascii(*psz2) ||
359                                 *psz1 == '\0' || *psz2 == '\0')
360                         break;
361                 psz1++;
362                 psz2++;
363         }
364         return (*psz1 - *psz2);
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 void string_replace( char *s, char oldc, char newc )
397 {
398         char *p;
399
400         /* this is quite a common operation, so we want it to be
401            fast. We optimise for the ascii case, knowing that all our
402            supported multi-byte character sets are ascii-compatible
403            (ie. they match for the first 128 chars) */
404
405         for (p = s; *p; p++) {
406                 if (*p & 0x80) /* mb string - slow path. */
407                         break;
408                 if (*p == oldc) {
409                         *p = newc;
410                 }
411         }
412
413         if (!*p)
414                 return;
415
416         /* Slow (mb) path. */
417 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
418         /* With compose characters we must restart from the beginning. JRA. */
419         p = s;
420 #endif
421
422         while (*p) {
423                 size_t c_size;
424                 next_codepoint(p, &c_size);
425
426                 if (c_size == 1) {
427                         if (*p == oldc) {
428                                 *p = newc;
429                         }
430                 }
431                 p += c_size;
432         }
433 }
434
435 /**
436  *  Skip past some strings in a buffer - old version - no checks.
437  *  **/
438
439 char *push_skip_string(char *buf)
440 {
441         buf += strlen(buf) + 1;
442         return(buf);
443 }
444
445 /**
446  Skip past a string in a buffer. Buffer may not be
447  null terminated. end_ptr points to the first byte after
448  then end of the buffer.
449 **/
450
451 char *skip_string(const char *base, size_t len, char *buf)
452 {
453         const char *end_ptr = base + len;
454
455         if (end_ptr < base || !base || !buf || buf >= end_ptr) {
456                 return NULL;
457         }
458
459         /* Skip the string */
460         while (*buf) {
461                 buf++;
462                 if (buf >= end_ptr) {
463                         return NULL;
464                 }
465         }
466         /* Skip the '\0' */
467         buf++;
468         return buf;
469 }
470
471 /**
472  Count the number of characters in a string. Normally this will
473  be the same as the number of bytes in a string for single byte strings,
474  but will be different for multibyte.
475 **/
476
477 size_t str_charnum(const char *s)
478 {
479         size_t ret, converted_size;
480         smb_ucs2_t *tmpbuf2 = NULL;
481         if (!push_ucs2_allocate(&tmpbuf2, s, &converted_size)) {
482                 return 0;
483         }
484         ret = strlen_w(tmpbuf2);
485         SAFE_FREE(tmpbuf2);
486         return ret;
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         size_t ret, converted_size;
498         char *tmpbuf2 = NULL;
499         if (!push_ascii_allocate(&tmpbuf2, s, &converted_size)) {
500                 return 0;
501         }
502         ret = strlen(tmpbuf2);
503         SAFE_FREE(tmpbuf2);
504         return ret;
505 }
506
507 bool trim_char(char *s,char cfront,char cback)
508 {
509         bool ret = false;
510         char *ep;
511         char *fp = s;
512
513         /* Ignore null or empty strings. */
514         if (!s || (s[0] == '\0'))
515                 return false;
516
517         if (cfront) {
518                 while (*fp && *fp == cfront)
519                         fp++;
520                 if (!*fp) {
521                         /* We ate the string. */
522                         s[0] = '\0';
523                         return true;
524                 }
525                 if (fp != s)
526                         ret = true;
527         }
528
529         ep = fp + strlen(fp) - 1;
530         if (cback) {
531                 /* Attempt ascii only. Bail for mb strings. */
532                 while ((ep >= fp) && (*ep == cback)) {
533                         ret = true;
534                         if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
535                                 /* Could be mb... bail back to tim_string. */
536                                 char fs[2], bs[2];
537                                 if (cfront) {
538                                         fs[0] = cfront;
539                                         fs[1] = '\0';
540                                 }
541                                 bs[0] = cback;
542                                 bs[1] = '\0';
543                                 return trim_string(s, cfront ? fs : NULL, bs);
544                         } else {
545                                 ep--;
546                         }
547                 }
548                 if (ep < fp) {
549                         /* We ate the string. */
550                         s[0] = '\0';
551                         return true;
552                 }
553         }
554
555         ep[1] = '\0';
556         memmove(s, fp, ep-fp+2);
557         return ret;
558 }
559
560 /**
561  Does a string have any uppercase chars in it?
562 **/
563
564 bool strhasupper(const char *s)
565 {
566         smb_ucs2_t *tmp, *p;
567         bool ret;
568         size_t converted_size;
569
570         if (!push_ucs2_allocate(&tmp, s, &converted_size)) {
571                 return false;
572         }
573
574         for(p = tmp; *p != 0; p++) {
575                 if(isupper_w(*p)) {
576                         break;
577                 }
578         }
579
580         ret = (*p != 0);
581         SAFE_FREE(tmp);
582         return ret;
583 }
584
585 /**
586  Does a string have any lowercase chars in it?
587 **/
588
589 bool strhaslower(const char *s)
590 {
591         smb_ucs2_t *tmp, *p;
592         bool ret;
593         size_t converted_size;
594
595         if (!push_ucs2_allocate(&tmp, s, &converted_size)) {
596                 return false;
597         }
598
599         for(p = tmp; *p != 0; p++) {
600                 if(islower_w(*p)) {
601                         break;
602                 }
603         }
604
605         ret = (*p != 0);
606         SAFE_FREE(tmp);
607         return ret;
608 }
609
610 /**
611  Safe string copy into a known length string. maxlength does not
612  include the terminating zero.
613 **/
614
615 char *safe_strcpy_fn(const char *fn,
616                 int line,
617                 char *dest,
618                 const char *src,
619                 size_t maxlength)
620 {
621         size_t len;
622
623         if (!dest) {
624                 DEBUG(0,("ERROR: NULL dest in safe_strcpy, "
625                         "called from [%s][%d]\n", fn, line));
626                 return NULL;
627         }
628
629 #ifdef DEVELOPER
630         clobber_region(fn,line,dest, maxlength+1);
631 #endif
632
633         if (!src) {
634                 *dest = 0;
635                 return dest;
636         }
637
638         len = strnlen(src, maxlength+1);
639
640         if (len > maxlength) {
641                 DEBUG(0,("ERROR: string overflow by "
642                         "%lu (%lu - %lu) in safe_strcpy [%.50s]\n",
643                          (unsigned long)(len-maxlength), (unsigned long)len,
644                          (unsigned long)maxlength, src));
645                 len = maxlength;
646         }
647
648         memmove(dest, src, len);
649         dest[len] = 0;
650         return dest;
651 }
652
653 /**
654  Safe string cat into a string. maxlength does not
655  include the terminating zero.
656 **/
657 char *safe_strcat_fn(const char *fn,
658                 int line,
659                 char *dest,
660                 const char *src,
661                 size_t maxlength)
662 {
663         size_t src_len, dest_len;
664
665         if (!dest) {
666                 DEBUG(0,("ERROR: NULL dest in safe_strcat, "
667                         "called from [%s][%d]\n", fn, line));
668                 return NULL;
669         }
670
671         if (!src)
672                 return dest;
673
674         src_len = strnlen(src, maxlength + 1);
675         dest_len = strnlen(dest, maxlength + 1);
676
677 #ifdef DEVELOPER
678         clobber_region(fn, line, dest + dest_len, maxlength + 1 - dest_len);
679 #endif
680
681         if (src_len + dest_len > maxlength) {
682                 DEBUG(0,("ERROR: string overflow by %d "
683                         "in safe_strcat [%.50s]\n",
684                          (int)(src_len + dest_len - maxlength), src));
685                 if (maxlength > dest_len) {
686                         memcpy(&dest[dest_len], src, maxlength - dest_len);
687                 }
688                 dest[maxlength] = 0;
689                 return NULL;
690         }
691
692         memcpy(&dest[dest_len], src, src_len);
693         dest[dest_len + src_len] = 0;
694         return dest;
695 }
696
697 /**
698  Paranoid strcpy into a buffer of given length (includes terminating
699  zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
700  and replaces with '_'. Deliberately does *NOT* check for multibyte
701  characters. Don't change it !
702 **/
703
704 char *alpha_strcpy_fn(const char *fn,
705                 int line,
706                 char *dest,
707                 const char *src,
708                 const char *other_safe_chars,
709                 size_t maxlength)
710 {
711         size_t len, i;
712
713 #ifdef DEVELOPER
714         clobber_region(fn, line, dest, maxlength);
715 #endif
716
717         if (!dest) {
718                 DEBUG(0,("ERROR: NULL dest in alpha_strcpy, "
719                         "called from [%s][%d]\n", fn, line));
720                 return NULL;
721         }
722
723         if (!src) {
724                 *dest = 0;
725                 return dest;
726         }
727
728         len = strlen(src);
729         if (len >= maxlength)
730                 len = maxlength - 1;
731
732         if (!other_safe_chars)
733                 other_safe_chars = "";
734
735         for(i = 0; i < len; i++) {
736                 int val = (src[i] & 0xff);
737                 if (isupper_ascii(val) || islower_ascii(val) ||
738                                 isdigit(val) || strchr_m(other_safe_chars, val))
739                         dest[i] = src[i];
740                 else
741                         dest[i] = '_';
742         }
743
744         dest[i] = '\0';
745
746         return dest;
747 }
748
749 /**
750  Like strncpy but always null terminates. Make sure there is room!
751  The variable n should always be one less than the available size.
752 **/
753 char *StrnCpy_fn(const char *fn, int line,char *dest,const char *src,size_t n)
754 {
755         char *d = dest;
756
757 #ifdef DEVELOPER
758         clobber_region(fn, line, dest, n+1);
759 #endif
760
761         if (!dest) {
762                 DEBUG(0,("ERROR: NULL dest in StrnCpy, "
763                         "called from [%s][%d]\n", fn, line));
764                 return(NULL);
765         }
766
767         if (!src) {
768                 *dest = 0;
769                 return(dest);
770         }
771
772         while (n-- && (*d = *src)) {
773                 d++;
774                 src++;
775         }
776
777         *d = 0;
778         return(dest);
779 }
780
781 #if 0
782 /**
783  Like strncpy but copies up to the character marker.  always null terminates.
784  returns a pointer to the character marker in the source string (src).
785 **/
786
787 static char *strncpyn(char *dest, const char *src, size_t n, char c)
788 {
789         char *p;
790         size_t str_len;
791
792 #ifdef DEVELOPER
793         clobber_region(dest, n+1);
794 #endif
795         p = strchr_m(src, c);
796         if (p == NULL) {
797                 DEBUG(5, ("strncpyn: separator character (%c) not found\n", c));
798                 return NULL;
799         }
800
801         str_len = PTR_DIFF(p, src);
802         strncpy(dest, src, MIN(n, str_len));
803         dest[str_len] = '\0';
804
805         return p;
806 }
807 #endif
808
809 /**
810  Check if a string is part of a list.
811 **/
812
813 bool in_list(const char *s, const char *list, bool casesensitive)
814 {
815         char *tok = NULL;
816         bool ret = false;
817         TALLOC_CTX *frame;
818
819         if (!list) {
820                 return false;
821         }
822
823         frame = talloc_stackframe();
824         while (next_token_talloc(frame, &list, &tok,LIST_SEP)) {
825                 if (casesensitive) {
826                         if (strcmp(tok,s) == 0) {
827                                 ret = true;
828                                 break;
829                         }
830                 } else {
831                         if (StrCaseCmp(tok,s) == 0) {
832                                 ret = true;
833                                 break;
834                         }
835                 }
836         }
837         TALLOC_FREE(frame);
838         return ret;
839 }
840
841 /* this is used to prevent lots of mallocs of size 1 */
842 static const char null_string[] = "";
843
844 /**
845  Set a string value, allocing the space for the string
846 **/
847
848 static bool string_init(char **dest,const char *src)
849 {
850         size_t l;
851
852         if (!src)
853                 src = "";
854
855         l = strlen(src);
856
857         if (l == 0) {
858                 *dest = CONST_DISCARD(char*, null_string);
859         } else {
860                 (*dest) = SMB_STRDUP(src);
861                 if ((*dest) == NULL) {
862                         DEBUG(0,("Out of memory in string_init\n"));
863                         return false;
864                 }
865         }
866         return(true);
867 }
868
869 /**
870  Free a string value.
871 **/
872
873 void string_free(char **s)
874 {
875         if (!s || !(*s))
876                 return;
877         if (*s == null_string)
878                 *s = NULL;
879         SAFE_FREE(*s);
880 }
881
882 /**
883  Set a string value, deallocating any existing space, and allocing the space
884  for the string
885 **/
886
887 bool string_set(char **dest,const char *src)
888 {
889         string_free(dest);
890         return(string_init(dest,src));
891 }
892
893 /**
894  Substitute a string for a pattern in another string. Make sure there is
895  enough room!
896
897  This routine looks for pattern in s and replaces it with
898  insert. It may do multiple replacements or just one.
899
900  Any of " ; ' $ or ` in the insert string are replaced with _
901  if len==0 then the string cannot be extended. This is different from the old
902  use of len==0 which was for no length checks to be done.
903 **/
904
905 void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
906                  bool remove_unsafe_characters, bool replace_once,
907                  bool allow_trailing_dollar)
908 {
909         char *p;
910         ssize_t ls,lp,li, i;
911
912         if (!insert || !pattern || !*pattern || !s)
913                 return;
914
915         ls = (ssize_t)strlen(s);
916         lp = (ssize_t)strlen(pattern);
917         li = (ssize_t)strlen(insert);
918
919         if (len == 0)
920                 len = ls + 1; /* len is number of *bytes* */
921
922         while (lp <= ls && (p = strstr_m(s,pattern))) {
923                 if (ls + (li-lp) >= len) {
924                         DEBUG(0,("ERROR: string overflow by "
925                                 "%d in string_sub(%.50s, %d)\n",
926                                  (int)(ls + (li-lp) - len),
927                                  pattern, (int)len));
928                         break;
929                 }
930                 if (li != lp) {
931                         memmove(p+li,p+lp,strlen(p+lp)+1);
932                 }
933                 for (i=0;i<li;i++) {
934                         switch (insert[i]) {
935                         case '`':
936                         case '"':
937                         case '\'':
938                         case ';':
939                         case '$':
940                                 /* allow a trailing $
941                                  * (as in machine accounts) */
942                                 if (allow_trailing_dollar && (i == li - 1 )) {
943                                         p[i] = insert[i];
944                                         break;
945                                 }
946                         case '%':
947                         case '\r':
948                         case '\n':
949                                 if ( remove_unsafe_characters ) {
950                                         p[i] = '_';
951                                         /* yes this break should be here
952                                          * since we want to fall throw if
953                                          * not replacing unsafe chars */
954                                         break;
955                                 }
956                         default:
957                                 p[i] = insert[i];
958                         }
959                 }
960                 s = p + li;
961                 ls += (li-lp);
962
963                 if (replace_once)
964                         break;
965         }
966 }
967
968 void string_sub_once(char *s, const char *pattern,
969                 const char *insert, size_t len)
970 {
971         string_sub2( s, pattern, insert, len, true, true, false );
972 }
973
974 void string_sub(char *s,const char *pattern, const char *insert, size_t len)
975 {
976         string_sub2( s, pattern, insert, len, true, false, false );
977 }
978
979 void fstring_sub(char *s,const char *pattern,const char *insert)
980 {
981         string_sub(s, pattern, insert, sizeof(fstring));
982 }
983
984 /**
985  Similar to string_sub2, but it will accept only allocated strings
986  and may realloc them so pay attention at what you pass on no
987  pointers inside strings, no const may be passed
988  as string.
989 **/
990
991 char *realloc_string_sub2(char *string,
992                         const char *pattern,
993                         const char *insert,
994                         bool remove_unsafe_characters,
995                         bool allow_trailing_dollar)
996 {
997         char *p, *in;
998         char *s;
999         ssize_t ls,lp,li,ld, i;
1000
1001         if (!insert || !pattern || !*pattern || !string || !*string)
1002                 return NULL;
1003
1004         s = string;
1005
1006         in = SMB_STRDUP(insert);
1007         if (!in) {
1008                 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
1009                 return NULL;
1010         }
1011         ls = (ssize_t)strlen(s);
1012         lp = (ssize_t)strlen(pattern);
1013         li = (ssize_t)strlen(insert);
1014         ld = li - lp;
1015         for (i=0;i<li;i++) {
1016                 switch (in[i]) {
1017                         case '`':
1018                         case '"':
1019                         case '\'':
1020                         case ';':
1021                         case '$':
1022                                 /* allow a trailing $
1023                                  * (as in machine accounts) */
1024                                 if (allow_trailing_dollar && (i == li - 1 )) {
1025                                         break;
1026                                 }
1027                         case '%':
1028                         case '\r':
1029                         case '\n':
1030                                 if ( remove_unsafe_characters ) {
1031                                         in[i] = '_';
1032                                         break;
1033                                 }
1034                         default:
1035                                 /* ok */
1036                                 break;
1037                 }
1038         }
1039
1040         while ((p = strstr_m(s,pattern))) {
1041                 if (ld > 0) {
1042                         int offset = PTR_DIFF(s,string);
1043                         string = (char *)SMB_REALLOC(string, ls + ld + 1);
1044                         if (!string) {
1045                                 DEBUG(0, ("realloc_string_sub: "
1046                                         "out of memory!\n"));
1047                                 SAFE_FREE(in);
1048                                 return NULL;
1049                         }
1050                         p = string + offset + (p - s);
1051                 }
1052                 if (li != lp) {
1053                         memmove(p+li,p+lp,strlen(p+lp)+1);
1054                 }
1055                 memcpy(p, in, li);
1056                 s = p + li;
1057                 ls += ld;
1058         }
1059         SAFE_FREE(in);
1060         return string;
1061 }
1062
1063 char *realloc_string_sub(char *string,
1064                         const char *pattern,
1065                         const char *insert)
1066 {
1067         return realloc_string_sub2(string, pattern, insert, true, false);
1068 }
1069
1070 /*
1071  * Internal guts of talloc_string_sub and talloc_all_string_sub.
1072  * talloc version of string_sub2.
1073  */
1074
1075 char *talloc_string_sub2(TALLOC_CTX *mem_ctx, const char *src,
1076                         const char *pattern,
1077                         const char *insert,
1078                         bool remove_unsafe_characters,
1079                         bool replace_once,
1080                         bool allow_trailing_dollar)
1081 {
1082         char *p, *in;
1083         char *s;
1084         char *string;
1085         ssize_t ls,lp,li,ld, i;
1086
1087         if (!insert || !pattern || !*pattern || !src) {
1088                 return NULL;
1089         }
1090
1091         string = talloc_strdup(mem_ctx, src);
1092         if (string == NULL) {
1093                 DEBUG(0, ("talloc_string_sub2: "
1094                         "talloc_strdup failed\n"));
1095                 return NULL;
1096         }
1097
1098         s = string;
1099
1100         in = SMB_STRDUP(insert);
1101         if (!in) {
1102                 DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
1103                 return NULL;
1104         }
1105         ls = (ssize_t)strlen(s);
1106         lp = (ssize_t)strlen(pattern);
1107         li = (ssize_t)strlen(insert);
1108         ld = li - lp;
1109
1110         for (i=0;i<li;i++) {
1111                 switch (in[i]) {
1112                         case '`':
1113                         case '"':
1114                         case '\'':
1115                         case ';':
1116                         case '$':
1117                                 /* allow a trailing $
1118                                  * (as in machine accounts) */
1119                                 if (allow_trailing_dollar && (i == li - 1 )) {
1120                                         break;
1121                                 }
1122                         case '%':
1123                         case '\r':
1124                         case '\n':
1125                                 if (remove_unsafe_characters) {
1126                                         in[i] = '_';
1127                                         break;
1128                                 }
1129                         default:
1130                                 /* ok */
1131                                 break;
1132                 }
1133         }
1134
1135         while ((p = strstr_m(s,pattern))) {
1136                 if (ld > 0) {
1137                         int offset = PTR_DIFF(s,string);
1138                         string = (char *)TALLOC_REALLOC(mem_ctx, string,
1139                                                         ls + ld + 1);
1140                         if (!string) {
1141                                 DEBUG(0, ("talloc_string_sub: out of "
1142                                           "memory!\n"));
1143                                 SAFE_FREE(in);
1144                                 return NULL;
1145                         }
1146                         p = string + offset + (p - s);
1147                 }
1148                 if (li != lp) {
1149                         memmove(p+li,p+lp,strlen(p+lp)+1);
1150                 }
1151                 memcpy(p, in, li);
1152                 s = p + li;
1153                 ls += ld;
1154
1155                 if (replace_once) {
1156                         break;
1157                 }
1158         }
1159         SAFE_FREE(in);
1160         return string;
1161 }
1162
1163 /* Same as string_sub, but returns a talloc'ed string */
1164
1165 char *talloc_string_sub(TALLOC_CTX *mem_ctx,
1166                         const char *src,
1167                         const char *pattern,
1168                         const char *insert)
1169 {
1170         return talloc_string_sub2(mem_ctx, src, pattern, insert,
1171                         true, false, false);
1172 }
1173
1174 /**
1175  Similar to string_sub() but allows for any character to be substituted.
1176  Use with caution!
1177  if len==0 then the string cannot be extended. This is different from the old
1178  use of len==0 which was for no length checks to be done.
1179 **/
1180
1181 void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
1182 {
1183         char *p;
1184         ssize_t ls,lp,li;
1185
1186         if (!insert || !pattern || !s)
1187                 return;
1188
1189         ls = (ssize_t)strlen(s);
1190         lp = (ssize_t)strlen(pattern);
1191         li = (ssize_t)strlen(insert);
1192
1193         if (!*pattern)
1194                 return;
1195
1196         if (len == 0)
1197                 len = ls + 1; /* len is number of *bytes* */
1198
1199         while (lp <= ls && (p = strstr_m(s,pattern))) {
1200                 if (ls + (li-lp) >= len) {
1201                         DEBUG(0,("ERROR: string overflow by "
1202                                 "%d in all_string_sub(%.50s, %d)\n",
1203                                  (int)(ls + (li-lp) - len),
1204                                  pattern, (int)len));
1205                         break;
1206                 }
1207                 if (li != lp) {
1208                         memmove(p+li,p+lp,strlen(p+lp)+1);
1209                 }
1210                 memcpy(p, insert, li);
1211                 s = p + li;
1212                 ls += (li-lp);
1213         }
1214 }
1215
1216 char *talloc_all_string_sub(TALLOC_CTX *ctx,
1217                                 const char *src,
1218                                 const char *pattern,
1219                                 const char *insert)
1220 {
1221         return talloc_string_sub2(ctx, src, pattern, insert,
1222                         false, false, false);
1223 }
1224
1225 /**
1226  Write an octal as a string.
1227 **/
1228
1229 char *octal_string(int i)
1230 {
1231         char *result;
1232         if (i == -1) {
1233                 result = talloc_strdup(talloc_tos(), "-1");
1234         }
1235         else {
1236                 result = talloc_asprintf(talloc_tos(), "0%o", i);
1237         }
1238         SMB_ASSERT(result != NULL);
1239         return result;
1240 }
1241
1242
1243 /**
1244  Truncate a string at a specified length.
1245 **/
1246
1247 char *string_truncate(char *s, unsigned int length)
1248 {
1249         if (s && strlen(s) > length)
1250                 s[length] = 0;
1251         return s;
1252 }
1253
1254 /**
1255  Strchr and strrchr_m are very hard to do on general multi-byte strings.
1256  We convert via ucs2 for now.
1257 **/
1258
1259 char *strchr_m(const char *src, char c)
1260 {
1261         smb_ucs2_t *ws = NULL;
1262         char *s2 = NULL;
1263         smb_ucs2_t *p;
1264         const char *s;
1265         char *ret;
1266         size_t converted_size;
1267
1268         /* characters below 0x3F are guaranteed to not appear in
1269            non-initial position in multi-byte charsets */
1270         if ((c & 0xC0) == 0) {
1271                 return strchr(src, c);
1272         }
1273
1274         /* this is quite a common operation, so we want it to be
1275            fast. We optimise for the ascii case, knowing that all our
1276            supported multi-byte character sets are ascii-compatible
1277            (ie. they match for the first 128 chars) */
1278
1279         for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
1280                 if (*s == c)
1281                         return (char *)s;
1282         }
1283
1284         if (!*s)
1285                 return NULL;
1286
1287 #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
1288         /* With compose characters we must restart from the beginning. JRA. */
1289         s = src;
1290 #endif
1291
1292         if (!push_ucs2_allocate(&ws, s, &converted_size)) {
1293                 /* Wrong answer, but what can we do... */
1294                 return strchr(src, c);
1295         }
1296         p = strchr_w(ws, UCS2_CHAR(c));
1297         if (!p) {
1298                 SAFE_FREE(ws);
1299                 return NULL;
1300         }
1301         *p = 0;
1302         if (!pull_ucs2_allocate(&s2, ws, &converted_size)) {
1303                 SAFE_FREE(ws);
1304                 /* Wrong answer, but what can we do... */
1305                 return strchr(src, c);
1306         }
1307         ret = (char *)(s+strlen(s2));
1308         SAFE_FREE(ws);
1309         SAFE_FREE(s2);
1310         return ret;
1311 }
1312
1313 char *strrchr_m(const char *s, char c)
1314 {
1315         /* characters below 0x3F are guaranteed to not appear in
1316            non-initial position in multi-byte charsets */
1317         if ((c & 0xC0) == 0) {
1318                 return strrchr(s, c);
1319         }
1320
1321         /* this is quite a common operation, so we want it to be
1322            fast. We optimise for the ascii case, knowing that all our
1323            supported multi-byte character sets are ascii-compatible
1324            (ie. they match for the first 128 chars). Also, in Samba
1325            we only search for ascii characters in 'c' and that
1326            in all mb character sets with a compound character
1327            containing c, if 'c' is not a match at position
1328            p, then p[-1] > 0x7f. JRA. */
1329
1330         {
1331                 size_t len = strlen(s);
1332                 const char *cp = s;
1333                 bool got_mb = false;
1334
1335                 if (len == 0)
1336                         return NULL;
1337                 cp += (len - 1);
1338                 do {
1339                         if (c == *cp) {
1340                                 /* Could be a match. Part of a multibyte ? */
1341                                 if ((cp > s) &&
1342                                         (((unsigned char)cp[-1]) & 0x80)) {
1343                                         /* Yep - go slow :-( */
1344                                         got_mb = true;
1345                                         break;
1346                                 }
1347                                 /* No - we have a match ! */
1348                                 return (char *)cp;
1349                         }
1350                 } while (cp-- != s);
1351                 if (!got_mb)
1352                         return NULL;
1353         }
1354
1355         /* String contained a non-ascii char. Slow path. */
1356         {
1357                 smb_ucs2_t *ws = NULL;
1358                 char *s2 = NULL;
1359                 smb_ucs2_t *p;
1360                 char *ret;
1361                 size_t converted_size;
1362
1363                 if (!push_ucs2_allocate(&ws, s, &converted_size)) {
1364                         /* Wrong answer, but what can we do. */
1365                         return strrchr(s, c);
1366                 }
1367                 p = strrchr_w(ws, UCS2_CHAR(c));
1368                 if (!p) {
1369                         SAFE_FREE(ws);
1370                         return NULL;
1371                 }
1372                 *p = 0;
1373                 if (!pull_ucs2_allocate(&s2, ws, &converted_size)) {
1374                         SAFE_FREE(ws);
1375                         /* Wrong answer, but what can we do. */
1376                         return strrchr(s, c);
1377                 }
1378                 ret = (char *)(s+strlen(s2));
1379                 SAFE_FREE(ws);
1380                 SAFE_FREE(s2);
1381                 return ret;
1382         }
1383 }
1384
1385 /***********************************************************************
1386  Return the equivalent of doing strrchr 'n' times - always going
1387  backwards.
1388 ***********************************************************************/
1389
1390 char *strnrchr_m(const char *s, char c, unsigned int n)
1391 {
1392         smb_ucs2_t *ws = NULL;
1393         char *s2 = NULL;
1394         smb_ucs2_t *p;
1395         char *ret;
1396         size_t converted_size;
1397
1398         if (!push_ucs2_allocate(&ws, s, &converted_size)) {
1399                 /* Too hard to try and get right. */
1400                 return NULL;
1401         }
1402         p = strnrchr_w(ws, UCS2_CHAR(c), n);
1403         if (!p) {
1404                 SAFE_FREE(ws);
1405                 return NULL;
1406         }
1407         *p = 0;
1408         if (!pull_ucs2_allocate(&s2, ws, &converted_size)) {
1409                 SAFE_FREE(ws);
1410                 /* Too hard to try and get right. */
1411                 return NULL;
1412         }
1413         ret = (char *)(s+strlen(s2));
1414         SAFE_FREE(ws);
1415         SAFE_FREE(s2);
1416         return ret;
1417 }
1418
1419 /***********************************************************************
1420  strstr_m - We convert via ucs2 for now.
1421 ***********************************************************************/
1422
1423 char *strstr_m(const char *src, const char *findstr)
1424 {
1425         smb_ucs2_t *p;
1426         smb_ucs2_t *src_w, *find_w;
1427         const char *s;
1428         char *s2;
1429         char *retp;
1430
1431         size_t converted_size, findstr_len = 0;
1432
1433         /* for correctness */
1434         if (!findstr[0]) {
1435                 return (char*)src;
1436         }
1437
1438         /* Samba does single character findstr calls a *lot*. */
1439         if (findstr[1] == '\0')
1440                 return strchr_m(src, *findstr);
1441
1442         /* We optimise for the ascii case, knowing that all our
1443            supported multi-byte character sets are ascii-compatible
1444            (ie. they match for the first 128 chars) */
1445
1446         for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
1447                 if (*s == *findstr) {
1448                         if (!findstr_len)
1449                                 findstr_len = strlen(findstr);
1450
1451                         if (strncmp(s, findstr, findstr_len) == 0) {
1452                                 return (char *)s;
1453                         }
1454                 }
1455         }
1456
1457         if (!*s)
1458                 return NULL;
1459
1460 #if 1 /* def BROKEN_UNICODE_COMPOSE_CHARACTERS */
1461         /* 'make check' fails unless we do this */
1462
1463         /* With compose characters we must restart from the beginning. JRA. */
1464         s = src;
1465 #endif
1466
1467         if (!push_ucs2_allocate(&src_w, src, &converted_size)) {
1468                 DEBUG(0,("strstr_m: src malloc fail\n"));
1469                 return NULL;
1470         }
1471
1472         if (!push_ucs2_allocate(&find_w, findstr, &converted_size)) {
1473                 SAFE_FREE(src_w);
1474                 DEBUG(0,("strstr_m: find malloc fail\n"));
1475                 return NULL;
1476         }
1477
1478         p = strstr_w(src_w, find_w);
1479
1480         if (!p) {
1481                 SAFE_FREE(src_w);
1482                 SAFE_FREE(find_w);
1483                 return NULL;
1484         }
1485
1486         *p = 0;
1487         if (!pull_ucs2_allocate(&s2, src_w, &converted_size)) {
1488                 SAFE_FREE(src_w);
1489                 SAFE_FREE(find_w);
1490                 DEBUG(0,("strstr_m: dest malloc fail\n"));
1491                 return NULL;
1492         }
1493         retp = (char *)(s+strlen(s2));
1494         SAFE_FREE(src_w);
1495         SAFE_FREE(find_w);
1496         SAFE_FREE(s2);
1497         return retp;
1498 }
1499
1500 /**
1501  Convert a string to lower case.
1502 **/
1503
1504 void strlower_m(char *s)
1505 {
1506         size_t len;
1507         int errno_save;
1508
1509         /* this is quite a common operation, so we want it to be
1510            fast. We optimise for the ascii case, knowing that all our
1511            supported multi-byte character sets are ascii-compatible
1512            (ie. they match for the first 128 chars) */
1513
1514         while (*s && !(((unsigned char)s[0]) & 0x80)) {
1515                 *s = tolower_ascii((unsigned char)*s);
1516                 s++;
1517         }
1518
1519         if (!*s)
1520                 return;
1521
1522         /* I assume that lowercased string takes the same number of bytes
1523          * as source string even in UTF-8 encoding. (VIV) */
1524         len = strlen(s) + 1;
1525         errno_save = errno;
1526         errno = 0;
1527         unix_strlower(s,len,s,len);
1528         /* Catch mb conversion errors that may not terminate. */
1529         if (errno)
1530                 s[len-1] = '\0';
1531         errno = errno_save;
1532 }
1533
1534 /**
1535  Convert a string to upper case.
1536 **/
1537
1538 void strupper_m(char *s)
1539 {
1540         size_t len;
1541         int errno_save;
1542
1543         /* this is quite a common operation, so we want it to be
1544            fast. We optimise for the ascii case, knowing that all our
1545            supported multi-byte character sets are ascii-compatible
1546            (ie. they match for the first 128 chars) */
1547
1548         while (*s && !(((unsigned char)s[0]) & 0x80)) {
1549                 *s = toupper_ascii_fast((unsigned char)*s);
1550                 s++;
1551         }
1552
1553         if (!*s)
1554                 return;
1555
1556         /* I assume that lowercased string takes the same number of bytes
1557          * as source string even in multibyte encoding. (VIV) */
1558         len = strlen(s) + 1;
1559         errno_save = errno;
1560         errno = 0;
1561         unix_strupper(s,len,s,len);
1562         /* Catch mb conversion errors that may not terminate. */
1563         if (errno)
1564                 s[len-1] = '\0';
1565         errno = errno_save;
1566 }
1567
1568 /**
1569  Count the number of UCS2 characters in a string. Normally this will
1570  be the same as the number of bytes in a string for single byte strings,
1571  but will be different for multibyte.
1572 **/
1573
1574 size_t strlen_m(const char *s)
1575 {
1576         size_t count = 0;
1577
1578         if (!s) {
1579                 return 0;
1580         }
1581
1582         while (*s && !(((uint8_t)*s) & 0x80)) {
1583                 s++;
1584                 count++;
1585         }
1586
1587         if (!*s) {
1588                 return count;
1589         }
1590
1591         while (*s) {
1592                 size_t c_size;
1593                 codepoint_t c = next_codepoint(s, &c_size);
1594                 if (c < 0x10000) {
1595                         /* Unicode char fits into 16 bits. */
1596                         count += 1;
1597                 } else {
1598                         /* Double-width unicode char - 32 bits. */
1599                         count += 2;
1600                 }
1601                 s += c_size;
1602         }
1603
1604         return count;
1605 }
1606
1607 /**
1608  Count the number of UCS2 characters in a string including the null
1609  terminator.
1610 **/
1611
1612 size_t strlen_m_term(const char *s)
1613 {
1614         if (!s) {
1615                 return 0;
1616         }
1617         return strlen_m(s) + 1;
1618 }
1619
1620 /*
1621  * Weird helper routine for the winreg pipe: If nothing is around, return 0,
1622  * if a string is there, include the terminator.
1623  */
1624
1625 size_t strlen_m_term_null(const char *s)
1626 {
1627         size_t len;
1628         if (!s) {
1629                 return 0;
1630         }
1631         len = strlen_m(s);
1632         if (len == 0) {
1633                 return 0;
1634         }
1635
1636         return len+1;
1637 }
1638 /**
1639  Return a RFC2254 binary string representation of a buffer.
1640  Used in LDAP filters.
1641  Caller must free.
1642 **/
1643
1644 char *binary_string_rfc2254(char *buf, int len)
1645 {
1646         char *s;
1647         int i, j;
1648         const char *hex = "0123456789ABCDEF";
1649         s = (char *)SMB_MALLOC(len * 3 + 1);
1650         if (!s)
1651                 return NULL;
1652         for (j=i=0;i<len;i++) {
1653                 s[j] = '\\';
1654                 s[j+1] = hex[((unsigned char)buf[i]) >> 4];
1655                 s[j+2] = hex[((unsigned char)buf[i]) & 0xF];
1656                 j += 3;
1657         }
1658         s[j] = 0;
1659         return s;
1660 }
1661
1662 char *binary_string(char *buf, int len)
1663 {
1664         char *s;
1665         int i, j;
1666         const char *hex = "0123456789ABCDEF";
1667         s = (char *)SMB_MALLOC(len * 2 + 1);
1668         if (!s)
1669                 return NULL;
1670         for (j=i=0;i<len;i++) {
1671                 s[j]   = hex[((unsigned char)buf[i]) >> 4];
1672                 s[j+1] = hex[((unsigned char)buf[i]) & 0xF];
1673                 j += 2;
1674         }
1675         s[j] = 0;
1676         return s;
1677 }
1678
1679 /**
1680  Just a typesafety wrapper for snprintf into a fstring.
1681 **/
1682
1683 int fstr_sprintf(fstring s, const char *fmt, ...)
1684 {
1685         va_list ap;
1686         int ret;
1687
1688         va_start(ap, fmt);
1689         ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
1690         va_end(ap);
1691         return ret;
1692 }
1693
1694 /**
1695  List of Strings manipulation functions
1696 **/
1697
1698 #define S_LIST_ABS 16 /* List Allocation Block Size */
1699
1700 /******************************************************************************
1701  version of standard_sub_basic() for string lists; uses talloc_sub_basic()
1702  for the work
1703  *****************************************************************************/
1704
1705 bool str_list_sub_basic( char **list, const char *smb_name,
1706                          const char *domain_name )
1707 {
1708         TALLOC_CTX *ctx = list;
1709         char *s, *tmpstr;
1710
1711         while ( *list ) {
1712                 s = *list;
1713                 tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
1714                 if ( !tmpstr ) {
1715                         DEBUG(0,("str_list_sub_basic: "
1716                                 "alloc_sub_basic() return NULL!\n"));
1717                         return false;
1718                 }
1719
1720                 TALLOC_FREE(*list);
1721                 *list = tmpstr;
1722
1723                 list++;
1724         }
1725
1726         return true;
1727 }
1728
1729 /******************************************************************************
1730  substritute a specific pattern in a string list
1731  *****************************************************************************/
1732
1733 bool str_list_substitute(char **list, const char *pattern, const char *insert)
1734 {
1735         TALLOC_CTX *ctx = list;
1736         char *p, *s, *t;
1737         ssize_t ls, lp, li, ld, i, d;
1738
1739         if (!list)
1740                 return false;
1741         if (!pattern)
1742                 return false;
1743         if (!insert)
1744                 return false;
1745
1746         lp = (ssize_t)strlen(pattern);
1747         li = (ssize_t)strlen(insert);
1748         ld = li -lp;
1749
1750         while (*list) {
1751                 s = *list;
1752                 ls = (ssize_t)strlen(s);
1753
1754                 while ((p = strstr_m(s, pattern))) {
1755                         t = *list;
1756                         d = p -t;
1757                         if (ld) {
1758                                 t = TALLOC_ARRAY(ctx, char, ls +ld +1);
1759                                 if (!t) {
1760                                         DEBUG(0,("str_list_substitute: "
1761                                                 "Unable to allocate memory"));
1762                                         return false;
1763                                 }
1764                                 memcpy(t, *list, d);
1765                                 memcpy(t +d +li, p +lp, ls -d -lp +1);
1766                                 TALLOC_FREE(*list);
1767                                 *list = t;
1768                                 ls += ld;
1769                                 s = t +d +li;
1770                         }
1771
1772                         for (i = 0; i < li; i++) {
1773                                 switch (insert[i]) {
1774                                         case '`':
1775                                         case '"':
1776                                         case '\'':
1777                                         case ';':
1778                                         case '$':
1779                                         case '%':
1780                                         case '\r':
1781                                         case '\n':
1782                                                 t[d +i] = '_';
1783                                                 break;
1784                                         default:
1785                                                 t[d +i] = insert[i];
1786                                 }
1787                         }
1788                 }
1789
1790                 list++;
1791         }
1792
1793         return true;
1794 }
1795
1796
1797 #define IPSTR_LIST_SEP  ","
1798 #define IPSTR_LIST_CHAR ','
1799
1800 /**
1801  * Add ip string representation to ipstr list. Used also
1802  * as part of @function ipstr_list_make
1803  *
1804  * @param ipstr_list pointer to string containing ip list;
1805  *        MUST BE already allocated and IS reallocated if necessary
1806  * @param ipstr_size pointer to current size of ipstr_list (might be changed
1807  *        as a result of reallocation)
1808  * @param ip IP address which is to be added to list
1809  * @return pointer to string appended with new ip and possibly
1810  *         reallocated to new length
1811  **/
1812
1813 static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service)
1814 {
1815         char *new_ipstr = NULL;
1816         char addr_buf[INET6_ADDRSTRLEN];
1817         int ret;
1818
1819         /* arguments checking */
1820         if (!ipstr_list || !service) {
1821                 return NULL;
1822         }
1823
1824         print_sockaddr(addr_buf,
1825                         sizeof(addr_buf),
1826                         &service->ss);
1827
1828         /* attempt to convert ip to a string and append colon separator to it */
1829         if (*ipstr_list) {
1830                 if (service->ss.ss_family == AF_INET) {
1831                         /* IPv4 */
1832                         ret = asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list,
1833                                        IPSTR_LIST_SEP, addr_buf,
1834                                        service->port);
1835                 } else {
1836                         /* IPv6 */
1837                         ret = asprintf(&new_ipstr, "%s%s[%s]:%d", *ipstr_list,
1838                                        IPSTR_LIST_SEP, addr_buf,
1839                                        service->port);
1840                 }
1841                 SAFE_FREE(*ipstr_list);
1842         } else {
1843                 if (service->ss.ss_family == AF_INET) {
1844                         /* IPv4 */
1845                         ret = asprintf(&new_ipstr, "%s:%d", addr_buf,
1846                                        service->port);
1847                 } else {
1848                         /* IPv6 */
1849                         ret = asprintf(&new_ipstr, "[%s]:%d", addr_buf,
1850                                        service->port);
1851                 }
1852         }
1853         if (ret == -1) {
1854                 return NULL;
1855         }
1856         *ipstr_list = new_ipstr;
1857         return *ipstr_list;
1858 }
1859
1860 /**
1861  * Allocate and initialise an ipstr list using ip adresses
1862  * passed as arguments.
1863  *
1864  * @param ipstr_list pointer to string meant to be allocated and set
1865  * @param ip_list array of ip addresses to place in the list
1866  * @param ip_count number of addresses stored in ip_list
1867  * @return pointer to allocated ip string
1868  **/
1869
1870 char *ipstr_list_make(char **ipstr_list,
1871                         const struct ip_service *ip_list,
1872                         int ip_count)
1873 {
1874         int i;
1875
1876         /* arguments checking */
1877         if (!ip_list || !ipstr_list) {
1878                 return 0;
1879         }
1880
1881         *ipstr_list = NULL;
1882
1883         /* process ip addresses given as arguments */
1884         for (i = 0; i < ip_count; i++) {
1885                 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
1886         }
1887
1888         return (*ipstr_list);
1889 }
1890
1891
1892 /**
1893  * Parse given ip string list into array of ip addresses
1894  * (as ip_service structures)
1895  *    e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ...
1896  *
1897  * @param ipstr ip string list to be parsed
1898  * @param ip_list pointer to array of ip addresses which is
1899  *        allocated by this function and must be freed by caller
1900  * @return number of successfully parsed addresses
1901  **/
1902
1903 int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list)
1904 {
1905         TALLOC_CTX *frame;
1906         char *token_str = NULL;
1907         size_t count;
1908         int i;
1909
1910         if (!ipstr_list || !ip_list)
1911                 return 0;
1912
1913         count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1;
1914         if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) {
1915                 DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n",
1916                                         (unsigned long)count));
1917                 return 0;
1918         }
1919
1920         frame = talloc_stackframe();
1921         for ( i=0; next_token_talloc(frame, &ipstr_list, &token_str,
1922                                 IPSTR_LIST_SEP) && i<count; i++ ) {
1923                 char *s = token_str;
1924                 char *p = strrchr(token_str, ':');
1925
1926                 if (p) {
1927                         *p = 0;
1928                         (*ip_list)[i].port = atoi(p+1);
1929                 }
1930
1931                 /* convert single token to ip address */
1932                 if (token_str[0] == '[') {
1933                         /* IPv6 address. */
1934                         s++;
1935                         p = strchr(token_str, ']');
1936                         if (!p) {
1937                                 continue;
1938                         }
1939                         *p = '\0';
1940                 }
1941                 if (!interpret_string_addr(&(*ip_list)[i].ss,
1942                                         s,
1943                                         AI_NUMERICHOST)) {
1944                         continue;
1945                 }
1946         }
1947         TALLOC_FREE(frame);
1948         return count;
1949 }
1950
1951 /**
1952  * Safely free ip string list
1953  *
1954  * @param ipstr_list ip string list to be freed
1955  **/
1956
1957 void ipstr_list_free(char* ipstr_list)
1958 {
1959         SAFE_FREE(ipstr_list);
1960 }
1961
1962 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1963
1964 /**
1965  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
1966  **/
1967 DATA_BLOB base64_decode_data_blob(const char *s)
1968 {
1969         int bit_offset, byte_offset, idx, i, n;
1970         DATA_BLOB decoded = data_blob(s, strlen(s)+1);
1971         unsigned char *d = decoded.data;
1972         char *p;
1973
1974         n=i=0;
1975
1976         while (*s && (p=strchr_m(b64,*s))) {
1977                 idx = (int)(p - b64);
1978                 byte_offset = (i*6)/8;
1979                 bit_offset = (i*6)%8;
1980                 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
1981                 if (bit_offset < 3) {
1982                         d[byte_offset] |= (idx << (2-bit_offset));
1983                         n = byte_offset+1;
1984                 } else {
1985                         d[byte_offset] |= (idx >> (bit_offset-2));
1986                         d[byte_offset+1] = 0;
1987                         d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
1988                         n = byte_offset+2;
1989                 }
1990                 s++; i++;
1991         }
1992
1993         if ((n > 0) && (*s == '=')) {
1994                 n -= 1;
1995         }
1996
1997         /* fix up length */
1998         decoded.length = n;
1999         return decoded;
2000 }
2001
2002 /**
2003  * Decode a base64 string in-place - wrapper for the above
2004  **/
2005 void base64_decode_inplace(char *s)
2006 {
2007         DATA_BLOB decoded = base64_decode_data_blob(s);
2008
2009         if ( decoded.length != 0 ) {
2010                 memcpy(s, decoded.data, decoded.length);
2011
2012                 /* null terminate */
2013                 s[decoded.length] = '\0';
2014         } else {
2015                 *s = '\0';
2016         }
2017
2018         data_blob_free(&decoded);
2019 }
2020
2021 /**
2022  * Encode a base64 string into a talloc()ed string caller to free.
2023  *
2024  * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
2025  * with adjustments
2026  **/
2027
2028 char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
2029 {
2030         int bits = 0;
2031         int char_count = 0;
2032         size_t out_cnt, len, output_len;
2033         char *result;
2034
2035         if (!data.length || !data.data)
2036                 return NULL;
2037
2038         out_cnt = 0;
2039         len = data.length;
2040         output_len = data.length * 2 + 4; /* Account for closing bytes. 4 is
2041                                            * random but should be enough for
2042                                            * the = and \0 */
2043         result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */
2044         SMB_ASSERT(result != NULL);
2045
2046         while (len-- && out_cnt < (data.length * 2) - 5) {
2047                 int c = (unsigned char) *(data.data++);
2048                 bits += c;
2049                 char_count++;
2050                 if (char_count == 3) {
2051                         result[out_cnt++] = b64[bits >> 18];
2052                         result[out_cnt++] = b64[(bits >> 12) & 0x3f];
2053                         result[out_cnt++] = b64[(bits >> 6) & 0x3f];
2054                         result[out_cnt++] = b64[bits & 0x3f];
2055                         bits = 0;
2056                         char_count = 0;
2057                 } else {
2058                         bits <<= 8;
2059                 }
2060         }
2061         if (char_count != 0) {
2062                 bits <<= 16 - (8 * char_count);
2063                 result[out_cnt++] = b64[bits >> 18];
2064                 result[out_cnt++] = b64[(bits >> 12) & 0x3f];
2065                 if (char_count == 1) {
2066                         result[out_cnt++] = '=';
2067                         result[out_cnt++] = '=';
2068                 } else {
2069                         result[out_cnt++] = b64[(bits >> 6) & 0x3f];
2070                         result[out_cnt++] = '=';
2071                 }
2072         }
2073         result[out_cnt] = '\0'; /* terminate */
2074         return result;
2075 }
2076
2077 /* read a SMB_BIG_UINT from a string */
2078 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
2079 {
2080
2081         uint64_t val = -1;
2082         const char *p = nptr;
2083
2084         if (!p) {
2085                 if (entptr) {
2086                         *entptr = p;
2087                 }
2088                 return val;
2089         }
2090
2091         while (*p && isspace(*p))
2092                 p++;
2093
2094         sscanf(p,"%"PRIu64,&val);
2095         if (entptr) {
2096                 while (*p && isdigit(*p))
2097                         p++;
2098                 *entptr = p;
2099         }
2100
2101         return val;
2102 }
2103
2104 /* Convert a size specification to a count of bytes. We accept the following
2105  * suffixes:
2106  *          bytes if there is no suffix
2107  *      kK  kibibytes
2108  *      mM  mebibytes
2109  *      gG  gibibytes
2110  *      tT  tibibytes
2111  *      pP  whatever the ISO name for petabytes is
2112  *
2113  *  Returns 0 if the string can't be converted.
2114  */
2115 SMB_OFF_T conv_str_size(const char * str)
2116 {
2117         SMB_OFF_T lval;
2118         char * end;
2119
2120         if (str == NULL || *str == '\0') {
2121                 return 0;
2122         }
2123
2124 #ifdef HAVE_STRTOULL
2125         if (sizeof(SMB_OFF_T) == 8) {
2126             lval = strtoull(str, &end, 10 /* base */);
2127         } else {
2128             lval = strtoul(str, &end, 10 /* base */);
2129         }
2130 #else
2131         lval = strtoul(str, &end, 10 /* base */);
2132 #endif
2133
2134         if (end == NULL || end == str) {
2135                 return 0;
2136         }
2137
2138         if (*end) {
2139                 SMB_OFF_T lval_orig = lval;
2140
2141                 if (strwicmp(end, "K") == 0) {
2142                         lval *= (SMB_OFF_T)1024;
2143                 } else if (strwicmp(end, "M") == 0) {
2144                         lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024);
2145                 } else if (strwicmp(end, "G") == 0) {
2146                         lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2147                                 (SMB_OFF_T)1024);
2148                 } else if (strwicmp(end, "T") == 0) {
2149                         lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2150                                 (SMB_OFF_T)1024 * (SMB_OFF_T)1024);
2151                 } else if (strwicmp(end, "P") == 0) {
2152                         lval *= ((SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2153                                 (SMB_OFF_T)1024 * (SMB_OFF_T)1024 *
2154                                 (SMB_OFF_T)1024);
2155                 } else {
2156                         return 0;
2157                 }
2158
2159                 /* Primitive attempt to detect wrapping on platforms with
2160                  * 4-byte SMB_OFF_T. It's better to let the caller handle
2161                  * a failure than some random number.
2162                  */
2163                 if (lval_orig <= lval) {
2164                         return 0;
2165                 }
2166         }
2167
2168         return lval;
2169 }
2170
2171 void string_append(char **left, const char *right)
2172 {
2173         int new_len = strlen(right) + 1;
2174
2175         if (*left == NULL) {
2176                 *left = (char *)SMB_MALLOC(new_len);
2177                 *left[0] = '\0';
2178         } else {
2179                 new_len += strlen(*left);
2180                 *left = (char *)SMB_REALLOC(*left, new_len);
2181         }
2182
2183         if (*left == NULL) {
2184                 return;
2185         }
2186
2187         safe_strcat(*left, right, new_len-1);
2188 }
2189
2190 bool add_string_to_array(TALLOC_CTX *mem_ctx,
2191                          const char *str, const char ***strings,
2192                          int *num)
2193 {
2194         char *dup_str = talloc_strdup(mem_ctx, str);
2195
2196         *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings,
2197                         const char *, (*num)+1);
2198
2199         if ((*strings == NULL) || (dup_str == NULL)) {
2200                 *num = 0;
2201                 return false;
2202         }
2203
2204         (*strings)[*num] = dup_str;
2205         *num += 1;
2206         return true;
2207 }
2208
2209 /* Append an sprintf'ed string. Double buffer size on demand. Usable without
2210  * error checking in between. The indiation that something weird happened is
2211  * string==NULL */
2212
2213 void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len,
2214                     size_t *bufsize, const char *fmt, ...)
2215 {
2216         va_list ap;
2217         char *newstr;
2218         int ret;
2219         bool increased;
2220
2221         /* len<0 is an internal marker that something failed */
2222         if (*len < 0)
2223                 goto error;
2224
2225         if (*string == NULL) {
2226                 if (*bufsize == 0)
2227                         *bufsize = 128;
2228
2229                 *string = TALLOC_ARRAY(mem_ctx, char, *bufsize);
2230                 if (*string == NULL)
2231                         goto error;
2232         }
2233
2234         va_start(ap, fmt);
2235         ret = vasprintf(&newstr, fmt, ap);
2236         va_end(ap);
2237
2238         if (ret < 0)
2239                 goto error;
2240
2241         increased = false;
2242
2243         while ((*len)+ret >= *bufsize) {
2244                 increased = true;
2245                 *bufsize *= 2;
2246                 if (*bufsize >= (1024*1024*256))
2247                         goto error;
2248         }
2249
2250         if (increased) {
2251                 *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char,
2252                                                *bufsize);
2253                 if (*string == NULL) {
2254                         goto error;
2255                 }
2256         }
2257
2258         StrnCpy((*string)+(*len), newstr, ret);
2259         (*len) += ret;
2260         free(newstr);
2261         return;
2262
2263  error:
2264         *len = -1;
2265         *string = NULL;
2266 }
2267
2268 /*
2269  * asprintf into a string and strupper_m it after that.
2270  */
2271
2272 int asprintf_strupper_m(char **strp, const char *fmt, ...)
2273 {
2274         va_list ap;
2275         char *result;
2276         int ret;
2277
2278         va_start(ap, fmt);
2279         ret = vasprintf(&result, fmt, ap);
2280         va_end(ap);
2281
2282         if (ret == -1)
2283                 return -1;
2284
2285         strupper_m(result);
2286         *strp = result;
2287         return ret;
2288 }
2289
2290 char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...)
2291 {
2292         va_list ap;
2293         char *ret;
2294
2295         va_start(ap, fmt);
2296         ret = talloc_vasprintf(t, fmt, ap);
2297         va_end(ap);
2298
2299         if (ret == NULL) {
2300                 return NULL;
2301         }
2302         strupper_m(ret);
2303         return ret;
2304 }
2305
2306 char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...)
2307 {
2308         va_list ap;
2309         char *ret;
2310
2311         va_start(ap, fmt);
2312         ret = talloc_vasprintf(t, fmt, ap);
2313         va_end(ap);
2314
2315         if (ret == NULL) {
2316                 return NULL;
2317         }
2318         strlower_m(ret);
2319         return ret;
2320 }
2321
2322
2323 /*
2324    Returns the substring from src between the first occurrence of
2325    the char "front" and the first occurence of the char "back".
2326    Mallocs the return string which must be freed.  Not for use
2327    with wide character strings.
2328 */
2329 char *sstring_sub(const char *src, char front, char back)
2330 {
2331         char *temp1, *temp2, *temp3;
2332         ptrdiff_t len;
2333
2334         temp1 = strchr(src, front);
2335         if (temp1 == NULL) return NULL;
2336         temp2 = strchr(src, back);
2337         if (temp2 == NULL) return NULL;
2338         len = temp2 - temp1;
2339         if (len <= 0) return NULL;
2340         temp3 = (char*)SMB_MALLOC(len);
2341         if (temp3 == NULL) {
2342                 DEBUG(1,("Malloc failure in sstring_sub\n"));
2343                 return NULL;
2344         }
2345         memcpy(temp3, temp1+1, len-1);
2346         temp3[len-1] = '\0';
2347         return temp3;
2348 }
2349
2350 /********************************************************************
2351  Check a string for any occurrences of a specified list of invalid
2352  characters.
2353 ********************************************************************/
2354
2355 bool validate_net_name( const char *name,
2356                 const char *invalid_chars,
2357                 int max_len)
2358 {
2359         int i;
2360
2361         for ( i=0; i<max_len && name[i]; i++ ) {
2362                 /* fail if strchr_m() finds one of the invalid characters */
2363                 if ( name[i] && strchr_m( invalid_chars, name[i] ) ) {
2364                         return false;
2365                 }
2366         }
2367
2368         return true;
2369 }
2370
2371
2372 /*******************************************************************
2373  Add a shell escape character '\' to any character not in a known list
2374  of characters. UNIX charset format.
2375 *******************************************************************/
2376
2377 #define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
2378 #define INSIDE_DQUOTE_LIST "$`\n\"\\"
2379
2380 char *escape_shell_string(const char *src)
2381 {
2382         size_t srclen = strlen(src);
2383         char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1);
2384         char *dest = ret;
2385         bool in_s_quote = false;
2386         bool in_d_quote = false;
2387         bool next_escaped = false;
2388
2389         if (!ret) {
2390                 return NULL;
2391         }
2392
2393         while (*src) {
2394                 size_t c_size;
2395                 codepoint_t c = next_codepoint(src, &c_size);
2396
2397                 if (c == INVALID_CODEPOINT) {
2398                         SAFE_FREE(ret);
2399                         return NULL;
2400                 }
2401
2402                 if (c_size > 1) {
2403                         memcpy(dest, src, c_size);
2404                         src += c_size;
2405                         dest += c_size;
2406                         next_escaped = false;
2407                         continue;
2408                 }
2409
2410                 /*
2411                  * Deal with backslash escaped state.
2412                  * This only lasts for one character.
2413                  */
2414
2415                 if (next_escaped) {
2416                         *dest++ = *src++;
2417                         next_escaped = false;
2418                         continue;
2419                 }
2420
2421                 /*
2422                  * Deal with single quote state. The
2423                  * only thing we care about is exiting
2424                  * this state.
2425                  */
2426
2427                 if (in_s_quote) {
2428                         if (*src == '\'') {
2429                                 in_s_quote = false;
2430                         }
2431                         *dest++ = *src++;
2432                         continue;
2433                 }
2434
2435                 /*
2436                  * Deal with double quote state. The most
2437                  * complex state. We must cope with \, meaning
2438                  * possibly escape next char (depending what it
2439                  * is), ", meaning exit this state, and possibly
2440                  * add an \ escape to any unprotected character
2441                  * (listed in INSIDE_DQUOTE_LIST).
2442                  */
2443
2444                 if (in_d_quote) {
2445                         if (*src == '\\') {
2446                                 /*
2447                                  * Next character might be escaped.
2448                                  * We have to peek. Inside double
2449                                  * quotes only INSIDE_DQUOTE_LIST
2450                                  * characters are escaped by a \.
2451                                  */
2452
2453                                 char nextchar;
2454
2455                                 c = next_codepoint(&src[1], &c_size);
2456                                 if (c == INVALID_CODEPOINT) {
2457                                         SAFE_FREE(ret);
2458                                         return NULL;
2459                                 }
2460                                 if (c_size > 1) {
2461                                         /*
2462                                          * Don't escape the next char.
2463                                          * Just copy the \.
2464                                          */
2465                                         *dest++ = *src++;
2466                                         continue;
2467                                 }
2468
2469                                 nextchar = src[1];
2470
2471                                 if (nextchar && strchr(INSIDE_DQUOTE_LIST,
2472                                                         (int)nextchar)) {
2473                                         next_escaped = true;
2474                                 }
2475                                 *dest++ = *src++;
2476                                 continue;
2477                         }
2478
2479                         if (*src == '\"') {
2480                                 /* Exit double quote state. */
2481                                 in_d_quote = false;
2482                                 *dest++ = *src++;
2483                                 continue;
2484                         }
2485
2486                         /*
2487                          * We know the character isn't \ or ",
2488                          * so escape it if it's any of the other
2489                          * possible unprotected characters.
2490                          */
2491
2492                         if (strchr(INSIDE_DQUOTE_LIST, (int)*src)) {
2493                                 *dest++ = '\\';
2494                         }
2495                         *dest++ = *src++;
2496                         continue;
2497                 }
2498
2499                 /*
2500                  * From here to the end of the loop we're
2501                  * not in the single or double quote state.
2502                  */
2503
2504                 if (*src == '\\') {
2505                         /* Next character must be escaped. */
2506                         next_escaped = true;
2507                         *dest++ = *src++;
2508                         continue;
2509                 }
2510
2511                 if (*src == '\'') {
2512                         /* Go into single quote state. */
2513                         in_s_quote = true;
2514                         *dest++ = *src++;
2515                         continue;
2516                 }
2517
2518                 if (*src == '\"') {
2519                         /* Go into double quote state. */
2520                         in_d_quote = true;
2521                         *dest++ = *src++;
2522                         continue;
2523                 }
2524
2525                 /* Check if we need to escape the character. */
2526
2527                 if (!strchr(INCLUDE_LIST, (int)*src)) {
2528                         *dest++ = '\\';
2529                 }
2530                 *dest++ = *src++;
2531         }
2532         *dest++ = '\0';
2533         return ret;
2534 }