b_print.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "crypto/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. 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. (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
  590. return 0;
  591. }
  592. } else {
  593. /*
  594. * In E_FORMAT there is always one significant digit in front
  595. * of the decimal point, so:
  596. * significant digits == 1 + decimal places
  597. */
  598. max--;
  599. }
  600. }
  601. if (realstyle == E_FORMAT)
  602. fvalue = tmpvalue;
  603. }
  604. ufvalue = abs_val(fvalue);
  605. /*
  606. * By subtracting 65535 (2^16-1) we cancel the low order 15 bits
  607. * of ULONG_MAX to avoid using imprecise floating point values.
  608. */
  609. if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
  610. /* Number too big */
  611. (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
  612. return 0;
  613. }
  614. intpart = (unsigned long)ufvalue;
  615. /*
  616. * sorry, we only support 9 digits past the decimal because of our
  617. * conversion method
  618. */
  619. if (max > 9)
  620. max = 9;
  621. /*
  622. * we "cheat" by converting the fractional part to integer by multiplying
  623. * by a factor of 10
  624. */
  625. max10 = roundv(pow_10(max));
  626. fracpart = roundv(pow_10(max) * (ufvalue - intpart));
  627. if (fracpart >= max10) {
  628. intpart++;
  629. fracpart -= max10;
  630. }
  631. /* convert integer part */
  632. do {
  633. iconvert[iplace++] = "0123456789"[intpart % 10];
  634. intpart = (intpart / 10);
  635. } while (intpart && (iplace < (int)sizeof(iconvert)));
  636. if (iplace == sizeof(iconvert))
  637. iplace--;
  638. iconvert[iplace] = 0;
  639. /* convert fractional part */
  640. while (fplace < max) {
  641. if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
  642. /* We strip trailing zeros in G_FORMAT */
  643. max--;
  644. fracpart = fracpart / 10;
  645. if (fplace < max)
  646. continue;
  647. break;
  648. }
  649. fconvert[fplace++] = "0123456789"[fracpart % 10];
  650. fracpart = (fracpart / 10);
  651. }
  652. if (fplace == sizeof(fconvert))
  653. fplace--;
  654. fconvert[fplace] = 0;
  655. /* convert exponent part */
  656. if (realstyle == E_FORMAT) {
  657. int tmpexp;
  658. if (exp < 0)
  659. tmpexp = -exp;
  660. else
  661. tmpexp = exp;
  662. do {
  663. econvert[eplace++] = "0123456789"[tmpexp % 10];
  664. tmpexp = (tmpexp / 10);
  665. } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
  666. /* Exponent is huge!! Too big to print */
  667. if (tmpexp > 0) {
  668. (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
  669. return 0;
  670. }
  671. /* Add a leading 0 for single digit exponents */
  672. if (eplace == 1)
  673. econvert[eplace++] = '0';
  674. }
  675. /*
  676. * -1 for decimal point (if we have one, i.e. max > 0),
  677. * another -1 if we are printing a sign
  678. */
  679. padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
  680. /* Take some off for exponent prefix "+e" and exponent */
  681. if (realstyle == E_FORMAT)
  682. padlen -= 2 + eplace;
  683. zpadlen = max - fplace;
  684. if (zpadlen < 0)
  685. zpadlen = 0;
  686. if (padlen < 0)
  687. padlen = 0;
  688. if (flags & DP_F_MINUS)
  689. padlen = -padlen;
  690. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  691. if (signvalue) {
  692. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  693. return 0;
  694. --padlen;
  695. signvalue = 0;
  696. }
  697. while (padlen > 0) {
  698. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  699. return 0;
  700. --padlen;
  701. }
  702. }
  703. while (padlen > 0) {
  704. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  705. return 0;
  706. --padlen;
  707. }
  708. if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  709. return 0;
  710. while (iplace > 0) {
  711. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
  712. return 0;
  713. }
  714. /*
  715. * Decimal point. This should probably use locale to find the correct
  716. * char to print out.
  717. */
  718. if (max > 0 || (flags & DP_F_NUM)) {
  719. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
  720. return 0;
  721. while (fplace > 0) {
  722. if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
  723. fconvert[--fplace]))
  724. return 0;
  725. }
  726. }
  727. while (zpadlen > 0) {
  728. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  729. return 0;
  730. --zpadlen;
  731. }
  732. if (realstyle == E_FORMAT) {
  733. char ech;
  734. if ((flags & DP_F_UP) == 0)
  735. ech = 'e';
  736. else
  737. ech = 'E';
  738. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
  739. return 0;
  740. if (exp < 0) {
  741. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
  742. return 0;
  743. } else {
  744. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
  745. return 0;
  746. }
  747. while (eplace > 0) {
  748. if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
  749. econvert[--eplace]))
  750. return 0;
  751. }
  752. }
  753. while (padlen < 0) {
  754. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  755. return 0;
  756. ++padlen;
  757. }
  758. return 1;
  759. }
  760. #define BUFFER_INC 1024
  761. static int
  762. doapr_outch(char **sbuffer,
  763. char **buffer, size_t *currlen, size_t *maxlen, int c)
  764. {
  765. /* If we haven't at least one buffer, someone has done a big booboo */
  766. if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
  767. return 0;
  768. /* |currlen| must always be <= |*maxlen| */
  769. if (!ossl_assert(*currlen <= *maxlen))
  770. return 0;
  771. if (buffer && *currlen == *maxlen) {
  772. if (*maxlen > INT_MAX - BUFFER_INC)
  773. return 0;
  774. *maxlen += BUFFER_INC;
  775. if (*buffer == NULL) {
  776. if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
  777. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  778. return 0;
  779. }
  780. if (*currlen > 0) {
  781. if (!ossl_assert(*sbuffer != NULL))
  782. return 0;
  783. memcpy(*buffer, *sbuffer, *currlen);
  784. }
  785. *sbuffer = NULL;
  786. } else {
  787. char *tmpbuf;
  788. tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
  789. if (tmpbuf == NULL) {
  790. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  791. return 0;
  792. }
  793. *buffer = tmpbuf;
  794. }
  795. }
  796. if (*currlen < *maxlen) {
  797. if (*sbuffer)
  798. (*sbuffer)[(*currlen)++] = (char)c;
  799. else
  800. (*buffer)[(*currlen)++] = (char)c;
  801. }
  802. return 1;
  803. }
  804. /***************************************************************************/
  805. int BIO_printf(BIO *bio, const char *format, ...)
  806. {
  807. va_list args;
  808. int ret;
  809. va_start(args, format);
  810. ret = BIO_vprintf(bio, format, args);
  811. va_end(args);
  812. return ret;
  813. }
  814. int BIO_vprintf(BIO *bio, const char *format, va_list args)
  815. {
  816. int ret;
  817. size_t retlen;
  818. char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
  819. * in small-stack environments, like threads
  820. * or DOS programs. */
  821. char *hugebufp = hugebuf;
  822. size_t hugebufsize = sizeof(hugebuf);
  823. char *dynbuf = NULL;
  824. int ignored;
  825. dynbuf = NULL;
  826. if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
  827. args)) {
  828. OPENSSL_free(dynbuf);
  829. return -1;
  830. }
  831. if (dynbuf) {
  832. ret = BIO_write(bio, dynbuf, (int)retlen);
  833. OPENSSL_free(dynbuf);
  834. } else {
  835. ret = BIO_write(bio, hugebuf, (int)retlen);
  836. }
  837. return ret;
  838. }
  839. /*
  840. * As snprintf is not available everywhere, we provide our own
  841. * implementation. This function has nothing to do with BIOs, but it's
  842. * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
  843. * function should be renamed, but to what?)
  844. */
  845. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  846. {
  847. va_list args;
  848. int ret;
  849. va_start(args, format);
  850. ret = BIO_vsnprintf(buf, n, format, args);
  851. va_end(args);
  852. return ret;
  853. }
  854. int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
  855. {
  856. size_t retlen;
  857. int truncated;
  858. if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
  859. return -1;
  860. if (truncated)
  861. /*
  862. * In case of truncation, return -1 like traditional snprintf.
  863. * (Current drafts for ISO/IEC 9899 say snprintf should return the
  864. * number of characters that would have been written, had the buffer
  865. * been large enough.)
  866. */
  867. return -1;
  868. return (retlen <= INT_MAX) ? (int)retlen : -1;
  869. }