b_print.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/ctype.h"
  13. #include "internal/numbers.h"
  14. #include <openssl/bio.h>
  15. /*
  16. * Copyright Patrick Powell 1995
  17. * This code is based on code written by Patrick Powell <papowell@astart.com>
  18. * It may be used for any purpose as long as this notice remains intact
  19. * on all source code distributions.
  20. */
  21. #ifdef HAVE_LONG_DOUBLE
  22. # define LDOUBLE long double
  23. #else
  24. # define LDOUBLE double
  25. #endif
  26. static int fmtstr(char **, char **, size_t *, size_t *,
  27. const char *, int, int, int);
  28. static int fmtint(char **, char **, size_t *, size_t *,
  29. int64_t, int, int, int, int);
  30. static int fmtfp(char **, char **, size_t *, size_t *,
  31. LDOUBLE, int, int, int, int);
  32. static int doapr_outch(char **, char **, size_t *, size_t *, int);
  33. static int _dopr(char **sbuffer, char **buffer,
  34. size_t *maxlen, size_t *retlen, int *truncated,
  35. const char *format, va_list args);
  36. /* format read states */
  37. #define DP_S_DEFAULT 0
  38. #define DP_S_FLAGS 1
  39. #define DP_S_MIN 2
  40. #define DP_S_DOT 3
  41. #define DP_S_MAX 4
  42. #define DP_S_MOD 5
  43. #define DP_S_CONV 6
  44. #define DP_S_DONE 7
  45. /* format flags - Bits */
  46. /* left-aligned padding */
  47. #define DP_F_MINUS (1 << 0)
  48. /* print an explicit '+' for a value with positive sign */
  49. #define DP_F_PLUS (1 << 1)
  50. /* print an explicit ' ' for a value with positive sign */
  51. #define DP_F_SPACE (1 << 2)
  52. /* print 0/0x prefix for octal/hex and decimal point for floating point */
  53. #define DP_F_NUM (1 << 3)
  54. /* print leading zeroes */
  55. #define DP_F_ZERO (1 << 4)
  56. /* print HEX in UPPPERcase */
  57. #define DP_F_UP (1 << 5)
  58. /* treat value as unsigned */
  59. #define DP_F_UNSIGNED (1 << 6)
  60. /* conversion flags */
  61. #define DP_C_SHORT 1
  62. #define DP_C_LONG 2
  63. #define DP_C_LDOUBLE 3
  64. #define DP_C_LLONG 4
  65. #define DP_C_SIZE 5
  66. /* Floating point formats */
  67. #define F_FORMAT 0
  68. #define E_FORMAT 1
  69. #define G_FORMAT 2
  70. /* some handy macros */
  71. #define char_to_int(p) (p - '0')
  72. #define OSSL_MAX(p,q) ((p >= q) ? p : q)
  73. static int
  74. _dopr(char **sbuffer,
  75. char **buffer,
  76. size_t *maxlen,
  77. size_t *retlen, int *truncated, const char *format, va_list args)
  78. {
  79. char ch;
  80. int64_t value;
  81. LDOUBLE fvalue;
  82. char *strvalue;
  83. int min;
  84. int max;
  85. int state;
  86. int flags;
  87. int cflags;
  88. size_t currlen;
  89. state = DP_S_DEFAULT;
  90. flags = currlen = cflags = min = 0;
  91. max = -1;
  92. ch = *format++;
  93. while (state != DP_S_DONE) {
  94. if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
  95. state = DP_S_DONE;
  96. switch (state) {
  97. case DP_S_DEFAULT:
  98. if (ch == '%')
  99. state = DP_S_FLAGS;
  100. else
  101. if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
  102. return 0;
  103. ch = *format++;
  104. break;
  105. case DP_S_FLAGS:
  106. switch (ch) {
  107. case '-':
  108. flags |= DP_F_MINUS;
  109. ch = *format++;
  110. break;
  111. case '+':
  112. flags |= DP_F_PLUS;
  113. ch = *format++;
  114. break;
  115. case ' ':
  116. flags |= DP_F_SPACE;
  117. ch = *format++;
  118. break;
  119. case '#':
  120. flags |= DP_F_NUM;
  121. ch = *format++;
  122. break;
  123. case '0':
  124. flags |= DP_F_ZERO;
  125. ch = *format++;
  126. break;
  127. default:
  128. state = DP_S_MIN;
  129. break;
  130. }
  131. break;
  132. case DP_S_MIN:
  133. if (ossl_isdigit(ch)) {
  134. min = 10 * min + char_to_int(ch);
  135. ch = *format++;
  136. } else if (ch == '*') {
  137. min = va_arg(args, int);
  138. ch = *format++;
  139. state = DP_S_DOT;
  140. } else
  141. state = DP_S_DOT;
  142. break;
  143. case DP_S_DOT:
  144. if (ch == '.') {
  145. state = DP_S_MAX;
  146. ch = *format++;
  147. } else
  148. state = DP_S_MOD;
  149. break;
  150. case DP_S_MAX:
  151. if (ossl_isdigit(ch)) {
  152. if (max < 0)
  153. max = 0;
  154. max = 10 * max + char_to_int(ch);
  155. ch = *format++;
  156. } else if (ch == '*') {
  157. max = va_arg(args, int);
  158. ch = *format++;
  159. state = DP_S_MOD;
  160. } else
  161. state = DP_S_MOD;
  162. break;
  163. case DP_S_MOD:
  164. switch (ch) {
  165. case 'h':
  166. cflags = DP_C_SHORT;
  167. ch = *format++;
  168. break;
  169. case 'l':
  170. if (*format == 'l') {
  171. cflags = DP_C_LLONG;
  172. format++;
  173. } else
  174. cflags = DP_C_LONG;
  175. ch = *format++;
  176. break;
  177. case 'q':
  178. case 'j':
  179. cflags = DP_C_LLONG;
  180. ch = *format++;
  181. break;
  182. case 'L':
  183. cflags = DP_C_LDOUBLE;
  184. ch = *format++;
  185. break;
  186. case 'z':
  187. cflags = DP_C_SIZE;
  188. ch = *format++;
  189. break;
  190. default:
  191. break;
  192. }
  193. state = DP_S_CONV;
  194. break;
  195. case DP_S_CONV:
  196. switch (ch) {
  197. case 'd':
  198. case 'i':
  199. switch (cflags) {
  200. case DP_C_SHORT:
  201. value = (short int)va_arg(args, int);
  202. break;
  203. case DP_C_LONG:
  204. value = va_arg(args, long int);
  205. break;
  206. case DP_C_LLONG:
  207. value = va_arg(args, int64_t);
  208. break;
  209. case DP_C_SIZE:
  210. value = va_arg(args, ossl_ssize_t);
  211. break;
  212. default:
  213. value = va_arg(args, int);
  214. break;
  215. }
  216. if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
  217. max, flags))
  218. return 0;
  219. break;
  220. case 'X':
  221. flags |= DP_F_UP;
  222. /* FALLTHROUGH */
  223. case 'x':
  224. case 'o':
  225. case 'u':
  226. flags |= DP_F_UNSIGNED;
  227. switch (cflags) {
  228. case DP_C_SHORT:
  229. value = (unsigned short int)va_arg(args, unsigned int);
  230. break;
  231. case DP_C_LONG:
  232. value = va_arg(args, unsigned long int);
  233. break;
  234. case DP_C_LLONG:
  235. value = va_arg(args, uint64_t);
  236. break;
  237. case DP_C_SIZE:
  238. value = va_arg(args, size_t);
  239. break;
  240. default:
  241. value = va_arg(args, unsigned int);
  242. break;
  243. }
  244. if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
  245. ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
  246. min, max, flags))
  247. return 0;
  248. break;
  249. case 'f':
  250. if (cflags == DP_C_LDOUBLE)
  251. fvalue = va_arg(args, LDOUBLE);
  252. else
  253. fvalue = va_arg(args, double);
  254. if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
  255. flags, F_FORMAT))
  256. return 0;
  257. break;
  258. case 'E':
  259. flags |= DP_F_UP;
  260. /* fall thru */
  261. case 'e':
  262. if (cflags == DP_C_LDOUBLE)
  263. fvalue = va_arg(args, LDOUBLE);
  264. else
  265. fvalue = va_arg(args, double);
  266. if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
  267. flags, E_FORMAT))
  268. return 0;
  269. break;
  270. case 'G':
  271. flags |= DP_F_UP;
  272. /* fall thru */
  273. case 'g':
  274. if (cflags == DP_C_LDOUBLE)
  275. fvalue = va_arg(args, LDOUBLE);
  276. else
  277. fvalue = va_arg(args, double);
  278. if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
  279. flags, G_FORMAT))
  280. return 0;
  281. break;
  282. case 'c':
  283. if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
  284. va_arg(args, int)))
  285. return 0;
  286. break;
  287. case 's':
  288. strvalue = va_arg(args, char *);
  289. if (max < 0) {
  290. if (buffer)
  291. max = INT_MAX;
  292. else
  293. max = *maxlen;
  294. }
  295. if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
  296. flags, min, max))
  297. return 0;
  298. break;
  299. case 'p':
  300. value = (size_t)va_arg(args, void *);
  301. if (!fmtint(sbuffer, buffer, &currlen, maxlen,
  302. value, 16, min, max, flags | DP_F_NUM))
  303. return 0;
  304. break;
  305. case 'n':
  306. {
  307. int *num;
  308. num = va_arg(args, int *);
  309. *num = currlen;
  310. }
  311. break;
  312. case '%':
  313. if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
  314. return 0;
  315. break;
  316. case 'w':
  317. /* not supported yet, treat as next char */
  318. ch = *format++;
  319. break;
  320. default:
  321. /* unknown, skip */
  322. break;
  323. }
  324. ch = *format++;
  325. state = DP_S_DEFAULT;
  326. flags = cflags = min = 0;
  327. max = -1;
  328. break;
  329. case DP_S_DONE:
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. /*
  336. * We have to truncate if there is no dynamic buffer and we have filled the
  337. * static buffer.
  338. */
  339. if (buffer == NULL) {
  340. *truncated = (currlen > *maxlen - 1);
  341. if (*truncated)
  342. currlen = *maxlen - 1;
  343. }
  344. if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
  345. return 0;
  346. *retlen = currlen - 1;
  347. return 1;
  348. }
  349. static int
  350. fmtstr(char **sbuffer,
  351. char **buffer,
  352. size_t *currlen,
  353. size_t *maxlen, const char *value, int flags, int min, int max)
  354. {
  355. int padlen;
  356. size_t strln;
  357. int cnt = 0;
  358. if (value == 0)
  359. value = "<NULL>";
  360. strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
  361. padlen = min - strln;
  362. if (min < 0 || padlen < 0)
  363. padlen = 0;
  364. if (max >= 0) {
  365. /*
  366. * Calculate the maximum output including padding.
  367. * Make sure max doesn't overflow into negativity
  368. */
  369. if (max < INT_MAX - padlen)
  370. max += padlen;
  371. else
  372. max = INT_MAX;
  373. }
  374. if (flags & DP_F_MINUS)
  375. padlen = -padlen;
  376. while ((padlen > 0) && (max < 0 || cnt < max)) {
  377. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  378. return 0;
  379. --padlen;
  380. ++cnt;
  381. }
  382. while (strln > 0 && (max < 0 || cnt < max)) {
  383. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
  384. return 0;
  385. --strln;
  386. ++cnt;
  387. }
  388. while ((padlen < 0) && (max < 0 || cnt < max)) {
  389. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  390. return 0;
  391. ++padlen;
  392. ++cnt;
  393. }
  394. return 1;
  395. }
  396. static int
  397. fmtint(char **sbuffer,
  398. char **buffer,
  399. size_t *currlen,
  400. size_t *maxlen, int64_t value, int base, int min, int max, int flags)
  401. {
  402. int signvalue = 0;
  403. const char *prefix = "";
  404. uint64_t uvalue;
  405. char convert[DECIMAL_SIZE(value) + 3];
  406. int place = 0;
  407. int spadlen = 0;
  408. int zpadlen = 0;
  409. int caps = 0;
  410. if (max < 0)
  411. max = 0;
  412. uvalue = value;
  413. if (!(flags & DP_F_UNSIGNED)) {
  414. if (value < 0) {
  415. signvalue = '-';
  416. uvalue = 0 - (uint64_t)value;
  417. } else if (flags & DP_F_PLUS)
  418. signvalue = '+';
  419. else if (flags & DP_F_SPACE)
  420. signvalue = ' ';
  421. }
  422. if (flags & DP_F_NUM) {
  423. if (base == 8)
  424. prefix = "0";
  425. if (base == 16)
  426. prefix = "0x";
  427. }
  428. if (flags & DP_F_UP)
  429. caps = 1;
  430. do {
  431. convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
  432. [uvalue % (unsigned)base];
  433. uvalue = (uvalue / (unsigned)base);
  434. } while (uvalue && (place < (int)sizeof(convert)));
  435. if (place == sizeof(convert))
  436. place--;
  437. convert[place] = 0;
  438. zpadlen = max - place;
  439. spadlen =
  440. min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
  441. if (zpadlen < 0)
  442. zpadlen = 0;
  443. if (spadlen < 0)
  444. spadlen = 0;
  445. if (flags & DP_F_ZERO) {
  446. zpadlen = OSSL_MAX(zpadlen, spadlen);
  447. spadlen = 0;
  448. }
  449. if (flags & DP_F_MINUS)
  450. spadlen = -spadlen;
  451. /* spaces */
  452. while (spadlen > 0) {
  453. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  454. return 0;
  455. --spadlen;
  456. }
  457. /* sign */
  458. if (signvalue)
  459. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  460. return 0;
  461. /* prefix */
  462. while (*prefix) {
  463. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
  464. return 0;
  465. prefix++;
  466. }
  467. /* zeros */
  468. if (zpadlen > 0) {
  469. while (zpadlen > 0) {
  470. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  471. return 0;
  472. --zpadlen;
  473. }
  474. }
  475. /* digits */
  476. while (place > 0) {
  477. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
  478. return 0;
  479. }
  480. /* left justified spaces */
  481. while (spadlen < 0) {
  482. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  483. return 0;
  484. ++spadlen;
  485. }
  486. return 1;
  487. }
  488. static LDOUBLE abs_val(LDOUBLE value)
  489. {
  490. LDOUBLE result = value;
  491. if (value < 0)
  492. result = -value;
  493. return result;
  494. }
  495. static LDOUBLE pow_10(int in_exp)
  496. {
  497. LDOUBLE result = 1;
  498. while (in_exp) {
  499. result *= 10;
  500. in_exp--;
  501. }
  502. return result;
  503. }
  504. static long roundv(LDOUBLE value)
  505. {
  506. long intpart;
  507. intpart = (long)value;
  508. value = value - intpart;
  509. if (value >= 0.5)
  510. intpart++;
  511. return intpart;
  512. }
  513. static int
  514. fmtfp(char **sbuffer,
  515. char **buffer,
  516. size_t *currlen,
  517. size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
  518. {
  519. int signvalue = 0;
  520. LDOUBLE ufvalue;
  521. LDOUBLE tmpvalue;
  522. char iconvert[20];
  523. char fconvert[20];
  524. char econvert[20];
  525. int iplace = 0;
  526. int fplace = 0;
  527. int eplace = 0;
  528. int padlen = 0;
  529. int zpadlen = 0;
  530. long exp = 0;
  531. unsigned long intpart;
  532. unsigned long fracpart;
  533. unsigned long max10;
  534. int realstyle;
  535. if (max < 0)
  536. max = 6;
  537. if (fvalue < 0)
  538. signvalue = '-';
  539. else if (flags & DP_F_PLUS)
  540. signvalue = '+';
  541. else if (flags & DP_F_SPACE)
  542. signvalue = ' ';
  543. /*
  544. * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
  545. * depending on the number to be printed. Work out which one it is and use
  546. * that from here on.
  547. */
  548. if (style == G_FORMAT) {
  549. if (fvalue == 0.0) {
  550. realstyle = F_FORMAT;
  551. } else if (fvalue < 0.0001) {
  552. realstyle = E_FORMAT;
  553. } else if ((max == 0 && fvalue >= 10)
  554. || (max > 0 && fvalue >= pow_10(max))) {
  555. realstyle = E_FORMAT;
  556. } else {
  557. realstyle = F_FORMAT;
  558. }
  559. } else {
  560. realstyle = style;
  561. }
  562. if (style != F_FORMAT) {
  563. tmpvalue = fvalue;
  564. /* Calculate the exponent */
  565. if (fvalue != 0.0) {
  566. while (tmpvalue < 1) {
  567. tmpvalue *= 10;
  568. exp--;
  569. }
  570. while (tmpvalue > 10) {
  571. tmpvalue /= 10;
  572. exp++;
  573. }
  574. }
  575. if (style == G_FORMAT) {
  576. /*
  577. * In G_FORMAT the "precision" represents significant digits. We
  578. * always have at least 1 significant digit.
  579. */
  580. if (max == 0)
  581. max = 1;
  582. /* Now convert significant digits to decimal places */
  583. if (realstyle == F_FORMAT) {
  584. max -= (exp + 1);
  585. if (max < 0) {
  586. /*
  587. * Should not happen. If we're in F_FORMAT then exp < max?
  588. */
  589. return 0;
  590. }
  591. } else {
  592. /*
  593. * In E_FORMAT there is always one significant digit in front
  594. * of the decimal point, so:
  595. * significant digits == 1 + decimal places
  596. */
  597. max--;
  598. }
  599. }
  600. if (realstyle == E_FORMAT)
  601. fvalue = tmpvalue;
  602. }
  603. ufvalue = abs_val(fvalue);
  604. if (ufvalue > ULONG_MAX) {
  605. /* Number too big */
  606. return 0;
  607. }
  608. intpart = (unsigned long)ufvalue;
  609. /*
  610. * sorry, we only support 9 digits past the decimal because of our
  611. * conversion method
  612. */
  613. if (max > 9)
  614. max = 9;
  615. /*
  616. * we "cheat" by converting the fractional part to integer by multiplying
  617. * by a factor of 10
  618. */
  619. max10 = roundv(pow_10(max));
  620. fracpart = roundv(pow_10(max) * (ufvalue - intpart));
  621. if (fracpart >= max10) {
  622. intpart++;
  623. fracpart -= max10;
  624. }
  625. /* convert integer part */
  626. do {
  627. iconvert[iplace++] = "0123456789"[intpart % 10];
  628. intpart = (intpart / 10);
  629. } while (intpart && (iplace < (int)sizeof(iconvert)));
  630. if (iplace == sizeof(iconvert))
  631. iplace--;
  632. iconvert[iplace] = 0;
  633. /* convert fractional part */
  634. while (fplace < max) {
  635. if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
  636. /* We strip trailing zeros in G_FORMAT */
  637. max--;
  638. fracpart = fracpart / 10;
  639. if (fplace < max)
  640. continue;
  641. break;
  642. }
  643. fconvert[fplace++] = "0123456789"[fracpart % 10];
  644. fracpart = (fracpart / 10);
  645. }
  646. if (fplace == sizeof(fconvert))
  647. fplace--;
  648. fconvert[fplace] = 0;
  649. /* convert exponent part */
  650. if (realstyle == E_FORMAT) {
  651. int tmpexp;
  652. if (exp < 0)
  653. tmpexp = -exp;
  654. else
  655. tmpexp = exp;
  656. do {
  657. econvert[eplace++] = "0123456789"[tmpexp % 10];
  658. tmpexp = (tmpexp / 10);
  659. } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
  660. /* Exponent is huge!! Too big to print */
  661. if (tmpexp > 0)
  662. return 0;
  663. /* Add a leading 0 for single digit exponents */
  664. if (eplace == 1)
  665. econvert[eplace++] = '0';
  666. }
  667. /*
  668. * -1 for decimal point (if we have one, i.e. max > 0),
  669. * another -1 if we are printing a sign
  670. */
  671. padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
  672. /* Take some off for exponent prefix "+e" and exponent */
  673. if (realstyle == E_FORMAT)
  674. padlen -= 2 + eplace;
  675. zpadlen = max - fplace;
  676. if (zpadlen < 0)
  677. zpadlen = 0;
  678. if (padlen < 0)
  679. padlen = 0;
  680. if (flags & DP_F_MINUS)
  681. padlen = -padlen;
  682. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  683. if (signvalue) {
  684. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  685. return 0;
  686. --padlen;
  687. signvalue = 0;
  688. }
  689. while (padlen > 0) {
  690. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  691. return 0;
  692. --padlen;
  693. }
  694. }
  695. while (padlen > 0) {
  696. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  697. return 0;
  698. --padlen;
  699. }
  700. if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  701. return 0;
  702. while (iplace > 0) {
  703. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
  704. return 0;
  705. }
  706. /*
  707. * Decimal point. This should probably use locale to find the correct
  708. * char to print out.
  709. */
  710. if (max > 0 || (flags & DP_F_NUM)) {
  711. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
  712. return 0;
  713. while (fplace > 0) {
  714. if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
  715. fconvert[--fplace]))
  716. return 0;
  717. }
  718. }
  719. while (zpadlen > 0) {
  720. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  721. return 0;
  722. --zpadlen;
  723. }
  724. if (realstyle == E_FORMAT) {
  725. char ech;
  726. if ((flags & DP_F_UP) == 0)
  727. ech = 'e';
  728. else
  729. ech = 'E';
  730. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
  731. return 0;
  732. if (exp < 0) {
  733. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
  734. return 0;
  735. } else {
  736. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
  737. return 0;
  738. }
  739. while (eplace > 0) {
  740. if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
  741. econvert[--eplace]))
  742. return 0;
  743. }
  744. }
  745. while (padlen < 0) {
  746. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  747. return 0;
  748. ++padlen;
  749. }
  750. return 1;
  751. }
  752. #define BUFFER_INC 1024
  753. static int
  754. doapr_outch(char **sbuffer,
  755. char **buffer, size_t *currlen, size_t *maxlen, int c)
  756. {
  757. /* If we haven't at least one buffer, someone has done a big booboo */
  758. if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
  759. return 0;
  760. /* |currlen| must always be <= |*maxlen| */
  761. if (!ossl_assert(*currlen <= *maxlen))
  762. return 0;
  763. if (buffer && *currlen == *maxlen) {
  764. if (*maxlen > INT_MAX - BUFFER_INC)
  765. return 0;
  766. *maxlen += BUFFER_INC;
  767. if (*buffer == NULL) {
  768. if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
  769. BIOerr(BIO_F_DOAPR_OUTCH, ERR_R_MALLOC_FAILURE);
  770. return 0;
  771. }
  772. if (*currlen > 0) {
  773. if (!ossl_assert(*sbuffer != NULL))
  774. return 0;
  775. memcpy(*buffer, *sbuffer, *currlen);
  776. }
  777. *sbuffer = NULL;
  778. } else {
  779. char *tmpbuf;
  780. tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
  781. if (tmpbuf == NULL)
  782. return 0;
  783. *buffer = tmpbuf;
  784. }
  785. }
  786. if (*currlen < *maxlen) {
  787. if (*sbuffer)
  788. (*sbuffer)[(*currlen)++] = (char)c;
  789. else
  790. (*buffer)[(*currlen)++] = (char)c;
  791. }
  792. return 1;
  793. }
  794. /***************************************************************************/
  795. int BIO_printf(BIO *bio, const char *format, ...)
  796. {
  797. va_list args;
  798. int ret;
  799. va_start(args, format);
  800. ret = BIO_vprintf(bio, format, args);
  801. va_end(args);
  802. return ret;
  803. }
  804. int BIO_vprintf(BIO *bio, const char *format, va_list args)
  805. {
  806. int ret;
  807. size_t retlen;
  808. char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
  809. * in small-stack environments, like threads
  810. * or DOS programs. */
  811. char *hugebufp = hugebuf;
  812. size_t hugebufsize = sizeof(hugebuf);
  813. char *dynbuf = NULL;
  814. int ignored;
  815. dynbuf = NULL;
  816. if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
  817. args)) {
  818. OPENSSL_free(dynbuf);
  819. return -1;
  820. }
  821. if (dynbuf) {
  822. ret = BIO_write(bio, dynbuf, (int)retlen);
  823. OPENSSL_free(dynbuf);
  824. } else {
  825. ret = BIO_write(bio, hugebuf, (int)retlen);
  826. }
  827. return ret;
  828. }
  829. /*
  830. * As snprintf is not available everywhere, we provide our own
  831. * implementation. This function has nothing to do with BIOs, but it's
  832. * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
  833. * function should be renamed, but to what?)
  834. */
  835. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  836. {
  837. va_list args;
  838. int ret;
  839. va_start(args, format);
  840. ret = BIO_vsnprintf(buf, n, format, args);
  841. va_end(args);
  842. return ret;
  843. }
  844. int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
  845. {
  846. size_t retlen;
  847. int truncated;
  848. if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
  849. return -1;
  850. if (truncated)
  851. /*
  852. * In case of truncation, return -1 like traditional snprintf.
  853. * (Current drafts for ISO/IEC 9899 say snprintf should return the
  854. * number of characters that would have been written, had the buffer
  855. * been large enough.)
  856. */
  857. return -1;
  858. else
  859. return (retlen <= INT_MAX) ? (int)retlen : -1;
  860. }