bio_print.c 26 KB

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