PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/src/string.c

https://gitlab.com/Exim/exim
C | 1854 lines | 1034 code | 309 blank | 511 comment | 417 complexity | 2ed39079929a02a010f066bcb11334d7 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*************************************************
  2. * Exim - an Internet mail transport agent *
  3. *************************************************/
  4. /* Copyright (c) University of Cambridge 1995 - 2014 */
  5. /* See the file NOTICE for conditions of use and distribution. */
  6. /* Miscellaneous string-handling functions. Some are not required for
  7. utilities and tests, and are cut out by the COMPILE_UTILITY macro. */
  8. #include "exim.h"
  9. #ifndef COMPILE_UTILITY
  10. /*************************************************
  11. * Test for IP address *
  12. *************************************************/
  13. /* This used just to be a regular expression, but with IPv6 things are a bit
  14. more complicated. If the address contains a colon, it is assumed to be a v6
  15. address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present,
  16. and maskptr is not NULL, its offset is placed there.
  17. Arguments:
  18. s a string
  19. maskptr NULL if no mask is permitted to follow
  20. otherwise, points to an int where the offset of '/' is placed
  21. if there is no / followed by trailing digits, *maskptr is set 0
  22. Returns: 0 if the string is not a textual representation of an IP address
  23. 4 if it is an IPv4 address
  24. 6 if it is an IPv6 address
  25. */
  26. int
  27. string_is_ip_address(const uschar *s, int *maskptr)
  28. {
  29. int i;
  30. int yield = 4;
  31. /* If an optional mask is permitted, check for it. If found, pass back the
  32. offset. */
  33. if (maskptr != NULL)
  34. {
  35. const uschar *ss = s + Ustrlen(s);
  36. *maskptr = 0;
  37. if (s != ss && isdigit(*(--ss)))
  38. {
  39. while (ss > s && isdigit(ss[-1])) ss--;
  40. if (ss > s && *(--ss) == '/') *maskptr = ss - s;
  41. }
  42. }
  43. /* A colon anywhere in the string => IPv6 address */
  44. if (Ustrchr(s, ':') != NULL)
  45. {
  46. BOOL had_double_colon = FALSE;
  47. BOOL v4end = FALSE;
  48. int count = 0;
  49. yield = 6;
  50. /* An IPv6 address must start with hex digit or double colon. A single
  51. colon is invalid. */
  52. if (*s == ':' && *(++s) != ':') return 0;
  53. /* Now read up to 8 components consisting of up to 4 hex digits each. There
  54. may be one and only one appearance of double colon, which implies any number
  55. of binary zero bits. The number of preceding components is held in count. */
  56. for (count = 0; count < 8; count++)
  57. {
  58. /* If the end of the string is reached before reading 8 components, the
  59. address is valid provided a double colon has been read. This also applies
  60. if we hit the / that introduces a mask or the % that introduces the
  61. interface specifier (scope id) of a link-local address. */
  62. if (*s == 0 || *s == '%' || *s == '/') return had_double_colon? yield : 0;
  63. /* If a component starts with an additional colon, we have hit a double
  64. colon. This is permitted to appear once only, and counts as at least
  65. one component. The final component may be of this form. */
  66. if (*s == ':')
  67. {
  68. if (had_double_colon) return 0;
  69. had_double_colon = TRUE;
  70. s++;
  71. continue;
  72. }
  73. /* If the remainder of the string contains a dot but no colons, we
  74. can expect a trailing IPv4 address. This is valid if either there has
  75. been no double-colon and this is the 7th component (with the IPv4 address
  76. being the 7th & 8th components), OR if there has been a double-colon
  77. and fewer than 6 components. */
  78. if (Ustrchr(s, ':') == NULL && Ustrchr(s, '.') != NULL)
  79. {
  80. if ((!had_double_colon && count != 6) ||
  81. (had_double_colon && count > 6)) return 0;
  82. v4end = TRUE;
  83. yield = 6;
  84. break;
  85. }
  86. /* Check for at least one and not more than 4 hex digits for this
  87. component. */
  88. if (!isxdigit(*s++)) return 0;
  89. if (isxdigit(*s) && isxdigit(*(++s)) && isxdigit(*(++s))) s++;
  90. /* If the component is terminated by colon and there is more to
  91. follow, skip over the colon. If there is no more to follow the address is
  92. invalid. */
  93. if (*s == ':' && *(++s) == 0) return 0;
  94. }
  95. /* If about to handle a trailing IPv4 address, drop through. Otherwise
  96. all is well if we are at the end of the string or at the mask or at a percent
  97. sign, which introduces the interface specifier (scope id) of a link local
  98. address. */
  99. if (!v4end)
  100. return (*s == 0 || *s == '%' ||
  101. (*s == '/' && maskptr != NULL && *maskptr != 0))? yield : 0;
  102. }
  103. /* Test for IPv4 address, which may be the tail-end of an IPv6 address. */
  104. for (i = 0; i < 4; i++)
  105. {
  106. if (i != 0 && *s++ != '.') return 0;
  107. if (!isdigit(*s++)) return 0;
  108. if (isdigit(*s) && isdigit(*(++s))) s++;
  109. }
  110. return (*s == 0 || (*s == '/' && maskptr != NULL && *maskptr != 0))?
  111. yield : 0;
  112. }
  113. #endif /* COMPILE_UTILITY */
  114. /*************************************************
  115. * Format message size *
  116. *************************************************/
  117. /* Convert a message size in bytes to printing form, rounding
  118. according to the magnitude of the number. A value of zero causes
  119. a string of spaces to be returned.
  120. Arguments:
  121. size the message size in bytes
  122. buffer where to put the answer
  123. Returns: pointer to the buffer
  124. a string of exactly 5 characters is normally returned
  125. */
  126. uschar *
  127. string_format_size(int size, uschar *buffer)
  128. {
  129. if (size == 0) Ustrcpy(buffer, " ");
  130. else if (size < 1024) sprintf(CS buffer, "%5d", size);
  131. else if (size < 10*1024)
  132. sprintf(CS buffer, "%4.1fK", (double)size / 1024.0);
  133. else if (size < 1024*1024)
  134. sprintf(CS buffer, "%4dK", (size + 512)/1024);
  135. else if (size < 10*1024*1024)
  136. sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0));
  137. else
  138. sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024));
  139. return buffer;
  140. }
  141. #ifndef COMPILE_UTILITY
  142. /*************************************************
  143. * Convert a number to base 62 format *
  144. *************************************************/
  145. /* Convert a long integer into an ASCII base 62 string. For Cygwin the value of
  146. BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a
  147. static area.
  148. Argument: a long integer
  149. Returns: pointer to base 62 string
  150. */
  151. uschar *
  152. string_base62(unsigned long int value)
  153. {
  154. static uschar yield[7];
  155. uschar *p = yield + sizeof(yield) - 1;
  156. *p = 0;
  157. while (p > yield)
  158. {
  159. *(--p) = base62_chars[value % BASE_62];
  160. value /= BASE_62;
  161. }
  162. return yield;
  163. }
  164. #endif /* COMPILE_UTILITY */
  165. /*************************************************
  166. * Interpret escape sequence *
  167. *************************************************/
  168. /* This function is called from several places where escape sequences are to be
  169. interpreted in strings.
  170. Arguments:
  171. pp points a pointer to the initiating "\" in the string;
  172. the pointer gets updated to point to the final character
  173. Returns: the value of the character escape
  174. */
  175. int
  176. string_interpret_escape(const uschar **pp)
  177. {
  178. #ifdef COMPILE_UTILITY
  179. const uschar *hex_digits= CUS"0123456789abcdef";
  180. #endif
  181. int ch;
  182. const uschar *p = *pp;
  183. ch = *(++p);
  184. if (isdigit(ch) && ch != '8' && ch != '9')
  185. {
  186. ch -= '0';
  187. if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
  188. {
  189. ch = ch * 8 + *(++p) - '0';
  190. if (isdigit(p[1]) && p[1] != '8' && p[1] != '9')
  191. ch = ch * 8 + *(++p) - '0';
  192. }
  193. }
  194. else switch(ch)
  195. {
  196. case 'b': ch = '\b'; break;
  197. case 'f': ch = '\f'; break;
  198. case 'n': ch = '\n'; break;
  199. case 'r': ch = '\r'; break;
  200. case 't': ch = '\t'; break;
  201. case 'v': ch = '\v'; break;
  202. case 'x':
  203. ch = 0;
  204. if (isxdigit(p[1]))
  205. {
  206. ch = ch * 16 +
  207. Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
  208. if (isxdigit(p[1])) ch = ch * 16 +
  209. Ustrchr(hex_digits, tolower(*(++p))) - hex_digits;
  210. }
  211. break;
  212. }
  213. *pp = p;
  214. return ch;
  215. }
  216. #ifndef COMPILE_UTILITY
  217. /*************************************************
  218. * Ensure string is printable *
  219. *************************************************/
  220. /* This function is called for critical strings. It checks for any
  221. non-printing characters, and if any are found, it makes a new copy
  222. of the string with suitable escape sequences. It is most often called by the
  223. macro string_printing(), which sets allow_tab TRUE.
  224. Arguments:
  225. s the input string
  226. allow_tab TRUE to allow tab as a printing character
  227. Returns: string with non-printers encoded as printing sequences
  228. */
  229. const uschar *
  230. string_printing2(const uschar *s, BOOL allow_tab)
  231. {
  232. int nonprintcount = 0;
  233. int length = 0;
  234. const uschar *t = s;
  235. uschar *ss, *tt;
  236. while (*t != 0)
  237. {
  238. int c = *t++;
  239. if (!mac_isprint(c) || (!allow_tab && c == '\t')) nonprintcount++;
  240. length++;
  241. }
  242. if (nonprintcount == 0) return s;
  243. /* Get a new block of store guaranteed big enough to hold the
  244. expanded string. */
  245. ss = store_get(length + nonprintcount * 3 + 1);
  246. /* Copy everying, escaping non printers. */
  247. t = s;
  248. tt = ss;
  249. while (*t != 0)
  250. {
  251. int c = *t;
  252. if (mac_isprint(c) && (allow_tab || c != '\t')) *tt++ = *t++; else
  253. {
  254. *tt++ = '\\';
  255. switch (*t)
  256. {
  257. case '\n': *tt++ = 'n'; break;
  258. case '\r': *tt++ = 'r'; break;
  259. case '\b': *tt++ = 'b'; break;
  260. case '\v': *tt++ = 'v'; break;
  261. case '\f': *tt++ = 'f'; break;
  262. case '\t': *tt++ = 't'; break;
  263. default: sprintf(CS tt, "%03o", *t); tt += 3; break;
  264. }
  265. t++;
  266. }
  267. }
  268. *tt = 0;
  269. return ss;
  270. }
  271. #endif /* COMPILE_UTILITY */
  272. /*************************************************
  273. * Undo printing escapes in string *
  274. *************************************************/
  275. /* This function is the reverse of string_printing2. It searches for
  276. backslash characters and if any are found, it makes a new copy of the
  277. string with escape sequences parsed. Otherwise it returns the original
  278. string.
  279. Arguments:
  280. s the input string
  281. Returns: string with printing escapes parsed back
  282. */
  283. uschar *
  284. string_unprinting(uschar *s)
  285. {
  286. uschar *p, *q, *r, *ss;
  287. int len, off;
  288. p = Ustrchr(s, '\\');
  289. if (!p) return s;
  290. len = Ustrlen(s) + 1;
  291. ss = store_get(len);
  292. q = ss;
  293. off = p - s;
  294. if (off)
  295. {
  296. memcpy(q, s, off);
  297. q += off;
  298. }
  299. while (*p)
  300. {
  301. if (*p == '\\')
  302. {
  303. *q++ = string_interpret_escape((const uschar **)&p);
  304. p++;
  305. }
  306. else
  307. {
  308. r = Ustrchr(p, '\\');
  309. if (!r)
  310. {
  311. off = Ustrlen(p);
  312. memcpy(q, p, off);
  313. p += off;
  314. q += off;
  315. break;
  316. }
  317. else
  318. {
  319. off = r - p;
  320. memcpy(q, p, off);
  321. q += off;
  322. p = r;
  323. }
  324. }
  325. }
  326. *q = '\0';
  327. return ss;
  328. }
  329. /*************************************************
  330. * Copy and save string *
  331. *************************************************/
  332. /* This function assumes that memcpy() is faster than strcpy().
  333. Argument: string to copy
  334. Returns: copy of string in new store
  335. */
  336. uschar *
  337. string_copy(const uschar *s)
  338. {
  339. int len = Ustrlen(s) + 1;
  340. uschar *ss = store_get(len);
  341. memcpy(ss, s, len);
  342. return ss;
  343. }
  344. /*************************************************
  345. * Copy and save string in malloc'd store *
  346. *************************************************/
  347. /* This function assumes that memcpy() is faster than strcpy().
  348. Argument: string to copy
  349. Returns: copy of string in new store
  350. */
  351. uschar *
  352. string_copy_malloc(const uschar *s)
  353. {
  354. int len = Ustrlen(s) + 1;
  355. uschar *ss = store_malloc(len);
  356. memcpy(ss, s, len);
  357. return ss;
  358. }
  359. /*************************************************
  360. * Copy, lowercase and save string *
  361. *************************************************/
  362. /*
  363. Argument: string to copy
  364. Returns: copy of string in new store, with letters lowercased
  365. */
  366. uschar *
  367. string_copylc(const uschar *s)
  368. {
  369. uschar *ss = store_get(Ustrlen(s) + 1);
  370. uschar *p = ss;
  371. while (*s != 0) *p++ = tolower(*s++);
  372. *p = 0;
  373. return ss;
  374. }
  375. /*************************************************
  376. * Copy and save string, given length *
  377. *************************************************/
  378. /* It is assumed the data contains no zeros. A zero is added
  379. onto the end.
  380. Arguments:
  381. s string to copy
  382. n number of characters
  383. Returns: copy of string in new store
  384. */
  385. uschar *
  386. string_copyn(const uschar *s, int n)
  387. {
  388. uschar *ss = store_get(n + 1);
  389. Ustrncpy(ss, s, n);
  390. ss[n] = 0;
  391. return ss;
  392. }
  393. /*************************************************
  394. * Copy, lowercase, and save string, given length *
  395. *************************************************/
  396. /* It is assumed the data contains no zeros. A zero is added
  397. onto the end.
  398. Arguments:
  399. s string to copy
  400. n number of characters
  401. Returns: copy of string in new store, with letters lowercased
  402. */
  403. uschar *
  404. string_copynlc(uschar *s, int n)
  405. {
  406. uschar *ss = store_get(n + 1);
  407. uschar *p = ss;
  408. while (n-- > 0) *p++ = tolower(*s++);
  409. *p = 0;
  410. return ss;
  411. }
  412. /*************************************************
  413. * Copy string if long, inserting newlines *
  414. *************************************************/
  415. /* If the given string is longer than 75 characters, it is copied, and within
  416. the copy, certain space characters are converted into newlines.
  417. Argument: pointer to the string
  418. Returns: pointer to the possibly altered string
  419. */
  420. uschar *
  421. string_split_message(uschar *msg)
  422. {
  423. uschar *s, *ss;
  424. if (msg == NULL || Ustrlen(msg) <= 75) return msg;
  425. s = ss = msg = string_copy(msg);
  426. for (;;)
  427. {
  428. int i = 0;
  429. while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++;
  430. if (*ss == 0) break;
  431. if (*ss == '\n')
  432. s = ++ss;
  433. else
  434. {
  435. uschar *t = ss + 1;
  436. uschar *tt = NULL;
  437. while (--t > s + 35)
  438. {
  439. if (*t == ' ')
  440. {
  441. if (t[-1] == ':') { tt = t; break; }
  442. if (tt == NULL) tt = t;
  443. }
  444. }
  445. if (tt == NULL) /* Can't split behind - try ahead */
  446. {
  447. t = ss + 1;
  448. while (*t != 0)
  449. {
  450. if (*t == ' ' || *t == '\n')
  451. { tt = t; break; }
  452. t++;
  453. }
  454. }
  455. if (tt == NULL) break; /* Can't find anywhere to split */
  456. *tt = '\n';
  457. s = ss = tt+1;
  458. }
  459. }
  460. return msg;
  461. }
  462. /*************************************************
  463. * Copy returned DNS domain name, de-escaping *
  464. *************************************************/
  465. /* If a domain name contains top-bit characters, some resolvers return
  466. the fully qualified name with those characters turned into escapes. The
  467. convention is a backslash followed by _decimal_ digits. We convert these
  468. back into the original binary values. This will be relevant when
  469. allow_utf8_domains is set true and UTF-8 characters are used in domain
  470. names. Backslash can also be used to escape other characters, though we
  471. shouldn't come across them in domain names.
  472. Argument: the domain name string
  473. Returns: copy of string in new store, de-escaped
  474. */
  475. uschar *
  476. string_copy_dnsdomain(uschar *s)
  477. {
  478. uschar *yield;
  479. uschar *ss = yield = store_get(Ustrlen(s) + 1);
  480. while (*s != 0)
  481. {
  482. if (*s != '\\')
  483. {
  484. *ss++ = *s++;
  485. }
  486. else if (isdigit(s[1]))
  487. {
  488. *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0';
  489. s += 4;
  490. }
  491. else if (*(++s) != 0)
  492. {
  493. *ss++ = *s++;
  494. }
  495. }
  496. *ss = 0;
  497. return yield;
  498. }
  499. #ifndef COMPILE_UTILITY
  500. /*************************************************
  501. * Copy space-terminated or quoted string *
  502. *************************************************/
  503. /* This function copies from a string until its end, or until whitespace is
  504. encountered, unless the string begins with a double quote, in which case the
  505. terminating quote is sought, and escaping within the string is done. The length
  506. of a de-quoted string can be no longer than the original, since escaping always
  507. turns n characters into 1 character.
  508. Argument: pointer to the pointer to the first character, which gets updated
  509. Returns: the new string
  510. */
  511. uschar *
  512. string_dequote(const uschar **sptr)
  513. {
  514. const uschar *s = *sptr;
  515. uschar *t, *yield;
  516. /* First find the end of the string */
  517. if (*s != '\"')
  518. {
  519. while (*s != 0 && !isspace(*s)) s++;
  520. }
  521. else
  522. {
  523. s++;
  524. while (*s != 0 && *s != '\"')
  525. {
  526. if (*s == '\\') (void)string_interpret_escape(&s);
  527. s++;
  528. }
  529. if (*s != 0) s++;
  530. }
  531. /* Get enough store to copy into */
  532. t = yield = store_get(s - *sptr + 1);
  533. s = *sptr;
  534. /* Do the copy */
  535. if (*s != '\"')
  536. {
  537. while (*s != 0 && !isspace(*s)) *t++ = *s++;
  538. }
  539. else
  540. {
  541. s++;
  542. while (*s != 0 && *s != '\"')
  543. {
  544. if (*s == '\\') *t++ = string_interpret_escape(&s);
  545. else *t++ = *s;
  546. s++;
  547. }
  548. if (*s != 0) s++;
  549. }
  550. /* Update the pointer and return the terminated copy */
  551. *sptr = s;
  552. *t = 0;
  553. return yield;
  554. }
  555. #endif /* COMPILE_UTILITY */
  556. /*************************************************
  557. * Format a string and save it *
  558. *************************************************/
  559. /* The formatting is done by string_format, which checks the length of
  560. everything.
  561. Arguments:
  562. format a printf() format - deliberately char * rather than uschar *
  563. because it will most usually be a literal string
  564. ... arguments for format
  565. Returns: pointer to fresh piece of store containing sprintf'ed string
  566. */
  567. uschar *
  568. string_sprintf(const char *format, ...)
  569. {
  570. va_list ap;
  571. uschar buffer[STRING_SPRINTF_BUFFER_SIZE];
  572. va_start(ap, format);
  573. if (!string_vformat(buffer, sizeof(buffer), format, ap))
  574. log_write(0, LOG_MAIN|LOG_PANIC_DIE,
  575. "string_sprintf expansion was longer than " SIZE_T_FMT " (%s)",
  576. sizeof(buffer), format);
  577. va_end(ap);
  578. return string_copy(buffer);
  579. }
  580. /*************************************************
  581. * Case-independent strncmp() function *
  582. *************************************************/
  583. /*
  584. Arguments:
  585. s first string
  586. t second string
  587. n number of characters to compare
  588. Returns: < 0, = 0, or > 0, according to the comparison
  589. */
  590. int
  591. strncmpic(const uschar *s, const uschar *t, int n)
  592. {
  593. while (n--)
  594. {
  595. int c = tolower(*s++) - tolower(*t++);
  596. if (c) return c;
  597. }
  598. return 0;
  599. }
  600. /*************************************************
  601. * Case-independent strcmp() function *
  602. *************************************************/
  603. /*
  604. Arguments:
  605. s first string
  606. t second string
  607. Returns: < 0, = 0, or > 0, according to the comparison
  608. */
  609. int
  610. strcmpic(const uschar *s, const uschar *t)
  611. {
  612. while (*s != 0)
  613. {
  614. int c = tolower(*s++) - tolower(*t++);
  615. if (c != 0) return c;
  616. }
  617. return *t;
  618. }
  619. /*************************************************
  620. * Case-independent strstr() function *
  621. *************************************************/
  622. /* The third argument specifies whether whitespace is required
  623. to follow the matched string.
  624. Arguments:
  625. s string to search
  626. t substring to search for
  627. space_follows if TRUE, match only if whitespace follows
  628. Returns: pointer to substring in string, or NULL if not found
  629. */
  630. uschar *
  631. strstric(uschar *s, uschar *t, BOOL space_follows)
  632. {
  633. uschar *p = t;
  634. uschar *yield = NULL;
  635. int cl = tolower(*p);
  636. int cu = toupper(*p);
  637. while (*s)
  638. {
  639. if (*s == cl || *s == cu)
  640. {
  641. if (yield == NULL) yield = s;
  642. if (*(++p) == 0)
  643. {
  644. if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield;
  645. yield = NULL;
  646. p = t;
  647. }
  648. cl = tolower(*p);
  649. cu = toupper(*p);
  650. s++;
  651. }
  652. else if (yield != NULL)
  653. {
  654. yield = NULL;
  655. p = t;
  656. cl = tolower(*p);
  657. cu = toupper(*p);
  658. }
  659. else s++;
  660. }
  661. return NULL;
  662. }
  663. #ifndef COMPILE_UTILITY
  664. /*************************************************
  665. * Get next string from separated list *
  666. *************************************************/
  667. /* Leading and trailing space is removed from each item. The separator in the
  668. list is controlled by the int pointed to by the separator argument as follows:
  669. If the value is > 0 it is used as the separator. This is typically used for
  670. sublists such as slash-separated options. The value is always a printing
  671. character.
  672. (If the value is actually > UCHAR_MAX there is only one item in the list.
  673. This is used for some cases when called via functions that sometimes
  674. plough through lists, and sometimes are given single items.)
  675. If the value is <= 0, the string is inspected for a leading <x, where x is an
  676. ispunct() or an iscntrl() character. If found, x is used as the separator. If
  677. not found:
  678. (a) if separator == 0, ':' is used
  679. (b) if separator <0, -separator is used
  680. In all cases the value of the separator that is used is written back to the
  681. int so that it is used on subsequent calls as we progress through the list.
  682. A literal ispunct() separator can be represented in an item by doubling, but
  683. there is no way to include an iscntrl() separator as part of the data.
  684. Arguments:
  685. listptr points to a pointer to the current start of the list; the
  686. pointer gets updated to point after the end of the next item
  687. separator a pointer to the separator character in an int (see above)
  688. buffer where to put a copy of the next string in the list; or
  689. NULL if the next string is returned in new memory
  690. buflen when buffer is not NULL, the size of buffer; otherwise ignored
  691. Returns: pointer to buffer, containing the next substring,
  692. or NULL if no more substrings
  693. */
  694. uschar *
  695. string_nextinlist(const uschar **listptr, int *separator, uschar *buffer, int buflen)
  696. {
  697. int sep = *separator;
  698. const uschar *s = *listptr;
  699. BOOL sep_is_special;
  700. if (s == NULL) return NULL;
  701. /* This allows for a fixed specified separator to be an iscntrl() character,
  702. but at the time of implementation, this is never the case. However, it's best
  703. to be conservative. */
  704. while (isspace(*s) && *s != sep) s++;
  705. /* A change of separator is permitted, so look for a leading '<' followed by an
  706. allowed character. */
  707. if (sep <= 0)
  708. {
  709. if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
  710. {
  711. sep = s[1];
  712. s += 2;
  713. while (isspace(*s) && *s != sep) s++;
  714. }
  715. else
  716. {
  717. sep = (sep == 0)? ':' : -sep;
  718. }
  719. *separator = sep;
  720. }
  721. /* An empty string has no list elements */
  722. if (*s == 0) return NULL;
  723. /* Note whether whether or not the separator is an iscntrl() character. */
  724. sep_is_special = iscntrl(sep);
  725. /* Handle the case when a buffer is provided. */
  726. if (buffer != NULL)
  727. {
  728. register int p = 0;
  729. for (; *s != 0; s++)
  730. {
  731. if (*s == sep && (*(++s) != sep || sep_is_special)) break;
  732. if (p < buflen - 1) buffer[p++] = *s;
  733. }
  734. while (p > 0 && isspace(buffer[p-1])) p--;
  735. buffer[p] = 0;
  736. }
  737. /* Handle the case when a buffer is not provided. */
  738. else
  739. {
  740. int size = 0;
  741. int ptr = 0;
  742. const uschar *ss;
  743. /* We know that *s != 0 at this point. However, it might be pointing to a
  744. separator, which could indicate an empty string, or (if an ispunct()
  745. character) could be doubled to indicate a separator character as data at the
  746. start of a string. Avoid getting working memory for an empty item. */
  747. if (*s == sep)
  748. {
  749. s++;
  750. if (*s != sep || sep_is_special)
  751. {
  752. *listptr = s;
  753. return string_copy(US"");
  754. }
  755. }
  756. /* Not an empty string; the first character is guaranteed to be a data
  757. character. */
  758. for (;;)
  759. {
  760. for (ss = s + 1; *ss != 0 && *ss != sep; ss++);
  761. buffer = string_cat(buffer, &size, &ptr, s, ss-s);
  762. s = ss;
  763. if (*s == 0 || *(++s) != sep || sep_is_special) break;
  764. }
  765. while (ptr > 0 && isspace(buffer[ptr-1])) ptr--;
  766. buffer[ptr] = 0;
  767. }
  768. /* Update the current pointer and return the new string */
  769. *listptr = s;
  770. return buffer;
  771. }
  772. #endif /* COMPILE_UTILITY */
  773. #ifndef COMPILE_UTILITY
  774. /************************************************
  775. * Add element to seperated list *
  776. ************************************************/
  777. /* This function is used to build a list, returning
  778. an allocated null-terminated growable string. The
  779. given element has any embedded seperator characters
  780. doubled.
  781. Arguments:
  782. list points to the start of the list that is being built, or NULL
  783. if this is a new list that has no contents yet
  784. sep list seperator charactoer
  785. ele new lement to be appended to the list
  786. Returns: pointer to the start of the list, changed if copied for expansion.
  787. */
  788. uschar *
  789. string_append_listele(uschar * list, uschar sep, const uschar * ele)
  790. {
  791. uschar * new = NULL;
  792. int sz = 0, off = 0;
  793. uschar * sp;
  794. if (list)
  795. {
  796. new = string_cat(new, &sz, &off, list, Ustrlen(list));
  797. new = string_cat(new, &sz, &off, &sep, 1);
  798. }
  799. while((sp = Ustrchr(ele, sep)))
  800. {
  801. new = string_cat(new, &sz, &off, ele, sp-ele+1);
  802. new = string_cat(new, &sz, &off, &sep, 1);
  803. ele = sp+1;
  804. }
  805. new = string_cat(new, &sz, &off, ele, Ustrlen(ele));
  806. new[off] = '\0';
  807. return new;
  808. }
  809. static const uschar *
  810. Ustrnchr(const uschar * s, int c, unsigned * len)
  811. {
  812. unsigned siz = *len;
  813. while (siz)
  814. {
  815. if (!*s) return NULL;
  816. if (*s == c)
  817. {
  818. *len = siz;
  819. return s;
  820. }
  821. s++;
  822. siz--;
  823. }
  824. return NULL;
  825. }
  826. uschar *
  827. string_append_listele_n(uschar * list, uschar sep, const uschar * ele,
  828. unsigned len)
  829. {
  830. uschar * new = NULL;
  831. int sz = 0, off = 0;
  832. const uschar * sp;
  833. if (list)
  834. {
  835. new = string_cat(new, &sz, &off, list, Ustrlen(list));
  836. new = string_cat(new, &sz, &off, &sep, 1);
  837. }
  838. while((sp = Ustrnchr(ele, sep, &len)))
  839. {
  840. new = string_cat(new, &sz, &off, ele, sp-ele+1);
  841. new = string_cat(new, &sz, &off, &sep, 1);
  842. ele = sp+1;
  843. len--;
  844. }
  845. new = string_cat(new, &sz, &off, ele, len);
  846. new[off] = '\0';
  847. return new;
  848. }
  849. #endif /* COMPILE_UTILITY */
  850. #ifndef COMPILE_UTILITY
  851. /*************************************************
  852. * Add chars to string *
  853. *************************************************/
  854. /* This function is used when building up strings of unknown length. Room is
  855. always left for a terminating zero to be added to the string that is being
  856. built. This function does not require the string that is being added to be NUL
  857. terminated, because the number of characters to add is given explicitly. It is
  858. sometimes called to extract parts of other strings.
  859. Arguments:
  860. string points to the start of the string that is being built, or NULL
  861. if this is a new string that has no contents yet
  862. size points to a variable that holds the current capacity of the memory
  863. block (updated if changed)
  864. ptr points to a variable that holds the offset at which to add
  865. characters, updated to the new offset
  866. s points to characters to add
  867. count count of characters to add; must not exceed the length of s, if s
  868. is a C string
  869. If string is given as NULL, *size and *ptr should both be zero.
  870. Returns: pointer to the start of the string, changed if copied for expansion.
  871. Note that a NUL is not added, though space is left for one. This is
  872. because string_cat() is often called multiple times to build up a
  873. string - there's no point adding the NUL till the end.
  874. */
  875. uschar *
  876. string_cat(uschar *string, int *size, int *ptr, const uschar *s, int count)
  877. {
  878. int p = *ptr;
  879. if (p + count >= *size)
  880. {
  881. int oldsize = *size;
  882. /* Mostly, string_cat() is used to build small strings of a few hundred
  883. characters at most. There are times, however, when the strings are very much
  884. longer (for example, a lookup that returns a vast number of alias addresses).
  885. To try to keep things reasonable, we use increments whose size depends on the
  886. existing length of the string. */
  887. int inc = (oldsize < 4096)? 100 : 1024;
  888. while (*size <= p + count) *size += inc;
  889. /* New string */
  890. if (string == NULL) string = store_get(*size);
  891. /* Try to extend an existing allocation. If the result of calling
  892. store_extend() is false, either there isn't room in the current memory block,
  893. or this string is not the top item on the dynamic store stack. We then have
  894. to get a new chunk of store and copy the old string. When building large
  895. strings, it is helpful to call store_release() on the old string, to release
  896. memory blocks that have become empty. (The block will be freed if the string
  897. is at its start.) However, we can do this only if we know that the old string
  898. was the last item on the dynamic memory stack. This is the case if it matches
  899. store_last_get. */
  900. else if (!store_extend(string, oldsize, *size))
  901. {
  902. BOOL release_ok = store_last_get[store_pool] == string;
  903. uschar *newstring = store_get(*size);
  904. memcpy(newstring, string, p);
  905. if (release_ok) store_release(string);
  906. string = newstring;
  907. }
  908. }
  909. /* Because we always specify the exact number of characters to copy, we can
  910. use memcpy(), which is likely to be more efficient than strncopy() because the
  911. latter has to check for zero bytes. */
  912. memcpy(string + p, s, count);
  913. *ptr = p + count;
  914. return string;
  915. }
  916. #endif /* COMPILE_UTILITY */
  917. #ifndef COMPILE_UTILITY
  918. /*************************************************
  919. * Append strings to another string *
  920. *************************************************/
  921. /* This function can be used to build a string from many other strings.
  922. It calls string_cat() to do the dirty work.
  923. Arguments:
  924. string points to the start of the string that is being built, or NULL
  925. if this is a new string that has no contents yet
  926. size points to a variable that holds the current capacity of the memory
  927. block (updated if changed)
  928. ptr points to a variable that holds the offset at which to add
  929. characters, updated to the new offset
  930. count the number of strings to append
  931. ... "count" uschar* arguments, which must be valid zero-terminated
  932. C strings
  933. Returns: pointer to the start of the string, changed if copied for expansion.
  934. The string is not zero-terminated - see string_cat() above.
  935. */
  936. uschar *
  937. string_append(uschar *string, int *size, int *ptr, int count, ...)
  938. {
  939. va_list ap;
  940. int i;
  941. va_start(ap, count);
  942. for (i = 0; i < count; i++)
  943. {
  944. uschar *t = va_arg(ap, uschar *);
  945. string = string_cat(string, size, ptr, t, Ustrlen(t));
  946. }
  947. va_end(ap);
  948. return string;
  949. }
  950. #endif
  951. /*************************************************
  952. * Format a string with length checks *
  953. *************************************************/
  954. /* This function is used to format a string with checking of the length of the
  955. output for all conversions. It protects Exim from absent-mindedness when
  956. calling functions like debug_printf and string_sprintf, and elsewhere. There
  957. are two different entry points to what is actually the same function, depending
  958. on whether the variable length list of data arguments are given explicitly or
  959. as a va_list item.
  960. The formats are the usual printf() ones, with some omissions (never used) and
  961. two additions for strings: %S forces lower case, and %#s or %#S prints nothing
  962. for a NULL string. Without the # "NULL" is printed (useful in debugging). There
  963. is also the addition of %D and %M, which insert the date in the form used for
  964. datestamped log files.
  965. Arguments:
  966. buffer a buffer in which to put the formatted string
  967. buflen the length of the buffer
  968. format the format string - deliberately char * and not uschar *
  969. ... or ap variable list of supplementary arguments
  970. Returns: TRUE if the result fitted in the buffer
  971. */
  972. BOOL
  973. string_format(uschar *buffer, int buflen, const char *format, ...)
  974. {
  975. BOOL yield;
  976. va_list ap;
  977. va_start(ap, format);
  978. yield = string_vformat(buffer, buflen, format, ap);
  979. va_end(ap);
  980. return yield;
  981. }
  982. BOOL
  983. string_vformat(uschar *buffer, int buflen, const char *format, va_list ap)
  984. {
  985. /* We assume numbered ascending order, C does not guarantee that */
  986. enum { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 };
  987. BOOL yield = TRUE;
  988. int width, precision;
  989. const char *fp = format; /* Deliberately not unsigned */
  990. uschar *p = buffer;
  991. uschar *last = buffer + buflen - 1;
  992. string_datestamp_offset = -1; /* Datestamp not inserted */
  993. string_datestamp_length = 0; /* Datestamp not inserted */
  994. string_datestamp_type = 0; /* Datestamp not inserted */
  995. /* Scan the format and handle the insertions */
  996. while (*fp != 0)
  997. {
  998. int length = L_NORMAL;
  999. int *nptr;
  1000. int slen;
  1001. const char *null = "NULL"; /* ) These variables */
  1002. const char *item_start, *s; /* ) are deliberately */
  1003. char newformat[16]; /* ) not unsigned */
  1004. /* Non-% characters just get copied verbatim */
  1005. if (*fp != '%')
  1006. {
  1007. if (p >= last) { yield = FALSE; break; }
  1008. *p++ = (uschar)*fp++;
  1009. continue;
  1010. }
  1011. /* Deal with % characters. Pick off the width and precision, for checking
  1012. strings, skipping over the flag and modifier characters. */
  1013. item_start = fp;
  1014. width = precision = -1;
  1015. if (strchr("-+ #0", *(++fp)) != NULL)
  1016. {
  1017. if (*fp == '#') null = "";
  1018. fp++;
  1019. }
  1020. if (isdigit((uschar)*fp))
  1021. {
  1022. width = *fp++ - '0';
  1023. while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0';
  1024. }
  1025. else if (*fp == '*')
  1026. {
  1027. width = va_arg(ap, int);
  1028. fp++;
  1029. }
  1030. if (*fp == '.')
  1031. {
  1032. if (*(++fp) == '*')
  1033. {
  1034. precision = va_arg(ap, int);
  1035. fp++;
  1036. }
  1037. else
  1038. {
  1039. precision = 0;
  1040. while (isdigit((uschar)*fp))
  1041. precision = precision*10 + *fp++ - '0';
  1042. }
  1043. }
  1044. /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */
  1045. if (*fp == 'h')
  1046. { fp++; length = L_SHORT; }
  1047. else if (*fp == 'L')
  1048. { fp++; length = L_LONGDOUBLE; }
  1049. else if (*fp == 'l')
  1050. {
  1051. if (fp[1] == 'l')
  1052. {
  1053. fp += 2;
  1054. length = L_LONGLONG;
  1055. }
  1056. else
  1057. {
  1058. fp++;
  1059. length = L_LONG;
  1060. }
  1061. }
  1062. else if (*fp == 'z')
  1063. { fp++; length = L_SIZE; }
  1064. /* Handle each specific format type. */
  1065. switch (*fp++)
  1066. {
  1067. case 'n':
  1068. nptr = va_arg(ap, int *);
  1069. *nptr = p - buffer;
  1070. break;
  1071. case 'd':
  1072. case 'o':
  1073. case 'u':
  1074. case 'x':
  1075. case 'X':
  1076. if (p >= last - ((length > L_LONG)? 24 : 12))
  1077. { yield = FALSE; goto END_FORMAT; }
  1078. strncpy(newformat, item_start, fp - item_start);
  1079. newformat[fp - item_start] = 0;
  1080. /* Short int is promoted to int when passing through ..., so we must use
  1081. int for va_arg(). */
  1082. switch(length)
  1083. {
  1084. case L_SHORT:
  1085. case L_NORMAL: sprintf(CS p, newformat, va_arg(ap, int)); break;
  1086. case L_LONG: sprintf(CS p, newformat, va_arg(ap, long int)); break;
  1087. case L_LONGLONG: sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break;
  1088. case L_SIZE: sprintf(CS p, newformat, va_arg(ap, size_t)); break;
  1089. }
  1090. while (*p) p++;
  1091. break;
  1092. case 'p':
  1093. if (p >= last - 24) { yield = FALSE; goto END_FORMAT; }
  1094. strncpy(newformat, item_start, fp - item_start);
  1095. newformat[fp - item_start] = 0;
  1096. sprintf(CS p, newformat, va_arg(ap, void *));
  1097. while (*p) p++;
  1098. break;
  1099. /* %f format is inherently insecure if the numbers that it may be
  1100. handed are unknown (e.g. 1e300). However, in Exim, %f is used for
  1101. printing load averages, and these are actually stored as integers
  1102. (load average * 1000) so the size of the numbers is constrained.
  1103. It is also used for formatting sending rates, where the simplicity
  1104. of the format prevents overflow. */
  1105. case 'f':
  1106. case 'e':
  1107. case 'E':
  1108. case 'g':
  1109. case 'G':
  1110. if (precision < 0) precision = 6;
  1111. if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; }
  1112. strncpy(newformat, item_start, fp - item_start);
  1113. newformat[fp-item_start] = 0;
  1114. if (length == L_LONGDOUBLE)
  1115. sprintf(CS p, newformat, va_arg(ap, long double));
  1116. else
  1117. sprintf(CS p, newformat, va_arg(ap, double));
  1118. while (*p) p++;
  1119. break;
  1120. /* String types */
  1121. case '%':
  1122. if (p >= last) { yield = FALSE; goto END_FORMAT; }
  1123. *p++ = '%';
  1124. break;
  1125. case 'c':
  1126. if (p >= last) { yield = FALSE; goto END_FORMAT; }
  1127. *p++ = va_arg(ap, int);
  1128. break;
  1129. case 'D': /* Insert daily datestamp for log file names */
  1130. s = CS tod_stamp(tod_log_datestamp_daily);
  1131. string_datestamp_offset = p - buffer; /* Passed back via global */
  1132. string_datestamp_length = Ustrlen(s); /* Passed back via global */
  1133. string_datestamp_type = tod_log_datestamp_daily;
  1134. slen = string_datestamp_length;
  1135. goto INSERT_STRING;
  1136. case 'M': /* Insert monthly datestamp for log file names */
  1137. s = CS tod_stamp(tod_log_datestamp_monthly);
  1138. string_datestamp_offset = p - buffer; /* Passed back via global */
  1139. string_datestamp_length = Ustrlen(s); /* Passed back via global */
  1140. string_datestamp_type = tod_log_datestamp_monthly;
  1141. slen = string_datestamp_length;
  1142. goto INSERT_STRING;
  1143. case 's':
  1144. case 'S': /* Forces *lower* case */
  1145. s = va_arg(ap, char *);
  1146. if (s == NULL) s = null;
  1147. slen = Ustrlen(s);
  1148. INSERT_STRING: /* Come to from %D or %M above */
  1149. /* If the width is specified, check that there is a precision
  1150. set; if not, set it to the width to prevent overruns of long
  1151. strings. */
  1152. if (width >= 0)
  1153. {
  1154. if (precision < 0) precision = width;
  1155. }
  1156. /* If a width is not specified and the precision is specified, set
  1157. the width to the precision, or the string length if shorted. */
  1158. else if (precision >= 0)
  1159. {
  1160. width = (precision < slen)? precision : slen;
  1161. }
  1162. /* If neither are specified, set them both to the string length. */
  1163. else width = precision = slen;
  1164. /* Check string space, and add the string to the buffer if ok. If
  1165. not OK, add part of the string (debugging uses this to show as
  1166. much as possible). */
  1167. if (p == last)
  1168. {
  1169. yield = FALSE;
  1170. goto END_FORMAT;
  1171. }
  1172. if (p >= last - width)
  1173. {
  1174. yield = FALSE;
  1175. width = precision = last - p - 1;
  1176. if (width < 0) width = 0;
  1177. if (precision < 0) precision = 0;
  1178. }
  1179. sprintf(CS p, "%*.*s", width, precision, s);
  1180. if (fp[-1] == 'S')
  1181. while (*p) { *p = tolower(*p); p++; }
  1182. else
  1183. while (*p) p++;
  1184. if (!yield) goto END_FORMAT;
  1185. break;
  1186. /* Some things are never used in Exim; also catches junk. */
  1187. default:
  1188. strncpy(newformat, item_start, fp - item_start);
  1189. newformat[fp-item_start] = 0;
  1190. log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type "
  1191. "in \"%s\" in \"%s\"", newformat, format);
  1192. break;
  1193. }
  1194. }
  1195. /* Ensure string is complete; return TRUE if got to the end of the format */
  1196. END_FORMAT:
  1197. *p = 0;
  1198. return yield;
  1199. }
  1200. #ifndef COMPILE_UTILITY
  1201. /*************************************************
  1202. * Generate an "open failed" message *
  1203. *************************************************/
  1204. /* This function creates a message after failure to open a file. It includes a
  1205. string supplied as data, adds the strerror() text, and if the failure was
  1206. "Permission denied", reads and includes the euid and egid.
  1207. Arguments:
  1208. eno the value of errno after the failure
  1209. format a text format string - deliberately not uschar *
  1210. ... arguments for the format string
  1211. Returns: a message, in dynamic store
  1212. */
  1213. uschar *
  1214. string_open_failed(int eno, const char *format, ...)
  1215. {
  1216. va_list ap;
  1217. uschar buffer[1024];
  1218. Ustrcpy(buffer, "failed to open ");
  1219. va_start(ap, format);
  1220. /* Use the checked formatting routine to ensure that the buffer
  1221. does not overflow. It should not, since this is called only for internally
  1222. specified messages. If it does, the message just gets truncated, and there
  1223. doesn't seem much we can do about that. */
  1224. (void)string_vformat(buffer+15, sizeof(buffer) - 15, format, ap);
  1225. return (eno == EACCES)?
  1226. string_sprintf("%s: %s (euid=%ld egid=%ld)", buffer, strerror(eno),
  1227. (long int)geteuid(), (long int)getegid()) :
  1228. string_sprintf("%s: %s", buffer, strerror(eno));
  1229. }
  1230. #endif /* COMPILE_UTILITY */
  1231. #ifndef COMPILE_UTILITY
  1232. /*************************************************
  1233. * Generate local prt for logging *
  1234. *************************************************/
  1235. /* This function is a subroutine for use in string_log_address() below.
  1236. Arguments:
  1237. addr the address being logged
  1238. yield the current dynamic buffer pointer
  1239. sizeptr points to current size
  1240. ptrptr points to current insert pointer
  1241. Returns: the new value of the buffer pointer
  1242. */
  1243. static uschar *
  1244. string_get_localpart(address_item *addr, uschar *yield, int *sizeptr,
  1245. int *ptrptr)
  1246. {
  1247. uschar * s;
  1248. s = addr->prefix;
  1249. if (testflag(addr, af_include_affixes) && s)
  1250. {
  1251. #ifdef EXPERIMENTAL_INTERNATIONAL
  1252. if (testflag(addr, af_utf8_downcvt))
  1253. s = string_localpart_utf8_to_alabel(s, NULL);
  1254. #endif
  1255. yield = string_cat(yield, sizeptr, ptrptr, s, Ustrlen(s));
  1256. }
  1257. s = addr->local_part;
  1258. #ifdef EXPERIMENTAL_INTERNATIONAL
  1259. if (testflag(addr, af_utf8_downcvt))
  1260. s = string_localpart_utf8_to_alabel(s, NULL);
  1261. #endif
  1262. yield = string_cat(yield, sizeptr, ptrptr, s, Ustrlen(s));
  1263. s = addr->suffix;
  1264. if (testflag(addr, af_include_affixes) && s)
  1265. {
  1266. #ifdef EXPERIMENTAL_INTERNATIONAL
  1267. if (testflag(addr, af_utf8_downcvt))
  1268. s = string_localpart_utf8_to_alabel(s, NULL);
  1269. #endif
  1270. yield = string_cat(yield, sizeptr, ptrptr, s, Ustrlen(s));
  1271. }
  1272. return yield;
  1273. }
  1274. /*************************************************
  1275. * Generate log address list *
  1276. *************************************************/
  1277. /* This function generates a list consisting of an address and its parents, for
  1278. use in logging lines. For saved onetime aliased addresses, the onetime parent
  1279. field is used. If the address was delivered by a transport with rcpt_include_
  1280. affixes set, the af_include_affixes bit will be set in the address. In that
  1281. case, we include the affixes here too.
  1282. Arguments:
  1283. addr bottom (ultimate) address
  1284. all_parents if TRUE, include all parents
  1285. success TRUE for successful delivery
  1286. Returns: a string in dynamic store
  1287. */
  1288. uschar *
  1289. string_log_address(address_item *addr, BOOL all_parents, BOOL success)
  1290. {
  1291. int size = 64;
  1292. int ptr = 0;
  1293. BOOL add_topaddr = TRUE;
  1294. uschar *yield = store_get(size);
  1295. address_item *topaddr;
  1296. /* Find the ultimate parent */
  1297. for (topaddr = addr; topaddr->parent != NULL; topaddr = topaddr->parent);
  1298. /* We start with just the local part for pipe, file, and reply deliveries, and
  1299. for successful local deliveries from routers that have the log_as_local flag
  1300. set. File deliveries from filters can be specified as non-absolute paths in
  1301. cases where the transport is goin to complete the path. If there is an error
  1302. before this happens (expansion failure) the local part will not be updated, and
  1303. so won't necessarily look like a path. Add extra text for this case. */
  1304. if (testflag(addr, af_pfr) ||
  1305. (success &&
  1306. addr->router != NULL && addr->router->log_as_local &&
  1307. addr->transport != NULL && addr->transport->info->local))
  1308. {
  1309. if (testflag(addr, af_file) && addr->local_part[0] != '/')
  1310. yield = string_cat(yield, &size, &ptr, CUS"save ", 5);
  1311. yield = string_get_localpart(addr, yield, &size, &ptr);
  1312. }
  1313. /* Other deliveries start with the full address. It we have split it into local
  1314. part and domain, use those fields. Some early failures can happen before the
  1315. splitting is done; in those cases use the original field. */
  1316. else
  1317. {
  1318. if (addr->local_part != NULL)
  1319. {
  1320. const uschar * s;
  1321. yield = string_get_localpart(addr, yield, &size, &ptr);
  1322. yield = string_cat(yield, &size, &ptr, US"@", 1);
  1323. s = addr->domain;
  1324. #ifdef EXPERIMENTAL_INTERNATIONAL
  1325. if (testflag(addr, af_utf8_downcvt))
  1326. s = string_localpart_utf8_to_alabel(s, NULL);
  1327. #endif
  1328. yield = string_cat(yield, &size, &ptr, s, Ustrlen(s) );
  1329. }
  1330. else
  1331. {
  1332. yield = string_cat(yield, &size, &ptr, addr->address, Ustrlen(addr->address));
  1333. }
  1334. yield[ptr] = 0;
  1335. /* If the address we are going to print is the same as the top address,
  1336. and all parents are not being included, don't add on the top address. First
  1337. of all, do a caseless comparison; if this succeeds, do a caseful comparison
  1338. on the local parts. */
  1339. if (strcmpic(yield, topaddr->address) == 0 &&
  1340. Ustrncmp(yield, topaddr->address, Ustrchr(yield, '@') - yield) == 0 &&
  1341. addr->onetime_parent == NULL &&
  1342. (!all_parents || addr->parent == NULL || addr->parent == topaddr))
  1343. add_topaddr = FALSE;
  1344. }
  1345. /* If all parents are requested, or this is a local pipe/file/reply, and
  1346. there is at least one intermediate parent, show it in brackets, and continue
  1347. with all of them if all are wanted. */
  1348. if ((all_parents || testflag(addr, af_pfr)) &&
  1349. addr->parent != NULL &&
  1350. addr->parent != topaddr)
  1351. {
  1352. uschar *s = US" (";
  1353. address_item *addr2;
  1354. for (addr2 = addr->parent; addr2 != topaddr; addr2 = addr2->parent)
  1355. {
  1356. yield = string_cat(yield, &size, &ptr, s, 2);
  1357. yield = string_cat(yield, &size, &ptr, addr2->address, Ustrlen(addr2->address));
  1358. if (!all_parents) break;
  1359. s = US", ";
  1360. }
  1361. yield = string_cat(yield, &size, &ptr, US")", 1);
  1362. }
  1363. /* Add the top address if it is required */
  1364. if (add_topaddr)
  1365. {
  1366. yield = string_cat(yield, &size, &ptr, US" <", 2);
  1367. if (addr->onetime_parent == NULL)
  1368. yield = string_cat(yield, &size, &ptr, topaddr->address,
  1369. Ustrlen(topaddr->address));
  1370. else
  1371. yield = string_cat(yield, &size, &ptr, addr->onetime_parent,
  1372. Ustrlen(addr->onetime_parent));
  1373. yield = string_cat(yield, &size, &ptr, US">", 1);
  1374. }
  1375. yield[ptr] = 0; /* string_cat() leaves space */
  1376. return yield;
  1377. }
  1378. #endif /* COMPILE_UTILITY */
  1379. /*************************************************
  1380. **************************************************
  1381. * Stand-alone test program *
  1382. **************************************************
  1383. *************************************************/
  1384. #ifdef STAND_ALONE
  1385. int main(void)
  1386. {
  1387. uschar buffer[256];
  1388. printf("Testing is_ip_address\n");
  1389. while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
  1390. {
  1391. int offset;
  1392. buffer[Ustrlen(buffer) - 1] = 0;
  1393. printf("%d\n", string_is_ip_address(buffer, NULL));
  1394. printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer);
  1395. }
  1396. printf("Testing string_nextinlist\n");
  1397. while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
  1398. {
  1399. uschar *list = buffer;
  1400. uschar *lp1, *lp2;
  1401. uschar item[256];
  1402. int sep1 = 0;
  1403. int sep2 = 0;
  1404. if (*list == '<')
  1405. {
  1406. sep1 = sep2 = list[1];
  1407. list += 2;
  1408. }
  1409. lp1 = lp2 = list;
  1410. for (;;)
  1411. {
  1412. uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item));
  1413. uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0);
  1414. if (item1 == NULL && item2 == NULL) break;
  1415. if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0)
  1416. {
  1417. printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n",
  1418. (item1 == NULL)? "NULL" : CS item1,
  1419. (item2 == NULL)? "NULL" : CS item2);
  1420. break;
  1421. }
  1422. else printf(" \"%s\"\n", CS item1);
  1423. }
  1424. }
  1425. /* This is a horrible lash-up, but it serves its purpose. */
  1426. printf("Testing string_format\n");
  1427. while (fgets(CS buffer, sizeof(buffer), stdin) != NULL)
  1428. {
  1429. void *args[3];
  1430. long long llargs[3];
  1431. double dargs[3];
  1432. int dflag = 0;
  1433. int llflag = 0;
  1434. int n = 0;
  1435. int count;
  1436. int countset = 0;
  1437. uschar format[256];
  1438. uschar outbuf[256];
  1439. uschar *s;
  1440. buffer[Ustrlen(buffer) - 1] = 0;
  1441. s = Ustrchr(buffer, ',');
  1442. if (s == NULL) s = buffer + Ustrlen(buffer);
  1443. Ustrncpy(format, buffer, s - buffer);
  1444. format[s-buffer] = 0;
  1445. if (*s == ',') s++;
  1446. while (*s != 0)
  1447. {
  1448. uschar *ss = s;
  1449. s = Ustrchr(ss, ',');
  1450. if (s == NULL) s = ss + Ustrlen(ss);
  1451. if (isdigit(*ss))
  1452. {
  1453. Ustrncpy(outbuf, ss, s-ss);
  1454. if (Ustrchr(outbuf, '.') != NULL)
  1455. {
  1456. dflag = 1;
  1457. dargs[n++] = Ustrtod(outbuf, NULL);
  1458. }
  1459. else if (Ustrstr(outbuf, "ll") != NULL)
  1460. {
  1461. llflag = 1;
  1462. llargs[n++] = strtoull(CS outbuf, NULL, 10);
  1463. }
  1464. else
  1465. {
  1466. args[n++] = (void *)Uatoi(outbuf);
  1467. }
  1468. }
  1469. else if (Ustrcmp(ss, "*") == 0)
  1470. {
  1471. args[n++] = (void *)(&count);
  1472. countset = 1;
  1473. }
  1474. else
  1475. {
  1476. uschar *sss = malloc(s - ss + 1);
  1477. Ustrncpy(sss, ss, s-ss);
  1478. args[n++] = sss;
  1479. }
  1480. if (*s == ',') s++;
  1481. }
  1482. if (!dflag && !llflag)
  1483. printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
  1484. args[0], args[1], args[2])? "True" : "False");
  1485. else if (dflag)
  1486. printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
  1487. dargs[0], dargs[1], dargs[2])? "True" : "False");
  1488. else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format,
  1489. llargs[0], llargs[1], llargs[2])? "True" : "False");
  1490. printf("%s\n", CS outbuf);
  1491. if (countset) printf("count=%d\n", count);
  1492. }
  1493. return 0;
  1494. }
  1495. #endif
  1496. /* End of string.c */