b_print.c 26 KB

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