mprintf.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1999 - 2016, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. *
  22. * Purpose:
  23. * A merge of Bjorn Reese's format() function and Daniel's dsprintf()
  24. * 1.0. A full blooded printf() clone with full support for <num>$
  25. * everywhere (parameters, widths and precisions) including variabled
  26. * sized parameters (like doubles, long longs, long doubles and even
  27. * void * in 64-bit architectures).
  28. *
  29. * Current restrictions:
  30. * - Max 128 parameters
  31. * - No 'long double' support.
  32. *
  33. * If you ever want truly portable and good *printf() clones, the project that
  34. * took on from here is named 'Trio' and you find more details on the trio web
  35. * page at https://daniel.haxx.se/projects/trio/
  36. */
  37. #include "curl_setup.h"
  38. #include <curl/mprintf.h>
  39. #include "curl_memory.h"
  40. /* The last #include file should be: */
  41. #include "memdebug.h"
  42. /*
  43. * If SIZEOF_SIZE_T has not been defined, default to the size of long.
  44. */
  45. #ifndef SIZEOF_SIZE_T
  46. # define SIZEOF_SIZE_T CURL_SIZEOF_LONG
  47. #endif
  48. #ifdef HAVE_LONGLONG
  49. # define LONG_LONG_TYPE long long
  50. # define HAVE_LONG_LONG_TYPE
  51. #else
  52. # if defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64)
  53. # define LONG_LONG_TYPE __int64
  54. # define HAVE_LONG_LONG_TYPE
  55. # else
  56. # undef LONG_LONG_TYPE
  57. # undef HAVE_LONG_LONG_TYPE
  58. # endif
  59. #endif
  60. /*
  61. * Non-ANSI integer extensions
  62. */
  63. #if (defined(__BORLANDC__) && (__BORLANDC__ >= 0x520)) || \
  64. (defined(__WATCOMC__) && defined(__386__)) || \
  65. (defined(__POCC__) && defined(_MSC_VER)) || \
  66. (defined(_WIN32_WCE)) || \
  67. (defined(__MINGW32__)) || \
  68. (defined(_MSC_VER) && (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64))
  69. # define MP_HAVE_INT_EXTENSIONS
  70. #endif
  71. /*
  72. * Max integer data types that mprintf.c is capable
  73. */
  74. #ifdef HAVE_LONG_LONG_TYPE
  75. # define mp_intmax_t LONG_LONG_TYPE
  76. # define mp_uintmax_t unsigned LONG_LONG_TYPE
  77. #else
  78. # define mp_intmax_t long
  79. # define mp_uintmax_t unsigned long
  80. #endif
  81. #define BUFFSIZE 256 /* buffer for long-to-str and float-to-str calcs */
  82. #define MAX_PARAMETERS 128 /* lame static limit */
  83. #ifdef __AMIGA__
  84. # undef FORMAT_INT
  85. #endif
  86. /* Lower-case digits. */
  87. static const char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  88. /* Upper-case digits. */
  89. static const char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  90. #define OUTCHAR(x) \
  91. do{ \
  92. if(stream((unsigned char)(x), (FILE *)data) != -1) \
  93. done++; \
  94. else \
  95. return done; /* return immediately on failure */ \
  96. } WHILE_FALSE
  97. /* Data type to read from the arglist */
  98. typedef enum {
  99. FORMAT_UNKNOWN = 0,
  100. FORMAT_STRING,
  101. FORMAT_PTR,
  102. FORMAT_INT,
  103. FORMAT_INTPTR,
  104. FORMAT_LONG,
  105. FORMAT_LONGLONG,
  106. FORMAT_DOUBLE,
  107. FORMAT_LONGDOUBLE,
  108. FORMAT_WIDTH /* For internal use */
  109. } FormatType;
  110. /* conversion and display flags */
  111. enum {
  112. FLAGS_NEW = 0,
  113. FLAGS_SPACE = 1<<0,
  114. FLAGS_SHOWSIGN = 1<<1,
  115. FLAGS_LEFT = 1<<2,
  116. FLAGS_ALT = 1<<3,
  117. FLAGS_SHORT = 1<<4,
  118. FLAGS_LONG = 1<<5,
  119. FLAGS_LONGLONG = 1<<6,
  120. FLAGS_LONGDOUBLE = 1<<7,
  121. FLAGS_PAD_NIL = 1<<8,
  122. FLAGS_UNSIGNED = 1<<9,
  123. FLAGS_OCTAL = 1<<10,
  124. FLAGS_HEX = 1<<11,
  125. FLAGS_UPPER = 1<<12,
  126. FLAGS_WIDTH = 1<<13, /* '*' or '*<num>$' used */
  127. FLAGS_WIDTHPARAM = 1<<14, /* width PARAMETER was specified */
  128. FLAGS_PREC = 1<<15, /* precision was specified */
  129. FLAGS_PRECPARAM = 1<<16, /* precision PARAMETER was specified */
  130. FLAGS_CHAR = 1<<17, /* %c story */
  131. FLAGS_FLOATE = 1<<18, /* %e or %E */
  132. FLAGS_FLOATG = 1<<19 /* %g or %G */
  133. };
  134. typedef struct {
  135. FormatType type;
  136. int flags;
  137. long width; /* width OR width parameter number */
  138. long precision; /* precision OR precision parameter number */
  139. union {
  140. char *str;
  141. void *ptr;
  142. union {
  143. mp_intmax_t as_signed;
  144. mp_uintmax_t as_unsigned;
  145. } num;
  146. double dnum;
  147. } data;
  148. } va_stack_t;
  149. struct nsprintf {
  150. char *buffer;
  151. size_t length;
  152. size_t max;
  153. };
  154. struct asprintf {
  155. char *buffer; /* allocated buffer */
  156. size_t len; /* length of string */
  157. size_t alloc; /* length of alloc */
  158. int fail; /* (!= 0) if an alloc has failed and thus
  159. the output is not the complete data */
  160. };
  161. static long dprintf_DollarString(char *input, char **end)
  162. {
  163. int number=0;
  164. while(ISDIGIT(*input)) {
  165. number *= 10;
  166. number += *input-'0';
  167. input++;
  168. }
  169. if(number && ('$'==*input++)) {
  170. *end = input;
  171. return number;
  172. }
  173. return 0;
  174. }
  175. static bool dprintf_IsQualifierNoDollar(const char *fmt)
  176. {
  177. #if defined(MP_HAVE_INT_EXTENSIONS)
  178. if(!strncmp(fmt, "I32", 3) || !strncmp(fmt, "I64", 3)) {
  179. return TRUE;
  180. }
  181. #endif
  182. switch(*fmt) {
  183. case '-': case '+': case ' ': case '#': case '.':
  184. case '0': case '1': case '2': case '3': case '4':
  185. case '5': case '6': case '7': case '8': case '9':
  186. case 'h': case 'l': case 'L': case 'z': case 'q':
  187. case '*': case 'O':
  188. #if defined(MP_HAVE_INT_EXTENSIONS)
  189. case 'I':
  190. #endif
  191. return TRUE;
  192. default:
  193. return FALSE;
  194. }
  195. }
  196. /******************************************************************
  197. *
  198. * Pass 1:
  199. * Create an index with the type of each parameter entry and its
  200. * value (may vary in size)
  201. *
  202. ******************************************************************/
  203. static long dprintf_Pass1(const char *format, va_stack_t *vto, char **endpos,
  204. va_list arglist)
  205. {
  206. char *fmt = (char *)format;
  207. int param_num = 0;
  208. long this_param;
  209. long width;
  210. long precision;
  211. int flags;
  212. long max_param=0;
  213. long i;
  214. while(*fmt) {
  215. if(*fmt++ == '%') {
  216. if(*fmt == '%') {
  217. fmt++;
  218. continue; /* while */
  219. }
  220. flags = FLAGS_NEW;
  221. /* Handle the positional case (N$) */
  222. param_num++;
  223. this_param = dprintf_DollarString(fmt, &fmt);
  224. if(0 == this_param)
  225. /* we got no positional, get the next counter */
  226. this_param = param_num;
  227. if(this_param > max_param)
  228. max_param = this_param;
  229. /*
  230. * The parameter with number 'i' should be used. Next, we need
  231. * to get SIZE and TYPE of the parameter. Add the information
  232. * to our array.
  233. */
  234. width = 0;
  235. precision = 0;
  236. /* Handle the flags */
  237. while(dprintf_IsQualifierNoDollar(fmt)) {
  238. #if defined(MP_HAVE_INT_EXTENSIONS)
  239. if(!strncmp(fmt, "I32", 3)) {
  240. flags |= FLAGS_LONG;
  241. fmt += 3;
  242. }
  243. else if(!strncmp(fmt, "I64", 3)) {
  244. flags |= FLAGS_LONGLONG;
  245. fmt += 3;
  246. }
  247. else
  248. #endif
  249. switch(*fmt++) {
  250. case ' ':
  251. flags |= FLAGS_SPACE;
  252. break;
  253. case '+':
  254. flags |= FLAGS_SHOWSIGN;
  255. break;
  256. case '-':
  257. flags |= FLAGS_LEFT;
  258. flags &= ~FLAGS_PAD_NIL;
  259. break;
  260. case '#':
  261. flags |= FLAGS_ALT;
  262. break;
  263. case '.':
  264. flags |= FLAGS_PREC;
  265. if('*' == *fmt) {
  266. /* The precision is picked from a specified parameter */
  267. flags |= FLAGS_PRECPARAM;
  268. fmt++;
  269. param_num++;
  270. i = dprintf_DollarString(fmt, &fmt);
  271. if(i)
  272. precision = i;
  273. else
  274. precision = param_num;
  275. if(precision > max_param)
  276. max_param = precision;
  277. }
  278. else {
  279. flags |= FLAGS_PREC;
  280. precision = strtol(fmt, &fmt, 10);
  281. }
  282. break;
  283. case 'h':
  284. flags |= FLAGS_SHORT;
  285. break;
  286. #if defined(MP_HAVE_INT_EXTENSIONS)
  287. case 'I':
  288. #if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
  289. flags |= FLAGS_LONGLONG;
  290. #else
  291. flags |= FLAGS_LONG;
  292. #endif
  293. break;
  294. #endif
  295. case 'l':
  296. if(flags & FLAGS_LONG)
  297. flags |= FLAGS_LONGLONG;
  298. else
  299. flags |= FLAGS_LONG;
  300. break;
  301. case 'L':
  302. flags |= FLAGS_LONGDOUBLE;
  303. break;
  304. case 'q':
  305. flags |= FLAGS_LONGLONG;
  306. break;
  307. case 'z':
  308. /* the code below generates a warning if -Wunreachable-code is
  309. used */
  310. #if (SIZEOF_SIZE_T > CURL_SIZEOF_LONG)
  311. flags |= FLAGS_LONGLONG;
  312. #else
  313. flags |= FLAGS_LONG;
  314. #endif
  315. break;
  316. case 'O':
  317. #if (CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
  318. flags |= FLAGS_LONGLONG;
  319. #else
  320. flags |= FLAGS_LONG;
  321. #endif
  322. break;
  323. case '0':
  324. if(!(flags & FLAGS_LEFT))
  325. flags |= FLAGS_PAD_NIL;
  326. /* FALLTHROUGH */
  327. case '1': case '2': case '3': case '4':
  328. case '5': case '6': case '7': case '8': case '9':
  329. flags |= FLAGS_WIDTH;
  330. width = strtol(fmt-1, &fmt, 10);
  331. break;
  332. case '*': /* Special case */
  333. flags |= FLAGS_WIDTHPARAM;
  334. param_num++;
  335. i = dprintf_DollarString(fmt, &fmt);
  336. if(i)
  337. width = i;
  338. else
  339. width = param_num;
  340. if(width > max_param)
  341. max_param=width;
  342. break;
  343. default:
  344. break;
  345. }
  346. } /* switch */
  347. /* Handle the specifier */
  348. i = this_param - 1;
  349. switch (*fmt) {
  350. case 'S':
  351. flags |= FLAGS_ALT;
  352. /* FALLTHROUGH */
  353. case 's':
  354. vto[i].type = FORMAT_STRING;
  355. break;
  356. case 'n':
  357. vto[i].type = FORMAT_INTPTR;
  358. break;
  359. case 'p':
  360. vto[i].type = FORMAT_PTR;
  361. break;
  362. case 'd': case 'i':
  363. vto[i].type = FORMAT_INT;
  364. break;
  365. case 'u':
  366. vto[i].type = FORMAT_INT;
  367. flags |= FLAGS_UNSIGNED;
  368. break;
  369. case 'o':
  370. vto[i].type = FORMAT_INT;
  371. flags |= FLAGS_OCTAL;
  372. break;
  373. case 'x':
  374. vto[i].type = FORMAT_INT;
  375. flags |= FLAGS_HEX|FLAGS_UNSIGNED;
  376. break;
  377. case 'X':
  378. vto[i].type = FORMAT_INT;
  379. flags |= FLAGS_HEX|FLAGS_UPPER|FLAGS_UNSIGNED;
  380. break;
  381. case 'c':
  382. vto[i].type = FORMAT_INT;
  383. flags |= FLAGS_CHAR;
  384. break;
  385. case 'f':
  386. vto[i].type = FORMAT_DOUBLE;
  387. break;
  388. case 'e':
  389. vto[i].type = FORMAT_DOUBLE;
  390. flags |= FLAGS_FLOATE;
  391. break;
  392. case 'E':
  393. vto[i].type = FORMAT_DOUBLE;
  394. flags |= FLAGS_FLOATE|FLAGS_UPPER;
  395. break;
  396. case 'g':
  397. vto[i].type = FORMAT_DOUBLE;
  398. flags |= FLAGS_FLOATG;
  399. break;
  400. case 'G':
  401. vto[i].type = FORMAT_DOUBLE;
  402. flags |= FLAGS_FLOATG|FLAGS_UPPER;
  403. break;
  404. default:
  405. vto[i].type = FORMAT_UNKNOWN;
  406. break;
  407. } /* switch */
  408. vto[i].flags = flags;
  409. vto[i].width = width;
  410. vto[i].precision = precision;
  411. if(flags & FLAGS_WIDTHPARAM) {
  412. /* we have the width specified from a parameter, so we make that
  413. parameter's info setup properly */
  414. long k = width - 1;
  415. vto[i].width = k;
  416. vto[k].type = FORMAT_WIDTH;
  417. vto[k].flags = FLAGS_NEW;
  418. /* can't use width or precision of width! */
  419. vto[k].width = 0;
  420. vto[k].precision = 0;
  421. }
  422. if(flags & FLAGS_PRECPARAM) {
  423. /* we have the precision specified from a parameter, so we make that
  424. parameter's info setup properly */
  425. long k = precision - 1;
  426. vto[i].precision = k;
  427. vto[k].type = FORMAT_WIDTH;
  428. vto[k].flags = FLAGS_NEW;
  429. /* can't use width or precision of width! */
  430. vto[k].width = 0;
  431. vto[k].precision = 0;
  432. }
  433. *endpos++ = fmt + 1; /* end of this sequence */
  434. }
  435. }
  436. /* Read the arg list parameters into our data list */
  437. for(i=0; i<max_param; i++) {
  438. /* Width/precision arguments must be read before the main argument
  439. they are attached to */
  440. if(vto[i].flags & FLAGS_WIDTHPARAM) {
  441. vto[vto[i].width].data.num.as_signed =
  442. (mp_intmax_t)va_arg(arglist, int);
  443. }
  444. if(vto[i].flags & FLAGS_PRECPARAM) {
  445. vto[vto[i].precision].data.num.as_signed =
  446. (mp_intmax_t)va_arg(arglist, int);
  447. }
  448. switch (vto[i].type) {
  449. case FORMAT_STRING:
  450. vto[i].data.str = va_arg(arglist, char *);
  451. break;
  452. case FORMAT_INTPTR:
  453. case FORMAT_UNKNOWN:
  454. case FORMAT_PTR:
  455. vto[i].data.ptr = va_arg(arglist, void *);
  456. break;
  457. case FORMAT_INT:
  458. #ifdef HAVE_LONG_LONG_TYPE
  459. if((vto[i].flags & FLAGS_LONGLONG) && (vto[i].flags & FLAGS_UNSIGNED))
  460. vto[i].data.num.as_unsigned =
  461. (mp_uintmax_t)va_arg(arglist, mp_uintmax_t);
  462. else if(vto[i].flags & FLAGS_LONGLONG)
  463. vto[i].data.num.as_signed =
  464. (mp_intmax_t)va_arg(arglist, mp_intmax_t);
  465. else
  466. #endif
  467. {
  468. if((vto[i].flags & FLAGS_LONG) && (vto[i].flags & FLAGS_UNSIGNED))
  469. vto[i].data.num.as_unsigned =
  470. (mp_uintmax_t)va_arg(arglist, unsigned long);
  471. else if(vto[i].flags & FLAGS_LONG)
  472. vto[i].data.num.as_signed =
  473. (mp_intmax_t)va_arg(arglist, long);
  474. else if(vto[i].flags & FLAGS_UNSIGNED)
  475. vto[i].data.num.as_unsigned =
  476. (mp_uintmax_t)va_arg(arglist, unsigned int);
  477. else
  478. vto[i].data.num.as_signed =
  479. (mp_intmax_t)va_arg(arglist, int);
  480. }
  481. break;
  482. case FORMAT_DOUBLE:
  483. vto[i].data.dnum = va_arg(arglist, double);
  484. break;
  485. case FORMAT_WIDTH:
  486. /* Argument has been read. Silently convert it into an integer
  487. * for later use
  488. */
  489. vto[i].type = FORMAT_INT;
  490. break;
  491. default:
  492. break;
  493. }
  494. }
  495. return max_param;
  496. }
  497. static int dprintf_formatf(
  498. void *data, /* untouched by format(), just sent to the stream() function in
  499. the second argument */
  500. /* function pointer called for each output character */
  501. int (*stream)(int, FILE *),
  502. const char *format, /* %-formatted string */
  503. va_list ap_save) /* list of parameters */
  504. {
  505. /* Base-36 digits for numbers. */
  506. const char *digits = lower_digits;
  507. /* Pointer into the format string. */
  508. char *f;
  509. /* Number of characters written. */
  510. int done = 0;
  511. long param; /* current parameter to read */
  512. long param_num=0; /* parameter counter */
  513. va_stack_t vto[MAX_PARAMETERS];
  514. char *endpos[MAX_PARAMETERS];
  515. char **end;
  516. char work[BUFFSIZE];
  517. va_stack_t *p;
  518. /* 'workend' points to the final buffer byte position, but with an extra
  519. byte as margin to avoid the (false?) warning Coverity gives us
  520. otherwise */
  521. char *workend = &work[sizeof(work) - 2];
  522. /* Do the actual %-code parsing */
  523. dprintf_Pass1(format, vto, endpos, ap_save);
  524. end = &endpos[0]; /* the initial end-position from the list dprintf_Pass1()
  525. created for us */
  526. f = (char *)format;
  527. while(*f != '\0') {
  528. /* Format spec modifiers. */
  529. int is_alt;
  530. /* Width of a field. */
  531. long width;
  532. /* Precision of a field. */
  533. long prec;
  534. /* Decimal integer is negative. */
  535. int is_neg;
  536. /* Base of a number to be written. */
  537. long base;
  538. /* Integral values to be written. */
  539. mp_uintmax_t num;
  540. /* Used to convert negative in positive. */
  541. mp_intmax_t signed_num;
  542. char *w;
  543. if(*f != '%') {
  544. /* This isn't a format spec, so write everything out until the next one
  545. OR end of string is reached. */
  546. do {
  547. OUTCHAR(*f);
  548. } while(*++f && ('%' != *f));
  549. continue;
  550. }
  551. ++f;
  552. /* Check for "%%". Note that although the ANSI standard lists
  553. '%' as a conversion specifier, it says "The complete format
  554. specification shall be `%%'," so we can avoid all the width
  555. and precision processing. */
  556. if(*f == '%') {
  557. ++f;
  558. OUTCHAR('%');
  559. continue;
  560. }
  561. /* If this is a positional parameter, the position must follow immediately
  562. after the %, thus create a %<num>$ sequence */
  563. param=dprintf_DollarString(f, &f);
  564. if(!param)
  565. param = param_num;
  566. else
  567. --param;
  568. param_num++; /* increase this always to allow "%2$s %1$s %s" and then the
  569. third %s will pick the 3rd argument */
  570. p = &vto[param];
  571. /* pick up the specified width */
  572. if(p->flags & FLAGS_WIDTHPARAM) {
  573. width = (long)vto[p->width].data.num.as_signed;
  574. param_num++; /* since the width is extracted from a parameter, we
  575. must skip that to get to the next one properly */
  576. if(width < 0) {
  577. /* "A negative field width is taken as a '-' flag followed by a
  578. positive field width." */
  579. width = -width;
  580. p->flags |= FLAGS_LEFT;
  581. p->flags &= ~FLAGS_PAD_NIL;
  582. }
  583. }
  584. else
  585. width = p->width;
  586. /* pick up the specified precision */
  587. if(p->flags & FLAGS_PRECPARAM) {
  588. prec = (long)vto[p->precision].data.num.as_signed;
  589. param_num++; /* since the precision is extracted from a parameter, we
  590. must skip that to get to the next one properly */
  591. if(prec < 0)
  592. /* "A negative precision is taken as if the precision were
  593. omitted." */
  594. prec = -1;
  595. }
  596. else if(p->flags & FLAGS_PREC)
  597. prec = p->precision;
  598. else
  599. prec = -1;
  600. is_alt = (p->flags & FLAGS_ALT) ? 1 : 0;
  601. switch (p->type) {
  602. case FORMAT_INT:
  603. num = p->data.num.as_unsigned;
  604. if(p->flags & FLAGS_CHAR) {
  605. /* Character. */
  606. if(!(p->flags & FLAGS_LEFT))
  607. while(--width > 0)
  608. OUTCHAR(' ');
  609. OUTCHAR((char) num);
  610. if(p->flags & FLAGS_LEFT)
  611. while(--width > 0)
  612. OUTCHAR(' ');
  613. break;
  614. }
  615. if(p->flags & FLAGS_OCTAL) {
  616. /* Octal unsigned integer. */
  617. base = 8;
  618. goto unsigned_number;
  619. }
  620. else if(p->flags & FLAGS_HEX) {
  621. /* Hexadecimal unsigned integer. */
  622. digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
  623. base = 16;
  624. goto unsigned_number;
  625. }
  626. else if(p->flags & FLAGS_UNSIGNED) {
  627. /* Decimal unsigned integer. */
  628. base = 10;
  629. goto unsigned_number;
  630. }
  631. /* Decimal integer. */
  632. base = 10;
  633. is_neg = (p->data.num.as_signed < (mp_intmax_t)0) ? 1 : 0;
  634. if(is_neg) {
  635. /* signed_num might fail to hold absolute negative minimum by 1 */
  636. signed_num = p->data.num.as_signed + (mp_intmax_t)1;
  637. signed_num = -signed_num;
  638. num = (mp_uintmax_t)signed_num;
  639. num += (mp_uintmax_t)1;
  640. }
  641. goto number;
  642. unsigned_number:
  643. /* Unsigned number of base BASE. */
  644. is_neg = 0;
  645. number:
  646. /* Number of base BASE. */
  647. /* Supply a default precision if none was given. */
  648. if(prec == -1)
  649. prec = 1;
  650. /* Put the number in WORK. */
  651. w = workend;
  652. while(num > 0) {
  653. *w-- = digits[num % base];
  654. num /= base;
  655. }
  656. width -= (long)(workend - w);
  657. prec -= (long)(workend - w);
  658. if(is_alt && base == 8 && prec <= 0) {
  659. *w-- = '0';
  660. --width;
  661. }
  662. if(prec > 0) {
  663. width -= prec;
  664. while(prec-- > 0)
  665. *w-- = '0';
  666. }
  667. if(is_alt && base == 16)
  668. width -= 2;
  669. if(is_neg || (p->flags & FLAGS_SHOWSIGN) || (p->flags & FLAGS_SPACE))
  670. --width;
  671. if(!(p->flags & FLAGS_LEFT) && !(p->flags & FLAGS_PAD_NIL))
  672. while(width-- > 0)
  673. OUTCHAR(' ');
  674. if(is_neg)
  675. OUTCHAR('-');
  676. else if(p->flags & FLAGS_SHOWSIGN)
  677. OUTCHAR('+');
  678. else if(p->flags & FLAGS_SPACE)
  679. OUTCHAR(' ');
  680. if(is_alt && base == 16) {
  681. OUTCHAR('0');
  682. if(p->flags & FLAGS_UPPER)
  683. OUTCHAR('X');
  684. else
  685. OUTCHAR('x');
  686. }
  687. if(!(p->flags & FLAGS_LEFT) && (p->flags & FLAGS_PAD_NIL))
  688. while(width-- > 0)
  689. OUTCHAR('0');
  690. /* Write the number. */
  691. while(++w <= workend) {
  692. OUTCHAR(*w);
  693. }
  694. if(p->flags & FLAGS_LEFT)
  695. while(width-- > 0)
  696. OUTCHAR(' ');
  697. break;
  698. case FORMAT_STRING:
  699. /* String. */
  700. {
  701. static const char null[] = "(nil)";
  702. const char *str;
  703. size_t len;
  704. str = (char *) p->data.str;
  705. if(str == NULL) {
  706. /* Write null[] if there's space. */
  707. if(prec == -1 || prec >= (long) sizeof(null) - 1) {
  708. str = null;
  709. len = sizeof(null) - 1;
  710. /* Disable quotes around (nil) */
  711. p->flags &= (~FLAGS_ALT);
  712. }
  713. else {
  714. str = "";
  715. len = 0;
  716. }
  717. }
  718. else if(prec != -1)
  719. len = (size_t)prec;
  720. else
  721. len = strlen(str);
  722. width -= (len > LONG_MAX) ? LONG_MAX : (long)len;
  723. if(p->flags & FLAGS_ALT)
  724. OUTCHAR('"');
  725. if(!(p->flags&FLAGS_LEFT))
  726. while(width-- > 0)
  727. OUTCHAR(' ');
  728. while((len-- > 0) && *str)
  729. OUTCHAR(*str++);
  730. if(p->flags&FLAGS_LEFT)
  731. while(width-- > 0)
  732. OUTCHAR(' ');
  733. if(p->flags & FLAGS_ALT)
  734. OUTCHAR('"');
  735. }
  736. break;
  737. case FORMAT_PTR:
  738. /* Generic pointer. */
  739. {
  740. void *ptr;
  741. ptr = (void *) p->data.ptr;
  742. if(ptr != NULL) {
  743. /* If the pointer is not NULL, write it as a %#x spec. */
  744. base = 16;
  745. digits = (p->flags & FLAGS_UPPER)? upper_digits : lower_digits;
  746. is_alt = 1;
  747. num = (size_t) ptr;
  748. is_neg = 0;
  749. goto number;
  750. }
  751. else {
  752. /* Write "(nil)" for a nil pointer. */
  753. static const char strnil[] = "(nil)";
  754. const char *point;
  755. width -= (long)(sizeof(strnil) - 1);
  756. if(p->flags & FLAGS_LEFT)
  757. while(width-- > 0)
  758. OUTCHAR(' ');
  759. for(point = strnil; *point != '\0'; ++point)
  760. OUTCHAR(*point);
  761. if(! (p->flags & FLAGS_LEFT))
  762. while(width-- > 0)
  763. OUTCHAR(' ');
  764. }
  765. }
  766. break;
  767. case FORMAT_DOUBLE:
  768. {
  769. char formatbuf[32]="%";
  770. char *fptr = &formatbuf[1];
  771. size_t left = sizeof(formatbuf)-strlen(formatbuf);
  772. int len;
  773. width = -1;
  774. if(p->flags & FLAGS_WIDTH)
  775. width = p->width;
  776. else if(p->flags & FLAGS_WIDTHPARAM)
  777. width = (long)vto[p->width].data.num.as_signed;
  778. prec = -1;
  779. if(p->flags & FLAGS_PREC)
  780. prec = p->precision;
  781. else if(p->flags & FLAGS_PRECPARAM)
  782. prec = (long)vto[p->precision].data.num.as_signed;
  783. if(p->flags & FLAGS_LEFT)
  784. *fptr++ = '-';
  785. if(p->flags & FLAGS_SHOWSIGN)
  786. *fptr++ = '+';
  787. if(p->flags & FLAGS_SPACE)
  788. *fptr++ = ' ';
  789. if(p->flags & FLAGS_ALT)
  790. *fptr++ = '#';
  791. *fptr = 0;
  792. if(width >= 0) {
  793. /* RECURSIVE USAGE */
  794. len = curl_msnprintf(fptr, left, "%ld", width);
  795. fptr += len;
  796. left -= len;
  797. }
  798. if(prec >= 0) {
  799. /* RECURSIVE USAGE */
  800. len = curl_msnprintf(fptr, left, ".%ld", prec);
  801. fptr += len;
  802. }
  803. if(p->flags & FLAGS_LONG)
  804. *fptr++ = 'l';
  805. if(p->flags & FLAGS_FLOATE)
  806. *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'E':'e');
  807. else if(p->flags & FLAGS_FLOATG)
  808. *fptr++ = (char)((p->flags & FLAGS_UPPER) ? 'G' : 'g');
  809. else
  810. *fptr++ = 'f';
  811. *fptr = 0; /* and a final zero termination */
  812. /* NOTE NOTE NOTE!! Not all sprintf implementations return number of
  813. output characters */
  814. (sprintf)(work, formatbuf, p->data.dnum);
  815. for(fptr=work; *fptr; fptr++)
  816. OUTCHAR(*fptr);
  817. }
  818. break;
  819. case FORMAT_INTPTR:
  820. /* Answer the count of characters written. */
  821. #ifdef HAVE_LONG_LONG_TYPE
  822. if(p->flags & FLAGS_LONGLONG)
  823. *(LONG_LONG_TYPE *) p->data.ptr = (LONG_LONG_TYPE)done;
  824. else
  825. #endif
  826. if(p->flags & FLAGS_LONG)
  827. *(long *) p->data.ptr = (long)done;
  828. else if(!(p->flags & FLAGS_SHORT))
  829. *(int *) p->data.ptr = (int)done;
  830. else
  831. *(short *) p->data.ptr = (short)done;
  832. break;
  833. default:
  834. break;
  835. }
  836. f = *end++; /* goto end of %-code */
  837. }
  838. return done;
  839. }
  840. /* fputc() look-alike */
  841. static int addbyter(int output, FILE *data)
  842. {
  843. struct nsprintf *infop=(struct nsprintf *)data;
  844. unsigned char outc = (unsigned char)output;
  845. if(infop->length < infop->max) {
  846. /* only do this if we haven't reached max length yet */
  847. infop->buffer[0] = outc; /* store */
  848. infop->buffer++; /* increase pointer */
  849. infop->length++; /* we are now one byte larger */
  850. return outc; /* fputc() returns like this on success */
  851. }
  852. return -1;
  853. }
  854. int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format,
  855. va_list ap_save)
  856. {
  857. int retcode;
  858. struct nsprintf info;
  859. info.buffer = buffer;
  860. info.length = 0;
  861. info.max = maxlength;
  862. retcode = dprintf_formatf(&info, addbyter, format, ap_save);
  863. if(info.max) {
  864. /* we terminate this with a zero byte */
  865. if(info.max == info.length)
  866. /* we're at maximum, scrap the last letter */
  867. info.buffer[-1] = 0;
  868. else
  869. info.buffer[0] = 0;
  870. }
  871. return retcode;
  872. }
  873. int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...)
  874. {
  875. int retcode;
  876. va_list ap_save; /* argument pointer */
  877. va_start(ap_save, format);
  878. retcode = curl_mvsnprintf(buffer, maxlength, format, ap_save);
  879. va_end(ap_save);
  880. return retcode;
  881. }
  882. /* fputc() look-alike */
  883. static int alloc_addbyter(int output, FILE *data)
  884. {
  885. struct asprintf *infop=(struct asprintf *)data;
  886. unsigned char outc = (unsigned char)output;
  887. if(!infop->buffer) {
  888. infop->buffer = malloc(32);
  889. if(!infop->buffer) {
  890. infop->fail = 1;
  891. return -1; /* fail */
  892. }
  893. infop->alloc = 32;
  894. infop->len =0;
  895. }
  896. else if(infop->len+1 >= infop->alloc) {
  897. char *newptr;
  898. newptr = realloc(infop->buffer, infop->alloc*2);
  899. if(!newptr) {
  900. infop->fail = 1;
  901. return -1; /* fail */
  902. }
  903. infop->buffer = newptr;
  904. infop->alloc *= 2;
  905. }
  906. infop->buffer[ infop->len ] = outc;
  907. infop->len++;
  908. return outc; /* fputc() returns like this on success */
  909. }
  910. char *curl_maprintf(const char *format, ...)
  911. {
  912. va_list ap_save; /* argument pointer */
  913. int retcode;
  914. struct asprintf info;
  915. info.buffer = NULL;
  916. info.len = 0;
  917. info.alloc = 0;
  918. info.fail = 0;
  919. va_start(ap_save, format);
  920. retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  921. va_end(ap_save);
  922. if((-1 == retcode) || info.fail) {
  923. if(info.alloc)
  924. free(info.buffer);
  925. return NULL;
  926. }
  927. if(info.alloc) {
  928. info.buffer[info.len] = 0; /* we terminate this with a zero byte */
  929. return info.buffer;
  930. }
  931. else
  932. return strdup("");
  933. }
  934. char *curl_mvaprintf(const char *format, va_list ap_save)
  935. {
  936. int retcode;
  937. struct asprintf info;
  938. info.buffer = NULL;
  939. info.len = 0;
  940. info.alloc = 0;
  941. info.fail = 0;
  942. retcode = dprintf_formatf(&info, alloc_addbyter, format, ap_save);
  943. if((-1 == retcode) || info.fail) {
  944. if(info.alloc)
  945. free(info.buffer);
  946. return NULL;
  947. }
  948. if(info.alloc) {
  949. info.buffer[info.len] = 0; /* we terminate this with a zero byte */
  950. return info.buffer;
  951. }
  952. else
  953. return strdup("");
  954. }
  955. static int storebuffer(int output, FILE *data)
  956. {
  957. char **buffer = (char **)data;
  958. unsigned char outc = (unsigned char)output;
  959. **buffer = outc;
  960. (*buffer)++;
  961. return outc; /* act like fputc() ! */
  962. }
  963. int curl_msprintf(char *buffer, const char *format, ...)
  964. {
  965. va_list ap_save; /* argument pointer */
  966. int retcode;
  967. va_start(ap_save, format);
  968. retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
  969. va_end(ap_save);
  970. *buffer=0; /* we terminate this with a zero byte */
  971. return retcode;
  972. }
  973. int curl_mprintf(const char *format, ...)
  974. {
  975. int retcode;
  976. va_list ap_save; /* argument pointer */
  977. va_start(ap_save, format);
  978. retcode = dprintf_formatf(stdout, fputc, format, ap_save);
  979. va_end(ap_save);
  980. return retcode;
  981. }
  982. int curl_mfprintf(FILE *whereto, const char *format, ...)
  983. {
  984. int retcode;
  985. va_list ap_save; /* argument pointer */
  986. va_start(ap_save, format);
  987. retcode = dprintf_formatf(whereto, fputc, format, ap_save);
  988. va_end(ap_save);
  989. return retcode;
  990. }
  991. int curl_mvsprintf(char *buffer, const char *format, va_list ap_save)
  992. {
  993. int retcode;
  994. retcode = dprintf_formatf(&buffer, storebuffer, format, ap_save);
  995. *buffer=0; /* we terminate this with a zero byte */
  996. return retcode;
  997. }
  998. int curl_mvprintf(const char *format, va_list ap_save)
  999. {
  1000. return dprintf_formatf(stdout, fputc, format, ap_save);
  1001. }
  1002. int curl_mvfprintf(FILE *whereto, const char *format, va_list ap_save)
  1003. {
  1004. return dprintf_formatf(whereto, fputc, format, ap_save);
  1005. }