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 23KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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, c;
  497. const gchar *buf_start = fmt, *fmt_start = NULL;
  498. gint d;
  499. gdouble f;
  500. glong written = 0, wr, slen;
  501. gint64 i64;
  502. guint64 ui64;
  503. guint width, sign, hex, humanize, bytes, frac_width, b32, b64;
  504. rspamd_fstring_t *v;
  505. rspamd_ftok_t *tok;
  506. GString *gs;
  507. GError *err;
  508. while (*fmt) {
  509. /*
  510. * "buf < last" means that we could copy at least one character:
  511. * the plain character, "%%", "%c", and minus without the checking
  512. */
  513. if (*fmt == '%') {
  514. /* Append what we have in buf */
  515. if (fmt > buf_start) {
  516. wr = func (buf_start, fmt - buf_start, apd);
  517. if (wr <= 0) {
  518. goto oob;
  519. }
  520. written += wr;
  521. }
  522. fmt_start = fmt;
  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. }
  666. if (G_UNLIKELY (b32)) {
  667. gchar *b32buf;
  668. if (G_UNLIKELY (slen == -1)) {
  669. if (G_LIKELY (width != 0)) {
  670. slen = width;
  671. }
  672. else {
  673. /* NULL terminated string */
  674. slen = strlen (p);
  675. }
  676. }
  677. b32buf = rspamd_encode_base32 (p, slen);
  678. if (b32buf) {
  679. RSPAMD_PRINTF_APPEND (b32buf, strlen (b32buf));
  680. g_free (b32buf);
  681. }
  682. else {
  683. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  684. }
  685. }
  686. else if (G_UNLIKELY (hex)) {
  687. gchar hexbuf[2];
  688. if (G_UNLIKELY (slen == -1)) {
  689. if (G_LIKELY (width != 0)) {
  690. slen = width;
  691. }
  692. else {
  693. /* NULL terminated string */
  694. slen = strlen (p);
  695. }
  696. }
  697. while (slen) {
  698. hexbuf[0] = hex == 2 ? _HEX[(*p >> 4) & 0xf] :
  699. _hex[(*p >> 4) & 0xf];
  700. hexbuf[1] = hex == 2 ? _HEX[*p & 0xf] : _hex[*p & 0xf];
  701. RSPAMD_PRINTF_APPEND_BUF (hexbuf, 2);
  702. p++;
  703. slen--;
  704. }
  705. fmt++;
  706. buf_start = fmt;
  707. }
  708. else if (G_UNLIKELY (b64)) {
  709. gchar *b64buf;
  710. gsize olen = 0;
  711. if (G_UNLIKELY (slen == -1)) {
  712. if (G_LIKELY (width != 0)) {
  713. slen = width;
  714. }
  715. else {
  716. /* NULL terminated string */
  717. slen = strlen (p);
  718. }
  719. }
  720. b64buf = rspamd_encode_base64 (p, slen, 0, &olen);
  721. if (b64buf) {
  722. RSPAMD_PRINTF_APPEND (b64buf, olen);
  723. g_free (b64buf);
  724. }
  725. else {
  726. RSPAMD_PRINTF_APPEND ("(NULL)", sizeof ("(NULL)") - 1);
  727. }
  728. }
  729. else {
  730. if (slen == -1) {
  731. /* NULL terminated string */
  732. slen = strlen (p);
  733. }
  734. if (G_UNLIKELY (width != 0)) {
  735. slen = MIN (slen, width);
  736. }
  737. RSPAMD_PRINTF_APPEND (p, slen);
  738. }
  739. continue;
  740. case 'O':
  741. i64 = (gint64) va_arg (args, off_t);
  742. sign = 1;
  743. break;
  744. case 'P':
  745. i64 = (gint64) va_arg (args, pid_t);
  746. sign = 1;
  747. break;
  748. case 't':
  749. i64 = (gint64) va_arg (args, time_t);
  750. sign = 1;
  751. break;
  752. case 'z':
  753. if (sign) {
  754. i64 = (gint64) va_arg (args, ssize_t);
  755. } else {
  756. ui64 = (guint64) va_arg (args, size_t);
  757. }
  758. break;
  759. case 'd':
  760. if (sign) {
  761. i64 = (gint64) va_arg (args, gint);
  762. } else {
  763. ui64 = (guint64) va_arg (args, guint);
  764. }
  765. break;
  766. case 'l':
  767. if (sign) {
  768. i64 = (gint64) va_arg (args, glong);
  769. } else {
  770. ui64 = (guint64) va_arg (args, gulong);
  771. }
  772. break;
  773. case 'D':
  774. if (sign) {
  775. i64 = (gint64) va_arg (args, gint32);
  776. } else {
  777. ui64 = (guint64) va_arg (args, guint32);
  778. }
  779. break;
  780. case 'L':
  781. if (sign) {
  782. i64 = va_arg (args, gint64);
  783. } else {
  784. ui64 = va_arg (args, guint64);
  785. }
  786. break;
  787. case 'f':
  788. f = (gdouble) va_arg (args, double);
  789. slen = fpconv_dtoa (f, dtoabuf, false);
  790. if (frac_width != 0) {
  791. const gchar *dot_pos = memchr (dtoabuf, '.', slen);
  792. if (dot_pos) {
  793. if (frac_width < (slen - ((dot_pos - dtoabuf) + 1))) {
  794. /* Truncate */
  795. slen = (dot_pos - dtoabuf) + 1 + /* xxx. */
  796. frac_width; /* .yyy */
  797. }
  798. else if (frac_width + dot_pos + 1 < dtoabuf + sizeof (dtoabuf)) {
  799. /* Expand */
  800. frac_width -= slen - ((dot_pos - dtoabuf) + 1);
  801. memset (dtoabuf + slen, '0', frac_width);
  802. slen += frac_width;
  803. }
  804. }
  805. else {
  806. /* Expand */
  807. frac_width = MIN (frac_width, sizeof (dtoabuf) - slen - 1);
  808. dtoabuf[slen ++] = '.';
  809. memset (dtoabuf + slen, '0', frac_width);
  810. slen += frac_width;
  811. }
  812. }
  813. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  814. continue;
  815. case 'g':
  816. f = (gdouble) va_arg (args, double);
  817. slen = fpconv_dtoa (f, dtoabuf, true);
  818. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  819. continue;
  820. case 'F':
  821. f = (gdouble) va_arg (args, long double);
  822. slen = fpconv_dtoa (f, dtoabuf, false);
  823. if (frac_width != 0) {
  824. const gchar *dot_pos = memchr (dtoabuf, '.', slen);
  825. if (dot_pos) {
  826. if (frac_width < (slen - ((dot_pos - dtoabuf) + 1))) {
  827. /* Truncate */
  828. slen = (dot_pos - dtoabuf) + 1 + /* xxx. */
  829. frac_width; /* .yyy */
  830. }
  831. else if (frac_width + dot_pos + 1 < dtoabuf + sizeof (dtoabuf)) {
  832. /* Expand */
  833. frac_width -= slen - ((dot_pos - dtoabuf) + 1);
  834. memset (dtoabuf + slen, '0', frac_width);
  835. slen += frac_width;
  836. }
  837. }
  838. else {
  839. /* Expand */
  840. frac_width = MIN (frac_width, sizeof (dtoabuf) - slen - 1);
  841. dtoabuf[slen ++] = '.';
  842. memset (dtoabuf + slen, '0', frac_width);
  843. slen += frac_width;
  844. }
  845. }
  846. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  847. continue;
  848. case 'G':
  849. f = (gdouble) va_arg (args, long double);
  850. slen = fpconv_dtoa (f, dtoabuf, true);
  851. RSPAMD_PRINTF_APPEND (dtoabuf, slen);
  852. continue;
  853. case 'p':
  854. ui64 = (uintptr_t) va_arg (args, void *);
  855. hex = 2;
  856. sign = 0;
  857. zero = '0';
  858. width = sizeof (void *) * 2;
  859. break;
  860. case 'c':
  861. c = va_arg (args, gint);
  862. c &= 0xff;
  863. if (G_UNLIKELY (hex)) {
  864. gchar hexbuf[2];
  865. hexbuf[0] = hex == 2 ? _HEX[(c >> 4) & 0xf] :
  866. _hex[(c >> 4) & 0xf];
  867. hexbuf[1] = hex == 2 ? _HEX[c & 0xf] : _hex[c & 0xf];
  868. RSPAMD_PRINTF_APPEND (hexbuf, 2);
  869. }
  870. else {
  871. RSPAMD_PRINTF_APPEND (&c, 1);
  872. }
  873. continue;
  874. case 'Z':
  875. c = '\0';
  876. RSPAMD_PRINTF_APPEND (&c, 1);
  877. continue;
  878. case 'N':
  879. c = '\n';
  880. RSPAMD_PRINTF_APPEND (&c, 1);
  881. continue;
  882. case '%':
  883. c = '%';
  884. RSPAMD_PRINTF_APPEND (&c, 1);
  885. continue;
  886. default:
  887. c = *fmt;
  888. RSPAMD_PRINTF_APPEND (&c, 1);
  889. continue;
  890. }
  891. /* Print number */
  892. p = numbuf;
  893. last = p + sizeof (numbuf);
  894. if (sign) {
  895. if (i64 < 0) {
  896. *p++ = '-';
  897. ui64 = (guint64) - i64;
  898. } else {
  899. ui64 = (guint64) i64;
  900. }
  901. }
  902. if (!humanize) {
  903. p = rspamd_sprintf_num (p, last, ui64, zero, hex, width);
  904. }
  905. else {
  906. p = rspamd_humanize_number (p, last, ui64, bytes);
  907. }
  908. slen = p - numbuf;
  909. RSPAMD_PRINTF_APPEND (numbuf, slen);
  910. } else {
  911. fmt++;
  912. }
  913. }
  914. /* Finish buffer */
  915. if (fmt > buf_start) {
  916. wr = func (buf_start, fmt - buf_start, apd);
  917. if (wr <= 0) {
  918. goto oob;
  919. }
  920. written += wr;
  921. }
  922. oob:
  923. return written;
  924. }