b_print.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /* crypto/bio/b_print.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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 <papowell@astart.com>
  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 <papowell@astart.com> (1995)
  96. * o Brandon Long <blong@fiction.net> (1996, for Mutt)
  97. * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
  98. * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
  99. * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
  100. * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
  101. * o Ralf S. Engelschall <rse@engelschall.com> (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. *truncated = (currlen > *maxlen - 1);
  409. if (*truncated)
  410. currlen = *maxlen - 1;
  411. if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
  412. return 0;
  413. *retlen = currlen - 1;
  414. return 1;
  415. }
  416. static int
  417. fmtstr(char **sbuffer,
  418. char **buffer,
  419. size_t *currlen,
  420. size_t *maxlen, const char *value, int flags, int min, int max)
  421. {
  422. int padlen;
  423. size_t strln;
  424. int cnt = 0;
  425. if (value == 0)
  426. value = "<NULL>";
  427. strln = strlen(value);
  428. if (strln > INT_MAX)
  429. strln = INT_MAX;
  430. padlen = min - strln;
  431. if (min < 0 || padlen < 0)
  432. padlen = 0;
  433. if (flags & DP_F_MINUS)
  434. padlen = -padlen;
  435. while ((padlen > 0) && (cnt < max)) {
  436. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  437. return 0;
  438. --padlen;
  439. ++cnt;
  440. }
  441. while (*value && (cnt < max)) {
  442. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
  443. return 0;
  444. ++cnt;
  445. }
  446. while ((padlen < 0) && (cnt < max)) {
  447. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  448. return 0;
  449. ++padlen;
  450. ++cnt;
  451. }
  452. return 1;
  453. }
  454. static int
  455. fmtint(char **sbuffer,
  456. char **buffer,
  457. size_t *currlen,
  458. size_t *maxlen, LLONG value, int base, int min, int max, int flags)
  459. {
  460. int signvalue = 0;
  461. const char *prefix = "";
  462. unsigned LLONG uvalue;
  463. char convert[DECIMAL_SIZE(value) + 3];
  464. int place = 0;
  465. int spadlen = 0;
  466. int zpadlen = 0;
  467. int caps = 0;
  468. if (max < 0)
  469. max = 0;
  470. uvalue = value;
  471. if (!(flags & DP_F_UNSIGNED)) {
  472. if (value < 0) {
  473. signvalue = '-';
  474. uvalue = -value;
  475. } else if (flags & DP_F_PLUS)
  476. signvalue = '+';
  477. else if (flags & DP_F_SPACE)
  478. signvalue = ' ';
  479. }
  480. if (flags & DP_F_NUM) {
  481. if (base == 8)
  482. prefix = "0";
  483. if (base == 16)
  484. prefix = "0x";
  485. }
  486. if (flags & DP_F_UP)
  487. caps = 1;
  488. do {
  489. convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
  490. [uvalue % (unsigned)base];
  491. uvalue = (uvalue / (unsigned)base);
  492. } while (uvalue && (place < (int)sizeof(convert)));
  493. if (place == sizeof(convert))
  494. place--;
  495. convert[place] = 0;
  496. zpadlen = max - place;
  497. spadlen =
  498. min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
  499. if (zpadlen < 0)
  500. zpadlen = 0;
  501. if (spadlen < 0)
  502. spadlen = 0;
  503. if (flags & DP_F_ZERO) {
  504. zpadlen = OSSL_MAX(zpadlen, spadlen);
  505. spadlen = 0;
  506. }
  507. if (flags & DP_F_MINUS)
  508. spadlen = -spadlen;
  509. /* spaces */
  510. while (spadlen > 0) {
  511. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  512. return 0;
  513. --spadlen;
  514. }
  515. /* sign */
  516. if (signvalue)
  517. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  518. return 0;
  519. /* prefix */
  520. while (*prefix) {
  521. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
  522. return 0;
  523. prefix++;
  524. }
  525. /* zeros */
  526. if (zpadlen > 0) {
  527. while (zpadlen > 0) {
  528. if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  529. return 0;
  530. --zpadlen;
  531. }
  532. }
  533. /* digits */
  534. while (place > 0) {
  535. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
  536. return 0;
  537. }
  538. /* left justified spaces */
  539. while (spadlen < 0) {
  540. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  541. return 0;
  542. ++spadlen;
  543. }
  544. return 1;
  545. }
  546. static LDOUBLE abs_val(LDOUBLE value)
  547. {
  548. LDOUBLE result = value;
  549. if (value < 0)
  550. result = -value;
  551. return result;
  552. }
  553. static LDOUBLE pow_10(int in_exp)
  554. {
  555. LDOUBLE result = 1;
  556. while (in_exp) {
  557. result *= 10;
  558. in_exp--;
  559. }
  560. return result;
  561. }
  562. static long roundv(LDOUBLE value)
  563. {
  564. long intpart;
  565. intpart = (long)value;
  566. value = value - intpart;
  567. if (value >= 0.5)
  568. intpart++;
  569. return intpart;
  570. }
  571. static int
  572. fmtfp(char **sbuffer,
  573. char **buffer,
  574. size_t *currlen,
  575. size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
  576. {
  577. int signvalue = 0;
  578. LDOUBLE ufvalue;
  579. char iconvert[20];
  580. char fconvert[20];
  581. int iplace = 0;
  582. int fplace = 0;
  583. int padlen = 0;
  584. int zpadlen = 0;
  585. long intpart;
  586. long fracpart;
  587. long max10;
  588. if (max < 0)
  589. max = 6;
  590. ufvalue = abs_val(fvalue);
  591. if (fvalue < 0)
  592. signvalue = '-';
  593. else if (flags & DP_F_PLUS)
  594. signvalue = '+';
  595. else if (flags & DP_F_SPACE)
  596. signvalue = ' ';
  597. intpart = (long)ufvalue;
  598. /*
  599. * sorry, we only support 9 digits past the decimal because of our
  600. * conversion method
  601. */
  602. if (max > 9)
  603. max = 9;
  604. /*
  605. * we "cheat" by converting the fractional part to integer by multiplying
  606. * by a factor of 10
  607. */
  608. max10 = roundv(pow_10(max));
  609. fracpart = roundv(pow_10(max) * (ufvalue - intpart));
  610. if (fracpart >= max10) {
  611. intpart++;
  612. fracpart -= max10;
  613. }
  614. /* convert integer part */
  615. do {
  616. iconvert[iplace++] = "0123456789"[intpart % 10];
  617. intpart = (intpart / 10);
  618. } while (intpart && (iplace < (int)sizeof(iconvert)));
  619. if (iplace == sizeof iconvert)
  620. iplace--;
  621. iconvert[iplace] = 0;
  622. /* convert fractional part */
  623. do {
  624. fconvert[fplace++] = "0123456789"[fracpart % 10];
  625. fracpart = (fracpart / 10);
  626. } while (fplace < max);
  627. if (fplace == sizeof fconvert)
  628. fplace--;
  629. fconvert[fplace] = 0;
  630. /* -1 for decimal point, another -1 if we are printing a sign */
  631. padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
  632. zpadlen = max - fplace;
  633. if (zpadlen < 0)
  634. zpadlen = 0;
  635. if (padlen < 0)
  636. padlen = 0;
  637. if (flags & DP_F_MINUS)
  638. padlen = -padlen;
  639. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  640. if (signvalue) {
  641. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  642. return 0;
  643. --padlen;
  644. signvalue = 0;
  645. }
  646. while (padlen > 0) {
  647. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  648. return 0;
  649. --padlen;
  650. }
  651. }
  652. while (padlen > 0) {
  653. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  654. return 0;
  655. --padlen;
  656. }
  657. if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
  658. return 0;
  659. while (iplace > 0) {
  660. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
  661. return 0;
  662. }
  663. /*
  664. * Decimal point. This should probably use locale to find the correct
  665. * char to print out.
  666. */
  667. if (max > 0 || (flags & DP_F_NUM)) {
  668. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
  669. return 0;
  670. while (fplace > 0) {
  671. if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
  672. fconvert[--fplace]))
  673. return 0;
  674. }
  675. }
  676. while (zpadlen > 0) {
  677. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
  678. return 0;
  679. --zpadlen;
  680. }
  681. while (padlen < 0) {
  682. if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
  683. return 0;
  684. ++padlen;
  685. }
  686. return 1;
  687. }
  688. #define BUFFER_INC 1024
  689. static int
  690. doapr_outch(char **sbuffer,
  691. char **buffer, size_t *currlen, size_t *maxlen, int c)
  692. {
  693. /* If we haven't at least one buffer, someone has doe a big booboo */
  694. assert(*sbuffer != NULL || buffer != NULL);
  695. /* |currlen| must always be <= |*maxlen| */
  696. assert(*currlen <= *maxlen);
  697. if (buffer && *currlen == *maxlen) {
  698. if (*maxlen > INT_MAX - BUFFER_INC)
  699. return 0;
  700. *maxlen += BUFFER_INC;
  701. if (*buffer == NULL) {
  702. *buffer = OPENSSL_malloc(*maxlen);
  703. if (*buffer == NULL)
  704. return 0;
  705. if (*currlen > 0) {
  706. assert(*sbuffer != NULL);
  707. memcpy(*buffer, *sbuffer, *currlen);
  708. }
  709. *sbuffer = NULL;
  710. } else {
  711. char *tmpbuf;
  712. tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
  713. if (tmpbuf == NULL)
  714. return 0;
  715. *buffer = tmpbuf;
  716. }
  717. }
  718. if (*currlen < *maxlen) {
  719. if (*sbuffer)
  720. (*sbuffer)[(*currlen)++] = (char)c;
  721. else
  722. (*buffer)[(*currlen)++] = (char)c;
  723. }
  724. return 1;
  725. }
  726. /***************************************************************************/
  727. int BIO_printf(BIO *bio, const char *format, ...)
  728. {
  729. va_list args;
  730. int ret;
  731. va_start(args, format);
  732. ret = BIO_vprintf(bio, format, args);
  733. va_end(args);
  734. return (ret);
  735. }
  736. int BIO_vprintf(BIO *bio, const char *format, va_list args)
  737. {
  738. int ret;
  739. size_t retlen;
  740. char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
  741. * in small-stack environments, like threads
  742. * or DOS programs. */
  743. char *hugebufp = hugebuf;
  744. size_t hugebufsize = sizeof(hugebuf);
  745. char *dynbuf = NULL;
  746. int ignored;
  747. dynbuf = NULL;
  748. CRYPTO_push_info("doapr()");
  749. if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
  750. args)) {
  751. OPENSSL_free(dynbuf);
  752. return -1;
  753. }
  754. if (dynbuf) {
  755. ret = BIO_write(bio, dynbuf, (int)retlen);
  756. OPENSSL_free(dynbuf);
  757. } else {
  758. ret = BIO_write(bio, hugebuf, (int)retlen);
  759. }
  760. CRYPTO_pop_info();
  761. return (ret);
  762. }
  763. /*
  764. * As snprintf is not available everywhere, we provide our own
  765. * implementation. This function has nothing to do with BIOs, but it's
  766. * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
  767. * function should be renamed, but to what?)
  768. */
  769. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  770. {
  771. va_list args;
  772. int ret;
  773. va_start(args, format);
  774. ret = BIO_vsnprintf(buf, n, format, args);
  775. va_end(args);
  776. return (ret);
  777. }
  778. int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
  779. {
  780. size_t retlen;
  781. int truncated;
  782. if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
  783. return -1;
  784. if (truncated)
  785. /*
  786. * In case of truncation, return -1 like traditional snprintf.
  787. * (Current drafts for ISO/IEC 9899 say snprintf should return the
  788. * number of characters that would have been written, had the buffer
  789. * been large enough.)
  790. */
  791. return -1;
  792. else
  793. return (retlen <= INT_MAX) ? (int)retlen : -1;
  794. }