mxml-string.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * String functions for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2003-2019 by Michael R Sweet.
  7. *
  8. * Licensed under Apache License v2.0. See the file "LICENSE" for more
  9. * information.
  10. */
  11. /*
  12. * Include necessary headers...
  13. */
  14. #include "config.h"
  15. /*
  16. * The va_copy macro is part of C99, but many compilers don't implement it.
  17. * Provide a "direct assignment" implmentation when va_copy isn't defined...
  18. */
  19. #ifndef va_copy
  20. # ifdef __va_copy
  21. # define va_copy(dst,src) __va_copy(dst,src)
  22. # else
  23. # define va_copy(dst,src) memcpy(&dst, &src, sizeof(va_list))
  24. # endif /* __va_copy */
  25. #endif /* va_copy */
  26. #ifndef HAVE_SNPRINTF
  27. /*
  28. * '_mxml_snprintf()' - Format a string.
  29. */
  30. int /* O - Number of bytes formatted */
  31. _mxml_snprintf(char *buffer, /* I - Output buffer */
  32. size_t bufsize, /* I - Size of output buffer */
  33. const char *format, /* I - Printf-style format string */
  34. ...) /* I - Additional arguments as needed */
  35. {
  36. va_list ap; /* Argument list */
  37. int bytes; /* Number of bytes formatted */
  38. va_start(ap, format);
  39. bytes = vsnprintf(buffer, bufsize, format, ap);
  40. va_end(ap);
  41. return (bytes);
  42. }
  43. #endif /* !HAVE_SNPRINTF */
  44. /*
  45. * '_mxml_strdup()' - Duplicate a string.
  46. */
  47. #ifndef HAVE_STRDUP
  48. char * /* O - New string pointer */
  49. _mxml_strdup(const char *s) /* I - String to duplicate */
  50. {
  51. char *t; /* New string pointer */
  52. if (s == NULL)
  53. return (NULL);
  54. if ((t = malloc(strlen(s) + 1)) == NULL)
  55. return (NULL);
  56. return (strcpy(t, s));
  57. }
  58. #endif /* !HAVE_STRDUP */
  59. /*
  60. * '_mxml_strdupf()' - Format and duplicate a string.
  61. */
  62. char * /* O - New string pointer */
  63. _mxml_strdupf(const char *format, /* I - Printf-style format string */
  64. ...) /* I - Additional arguments as needed */
  65. {
  66. va_list ap; /* Pointer to additional arguments */
  67. char *s; /* Pointer to formatted string */
  68. /*
  69. * Get a pointer to the additional arguments, format the string,
  70. * and return it...
  71. */
  72. va_start(ap, format);
  73. #ifdef HAVE_VASPRINTF
  74. if (vasprintf(&s, format, ap) < 0)
  75. s = NULL;
  76. #else
  77. s = _mxml_vstrdupf(format, ap);
  78. #endif /* HAVE_VASPRINTF */
  79. va_end(ap);
  80. return (s);
  81. }
  82. #ifndef HAVE_STRLCAT
  83. /*
  84. * '_mxml_strlcat()' - Safely concatenate a string.
  85. */
  86. size_t /* O - Number of bytes copied */
  87. _mxml_strlcat(char *dst, /* I - Destination buffer */
  88. const char *src, /* I - Source string */
  89. size_t dstsize) /* I - Size of destinatipon buffer */
  90. {
  91. size_t srclen; /* Length of source string */
  92. size_t dstlen; /* Length of destination string */
  93. /*
  94. * Figure out how much room is left...
  95. */
  96. dstlen = strlen(dst);
  97. if (dstsize <= (dstlen + 1))
  98. return (dstlen); /* No room, return immediately... */
  99. dstsize -= dstlen + 1;
  100. /*
  101. * Figure out how much room is needed...
  102. */
  103. srclen = strlen(src);
  104. /*
  105. * Copy the appropriate amount...
  106. */
  107. if (srclen > dstsize)
  108. srclen = dstsize;
  109. memmove(dst + dstlen, src, srclen);
  110. dst[dstlen + srclen] = '\0';
  111. return (dstlen + srclen);
  112. }
  113. #endif /* !HAVE_STRLCAT */
  114. #ifndef HAVE_STRLCPY
  115. /*
  116. * '_mxml_strlcpy()' - Safely copy a string.
  117. */
  118. size_t /* O - Number of bytes copied */
  119. _mxml_strlcpy(char *dst, /* I - Destination buffer */
  120. const char *src, /* I - Source string */
  121. size_t dstsize) /* I - Size of destinatipon buffer */
  122. {
  123. size_t srclen; /* Length of source string */
  124. /*
  125. * Figure out how much room is needed...
  126. */
  127. dstsize --;
  128. srclen = strlen(src);
  129. /*
  130. * Copy the appropriate amount...
  131. */
  132. if (srclen > dstsize)
  133. srclen = dstsize;
  134. memmove(dst, src, srclen);
  135. dst[srclen] = '\0';
  136. return (srclen);
  137. }
  138. #endif /* !HAVE_STRLCPY */
  139. #ifndef HAVE_VSNPRINTF
  140. /*
  141. * '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
  142. */
  143. int /* O - Number of bytes formatted */
  144. _mxml_vsnprintf(char *buffer, /* O - Output buffer */
  145. size_t bufsize, /* O - Size of output buffer */
  146. const char *format, /* I - Printf-style format string */
  147. va_list ap) /* I - Pointer to additional arguments */
  148. {
  149. char *bufptr, /* Pointer to position in buffer */
  150. *bufend, /* Pointer to end of buffer */
  151. sign, /* Sign of format width */
  152. size, /* Size character (h, l, L) */
  153. type; /* Format type character */
  154. int width, /* Width of field */
  155. prec; /* Number of characters of precision */
  156. char tformat[100], /* Temporary format string for sprintf() */
  157. *tptr, /* Pointer into temporary format */
  158. temp[1024]; /* Buffer for formatted numbers */
  159. char *s; /* Pointer to string */
  160. int slen; /* Length of string */
  161. int bytes; /* Total number of bytes needed */
  162. /*
  163. * Loop through the format string, formatting as needed...
  164. */
  165. bufptr = buffer;
  166. bufend = buffer + bufsize - 1;
  167. bytes = 0;
  168. while (*format)
  169. {
  170. if (*format == '%')
  171. {
  172. tptr = tformat;
  173. *tptr++ = *format++;
  174. if (*format == '%')
  175. {
  176. if (bufptr && bufptr < bufend)
  177. *bufptr++ = *format;
  178. bytes ++;
  179. format ++;
  180. continue;
  181. }
  182. else if (strchr(" -+#\'", *format))
  183. {
  184. *tptr++ = *format;
  185. sign = *format++;
  186. }
  187. else
  188. sign = 0;
  189. if (*format == '*')
  190. {
  191. /*
  192. * Get width from argument...
  193. */
  194. format ++;
  195. width = va_arg(ap, int);
  196. snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width);
  197. tptr += strlen(tptr);
  198. }
  199. else
  200. {
  201. width = 0;
  202. while (isdigit(*format & 255))
  203. {
  204. if (tptr < (tformat + sizeof(tformat) - 1))
  205. *tptr++ = *format;
  206. width = width * 10 + *format++ - '0';
  207. }
  208. }
  209. if (*format == '.')
  210. {
  211. if (tptr < (tformat + sizeof(tformat) - 1))
  212. *tptr++ = *format;
  213. format ++;
  214. if (*format == '*')
  215. {
  216. /*
  217. * Get precision from argument...
  218. */
  219. format ++;
  220. prec = va_arg(ap, int);
  221. snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec);
  222. tptr += strlen(tptr);
  223. }
  224. else
  225. {
  226. prec = 0;
  227. while (isdigit(*format & 255))
  228. {
  229. if (tptr < (tformat + sizeof(tformat) - 1))
  230. *tptr++ = *format;
  231. prec = prec * 10 + *format++ - '0';
  232. }
  233. }
  234. }
  235. else
  236. prec = -1;
  237. if (*format == 'l' && format[1] == 'l')
  238. {
  239. size = 'L';
  240. if (tptr < (tformat + sizeof(tformat) - 2))
  241. {
  242. *tptr++ = 'l';
  243. *tptr++ = 'l';
  244. }
  245. format += 2;
  246. }
  247. else if (*format == 'h' || *format == 'l' || *format == 'L')
  248. {
  249. if (tptr < (tformat + sizeof(tformat) - 1))
  250. *tptr++ = *format;
  251. size = *format++;
  252. }
  253. if (!*format)
  254. break;
  255. if (tptr < (tformat + sizeof(tformat) - 1))
  256. *tptr++ = *format;
  257. type = *format++;
  258. *tptr = '\0';
  259. switch (type)
  260. {
  261. case 'E' : /* Floating point formats */
  262. case 'G' :
  263. case 'e' :
  264. case 'f' :
  265. case 'g' :
  266. if ((width + 2) > sizeof(temp))
  267. break;
  268. sprintf(temp, tformat, va_arg(ap, double));
  269. bytes += strlen(temp);
  270. if (bufptr)
  271. {
  272. if ((bufptr + strlen(temp)) > bufend)
  273. {
  274. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  275. bufptr = bufend;
  276. }
  277. else
  278. {
  279. strcpy(bufptr, temp);
  280. bufptr += strlen(temp);
  281. }
  282. }
  283. break;
  284. case 'B' : /* Integer formats */
  285. case 'X' :
  286. case 'b' :
  287. case 'd' :
  288. case 'i' :
  289. case 'o' :
  290. case 'u' :
  291. case 'x' :
  292. if ((width + 2) > sizeof(temp))
  293. break;
  294. #ifdef HAVE_LONG_LONG
  295. if (size == 'L')
  296. sprintf(temp, tformat, va_arg(ap, long long));
  297. else
  298. #endif /* HAVE_LONG_LONG */
  299. sprintf(temp, tformat, va_arg(ap, int));
  300. bytes += strlen(temp);
  301. if (bufptr)
  302. {
  303. if ((bufptr + strlen(temp)) > bufend)
  304. {
  305. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  306. bufptr = bufend;
  307. }
  308. else
  309. {
  310. strcpy(bufptr, temp);
  311. bufptr += strlen(temp);
  312. }
  313. }
  314. break;
  315. case 'p' : /* Pointer value */
  316. if ((width + 2) > sizeof(temp))
  317. break;
  318. sprintf(temp, tformat, va_arg(ap, void *));
  319. bytes += strlen(temp);
  320. if (bufptr)
  321. {
  322. if ((bufptr + strlen(temp)) > bufend)
  323. {
  324. strncpy(bufptr, temp, (size_t)(bufend - bufptr));
  325. bufptr = bufend;
  326. }
  327. else
  328. {
  329. strcpy(bufptr, temp);
  330. bufptr += strlen(temp);
  331. }
  332. }
  333. break;
  334. case 'c' : /* Character or character array */
  335. bytes += width;
  336. if (bufptr)
  337. {
  338. if (width <= 1)
  339. *bufptr++ = va_arg(ap, int);
  340. else
  341. {
  342. if ((bufptr + width) > bufend)
  343. width = bufend - bufptr;
  344. memcpy(bufptr, va_arg(ap, char *), (size_t)width);
  345. bufptr += width;
  346. }
  347. }
  348. break;
  349. case 's' : /* String */
  350. if ((s = va_arg(ap, char *)) == NULL)
  351. s = "(null)";
  352. slen = strlen(s);
  353. if (slen > width && prec != width)
  354. width = slen;
  355. bytes += width;
  356. if (bufptr)
  357. {
  358. if ((bufptr + width) > bufend)
  359. width = bufend - bufptr;
  360. if (slen > width)
  361. slen = width;
  362. if (sign == '-')
  363. {
  364. strncpy(bufptr, s, (size_t)slen);
  365. memset(bufptr + slen, ' ', (size_t)(width - slen));
  366. }
  367. else
  368. {
  369. memset(bufptr, ' ', (size_t)(width - slen));
  370. strncpy(bufptr + width - slen, s, (size_t)slen);
  371. }
  372. bufptr += width;
  373. }
  374. break;
  375. case 'n' : /* Output number of chars so far */
  376. *(va_arg(ap, int *)) = bytes;
  377. break;
  378. }
  379. }
  380. else
  381. {
  382. bytes ++;
  383. if (bufptr && bufptr < bufend)
  384. *bufptr++ = *format;
  385. format ++;
  386. }
  387. }
  388. /*
  389. * Nul-terminate the string and return the number of characters needed.
  390. */
  391. *bufptr = '\0';
  392. return (bytes);
  393. }
  394. #endif /* !HAVE_VSNPRINTF */
  395. /*
  396. * '_mxml_vstrdupf()' - Format and duplicate a string.
  397. */
  398. char * /* O - New string pointer */
  399. _mxml_vstrdupf(const char *format, /* I - Printf-style format string */
  400. va_list ap) /* I - Pointer to additional arguments */
  401. {
  402. #ifdef HAVE_VASPRINTF
  403. char *s; /* String */
  404. if (vasprintf(&s, format, ap) < 0)
  405. s = NULL;
  406. return (s);
  407. #else
  408. int bytes; /* Number of bytes required */
  409. char *buffer, /* String buffer */
  410. temp[256]; /* Small buffer for first vsnprintf */
  411. /*
  412. * First format with a tiny buffer; this will tell us how many bytes are
  413. * needed...
  414. */
  415. # ifdef _WIN32
  416. bytes = _vscprintf(format, ap);
  417. # else
  418. va_list apcopy; /* Copy of argument list */
  419. va_copy(apcopy, ap);
  420. if ((bytes = vsnprintf(temp, sizeof(temp), format, apcopy)) < sizeof(temp))
  421. {
  422. /*
  423. * Hey, the formatted string fits in the tiny buffer, so just dup that...
  424. */
  425. return (strdup(temp));
  426. }
  427. # endif /* _WIN32 */
  428. /*
  429. * Allocate memory for the whole thing and reformat to the new buffer...
  430. */
  431. if ((buffer = calloc(1, bytes + 1)) != NULL)
  432. vsnprintf(buffer, bytes + 1, format, ap);
  433. /*
  434. * Return the new string...
  435. */
  436. return (buffer);
  437. #endif /* HAVE_VASPRINTF */
  438. }