/
string.c
1980 lines (1622 loc) · 49.7 KB
/
string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************
* Exim - an Internet mail transport agent *
*************************************************/
/* Copyright (c) The Exim Maintainers 2020 - 2023 */
/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Miscellaneous string-handling functions. Some are not required for
utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
#include "exim.h"
#include <assert.h>
#ifndef COMPILE_UTILITY
/*************************************************
* Test for IP address *
*************************************************/
/* This used just to be a regular expression, but with IPv6 things are a bit
more complicated. If the address contains a colon, it is assumed to be a v6
address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present,
and maskptr is not NULL, its offset is placed there.
Arguments:
s a string
maskptr NULL if no mask is permitted to follow
otherwise, points to an int where the offset of '/' is placed
if there is no / followed by trailing digits, *maskptr is set 0
errp NULL if no diagnostic information is required, and if the netmask
length should not be checked. Otherwise it is set pointing to a short
descriptive text.
Returns: 0 if the string is not a textual representation of an IP address
4 if it is an IPv4 address
6 if it is an IPv6 address
The legacy string_is_ip_address() function follows below.
*/
int
string_is_ip_addressX(const uschar * ip_addr, int * maskptr, const uschar ** errp)
{
uschar * slash, * percent, * endp = NULL;
long int mask = 0;
const uschar * addr = NULL;
int af;
union { /* we do not need this, but inet_pton() needs a place for storage */
struct in_addr sa4;
struct in6_addr sa6;
} sa;
/* If there is a slash, but we didn't request a (optional) netmask,
we return failure, as we do if the mask isn't a pure numerical value,
or if it is negative. The actual length is checked later, once we know
the address family. */
if (slash = Ustrchr(ip_addr, '/'))
{
uschar * rest;
if (!maskptr)
{
if (errp) *errp = US"netmask found, but not requested";
return 0;
}
mask = Ustrtol(slash+1, &rest, 10);
if (*rest || mask < 0)
{
if (errp) *errp = US"netmask not numeric or <0";
return 0;
}
*maskptr = slash - ip_addr; /* offset of the slash */
endp = slash;
}
else if (maskptr)
*maskptr = 0; /* no slash found */
/* The interface-ID suffix (%<id>) is optional (for IPv6). If it
exists, we check it syntactically. Later, if we know the address
family is IPv4, we might reject it.
The interface-ID is mutually exclusive with the netmask, to the
best of my knowledge. */
if (percent = Ustrchr(ip_addr, '%'))
{
if (slash)
{
if (errp) *errp = US"interface-ID and netmask are mutually exclusive";
return 0;
}
for (uschar *p = percent+1; *p; p++)
if (!isalnum(*p) && !ispunct(*p))
{
if (errp) *errp = US"interface-ID must match [[:alnum:][:punct:]]";
return 0;
}
endp = percent;
}
/* inet_pton() can't parse netmasks and interface IDs, so work on a shortened copy
allocated on the current stack */
if (endp)
{
ptrdiff_t l = endp - ip_addr;
if (l > 255)
{
if (errp) *errp = US"rudiculous long ip address string";
return 0;
}
addr = string_copyn(ip_addr, l);
}
else
addr = ip_addr;
af = Ustrchr(addr, ':') ? AF_INET6 : AF_INET;
if (!inet_pton(af, CCS addr, &sa))
{
if (errp) *errp = af == AF_INET6 ? US"IP address string not parsable as IPv6"
: US"IP address string not parsable IPv4";
return 0;
}
/* we do not check the values of the mask here, as
this is done on the callers side (but I don't understand why), so
actually I'd like to do it here, but it breaks at least testcase 0002 */
switch (af)
{
case AF_INET6:
if (errp && mask > 128)
{
*errp = US"IPv6 netmask value must not be >128";
return 0;
}
return 6;
case AF_INET:
if (percent)
{
if (errp) *errp = US"IPv4 address string must not have an interface-ID";
return 0;
}
if (errp && mask > 32)
{
*errp = US"IPv4 netmask value must not be >32";
return 0;
}
return 4;
default:
if (errp) *errp = US"unknown address family (should not happen)";
return 0;
}
}
int
string_is_ip_address(const uschar * ip_addr, int * maskptr)
{
return string_is_ip_addressX(ip_addr, maskptr, NULL);
}
#endif /* COMPILE_UTILITY */
/*************************************************
* Format message size *
*************************************************/
/* Convert a message size in bytes to printing form, rounding
according to the magnitude of the number. A value of zero causes
a string of spaces to be returned.
Arguments:
size the message size in bytes
buffer where to put the answer
Returns: pointer to the buffer
a string of exactly 5 characters is normally returned
*/
uschar *
string_format_size(int size, uschar *buffer)
{
if (size == 0) Ustrcpy(buffer, US" ");
else if (size < 1024) sprintf(CS buffer, "%5d", size);
else if (size < 10*1024)
sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
else if (size < 1024*1024)
sprintf(CS buffer, "%4dK", (size + 512)/1024);
else if (size < 10*1024*1024)
sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
else
sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
return buffer;
}
#ifndef COMPILE_UTILITY
/*************************************************
* Convert a number to base 62 format *
*************************************************/
/* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
BASE_62 is actually 36. Always return exactly 6 characters plus a NUL, in a
static area. This is enough for a 32b input, for 62 (for 64b we would want 11+nul);
but with 36 we lose half the input range of a 32b input.
Argument: a long integer
Returns: pointer to base 62 string
*/
uschar *
string_base62_32(unsigned long int value)
{
static uschar yield[7];
uschar * p = yield + sizeof(yield) - 1;
*p = 0;
while (p > yield)
{
*--p = base62_chars[value % BASE_62];
value /= BASE_62;
}
return yield;
}
uschar *
string_base62_64(unsigned long int value)
{
static uschar yield[12];
uschar * p = yield + sizeof(yield) - 1;
*p = '\0';
while (p > yield)
if (value)
{
*--p = base62_chars[value % BASE_62];
value /= BASE_62;
}
else
*--p = '0';
return yield;
}
#endif /* COMPILE_UTILITY */
/*************************************************
* Interpret escape sequence *
*************************************************/
/* This function is called from several places where escape sequences are to be
interpreted in strings.
Arguments:
pp points a pointer to the initiating "\" in the string;
the pointer gets updated to point to the final character
If the backslash is the last character in the string, it
is not interpreted.
Returns: the value of the character escape
*/
int
string_interpret_escape(const uschar **pp)
{
#ifdef COMPILE_UTILITY
const uschar *hex_digits= CUS"0123456789abcdef";
#endif
int ch;
const uschar *p = *pp;
ch = *(++p);
if (ch == '\0') return **pp;
if (isdigit(ch) && ch != '8' && ch != '9')
{
ch -= '0';
if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
{
ch = ch * 8 + *(++p) - '0';
if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
ch = ch * 8 + *(++p) - '0';
}
}
else switch(ch)
{
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'v': ch = '\v'; break;
case 'x':
ch = 0;
if (isxdigit(p[1]))
{
ch = ch * 16 +
Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
if (isxdigit(p[1])) ch = ch * 16 +
Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
}
break;
}
*pp = p;
return ch;
}
#ifndef COMPILE_UTILITY
/*************************************************
* Ensure string is printable *
*************************************************/
/* This function is called for critical strings. It checks for any
non-printing characters, and if any are found, it makes a new copy
of the string with suitable escape sequences. It is most often called by the
macro string_printing(), which sets flags to 0.
Arguments:
s the input string
flags Bit 0: convert tabs. Bit 1: convert spaces.
Returns: string with non-printers encoded as printing sequences
*/
const uschar *
string_printing2(const uschar *s, int flags)
{
int nonprintcount = 0;
int length = 0;
const uschar *t = s;
uschar *ss, *tt;
while (*t)
{
int c = *t++;
if ( !mac_isprint(c)
|| flags & SP_TAB && c == '\t'
|| flags & SP_SPACE && c == ' '
) nonprintcount++;
length++;
}
if (nonprintcount == 0) return s;
/* Get a new block of store guaranteed big enough to hold the
expanded string. */
tt = ss = store_get(length + nonprintcount * 3 + 1, s);
/* Copy everything, escaping non printers. */
for (t = s; *t; )
{
int c = *t;
if ( mac_isprint(c)
&& (!(flags & SP_TAB) || c != '\t')
&& (!(flags & SP_SPACE) || c != ' ')
)
*tt++ = *t++;
else
{
*tt++ = '\\';
switch (*t)
{
case '\n': *tt++ = 'n'; break;
case '\r': *tt++ = 'r'; break;
case '\b': *tt++ = 'b'; break;
case '\v': *tt++ = 'v'; break;
case '\f': *tt++ = 'f'; break;
case '\t': *tt++ = 't'; break;
default: sprintf(CS tt, "%03o", *t); tt += 3; break;
}
t++;
}
}
*tt = 0;
return ss;
}
#endif /* COMPILE_UTILITY */
/*************************************************
* Undo printing escapes in string *
*************************************************/
/* This function is the reverse of string_printing2. It searches for
backslash characters and if any are found, it makes a new copy of the
string with escape sequences parsed. Otherwise it returns the original
string.
Arguments:
s the input string
Returns: string with printing escapes parsed back
*/
uschar *
string_unprinting(uschar *s)
{
uschar *p, *q, *r, *ss;
int len, off;
p = Ustrchr(s, '\\');
if (!p) return s;
len = Ustrlen(s) + 1;
ss = store_get(len, s);
q = ss;
off = p - s;
if (off)
{
memcpy(q, s, off);
q += off;
}
while (*p)
{
if (*p == '\\')
{
*q++ = string_interpret_escape((const uschar **)&p);
p++;
}
else
{
r = Ustrchr(p, '\\');
if (!r)
{
off = Ustrlen(p);
memcpy(q, p, off);
p += off;
q += off;
break;
}
else
{
off = r - p;
memcpy(q, p, off);
q += off;
p = r;
}
}
}
*q = '\0';
return ss;
}
#if (defined(HAVE_LOCAL_SCAN) || defined(EXPAND_DLFUNC)) \
&& !defined(MACRO_PREDEF) && !defined(COMPILE_UTILITY)
/*************************************************
* Copy and save string *
*************************************************/
/*
Argument: string to copy
Returns: copy of string in new store with the same taint status
*/
uschar *
string_copy_function(const uschar * s)
{
return string_copy_taint(s, s);
}
/* As above, but explicitly specifying the result taint status
*/
uschar *
string_copy_taint_function(const uschar * s, const void * proto_mem)
{
return string_copy_taint(s, proto_mem);
}
/*************************************************
* Copy and save string, given length *
*************************************************/
/* It is assumed the data contains no zeros. A zero is added
onto the end.
Arguments:
s string to copy
n number of characters
Returns: copy of string in new store
*/
uschar *
string_copyn_function(const uschar * s, int n)
{
return string_copyn(s, n);
}
#endif
/*************************************************
* Copy and save string in malloc'd store *
*************************************************/
/* This function assumes that memcpy() is faster than strcpy().
Argument: string to copy
Returns: copy of string in new store
*/
uschar *
string_copy_malloc(const uschar * s)
{
int len = Ustrlen(s) + 1;
uschar * ss = store_malloc(len);
memcpy(ss, s, len);
return ss;
}
/*************************************************
* Copy string if long, inserting newlines *
*************************************************/
/* If the given string is longer than 75 characters, it is copied, and within
the copy, certain space characters are converted into newlines.
Argument: pointer to the string
Returns: pointer to the possibly altered string
*/
uschar *
string_split_message(uschar * msg)
{
uschar *s, *ss;
if (!msg || Ustrlen(msg) <= 75) return msg;
s = ss = msg = string_copy(msg);
for (;;)
{
int i = 0;
while (i < 75 && *ss && *ss != '\n') ss++, i++;
if (!*ss) break;
if (*ss == '\n')
s = ++ss;
else
{
uschar * t = ss + 1;
uschar * tt = NULL;
while (--t > s + 35)
{
if (*t == ' ')
{
if (t[-1] == ':') { tt = t; break; }
if (!tt) tt = t;
}
}
if (!tt) /* Can't split behind - try ahead */
{
t = ss + 1;
while (*t)
{
if (*t == ' ' || *t == '\n')
{ tt = t; break; }
t++;
}
}
if (!tt) break; /* Can't find anywhere to split */
*tt = '\n';
s = ss = tt+1;
}
}
return msg;
}
/*************************************************
* Copy returned DNS domain name, de-escaping *
*************************************************/
/* If a domain name contains top-bit characters, some resolvers return
the fully qualified name with those characters turned into escapes. The
convention is a backslash followed by _decimal_ digits. We convert these
back into the original binary values. This will be relevant when
allow_utf8_domains is set true and UTF-8 characters are used in domain
names. Backslash can also be used to escape other characters, though we
shouldn't come across them in domain names.
Argument: the domain name string
Returns: copy of string in new store, de-escaped
*/
uschar *
string_copy_dnsdomain(uschar * s)
{
uschar * yield;
uschar * ss = yield = store_get(Ustrlen(s) + 1, GET_TAINTED); /* always treat as tainted */
while (*s)
{
if (*s != '\\')
*ss++ = *s++;
else if (isdigit(s[1]))
{
*ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
s += 4;
}
else if (*++s)
*ss++ = *s++;
}
*ss = 0;
return yield;
}
#ifndef COMPILE_UTILITY
/*************************************************
* Copy space-terminated or quoted string *
*************************************************/
/* This function copies from a string until its end, or until whitespace is
encountered, unless the string begins with a double quote, in which case the
terminating quote is sought, and escaping within the string is done. The length
of a de-quoted string can be no longer than the original, since escaping always
turns n characters into 1 character.
Argument: pointer to the pointer to the first character, which gets updated
Returns: the new string
*/
uschar *
string_dequote(const uschar ** sptr)
{
const uschar * s = * sptr;
uschar * t, * yield;
/* First find the end of the string */
if (*s != '\"')
Uskip_nonwhite(&s);
else
{
s++;
while (*s && *s != '\"')
{
if (*s == '\\') (void)string_interpret_escape(&s);
s++;
}
if (*s) s++;
}
/* Get enough store to copy into */
t = yield = store_get(s - *sptr + 1, *sptr);
s = *sptr;
/* Do the copy */
if (*s != '\"')
while (*s && !isspace(*s)) *t++ = *s++;
else
{
s++;
while (*s && *s != '\"')
{
*t++ = *s == '\\' ? string_interpret_escape(&s) : *s;
s++;
}
if (*s) s++;
}
/* Update the pointer and return the terminated copy */
*sptr = s;
*t = 0;
return yield;
}
#endif /* COMPILE_UTILITY */
/*************************************************
* Format a string and save it *
*************************************************/
/* The formatting is done by string_vformat, which checks the length of
everything. Taint is taken from the worst of the arguments.
Arguments:
format a printf() format - deliberately char * rather than uschar *
because it will most usually be a literal string
func caller, for debug
line caller, for debug
... arguments for format
Returns: pointer to fresh piece of store containing sprintf'ed string
*/
uschar *
string_sprintf_trc(const char * format, const uschar * func, unsigned line, ...)
{
#ifdef COMPILE_UTILITY
uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
gstring gs = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer };
gstring * g = &gs;
unsigned flags = 0;
#else
gstring * g = NULL;
unsigned flags = SVFMT_REBUFFER|SVFMT_EXTEND;
#endif
va_list ap;
va_start(ap, line);
g = string_vformat_trc(g, func, line, STRING_SPRINTF_BUFFER_SIZE,
flags, format, ap);
va_end(ap);
if (!g)
log_write(0, LOG_MAIN|LOG_PANIC_DIE,
"string_sprintf expansion was longer than %d; format string was (%s)\n"
" called from %s %d\n",
STRING_SPRINTF_BUFFER_SIZE, format, func, line);
#ifdef COMPILE_UTILITY
return string_copyn(g->s, g->ptr);
#else
gstring_release_unused(g);
return string_from_gstring(g);
#endif
}
/*************************************************
* Case-independent strncmp() function *
*************************************************/
/*
Arguments:
s first string
t second string
n number of characters to compare
Returns: < 0, = 0, or > 0, according to the comparison
*/
int
strncmpic(const uschar * s, const uschar * t, int n)
{
while (n--)
{
int c = tolower(*s++) - tolower(*t++);
if (c) return c;
}
return 0;
}
/*************************************************
* Case-independent strcmp() function *
*************************************************/
/*
Arguments:
s first string
t second string
Returns: < 0, = 0, or > 0, according to the comparison
*/
int
strcmpic(const uschar * s, const uschar * t)
{
while (*s)
{
int c = tolower(*s++) - tolower(*t++);
if (c != 0) return c;
}
return *t;
}
/*************************************************
* Case-independent strstr() function *
*************************************************/
/* The third argument specifies whether whitespace is required
to follow the matched string.
Arguments:
s string to search
t substring to search for
space_follows if TRUE, match only if whitespace follows
Returns: pointer to substring in string, or NULL if not found
*/
const uschar *
strstric_c(const uschar * s, const uschar * t, BOOL space_follows)
{
const uschar * p = t;
const uschar * yield = NULL;
int cl = tolower(*p);
int cu = toupper(*p);
while (*s)
{
if (*s == cl || *s == cu)
{
if (!yield) yield = s;
if (!*++p)
{
if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
yield = NULL;
p = t;
}
cl = tolower(*p);
cu = toupper(*p);
s++;
}
else if (yield)
{
yield = NULL;
p = t;
cl = tolower(*p);
cu = toupper(*p);
}
else s++;
}
return NULL;
}
uschar *
strstric(uschar * s, uschar * t, BOOL space_follows)
{
return US strstric_c(s, t, space_follows);
}
#ifdef COMPILE_UTILITY
/* Dummy version for this function; it should never be called */
static void
gstring_grow(gstring * g, int count)
{
assert(FALSE);
}
#endif
#ifndef COMPILE_UTILITY
/*************************************************
* Get next string from separated list *
*************************************************/
/* Leading and trailing space is removed from each item. The separator in the
list is controlled by the int pointed to by the separator argument as follows:
If the value is > 0 it is used as the separator. This is typically used for
sublists such as slash-separated options. The value is always a printing
character.
(If the value is actually > UCHAR_MAX there is only one item in the list.
This is used for some cases when called via functions that sometimes
plough through lists, and sometimes are given single items.)
If the value is <= 0, the string is inspected for a leading <x, where x is an
ispunct() or an iscntrl() character. If found, x is used as the separator. If
not found:
(a) if separator == 0, ':' is used
(b) if separator <0, -separator is used
In all cases the value of the separator that is used is written back to the
int so that it is used on subsequent calls as we progress through the list.
A literal ispunct() separator can be represented in an item by doubling, but
there is no way to include an iscntrl() separator as part of the data.
Arguments:
listptr points to a pointer to the current start of the list; the
pointer gets updated to point after the end of the next item
separator a pointer to the separator character in an int (see above)
buffer where to put a copy of the next string in the list; or
NULL if the next string is returned in new memory
Note that if the list is tainted then a provided buffer must be
also (else we trap, with a message referencing the callsite).
If we do the allocation, taint is handled there.
buflen when buffer is not NULL, the size of buffer; otherwise ignored
func caller, for debug
line caller, for debug
Returns: pointer to buffer, containing the next substring,
or NULL if no more substrings
*/
uschar *
string_nextinlist_trc(const uschar ** listptr, int * separator, uschar * buffer,
int buflen, const uschar * func, int line)
{
int sep = *separator;
const uschar * s = *listptr;
BOOL sep_is_special;
if (!s) return NULL;
/* This allows for a fixed specified separator to be an iscntrl() character,
but at the time of implementation, this is never the case. However, it's best
to be conservative. */
while (isspace(*s) && *s != sep) s++;
/* A change of separator is permitted, so look for a leading '<' followed by an
allowed character. */
if (sep <= 0)
{
if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
{
sep = s[1];
if (*++s) ++s;
while (isspace(*s) && *s != sep) s++;
}
else
sep = sep ? -sep : ':';
*separator = sep;
}
/* An empty string has no list elements */
if (!*s) return NULL;
/* Note whether whether or not the separator is an iscntrl() character. */
sep_is_special = iscntrl(sep);
/* Handle the case when a buffer is provided. */
/*XXX need to also deal with qouted-requirements mismatch */
if (buffer)
{
int p = 0;
if (is_tainted(s) && !is_tainted(buffer))
die_tainted(US"string_nextinlist", func, line);
for (; *s; s++)
{
if (*s == sep && (*(++s) != sep || sep_is_special)) break;
if (p < buflen - 1) buffer[p++] = *s;
}
while (p > 0 && isspace(buffer[p-1])) p--;
buffer[p] = '\0';
}
/* Handle the case when a buffer is not provided. */
else
{
gstring * g = NULL;
/* We know that *s != 0 at this point. However, it might be pointing to a
separator, which could indicate an empty string, or (if an ispunct()
character) could be doubled to indicate a separator character as data at the
start of a string. Avoid getting working memory for an empty item. */
if (*s == sep)
if (*++s != sep || sep_is_special)
{
*listptr = s;
return string_copy(US"");
}
/* Not an empty string; the first character is guaranteed to be a data
character. */
for (;;)
{
const uschar * ss;
for (ss = s + 1; *ss && *ss != sep; ) ss++;
g = string_catn(g, s, ss-s);
s = ss;
if (!*s || *++s != sep || sep_is_special) break;
}
/* Trim trailing spaces from the returned string */
/* while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--; */
while ( g->ptr > 0 && isspace(g->s[g->ptr-1])