You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printf.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Copyright (C) 2002-2015 Igor Sysoev
  17. * Copyright (C) 2011-2015 Nginx, Inc.
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. * * Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * * Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  29. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include "printf.h"
  40. #include "str_util.h"
  41. #include "contrib/fpconv/fpconv.h"
  42. /**
  43. * From FreeBSD libutil code
  44. */
  45. static const int maxscale = 6;
  46. static const gchar _hex[] = "0123456789abcdef";
  47. static const gchar _HEX[] = "0123456789ABCDEF";
  48. static gchar *
  49. rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes)
  50. {
  51. const gchar *prefixes;
  52. int i, r, remainder, sign;
  53. gint64 divisor;
  54. gsize len = last - buf;
  55. remainder = 0;
  56. if (!bytes) {
  57. divisor = 1000;
  58. prefixes = "\0\0\0\0k\0\0\0M\0\0\0G\0\0\0T\0\0\0P\0\0\0E";
  59. }
  60. else {
  61. divisor = 1024;
  62. prefixes = "B\0\0\0KiB\0MiB\0GiB\0TiB\0PiB\0EiB";
  63. }
  64. #define SCALE2PREFIX(scale) (&prefixes[(scale) * 4])
  65. if (num < 0) {
  66. sign = -1;
  67. num = -num;
  68. }
  69. else {
  70. sign = 1;
  71. }
  72. /*
  73. * Divide the number until it fits the given column.
  74. * If there will be an overflow by the rounding below,
  75. * divide once more.
  76. */
  77. for (i = 0; i < maxscale && num > divisor; i++) {
  78. remainder = num % divisor;
  79. num /= divisor;
  80. }
  81. if (remainder == 0 || num > divisor / 2) {
  82. r = rspamd_snprintf (buf, len, "%L%s",
  83. sign * (num + (remainder + 50) / divisor),
  84. SCALE2PREFIX (i));
  85. }
  86. else {
  87. /* Floating point version */
  88. r = rspamd_snprintf (buf, len, "%.2f%s",
  89. sign * (num + remainder / (gdouble)divisor),
  90. SCALE2PREFIX (i));
  91. }
  92. #undef SCALE2PREFIX
  93. return buf + r;
  94. }
  95. static inline unsigned
  96. rspamd_decimal_digits32 (guint32 val)
  97. {
  98. static const guint32 powers_of_10[] = {
  99. 0,
  100. 10,
  101. 100,
  102. 1000,
  103. 10000,
  104. 100000,
  105. 1000000,
  106. 10000000,
  107. 100000000,
  108. 1000000000
  109. };
  110. unsigned tmp;
  111. #if defined(_MSC_VER)
  112. unsigned long r = 0;
  113. _BitScanReverse (&r, val | 1);
  114. tmp = (r + 1) * 1233 >> 12;
  115. #elif defined(__GNUC__) && (__GNUC__ >= 3)
  116. tmp = (32 - __builtin_clz (val | 1U)) * 1233 >> 12;
  117. #else /* Software version */
  118. static const unsigned debruijn_tbl[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
  119. 11, 14, 16, 18, 22, 25, 3, 30,
  120. 8, 12, 20, 28, 15, 17, 24, 7,
  121. 19, 27, 23, 6, 26, 5, 4, 31 };
  122. guint32 v = val | 1;
  123. v |= v >> 1;
  124. v |= v >> 2;
  125. v |= v >> 4;
  126. v |= v >> 8;
  127. v |= v >> 16;
  128. tmp = (1 + debruijn_tbl[(v * 0x07C4ACDDU) >> 27]) * 1233 >> 12;
  129. #endif
  130. return tmp - (val < powers_of_10[tmp]) + 1;
  131. }
  132. static inline unsigned
  133. rspamd_decimal_digits64 (guint64 val)
  134. {
  135. static const guint64 powers_of_10[] = {
  136. 0,
  137. 10ULL,
  138. 100ULL,
  139. 1000ULL,
  140. 10000ULL,
  141. 100000ULL,
  142. 1000000ULL,
  143. 10000000ULL,
  144. 100000000ULL,
  145. 1000000000ULL,
  146. 10000000000ULL,
  147. 100000000000ULL,
  148. 1000000000000ULL,
  149. 10000000000000ULL,
  150. 100000000000000ULL,
  151. 1000000000000000ULL,
  152. 10000000000000000ULL,
  153. 100000000000000000ULL,
  154. 1000000000000000000ULL,
  155. 10000000000000000000ULL
  156. };
  157. unsigned tmp;
  158. #if defined(_MSC_VER)
  159. #if _M_IX86
  160. unsigned long r = 0;
  161. guint64 m = val | 1;
  162. if (_BitScanReverse (&r, m >> 32)) {
  163. r += 32;
  164. }
  165. else {
  166. _BitScanReverse (&r, m & 0xFFFFFFFF);
  167. }
  168. tmp = (r + 1) * 1233 >> 12;
  169. #else
  170. unsigned long r = 0;
  171. _BitScanReverse64 (&r, val | 1);
  172. tmp = (r + 1) * 1233 >> 12;
  173. #endif
  174. #elif defined(__GNUC__) && (__GNUC__ >= 3)
  175. tmp = (64 - __builtin_clzll (val | 1ULL)) * 1233 >> 12;
  176. #else /* Software version */
  177. static const unsigned debruijn_tbl[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
  178. 11, 14, 16, 18, 22, 25, 3, 30,
  179. 8, 12, 20, 28, 15, 17, 24, 7,
  180. 19, 27, 23, 6, 26, 5, 4, 31 };
  181. guint32 v = val >> 32;
  182. if (v) {
  183. v |= 1;
  184. v |= v >> 1;
  185. v |= v >> 2;
  186. v |= v >> 4;
  187. v |= v >> 8;
  188. v |= v >> 16;
  189. tmp = 32 + debruijn_tbl[(v * 0x07C4ACDDU) >> 27];
  190. }
  191. else {
  192. v = val & 0xFFFFFFFF;
  193. v |= 1;
  194. v |= v >> 1;
  195. v |= v >> 2;
  196. v |= v >> 4;
  197. v |= v >> 8;
  198. v |= v >> 16;
  199. tmp = debruijn_tbl[(v * 0x07C4ACDDU) >> 27];
  200. }
  201. tmp = (tmp + 1) * 1233 >> 12;
  202. #endif
  203. return tmp - (val < powers_of_10[tmp]) + 1;
  204. }
  205. /*
  206. * Idea from https://github.com/miloyip/itoa-benchmark:
  207. * Uses lookup table (LUT) of digit pairs for division/modulo of 100.
  208. *
  209. * Mentioned in:
  210. * https://www.slideshare.net/andreialexandrescu1/three-optimization-tips-for-c-15708507
  211. */
  212. static const char int_lookup_table[200] = {
  213. '0','0','0','1','0','2','0','3','0','4',
  214. '0','5','0','6','0','7','0','8','0','9',
  215. '1','0','1','1','1','2','1','3','1','4',
  216. '1','5','1','6','1','7','1','8','1','9',
  217. '2','0','2','1','2','2','2','3','2','4',
  218. '2','5','2','6','2','7','2','8','2','9',
  219. '3','0','3','1','3','2','3','3','3','4',
  220. '3','5','3','6','3','7','3','8','3','9',
  221. '4','0','4','1','4','2','4','3','4','4',
  222. '4','5','4','6','4','7','4','8','4','9',
  223. '5','0','5','1','5','2','5','3','5','4',
  224. '5','5','5','6','5','7','5','8','5','9',
  225. '6','0','6','1','6','2','6','3','6','4',
  226. '6','5','6','6','6','7','6','8','6','9',
  227. '7','0','7','1','7','2','7','3','7','4',
  228. '7','5','7','6','7','7','7','8','7','9',
  229. '8','0','8','1','8','2','8','3','8','4',
  230. '8','5','8','6','8','7','8','8','8','9',
  231. '9','0','9','1','9','2','9','3','9','4',
  232. '9','5','9','6','9','7','9','8','9','9'
  233. };
  234. static inline guint
  235. rspamd_uint32_print (guint32 in, gchar *out)
  236. {
  237. guint ndigits = rspamd_decimal_digits32 (in);
  238. gchar *p;
  239. p = out + ndigits - 1;
  240. while (in >= 100) {
  241. unsigned idx = (in % 100) * 2;
  242. /* Do two digits at once */
  243. *p-- = int_lookup_table[idx + 1];
  244. *p-- = int_lookup_table[idx];
  245. in /= 100;
  246. }
  247. if (in < 10) {
  248. *p = ((char)in) + '0';
  249. }
  250. else {
  251. unsigned idx = in * 2;
  252. *p-- = int_lookup_table[idx + 1];
  253. *p = int_lookup_table[idx];
  254. }
  255. return ndigits;
  256. }
  257. static inline guint
  258. rspamd_uint64_print (guint64 in, gchar *out)
  259. {
  260. guint ndigits = rspamd_decimal_digits64 (in);
  261. guint32 v32;
  262. gchar *p;
  263. p = out + ndigits - 1;
  264. while (in >= 100000000) {
  265. v32 = (guint32)(in % 100000000);
  266. guint32 a, b, a1, a2, b1, b2;
  267. /* Initial spill */
  268. a = v32 / 10000;
  269. b = v32 % 10000;
  270. a1 = (a / 100) * 2;
  271. a2 = (a % 100) * 2;
  272. b1 = (b / 100) * 2;
  273. b2 = (b % 100) * 2;
  274. /* Fill 8 digits at once */
  275. *p-- = int_lookup_table[b2 + 1];
  276. *p-- = int_lookup_table[b2];
  277. *p-- = int_lookup_table[b1 + 1];
  278. *p-- = int_lookup_table[b1];
  279. *p-- = int_lookup_table[a2 + 1];
  280. *p-- = int_lookup_table[a2];
  281. *p-- = int_lookup_table[a1 + 1];
  282. *p-- = int_lookup_table[a1];
  283. in /= 100000000;
  284. }
  285. /* Remaining 32 bit */
  286. v32 = (guint32)in;
  287. while (v32 >= 100) {
  288. unsigned idx = (v32 % 100) << 1;
  289. /* Do 2 digits at once */
  290. *p-- = int_lookup_table[idx + 1];
  291. *p-- = int_lookup_table[idx];
  292. v32 /= 100;
  293. }
  294. if (v32 < 10) {
  295. *p = ((char)v32) + '0';
  296. }
  297. else {
  298. unsigned idx = v32 * 2;
  299. *p-- = int_lookup_table[idx + 1];
  300. *p = int_lookup_table[idx];
  301. }
  302. return ndigits;
  303. }
  304. static gchar *
  305. rspamd_sprintf_num (gchar *buf, gchar *last, guint64 ui64, gchar zero,
  306. guint hexadecimal, guint width)
  307. {
  308. gchar *p, temp[sizeof ("18446744073709551615")];
  309. size_t len;
  310. if (hexadecimal == 0) {
  311. p = temp;
  312. if (ui64 < G_MAXUINT32) {
  313. len = rspamd_uint32_print ((guint32)ui64, temp);
  314. }
  315. else {
  316. len = rspamd_uint64_print (ui64, temp);
  317. }
  318. }
  319. else if (hexadecimal == 1) {
  320. p = temp + sizeof(temp);
  321. do {
  322. *--p = _hex[(guint32) (ui64 & 0xf)];
  323. } while (ui64 >>= 4);
  324. len = (temp + sizeof (temp)) - p;
  325. }
  326. else { /* hexadecimal == 2 */
  327. p = temp + sizeof(temp);
  328. do {
  329. *--p = _HEX[(guint32) (ui64 & 0xf)];
  330. } while (ui64 >>= 4);
  331. len = (temp + sizeof (temp)) - p;
  332. }
  333. /* zero or space padding */
  334. if (len < width) {
  335. width -= len;
  336. while (width-- > 0 && buf < last) {
  337. *buf++ = zero;
  338. }
  339. }
  340. /* number safe copy */
  341. if (buf + len > last) {
  342. len = last - buf;
  343. }
  344. return ((gchar *)memcpy (buf, p, len)) + len;
  345. }
  346. struct rspamd_printf_char_buf {
  347. char *begin;
  348. char *pos;
  349. glong remain;
  350. };
  351. static glong
  352. rspamd_printf_append_char (const gchar *buf, glong buflen, gpointer ud)
  353. {
  354. struct rspamd_printf_char_buf *dst = (struct rspamd_printf_char_buf *)ud;
  355. glong wr;
  356. if (dst->remain <= 0) {
  357. return dst->remain;
  358. }
  359. wr = MIN (dst->remain, buflen);
  360. memcpy (dst->pos, buf, wr);
  361. dst->remain -= wr;
  362. dst->pos += wr;
  363. return wr;
  364. }
  365. static glong
  366. rspamd_printf_append_file (const gchar *buf, glong buflen, gpointer ud)
  367. {
  368. FILE *dst = (FILE *)ud;
  369. if (buflen > 0) {
  370. return fwrite (buf, 1, buflen, dst);
  371. }
  372. else {
  373. return 0;
  374. }
  375. }
  376. static glong
  377. rspamd_printf_append_gstring (const gchar *buf, glong buflen, gpointer ud)
  378. {
  379. GString *dst = (GString *)ud;
  380. if (buflen > 0) {
  381. g_string_append_len (dst, buf, buflen);
  382. }
  383. return buflen;
  384. }
  385. static glong
  386. rspamd_printf_append_fstring (const gchar *buf, glong buflen, gpointer ud)
  387. {
  388. rspamd_fstring_t **dst = ud;
  389. if (buflen > 0) {
  390. *dst = rspamd_fstring_append (*dst, buf, buflen);
  391. }
  392. return buflen;
  393. }
  394. glong
  395. rspamd_fprintf (FILE *f, const gchar *fmt, ...)
  396. {
  397. va_list args;
  398. glong r;
  399. va_start (args, fmt);
  400. r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args);
  401. va_end (args);
  402. return r;
  403. }
  404. glong
  405. rspamd_printf (const gchar *fmt, ...)
  406. {
  407. va_list args;
  408. glong r;
  409. va_start (args, fmt);
  410. r = rspamd_vprintf_common (rspamd_printf_append_file, stdout, fmt, args);
  411. va_end (args);
  412. return r;
  413. }
  414. glong
  415. rspamd_log_fprintf (FILE *f, const gchar *fmt, ...)
  416. {
  417. va_list args;
  418. glong r;
  419. va_start (args, fmt);
  420. r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args);
  421. va_end (args);
  422. fflush (f);
  423. return r;
  424. }
  425. glong
  426. rspamd_snprintf (gchar *buf, glong max, const gchar *fmt, ...)
  427. {
  428. gchar *r;
  429. va_list args;
  430. va_start (args, fmt);
  431. r = rspamd_vsnprintf (buf, max, fmt, args);
  432. va_end (args);
  433. return (r - buf);
  434. }
  435. gchar *
  436. rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, va_list args)
  437. {
  438. struct rspamd_printf_char_buf dst;
  439. dst.begin = buf;
  440. dst.pos = dst.begin;
  441. dst.remain = max - 1;
  442. (void)rspamd_vprintf_common (rspamd_printf_append_char, &dst, fmt, args);
  443. *dst.pos = '\0';
  444. return dst.pos;
  445. }
  446. glong
  447. rspamd_printf_gstring (GString *s, const gchar *fmt, ...)
  448. {
  449. va_list args;
  450. glong r;
  451. va_start (args, fmt);
  452. r = rspamd_vprintf_gstring (s, fmt, args);
  453. va_end (args);
  454. return r;
  455. }
  456. glong
  457. rspamd_vprintf_gstring (GString *s, const gchar *fmt, va_list args)
  458. {
  459. return rspamd_vprintf_common (rspamd_printf_append_gstring, s, fmt, args);
  460. }
  461. glong
  462. rspamd_printf_fstring (rspamd_fstring_t **s, const gchar *fmt, ...)
  463. {
  464. va_list args;
  465. glong r;
  466. va_start (args, fmt);
  467. r = rspamd_vprintf_fstring (s, fmt, args);
  468. va_end (args);
  469. return r;
  470. }
  471. glong
  472. rspamd_vprintf_fstring (rspamd_fstring_t **s, const gchar *fmt, va_list args)
  473. {
  474. return rspamd_vprintf_common (rspamd_printf_append_fstring, s, fmt, args);
  475. }
  476. #define RSPAMD_PRINTF_APPEND(buf, len) \
  477. do { \
  478. RSPAMD_PRINTF_APPEND_BUF(buf, len); \
  479. fmt++; \
  480. buf_start = fmt; \
  481. } while (0)
  482. #define RSPAMD_PRINTF_APPEND_BUF(buf, len) \
  483. do { \
  484. wr = func ((buf), (len), apd); \
  485. if (wr < (__typeof (wr))(len)) { \
  486. goto oob; \
  487. } \
  488. written += wr; \
  489. } while (0)
  490. glong
  491. rspamd_vprintf_common (rspamd_printf_append_func func,
  492. gpointer apd,
  493. const gchar *fmt,
  494. va_list args)
  495. {
  496. gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], dtoabuf[32], *p, *last;
  497. guchar c;
  498. const gchar *buf_start = fmt;
  499. gint d;
  500. gdouble f;
  501. glong written = 0, wr, slen;
  502. gint64 i64;
  503. guint64 ui64;
  504. guint width, sign, hex, humanize, bytes, frac_width, b32, b64;
  505. rspamd_fstring_t *v;
  506. rspamd_ftok_t *tok;
  507. GString *gs;
  508. GError *err;
  509. while (*fmt) {
  510. /*
  511. * "buf < last" means that we could copy at least one character:
  512. * the plain character, "%%", "%c", and minus without the checking
  513. */
  514. if (*fmt == '%') {
  515. /* Append what we have in buf */
  516. if (fmt > buf_start) {
  517. wr = func (buf_start, fmt - buf_start, apd);
  518. if (wr <= 0) {
  519. goto oob;
  520. }
  521. written += wr;
  522. }
  523. i64 = 0;
  524. ui64 = 0;
  525. zero = (gchar) ((*++fmt == '0') ? '0' : ' ');
  526. width = 0;
  527. sign = 1;
  528. hex = 0;
  529. b32 = 0;
  530. b64 = 0;
  531. bytes = 0;
  532. humanize = 0;
  533. frac_width = 0;
  534. slen = -1;
  535. while (*fmt >= '0' && *fmt <= '9') {
  536. width = width * 10 + *fmt++ - '0';
  537. }
  538. for (;; ) {
  539. switch (*fmt) {
  540. case 'u':
  541. sign = 0;
  542. fmt++;
  543. continue;
  544. case 'm':
  545. fmt++;
  546. continue;
  547. case 'X':
  548. hex = 2;
  549. sign = 0;
  550. fmt++;
  551. continue;
  552. case 'x':
  553. hex = 1;
  554. sign = 0;
  555. fmt++;
  556. continue;
  557. case 'b':
  558. b32 = 1;
  559. sign = 0;
  560. fmt++;
  561. continue;
  562. case 'B':
  563. b64 = 1;
  564. sign = 0;
  565. fmt++;
  566. continue;
  567. case 'H':
  568. humanize = 1;
  569. bytes = 1;
  570. sign = 0;
  571. fmt++;
  572. continue;
  573. case 'h':
  574. humanize = 1;
  575. sign = 0;
  576. fmt++;
  577. continue;
  578. case '.':
  579. fmt++;
  580. if (*fmt == '*') {
  581. d = (gint)va_arg (args, gint);
  582. if (G_UNLIKELY (d < 0)) {
  583. return 0;
  584. }
  585. frac_width = (guint)d;
  586. fmt++;
  587. }
  588. else {
  589. while (*fmt >= '0' && *fmt <= '9') {
  590. frac_width = frac_width * 10 + *fmt++ - '0';
  591. }
  592. }
  593. break;
  594. case '*':
  595. d = (gint)va_arg (args, gint);
  596. if (G_UNLIKELY (d < 0)) {
  597. return 0;
  598. }
  599. slen = (glong)d;
  600. fmt++;
  601. continue;
  602. default:
  603. break;
  604. }
  605. break;
  606. }
  607. switch (*fmt) {
  608. case 'V':
  609. v = va_arg (args, rspamd_fstring_t *);
  610. if (v) {
  611. slen = v->len;
  612. if (G_UNLIKELY (width != 0)) {
  613. slen = MIN (v->len, width);
  614. }
  615. RSPAMD_PRINTF_APPEND (v->str, slen);
  616. }
  617. else {
  618. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  619. }
  620. continue;
  621. case 'T':
  622. tok = va_arg (args, rspamd_ftok_t *);
  623. if (tok) {
  624. slen = tok->len;
  625. if (G_UNLIKELY (width != 0)) {
  626. slen = MIN (tok->len, width);
  627. }
  628. RSPAMD_PRINTF_APPEND (tok->begin, slen);
  629. }
  630. else {
  631. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  632. }
  633. continue;
  634. case 'v':
  635. gs = va_arg (args, GString *);
  636. if (gs) {
  637. slen = gs->len;
  638. if (G_UNLIKELY (width != 0)) {
  639. slen = MIN (gs->len, width);
  640. }
  641. RSPAMD_PRINTF_APPEND (gs->str, slen);
  642. }
  643. else {
  644. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  645. }
  646. continue;
  647. case 'e':
  648. err = va_arg (args, GError *);
  649. if (err) {
  650. p = err->message;
  651. if (p == NULL) {
  652. p = "(NULL)";
  653. }
  654. }
  655. else {
  656. p = "unknown error";
  657. }
  658. slen = strlen (p);
  659. RSPAMD_PRINTF_APPEND (p, slen);
  660. continue;
  661. case 's':
  662. p = va_arg (args, gchar *);
  663. if (p == NULL) {
  664. p = "(NULL)";
  665. slen = sizeof ("(NULL)") - 1;
  666. }
  667. if (G_UNLIKELY (b32)) {
  668. gchar *b32buf;
  669. if (G_UNLIKELY (slen == -1)) {
  670. if (G_LIKELY (width != 0)) {
  671. slen = width;
  672. }
  673. else {
  674. /* NULL terminated string */
  675. slen = strlen (p);
  676. }
  677. }
  678. b32buf = rspamd_encode_base32 (p, slen, RSPAMD_BASE32_DEFAULT);
  679. if (b32buf) {
  680. RSPAMD_PRINTF_APPEND (b32buf, strlen (b32buf));
  681. g_free (b32buf);
  682. }
  683. else {
  684. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  685. }
  686. }
  687. else if (G_UNLIKELY (hex)) {
  688. gchar hexbuf[2];
  689. if (G_UNLIKELY (slen == -1)) {
  690. if (G_LIKELY (width != 0)) {
  691. slen = width;
  692. }
  693. else {
  694. /* NULL terminated string */
  695. slen = strlen (p);
  696. }
  697. }
  698. while (slen) {
  699. hexbuf[0] = hex == 2 ? _HEX[(*p >> 4u) & 0xfu] :
  700. _hex[(*p >> 4u) & 0xfu];
  701. hexbuf[1] = hex == 2 ? _HEX[*p & 0xfu] : _hex[*p & 0xfu];
  702. RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2);
  703. p++;
  704. slen--;
  705. }
  706. fmt++;
  707. buf_start = fmt;
  708. }
  709. else if (G_UNLIKELY (b64)) {
  710. gchar *b64buf;
  711. gsize olen = 0;
  712. if (G_UNLIKELY (slen == -1)) {
  713. if (G_LIKELY (width != 0)) {
  714. slen = width;
  715. }
  716. else {
  717. /* NULL terminated string */
  718. slen = strlen (p);
  719. }
  720. }
  721. b64buf = rspamd_encode_base64 (p, slen, 0, &olen);
  722. if (b64buf) {
  723. RSPAMD_PRINTF_APPEND (b64buf, olen);
  724. g_free (b64buf);
  725. }
  726. else {
  727. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  728. }
  729. }
  730. else {
  731. if (slen == -1) {
  732. /* NULL terminated string */
  733. slen = strlen (p);
  734. }
  735. if (G_UNLIKELY (width != 0)) {
  736. slen = MIN (slen, width);
  737. }
  738. RSPAMD_PRINTF_APPEND (p, slen);
  739. }
  740. continue;
  741. case 'O':
  742. i64 = (gint64) va_arg (args, off_t);
  743. sign = 1;
  744. break;
  745. case 'P':
  746. i64 = (gint64) va_arg (args, pid_t);
  747. sign = 1;
  748. break;
  749. case 't':
  750. i64 = (gint64) va_arg (args, time_t);
  751. sign = 1;
  752. break;
  753. case 'z':
  754. if (sign) {
  755. i64 = (gint64) va_arg (args, ssize_t);
  756. } else {
  757. ui64 = (guint64) va_arg (args, size_t);
  758. }
  759. break;
  760. case 'd':
  761. if (sign) {
  762. i64 = (gint64) va_arg (args, gint);
  763. } else {
  764. ui64 = (guint64) va_arg (args, guint);
  765. }
  766. break;
  767. case 'l':
  768. if (sign) {
  769. i64 = (gint64) va_arg (args, glong);
  770. } else {
  771. ui64 = (guint64) va_arg (args, gulong);
  772. }
  773. break;
  774. case 'D':
  775. if (sign) {
  776. i64 = (gint64) va_arg (args, gint32);
  777. } else {
  778. ui64 = (guint64) va_arg (args, guint32);
  779. }
  780. break;
  781. case 'L':
  782. if (sign) {
  783. i64 = va_arg (args, gint64);
  784. } else {
  785. ui64 = va_arg (args, guint64);
  786. }
  787. break;
  788. case 'f':
  789. f = (gdouble) va_arg (args, double);
  790. slen = fpconv_dtoa (f, dtoabuf, frac_width, false);
  791. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  792. continue;
  793. case 'g':
  794. f = (gdouble) va_arg (args, double);
  795. slen = fpconv_dtoa (f, dtoabuf, 0, true);
  796. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  797. continue;
  798. case 'F':
  799. f = (gdouble) va_arg (args, long double);
  800. slen = fpconv_dtoa (f, dtoabuf, frac_width, false);
  801. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  802. continue;
  803. case 'G':
  804. f = (gdouble) va_arg (args, long double);
  805. slen = fpconv_dtoa (f, dtoabuf, 0, true);
  806. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  807. continue;
  808. case 'p':
  809. ui64 = (uintptr_t) va_arg (args, void *);
  810. hex = 2;
  811. sign = 0;
  812. zero = '0';
  813. width = sizeof (void *) * 2;
  814. break;
  815. case 'c':
  816. c = va_arg (args, gint);
  817. c &= 0xffu;
  818. if (G_UNLIKELY (hex)) {
  819. gchar hexbuf[2];
  820. hexbuf[0] = hex == 2 ? _HEX[(c >> 4u) & 0xfu] :
  821. _hex[(c >> 4u) & 0xfu];
  822. hexbuf[1] = hex == 2 ? _HEX[c & 0xfu] : _hex[c & 0xfu];
  823. RSPAMD_PRINTF_APPEND (hexbuf, 2);
  824. }
  825. else {
  826. RSPAMD_PRINTF_APPEND (&c, 1);
  827. }
  828. continue;
  829. case 'Z':
  830. c = '\0';
  831. RSPAMD_PRINTF_APPEND (&c, 1);
  832. continue;
  833. case 'N':
  834. c = '\n';
  835. RSPAMD_PRINTF_APPEND (&c, 1);
  836. continue;
  837. case '%':
  838. c = '%';
  839. RSPAMD_PRINTF_APPEND (&c, 1);
  840. continue;
  841. default:
  842. c = *fmt;
  843. RSPAMD_PRINTF_APPEND (&c, 1);
  844. continue;
  845. }
  846. /* Print number */
  847. p = numbuf;
  848. last = p + sizeof (numbuf);
  849. if (sign) {
  850. if (i64 < 0) {
  851. *p++ = '-';
  852. ui64 = (guint64) - i64;
  853. } else {
  854. ui64 = (guint64) i64;
  855. }
  856. }
  857. if (!humanize) {
  858. p = rspamd_sprintf_num (p, last, ui64, zero, hex, width);
  859. }
  860. else {
  861. p = rspamd_humanize_number (p, last, ui64, bytes);
  862. }
  863. slen = p - numbuf;
  864. RSPAMD_PRINTF_APPEND (numbuf, slen);
  865. } else {
  866. fmt++;
  867. }
  868. }
  869. /* Finish buffer */
  870. if (fmt > buf_start) {
  871. wr = func (buf_start, fmt - buf_start, apd);
  872. if (wr <= 0) {
  873. goto oob;
  874. }
  875. written += wr;
  876. }
  877. oob:
  878. return written;
  879. }