b_print.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /* crypto/bio/b_print.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* disable assert() unless BIO_DEBUG has been defined */
  59. #ifndef BIO_DEBUG
  60. # ifndef NDEBUG
  61. # define NDEBUG
  62. # endif
  63. #endif
  64. /*
  65. * Stolen from tjh's ssl/ssl_trc.c stuff.
  66. */
  67. #include <stdio.h>
  68. #include <string.h>
  69. #include <ctype.h>
  70. #include <assert.h>
  71. #include <limits.h>
  72. #include "cryptlib.h"
  73. #ifndef NO_SYS_TYPES_H
  74. # include <sys/types.h>
  75. #endif
  76. #include <openssl/bn.h> /* To get BN_LLONG properly defined */
  77. #include <openssl/bio.h>
  78. #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
  79. # ifndef HAVE_LONG_LONG
  80. # define HAVE_LONG_LONG 1
  81. # endif
  82. #endif
  83. /***************************************************************************/
  84. /*
  85. * Copyright Patrick Powell 1995
  86. * This code is based on code written by Patrick Powell <[email protected]>
  87. * It may be used for any purpose as long as this notice remains intact
  88. * on all source code distributions.
  89. */
  90. /*-
  91. * This code contains numerious changes and enhancements which were
  92. * made by lots of contributors over the last years to Patrick Powell's
  93. * original code:
  94. *
  95. * o Patrick Powell <[email protected]> (1995)
  96. * o Brandon Long <[email protected]> (1996, for Mutt)
  97. * o Thomas Roessler <[email protected]> (1998, for Mutt)
  98. * o Michael Elkins <[email protected]> (1998, for Mutt)
  99. * o Andrew Tridgell <[email protected]> (1998, for Samba)
  100. * o Luke Mewburn <[email protected]> (1999, for LukemFTP)
  101. * o Ralf S. Engelschall <[email protected]> (1999, for Pth)
  102. * o ... (for OpenSSL)
  103. */
  104. #ifdef HAVE_LONG_DOUBLE
  105. # define LDOUBLE long double
  106. #else
  107. # define LDOUBLE double
  108. #endif
  109. #ifdef HAVE_LONG_LONG
  110. # if defined(_WIN32) && !defined(__GNUC__)
  111. # define LLONG __int64
  112. # else
  113. # define LLONG long long
  114. # endif
  115. #else
  116. # define LLONG long
  117. #endif
  118. static int fmtstr(char **, char **, size_t *, size_t *,
  119. const char *, int, int, int);
  120. static int fmtint(char **, char **, size_t *, size_t *,
  121. LLONG, int, int, int, int);
  122. static int fmtfp(char **, char **, size_t *, size_t *,
  123. LDOUBLE, int, int, int);
  124. static int doapr_outch(char **, char **, size_t *, size_t *, int);
  125. static int _dopr(char **sbuffer, char **buffer,
  126. size_t *maxlen, size_t *retlen, int *truncated,
  127. const char *format, va_list args);
  128. /* format read states */
  129. #define DP_S_DEFAULT 0
  130. #define DP_S_FLAGS 1
  131. #define DP_S_MIN 2
  132. #define DP_S_DOT 3
  133. #define DP_S_MAX 4
  134. #define DP_S_MOD 5
  135. #define DP_S_CONV 6
  136. #define DP_S_DONE 7
  137. /* format flags - Bits */
  138. #define DP_F_MINUS (1 << 0)
  139. #define DP_F_PLUS (1 << 1)
  140. #define DP_F_SPACE (1 << 2)
  141. #define DP_F_NUM (1 << 3)
  142. #define DP_F_ZERO (1 << 4)
  143. #define DP_F_UP (1 << 5)
  144. #define DP_F_UNSIGNED (1 << 6)
  145. /* conversion flags */
  146. #define DP_C_SHORT 1
  147. #define DP_C_LONG 2
  148. #define DP_C_LDOUBLE 3
  149. #define DP_C_LLONG 4
  150. /* some handy macros */
  151. #define char_to_int(p) (p - '0')
  152. #define OSSL_MAX(p,q) ((p >= q) ? p : q)
  153. static int
  154. _dopr(char **sbuffer,
  155. char **buffer,
  156. size_t *maxlen,
  157. size_t *retlen, int *truncated, const char *format, va_list args)
  158. {
  159. char ch;
  160. LLONG value;
  161. LDOUBLE fvalue;
  162. char *strvalue;
  163. int min;
  164. int max;
  165. int state;
  166. int flags;
  167. int cflags;
  168. size_t currlen;
  169. state = DP_S_DEFAULT;
  170. flags = currlen = cflags = min = 0;
  171. max = -1;
  172. ch = *format++;
  173. while (state != DP_S_DONE) {
  174. if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
  175. state = DP_S_DONE;
  176. switch (state) {
  177. case DP_S_DEFAULT:
  178. if (ch == '%')
  179. state = DP_S_FLAGS;
  180. else
  181. if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
  182. return 0;
  183. ch = *format++;
  184. break;
  185. case DP_S_FLAGS:
  186. switch (ch) {
  187. case '-':
  188. flags |= DP_F_MINUS;
  189. ch = *format++;
  190. break;
  191. case '+':
  192. flags |= DP_F_PLUS;
  193. ch = *format++;
  194. break;
  195. case ' ':
  196. flags |= DP_F_SPACE;
  197. ch = *format++;
  198. break;
  199. case '#':
  200. flags |= DP_F_NUM;
  201. ch = *format++;
  202. break;
  203. case '0':
  204. flags |= DP_F_ZERO;
  205. ch = *format++;
  206. break;
  207. default:
  208. state = DP_S_MIN;
  209. break;
  210. }
  211. break;
  212. case DP_S_MIN:
  213. if (isdigit((unsigned char)ch)) {
  214. min = 10 * min + char_to_int(ch);
  215. ch = *format++;
  216. } else if (ch == '*') {
  217. min = va_arg(args, int);
  218. ch = *format++;
  219. state = DP_S_DOT;
  220. } else
  221. state = DP_S_DOT;
  222. break;
  223. case DP_S_DOT:
  224. if (ch == '.') {
  225. state = DP_S_MAX;
  226. ch = *format++;
  227. } else
  228. state = DP_S_MOD;
  229. break;
  230. case DP_S_MAX:
  231. if (isdigit((unsigned char)ch)) {
  232. if (max < 0)
  233. max = 0;
  234. max = 10 * max + char_to_int(ch);
  235. ch = *format++;
  236. } else if (ch == '*') {
  237. max = va_arg(args, int);
  238. ch = *format++;
  239. state = DP_S_MOD;
  240. } else
  241. state = DP_S_MOD;
  242. break;
  243. case DP_S_MOD:
  244. switch (ch) {
  245. case 'h':
  246. cflags = DP_C_SHORT;
  247. ch = *format++;
  248. break;
  249. case 'l':
  250. if (*format == 'l') {
  251. cflags = DP_C_LLONG;
  252. format++;
  253. } else
  254. cflags = DP_C_LONG;
  255. ch = *format++;
  256. break;
  257. case 'q':
  258. cflags = DP_C_LLONG;
  259. ch = *format++;
  260. break;
  261. case 'L':
  262. cflags = DP_C_LDOUBLE;
  263. ch = *format++;
  264. break;
  265. default:
  266. break;
  267. }
  268. state = DP_S_CONV;
  269. break;
  270. case DP_S_CONV:
  271. switch (ch) {
  272. case 'd':
  273. case 'i':
  274. switch (cflags) {
  275. case DP_C_SHORT:
  276. value = (short int)va_arg(args, int);
  277. break;
  278. case DP_C_LONG:
  279. value = va_arg(args, long int);
  280. break;
  281. case DP_C_LLONG:
  282. value = va_arg(args, LLONG);
  283. break;
  284. default:
  285. value = va_arg(args, int);
  286. break;
  287. }
  288. if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
  289. max, flags))
  290. return 0;
  291. break;
  292. case 'X':
  293. flags |= DP_F_UP;
  294. /* FALLTHROUGH */
  295. case 'x':
  296. case 'o':
  297. case 'u':
  298. flags |= DP_F_UNSIGNED;
  299. switch (cflags) {
  300. case DP_C_SHORT:
  301. value = (unsigned short int)va_arg(args, unsigned int);
  302. break;
  303. case DP_C_LONG:
  304. value = (LLONG) va_arg(args, unsigned long int);
  305. break;
  306. case DP_C_LLONG:
  307. value = va_arg(args, unsigned LLONG);
  308. break;
  309. default:
  310. value = (LLONG) va_arg(args, unsigned int);
  311. break;
  312. }
  313. if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
  314. ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
  315. min, max, flags))
  316. return 0;
  317. break;
  318. case 'f':
  319. if (cflags == DP_C_LDOUBLE)
  320. fvalue = va_arg(args, LDOUBLE);
  321. else
  322. fvalue = va_arg(args, double);
  323. if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
  324. flags))
  325. return 0;
  326. break;
  327. case 'E':
  328. flags |= DP_F_UP;
  329. case 'e':
  330. if (cflags == DP_C_LDOUBLE)
  331. fvalue = va_arg(args, LDOUBLE);
  332. else
  333. fvalue = va_arg(args, double);
  334. break;
  335. case 'G':
  336. flags |= DP_F_UP;
  337. case 'g':
  338. if (cflags == DP_C_LDOUBLE)
  339. fvalue = va_arg(args, LDOUBLE);
  340. else
  341. fvalue = va_arg(args, double);
  342. break;
  343. case 'c':
  344. if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
  345. va_arg(args, int)))
  346. return 0;
  347. break;
  348. case 's':
  349. strvalue = va_arg(args, char *);
  350. if (max < 0) {
  351. if (buffer)
  352. max = INT_MAX;
  353. else
  354. max = *maxlen;
  355. }
  356. if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
  357. flags, min, max))
  358. return 0;
  359. break;
  360. case 'p':
  361. value = (long)va_arg(args, void *);
  362. if (!fmtint(sbuffer, buffer, &currlen, maxlen,
  363. value, 16, min, max, flags | DP_F_NUM))
  364. return 0;
  365. break;
  366. case 'n': /* XXX */
  367. if (cflags == DP_C_SHORT) {
  368. short int *num;
  369. num = va_arg(args, short int *);
  370. *num = currlen;
  371. } else if (cflags == DP_C_LONG) { /* XXX */
  372. long int *num;
  373. num = va_arg(args, long int *);
  374. *num = (long int)currlen;
  375. } else if (cflags == DP_C_LLONG) { /* XXX */
  376. LLONG *num;
  377. num = va_arg(args, LLONG *);
  378. *num = (LLONG) currlen;
  379. } else {
  380. int *num;
  381. num = va_arg(args, int *);
  382. *num = currlen;
  383. }
  384. break;
  385. case '%':
  386. if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
  387. return 0;
  388. break;
  389. case 'w':
  390. /* not supported yet, treat as next char */
  391. ch = *format++;
  392. break;
  393. default:
  394. /* unknown, skip */
  395. break;
  396. }
  397. ch = *format++;
  398. state = DP_S_DEFAULT;
  399. flags = cflags = min = 0;
  400. max = -1;
  401. break;
  402. case DP_S_DONE:
  403. break;
  404. default:
  405. break;
  406. }
  407. }
  408. /*
  409. * We have to truncate if there is no dynamic buffer and we have filled the
  410. * static buffer.
  411. */
  412. if (buffer == NULL) {
  413. *truncated = (currlen > *maxlen - 1);
  414. if (*truncated)
  415. currlen = *maxlen - 1;
  416. }
  417. if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
  418. return 0;
  419. *retlen = currlen - 1;
  420. return 1;
  421. }
  422. static int
  423. fmtstr(char **sbuffer,
  424. char **buffer,
  425. size_t *currlen,
  426. size_t *maxlen, const char *value, int flags, int min, int max)
  427. {
  428. int padlen;
  429. size_t strln;
  430. int cnt = 0;
  431. if (value == 0)
  432. value = "<NULL>";
  433. strln = strlen(value);
  434. if (strln > INT_MAX)
  435. strln = INT_MAX;
  436. padlen = min - strln;
  437. if (min < 0 || padlen < 0)
  438. padlen = 0;
  439. if (flags & DP_F_MINUS)
  440. padlen = -padlen;
  441. while ((padlen > 0) && (cnt < max)) {
  442. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  443. return 0;
  444. --padlen;
  445. ++cnt;
  446. }
  447. while (*value && (cnt < max)) {
  448. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
  449. return 0;
  450. ++cnt;
  451. }
  452. while ((padlen < 0) && (cnt < max)) {
  453. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  454. return 0;
  455. ++padlen;
  456. ++cnt;
  457. }
  458. return 1;
  459. }
  460. static int
  461. fmtint(char **sbuffer,
  462. char **buffer,
  463. size_t *currlen,
  464. size_t *maxlen, LLONG value, int base, int min, int max, int flags)
  465. {
  466. int signvalue = 0;
  467. const char *prefix = "";
  468. unsigned LLONG uvalue;
  469. char convert[DECIMAL_SIZE(value) + 3];
  470. int place = 0;
  471. int spadlen = 0;
  472. int zpadlen = 0;
  473. int caps = 0;
  474. if (max < 0)
  475. max = 0;
  476. uvalue = value;
  477. if (!(flags & DP_F_UNSIGNED)) {
  478. if (value < 0) {
  479. signvalue = '-';
  480. uvalue = -(unsigned LLONG)value;
  481. } else if (flags & DP_F_PLUS)
  482. signvalue = '+';
  483. else if (flags & DP_F_SPACE)
  484. signvalue = ' ';
  485. }
  486. if (flags & DP_F_NUM) {
  487. if (base == 8)
  488. prefix = "0";
  489. if (base == 16)
  490. prefix = "0x";
  491. }
  492. if (flags & DP_F_UP)
  493. caps = 1;
  494. do {
  495. convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
  496. [uvalue % (unsigned)base];
  497. uvalue = (uvalue / (unsigned)base);
  498. } while (uvalue && (place < (int)sizeof(convert)));
  499. if (place == sizeof(convert))
  500. place--;
  501. convert[place] = 0;
  502. zpadlen = max - place;
  503. spadlen =
  504. min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
  505. if (zpadlen < 0)
  506. zpadlen = 0;
  507. if (spadlen < 0)
  508. spadlen = 0;
  509. if (flags & DP_F_ZERO) {
  510. zpadlen = OSSL_MAX(zpadlen, spadlen);
  511. spadlen = 0;
  512. }
  513. if (flags & DP_F_MINUS)
  514. spadlen = -spadlen;
  515. /* spaces */
  516. while (spadlen > 0) {
  517. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  518. return 0;
  519. --spadlen;
  520. }
  521. /* sign */
  522. if (signvalue)
  523. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  524. return 0;
  525. /* prefix */
  526. while (*prefix) {
  527. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
  528. return 0;
  529. prefix++;
  530. }
  531. /* zeros */
  532. if (zpadlen > 0) {
  533. while (zpadlen > 0) {
  534. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  535. return 0;
  536. --zpadlen;
  537. }
  538. }
  539. /* digits */
  540. while (place > 0) {
  541. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
  542. return 0;
  543. }
  544. /* left justified spaces */
  545. while (spadlen < 0) {
  546. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  547. return 0;
  548. ++spadlen;
  549. }
  550. return 1;
  551. }
  552. static LDOUBLE abs_val(LDOUBLE value)
  553. {
  554. LDOUBLE result = value;
  555. if (value < 0)
  556. result = -value;
  557. return result;
  558. }
  559. static LDOUBLE pow_10(int in_exp)
  560. {
  561. LDOUBLE result = 1;
  562. while (in_exp) {
  563. result *= 10;
  564. in_exp--;
  565. }
  566. return result;
  567. }
  568. static long roundv(LDOUBLE value)
  569. {
  570. long intpart;
  571. intpart = (long)value;
  572. value = value - intpart;
  573. if (value >= 0.5)
  574. intpart++;
  575. return intpart;
  576. }
  577. static int
  578. fmtfp(char **sbuffer,
  579. char **buffer,
  580. size_t *currlen,
  581. size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
  582. {
  583. int signvalue = 0;
  584. LDOUBLE ufvalue;
  585. char iconvert[20];
  586. char fconvert[20];
  587. int iplace = 0;
  588. int fplace = 0;
  589. int padlen = 0;
  590. int zpadlen = 0;
  591. long intpart;
  592. long fracpart;
  593. long max10;
  594. if (max < 0)
  595. max = 6;
  596. ufvalue = abs_val(fvalue);
  597. if (fvalue < 0)
  598. signvalue = '-';
  599. else if (flags & DP_F_PLUS)
  600. signvalue = '+';
  601. else if (flags & DP_F_SPACE)
  602. signvalue = ' ';
  603. intpart = (long)ufvalue;
  604. /*
  605. * sorry, we only support 9 digits past the decimal because of our
  606. * conversion method
  607. */
  608. if (max > 9)
  609. max = 9;
  610. /*
  611. * we "cheat" by converting the fractional part to integer by multiplying
  612. * by a factor of 10
  613. */
  614. max10 = roundv(pow_10(max));
  615. fracpart = roundv(pow_10(max) * (ufvalue - intpart));
  616. if (fracpart >= max10) {
  617. intpart++;
  618. fracpart -= max10;
  619. }
  620. /* convert integer part */
  621. do {
  622. iconvert[iplace++] = "0123456789"[intpart % 10];
  623. intpart = (intpart / 10);
  624. } while (intpart && (iplace < (int)sizeof(iconvert)));
  625. if (iplace == sizeof iconvert)
  626. iplace--;
  627. iconvert[iplace] = 0;
  628. /* convert fractional part */
  629. do {
  630. fconvert[fplace++] = "0123456789"[fracpart % 10];
  631. fracpart = (fracpart / 10);
  632. } while (fplace < max);
  633. if (fplace == sizeof fconvert)
  634. fplace--;
  635. fconvert[fplace] = 0;
  636. /* -1 for decimal point, another -1 if we are printing a sign */
  637. padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
  638. zpadlen = max - fplace;
  639. if (zpadlen < 0)
  640. zpadlen = 0;
  641. if (padlen < 0)
  642. padlen = 0;
  643. if (flags & DP_F_MINUS)
  644. padlen = -padlen;
  645. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  646. if (signvalue) {
  647. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  648. return 0;
  649. --padlen;
  650. signvalue = 0;
  651. }
  652. while (padlen > 0) {
  653. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  654. return 0;
  655. --padlen;
  656. }
  657. }
  658. while (padlen > 0) {
  659. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  660. return 0;
  661. --padlen;
  662. }
  663. if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  664. return 0;
  665. while (iplace > 0) {
  666. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
  667. return 0;
  668. }
  669. /*
  670. * Decimal point. This should probably use locale to find the correct
  671. * char to print out.
  672. */
  673. if (max > 0 || (flags & DP_F_NUM)) {
  674. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
  675. return 0;
  676. while (fplace > 0) {
  677. if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
  678. fconvert[--fplace]))
  679. return 0;
  680. }
  681. }
  682. while (zpadlen > 0) {
  683. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  684. return 0;
  685. --zpadlen;
  686. }
  687. while (padlen < 0) {
  688. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  689. return 0;
  690. ++padlen;
  691. }
  692. return 1;
  693. }
  694. #define BUFFER_INC 1024
  695. static int
  696. doapr_outch(char **sbuffer,
  697. char **buffer, size_t *currlen, size_t *maxlen, int c)
  698. {
  699. /* If we haven't at least one buffer, someone has doe a big booboo */
  700. assert(*sbuffer != NULL || buffer != NULL);
  701. /* |currlen| must always be <= |*maxlen| */
  702. assert(*currlen <= *maxlen);
  703. if (buffer && *currlen == *maxlen) {
  704. if (*maxlen > INT_MAX - BUFFER_INC)
  705. return 0;
  706. *maxlen += BUFFER_INC;
  707. if (*buffer == NULL) {
  708. *buffer = OPENSSL_malloc(*maxlen);
  709. if (*buffer == NULL)
  710. return 0;
  711. if (*currlen > 0) {
  712. assert(*sbuffer != NULL);
  713. memcpy(*buffer, *sbuffer, *currlen);
  714. }
  715. *sbuffer = NULL;
  716. } else {
  717. char *tmpbuf;
  718. tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
  719. if (tmpbuf == NULL)
  720. return 0;
  721. *buffer = tmpbuf;
  722. }
  723. }
  724. if (*currlen < *maxlen) {
  725. if (*sbuffer)
  726. (*sbuffer)[(*currlen)++] = (char)c;
  727. else
  728. (*buffer)[(*currlen)++] = (char)c;
  729. }
  730. return 1;
  731. }
  732. /***************************************************************************/
  733. int BIO_printf(BIO *bio, const char *format, ...)
  734. {
  735. va_list args;
  736. int ret;
  737. va_start(args, format);
  738. ret = BIO_vprintf(bio, format, args);
  739. va_end(args);
  740. return (ret);
  741. }
  742. int BIO_vprintf(BIO *bio, const char *format, va_list args)
  743. {
  744. int ret;
  745. size_t retlen;
  746. char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
  747. * in small-stack environments, like threads
  748. * or DOS programs. */
  749. char *hugebufp = hugebuf;
  750. size_t hugebufsize = sizeof(hugebuf);
  751. char *dynbuf = NULL;
  752. int ignored;
  753. dynbuf = NULL;
  754. CRYPTO_push_info("doapr()");
  755. if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
  756. args)) {
  757. OPENSSL_free(dynbuf);
  758. return -1;
  759. }
  760. if (dynbuf) {
  761. ret = BIO_write(bio, dynbuf, (int)retlen);
  762. OPENSSL_free(dynbuf);
  763. } else {
  764. ret = BIO_write(bio, hugebuf, (int)retlen);
  765. }
  766. CRYPTO_pop_info();
  767. return (ret);
  768. }
  769. /*
  770. * As snprintf is not available everywhere, we provide our own
  771. * implementation. This function has nothing to do with BIOs, but it's
  772. * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
  773. * function should be renamed, but to what?)
  774. */
  775. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  776. {
  777. va_list args;
  778. int ret;
  779. va_start(args, format);
  780. ret = BIO_vsnprintf(buf, n, format, args);
  781. va_end(args);
  782. return (ret);
  783. }
  784. int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
  785. {
  786. size_t retlen;
  787. int truncated;
  788. if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
  789. return -1;
  790. if (truncated)
  791. /*
  792. * In case of truncation, return -1 like traditional snprintf.
  793. * (Current drafts for ISO/IEC 9899 say snprintf should return the
  794. * number of characters that would have been written, had the buffer
  795. * been large enough.)
  796. */
  797. return -1;
  798. else
  799. return (retlen <= INT_MAX) ? (int)retlen : -1;
  800. }