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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 binary, guint width)
  307. {
  308. gchar *p, temp[64];
  309. size_t len = 0;
  310. if (G_LIKELY(hexadecimal == 0 && binary == 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 if (hexadecimal == 2) { /* 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. else if (binary > 0) {
  334. int first_bit = MIN(sizeof(temp), ffsll(ui64));
  335. p = temp + sizeof(temp);
  336. for (int i = 0; i <= first_bit; i ++, ui64 >>= 1) {
  337. *--p = '0' + (ui64 & 0x1);
  338. }
  339. len = (temp + sizeof (temp)) - p;
  340. }
  341. /* zero or space padding */
  342. if (len < width) {
  343. width -= len;
  344. while (width-- > 0 && buf < last) {
  345. *buf++ = zero;
  346. }
  347. }
  348. /* number safe copy */
  349. if (buf + len > last) {
  350. len = last - buf;
  351. }
  352. return ((gchar *)memcpy (buf, p, len)) + len;
  353. }
  354. struct rspamd_printf_char_buf {
  355. char *begin;
  356. char *pos;
  357. glong remain;
  358. };
  359. static glong
  360. rspamd_printf_append_char (const gchar *buf, glong buflen, gpointer ud)
  361. {
  362. struct rspamd_printf_char_buf *dst = (struct rspamd_printf_char_buf *)ud;
  363. glong wr;
  364. if (dst->remain <= 0) {
  365. return dst->remain;
  366. }
  367. wr = MIN (dst->remain, buflen);
  368. memcpy (dst->pos, buf, wr);
  369. dst->remain -= wr;
  370. dst->pos += wr;
  371. return wr;
  372. }
  373. static glong
  374. rspamd_printf_append_file (const gchar *buf, glong buflen, gpointer ud)
  375. {
  376. FILE *dst = (FILE *)ud;
  377. if (buflen > 0) {
  378. return fwrite (buf, 1, buflen, dst);
  379. }
  380. else {
  381. return 0;
  382. }
  383. }
  384. static glong
  385. rspamd_printf_append_gstring (const gchar *buf, glong buflen, gpointer ud)
  386. {
  387. GString *dst = (GString *)ud;
  388. if (buflen > 0) {
  389. g_string_append_len (dst, buf, buflen);
  390. }
  391. return buflen;
  392. }
  393. static glong
  394. rspamd_printf_append_fstring (const gchar *buf, glong buflen, gpointer ud)
  395. {
  396. rspamd_fstring_t **dst = ud;
  397. if (buflen > 0) {
  398. *dst = rspamd_fstring_append (*dst, buf, buflen);
  399. }
  400. return buflen;
  401. }
  402. glong
  403. rspamd_fprintf (FILE *f, const gchar *fmt, ...)
  404. {
  405. va_list args;
  406. glong r;
  407. va_start (args, fmt);
  408. r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args);
  409. va_end (args);
  410. return r;
  411. }
  412. glong
  413. rspamd_printf (const gchar *fmt, ...)
  414. {
  415. va_list args;
  416. glong r;
  417. va_start (args, fmt);
  418. r = rspamd_vprintf_common (rspamd_printf_append_file, stdout, fmt, args);
  419. va_end (args);
  420. return r;
  421. }
  422. glong
  423. rspamd_log_fprintf (FILE *f, const gchar *fmt, ...)
  424. {
  425. va_list args;
  426. glong r;
  427. va_start (args, fmt);
  428. r = rspamd_vprintf_common (rspamd_printf_append_file, f, fmt, args);
  429. va_end (args);
  430. fflush (f);
  431. return r;
  432. }
  433. glong
  434. rspamd_snprintf (gchar *buf, glong max, const gchar *fmt, ...)
  435. {
  436. gchar *r;
  437. va_list args;
  438. va_start (args, fmt);
  439. r = rspamd_vsnprintf (buf, max, fmt, args);
  440. va_end (args);
  441. return (r - buf);
  442. }
  443. gchar *
  444. rspamd_vsnprintf (gchar *buf, glong max, const gchar *fmt, va_list args)
  445. {
  446. struct rspamd_printf_char_buf dst;
  447. dst.begin = buf;
  448. dst.pos = dst.begin;
  449. dst.remain = max - 1;
  450. (void)rspamd_vprintf_common (rspamd_printf_append_char, &dst, fmt, args);
  451. *dst.pos = '\0';
  452. return dst.pos;
  453. }
  454. glong
  455. rspamd_printf_gstring (GString *s, const gchar *fmt, ...)
  456. {
  457. va_list args;
  458. glong r;
  459. va_start (args, fmt);
  460. r = rspamd_vprintf_gstring (s, fmt, args);
  461. va_end (args);
  462. return r;
  463. }
  464. glong
  465. rspamd_vprintf_gstring (GString *s, const gchar *fmt, va_list args)
  466. {
  467. return rspamd_vprintf_common (rspamd_printf_append_gstring, s, fmt, args);
  468. }
  469. glong
  470. rspamd_printf_fstring (rspamd_fstring_t **s, const gchar *fmt, ...)
  471. {
  472. va_list args;
  473. glong r;
  474. va_start (args, fmt);
  475. r = rspamd_vprintf_fstring (s, fmt, args);
  476. va_end (args);
  477. return r;
  478. }
  479. glong
  480. rspamd_vprintf_fstring (rspamd_fstring_t **s, const gchar *fmt, va_list args)
  481. {
  482. return rspamd_vprintf_common (rspamd_printf_append_fstring, s, fmt, args);
  483. }
  484. #define RSPAMD_PRINTF_APPEND(buf, len) \
  485. do { \
  486. RSPAMD_PRINTF_APPEND_BUF(buf, len); \
  487. fmt++; \
  488. buf_start = fmt; \
  489. } while (0)
  490. #define RSPAMD_PRINTF_APPEND_BUF(buf, len) \
  491. do { \
  492. wr = func ((buf), (len), apd); \
  493. if (wr < (__typeof (wr))(len)) { \
  494. goto oob; \
  495. } \
  496. written += wr; \
  497. } while (0)
  498. glong
  499. rspamd_vprintf_common (rspamd_printf_append_func func,
  500. gpointer apd,
  501. const gchar *fmt,
  502. va_list args)
  503. {
  504. gchar zero, numbuf[G_ASCII_DTOSTR_BUF_SIZE], dtoabuf[32], *p, *last;
  505. guchar c;
  506. const gchar *buf_start = fmt;
  507. gint d;
  508. gdouble f;
  509. glong written = 0, wr, slen;
  510. gint64 i64;
  511. guint64 ui64;
  512. guint width, sign, hex, humanize, bytes, frac_width, b32, b64;
  513. rspamd_fstring_t *v;
  514. rspamd_ftok_t *tok;
  515. GString *gs;
  516. GError *err;
  517. while (*fmt) {
  518. /*
  519. * "buf < last" means that we could copy at least one character:
  520. * the plain character, "%%", "%c", and minus without the checking
  521. */
  522. if (*fmt == '%') {
  523. /* Append what we have in buf */
  524. if (fmt > buf_start) {
  525. wr = func (buf_start, fmt - buf_start, apd);
  526. if (wr <= 0) {
  527. goto oob;
  528. }
  529. written += wr;
  530. }
  531. i64 = 0;
  532. ui64 = 0;
  533. zero = (gchar) ((*++fmt == '0') ? '0' : ' ');
  534. width = 0;
  535. sign = 1;
  536. hex = 0;
  537. b32 = 0;
  538. b64 = 0;
  539. bytes = 0;
  540. humanize = 0;
  541. frac_width = 0;
  542. slen = -1;
  543. while (*fmt >= '0' && *fmt <= '9') {
  544. width = width * 10 + *fmt++ - '0';
  545. }
  546. for (;; ) {
  547. switch (*fmt) {
  548. case 'u':
  549. sign = 0;
  550. fmt++;
  551. continue;
  552. case 'm':
  553. fmt++;
  554. continue;
  555. case 'X':
  556. hex = 2;
  557. sign = 0;
  558. fmt++;
  559. continue;
  560. case 'x':
  561. hex = 1;
  562. sign = 0;
  563. fmt++;
  564. continue;
  565. case 'b':
  566. b32 = 1;
  567. sign = 0;
  568. fmt++;
  569. continue;
  570. case 'B':
  571. b64 = 1;
  572. sign = 0;
  573. fmt++;
  574. continue;
  575. case 'H':
  576. humanize = 1;
  577. bytes = 1;
  578. sign = 0;
  579. fmt++;
  580. continue;
  581. case 'h':
  582. humanize = 1;
  583. sign = 0;
  584. fmt++;
  585. continue;
  586. case '.':
  587. fmt++;
  588. if (*fmt == '*') {
  589. d = (gint)va_arg (args, gint);
  590. if (G_UNLIKELY (d < 0)) {
  591. return 0;
  592. }
  593. frac_width = (guint)d;
  594. fmt++;
  595. }
  596. else {
  597. while (*fmt >= '0' && *fmt <= '9') {
  598. frac_width = frac_width * 10 + *fmt++ - '0';
  599. }
  600. }
  601. break;
  602. case '*':
  603. d = (gint)va_arg (args, gint);
  604. if (G_UNLIKELY (d < 0)) {
  605. return 0;
  606. }
  607. slen = (glong)d;
  608. fmt++;
  609. continue;
  610. default:
  611. break;
  612. }
  613. break;
  614. }
  615. switch (*fmt) {
  616. case 'V':
  617. v = va_arg (args, rspamd_fstring_t *);
  618. if (v) {
  619. slen = v->len;
  620. if (G_UNLIKELY (width != 0)) {
  621. slen = MIN (v->len, width);
  622. }
  623. RSPAMD_PRINTF_APPEND (v->str, slen);
  624. }
  625. else {
  626. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  627. }
  628. continue;
  629. case 'T':
  630. tok = va_arg (args, rspamd_ftok_t *);
  631. if (tok) {
  632. slen = tok->len;
  633. if (G_UNLIKELY (width != 0)) {
  634. slen = MIN (tok->len, width);
  635. }
  636. RSPAMD_PRINTF_APPEND (tok->begin, slen);
  637. }
  638. else {
  639. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  640. }
  641. continue;
  642. case 'v':
  643. gs = va_arg (args, GString *);
  644. if (gs) {
  645. slen = gs->len;
  646. if (G_UNLIKELY (width != 0)) {
  647. slen = MIN (gs->len, width);
  648. }
  649. RSPAMD_PRINTF_APPEND (gs->str, slen);
  650. }
  651. else {
  652. RSPAMD_PRINTF_APPEND ("(NULL)", 6);
  653. }
  654. continue;
  655. case 'e':
  656. err = va_arg (args, GError *);
  657. if (err) {
  658. p = err->message;
  659. if (p == NULL) {
  660. p = "(NULL)";
  661. }
  662. }
  663. else {
  664. p = "unknown error";
  665. }
  666. slen = strlen (p);
  667. RSPAMD_PRINTF_APPEND (p, slen);
  668. continue;
  669. case 's':
  670. p = va_arg (args, gchar *);
  671. if (p == NULL) {
  672. p = "(NULL)";
  673. slen = sizeof ("(NULL)") - 1;
  674. }
  675. if (G_UNLIKELY (b32)) {
  676. gchar *b32buf;
  677. if (G_UNLIKELY (slen == -1)) {
  678. if (G_LIKELY (width != 0)) {
  679. slen = width;
  680. }
  681. else {
  682. /* NULL terminated string */
  683. slen = strlen (p);
  684. }
  685. }
  686. b32buf = rspamd_encode_base32 (p, slen, RSPAMD_BASE32_DEFAULT);
  687. if (b32buf) {
  688. RSPAMD_PRINTF_APPEND (b32buf, strlen (b32buf));
  689. g_free (b32buf);
  690. }
  691. else {
  692. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  693. }
  694. }
  695. else if (G_UNLIKELY (hex)) {
  696. gchar hexbuf[2];
  697. if (G_UNLIKELY (slen == -1)) {
  698. if (G_LIKELY (width != 0)) {
  699. slen = width;
  700. }
  701. else {
  702. /* NULL terminated string */
  703. slen = strlen (p);
  704. }
  705. }
  706. while (slen) {
  707. hexbuf[0] = hex == 2 ? _HEX[(*p >> 4u) & 0xfu] :
  708. _hex[(*p >> 4u) & 0xfu];
  709. hexbuf[1] = hex == 2 ? _HEX[*p & 0xfu] : _hex[*p & 0xfu];
  710. RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2);
  711. p++;
  712. slen--;
  713. }
  714. fmt++;
  715. buf_start = fmt;
  716. }
  717. else if (G_UNLIKELY (b64)) {
  718. gchar *b64buf;
  719. gsize olen = 0;
  720. if (G_UNLIKELY (slen == -1)) {
  721. if (G_LIKELY (width != 0)) {
  722. slen = width;
  723. }
  724. else {
  725. /* NULL terminated string */
  726. slen = strlen (p);
  727. }
  728. }
  729. b64buf = rspamd_encode_base64 (p, slen, 0, &olen);
  730. if (b64buf) {
  731. RSPAMD_PRINTF_APPEND (b64buf, olen);
  732. g_free (b64buf);
  733. }
  734. else {
  735. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  736. }
  737. }
  738. else {
  739. if (slen == -1) {
  740. /* NULL terminated string */
  741. slen = strlen (p);
  742. }
  743. if (G_UNLIKELY (width != 0)) {
  744. slen = MIN (slen, width);
  745. }
  746. RSPAMD_PRINTF_APPEND (p, slen);
  747. }
  748. continue;
  749. case 'O':
  750. i64 = (gint64) va_arg (args, off_t);
  751. sign = 1;
  752. break;
  753. case 'P':
  754. i64 = (gint64) va_arg (args, pid_t);
  755. sign = 1;
  756. break;
  757. case 't':
  758. i64 = (gint64) va_arg (args, time_t);
  759. sign = 1;
  760. break;
  761. case 'z':
  762. if (sign) {
  763. i64 = (gint64) va_arg (args, ssize_t);
  764. } else {
  765. ui64 = (guint64) va_arg (args, size_t);
  766. }
  767. break;
  768. case 'd':
  769. if (sign) {
  770. i64 = (gint64) va_arg (args, gint);
  771. } else {
  772. ui64 = (guint64) va_arg (args, guint);
  773. }
  774. break;
  775. case 'l':
  776. if (sign) {
  777. i64 = (gint64) va_arg (args, glong);
  778. } else {
  779. ui64 = (guint64) va_arg (args, gulong);
  780. }
  781. break;
  782. case 'D':
  783. if (sign) {
  784. i64 = (gint64) va_arg (args, gint32);
  785. } else {
  786. ui64 = (guint64) va_arg (args, guint32);
  787. }
  788. break;
  789. case 'L':
  790. if (sign) {
  791. i64 = va_arg (args, gint64);
  792. } else {
  793. ui64 = va_arg (args, guint64);
  794. }
  795. break;
  796. case 'f':
  797. f = (gdouble) va_arg (args, double);
  798. slen = fpconv_dtoa (f, dtoabuf, frac_width, false);
  799. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  800. continue;
  801. case 'g':
  802. f = (gdouble) va_arg (args, double);
  803. slen = fpconv_dtoa (f, dtoabuf, 0, true);
  804. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  805. continue;
  806. case 'F':
  807. f = (gdouble) va_arg (args, long double);
  808. slen = fpconv_dtoa (f, dtoabuf, frac_width, false);
  809. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  810. continue;
  811. case 'G':
  812. f = (gdouble) va_arg (args, long double);
  813. slen = fpconv_dtoa (f, dtoabuf, 0, true);
  814. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  815. continue;
  816. case 'p':
  817. ui64 = (uintptr_t) va_arg (args, void *);
  818. hex = 2;
  819. sign = 0;
  820. zero = '0';
  821. width = sizeof (void *) * 2;
  822. break;
  823. case 'c':
  824. c = va_arg (args, gint);
  825. c &= 0xffu;
  826. if (G_UNLIKELY (hex)) {
  827. gchar hexbuf[2];
  828. hexbuf[0] = hex == 2 ? _HEX[(c >> 4u) & 0xfu] :
  829. _hex[(c >> 4u) & 0xfu];
  830. hexbuf[1] = hex == 2 ? _HEX[c & 0xfu] : _hex[c & 0xfu];
  831. RSPAMD_PRINTF_APPEND (hexbuf, 2);
  832. }
  833. else {
  834. RSPAMD_PRINTF_APPEND (&c, 1);
  835. }
  836. continue;
  837. case 'Z':
  838. c = '\0';
  839. RSPAMD_PRINTF_APPEND (&c, 1);
  840. continue;
  841. case 'N':
  842. c = '\n';
  843. RSPAMD_PRINTF_APPEND (&c, 1);
  844. continue;
  845. case '%':
  846. c = '%';
  847. RSPAMD_PRINTF_APPEND (&c, 1);
  848. continue;
  849. default:
  850. c = *fmt;
  851. RSPAMD_PRINTF_APPEND (&c, 1);
  852. continue;
  853. }
  854. /* Print number */
  855. p = numbuf;
  856. last = p + sizeof (numbuf);
  857. if (sign) {
  858. if (i64 < 0) {
  859. *p++ = '-';
  860. ui64 = (guint64) - i64;
  861. } else {
  862. ui64 = (guint64) i64;
  863. }
  864. }
  865. if (!humanize) {
  866. p = rspamd_sprintf_num (p, last, ui64, zero, hex, b64 + b32, width);
  867. }
  868. else {
  869. p = rspamd_humanize_number (p, last, ui64, bytes);
  870. }
  871. slen = p - numbuf;
  872. RSPAMD_PRINTF_APPEND (numbuf, slen);
  873. } else {
  874. fmt++;
  875. }
  876. }
  877. /* Finish buffer */
  878. if (fmt > buf_start) {
  879. wr = func (buf_start, fmt - buf_start, apd);
  880. if (wr <= 0) {
  881. goto oob;
  882. }
  883. written += wr;
  884. }
  885. oob:
  886. return written;
  887. }