b_print.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. #ifdef BN_LLONG
  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. #if 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 void fmtstr (char **, char **, size_t *, size_t *,
  119. const char *, int, int, int);
  120. static void fmtint (char **, char **, size_t *, size_t *,
  121. LLONG, int, int, int, int);
  122. static void fmtfp (char **, char **, size_t *, size_t *,
  123. LDOUBLE, int, int, int);
  124. static void doapr_outch (char **, char **, size_t *, size_t *, int);
  125. static void _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 void
  154. _dopr(
  155. char **sbuffer,
  156. char **buffer,
  157. size_t *maxlen,
  158. size_t *retlen,
  159. int *truncated,
  160. const char *format,
  161. va_list args)
  162. {
  163. char ch;
  164. LLONG value;
  165. LDOUBLE fvalue;
  166. char *strvalue;
  167. int min;
  168. int max;
  169. int state;
  170. int flags;
  171. int cflags;
  172. size_t currlen;
  173. state = DP_S_DEFAULT;
  174. flags = currlen = cflags = min = 0;
  175. max = -1;
  176. ch = *format++;
  177. while (state != DP_S_DONE) {
  178. if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
  179. state = DP_S_DONE;
  180. switch (state) {
  181. case DP_S_DEFAULT:
  182. if (ch == '%')
  183. state = DP_S_FLAGS;
  184. else
  185. doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
  186. ch = *format++;
  187. break;
  188. case DP_S_FLAGS:
  189. switch (ch) {
  190. case '-':
  191. flags |= DP_F_MINUS;
  192. ch = *format++;
  193. break;
  194. case '+':
  195. flags |= DP_F_PLUS;
  196. ch = *format++;
  197. break;
  198. case ' ':
  199. flags |= DP_F_SPACE;
  200. ch = *format++;
  201. break;
  202. case '#':
  203. flags |= DP_F_NUM;
  204. ch = *format++;
  205. break;
  206. case '0':
  207. flags |= DP_F_ZERO;
  208. ch = *format++;
  209. break;
  210. default:
  211. state = DP_S_MIN;
  212. break;
  213. }
  214. break;
  215. case DP_S_MIN:
  216. if (isdigit((unsigned char)ch)) {
  217. min = 10 * min + char_to_int(ch);
  218. ch = *format++;
  219. } else if (ch == '*') {
  220. min = va_arg(args, int);
  221. ch = *format++;
  222. state = DP_S_DOT;
  223. } else
  224. state = DP_S_DOT;
  225. break;
  226. case DP_S_DOT:
  227. if (ch == '.') {
  228. state = DP_S_MAX;
  229. ch = *format++;
  230. } else
  231. state = DP_S_MOD;
  232. break;
  233. case DP_S_MAX:
  234. if (isdigit((unsigned char)ch)) {
  235. if (max < 0)
  236. max = 0;
  237. max = 10 * max + char_to_int(ch);
  238. ch = *format++;
  239. } else if (ch == '*') {
  240. max = va_arg(args, int);
  241. ch = *format++;
  242. state = DP_S_MOD;
  243. } else
  244. state = DP_S_MOD;
  245. break;
  246. case DP_S_MOD:
  247. switch (ch) {
  248. case 'h':
  249. cflags = DP_C_SHORT;
  250. ch = *format++;
  251. break;
  252. case 'l':
  253. if (*format == 'l') {
  254. cflags = DP_C_LLONG;
  255. format++;
  256. } else
  257. cflags = DP_C_LONG;
  258. ch = *format++;
  259. break;
  260. case 'q':
  261. cflags = DP_C_LLONG;
  262. ch = *format++;
  263. break;
  264. case 'L':
  265. cflags = DP_C_LDOUBLE;
  266. ch = *format++;
  267. break;
  268. default:
  269. break;
  270. }
  271. state = DP_S_CONV;
  272. break;
  273. case DP_S_CONV:
  274. switch (ch) {
  275. case 'd':
  276. case 'i':
  277. switch (cflags) {
  278. case DP_C_SHORT:
  279. value = (short int)va_arg(args, int);
  280. break;
  281. case DP_C_LONG:
  282. value = va_arg(args, long int);
  283. break;
  284. case DP_C_LLONG:
  285. value = va_arg(args, LLONG);
  286. break;
  287. default:
  288. value = va_arg(args, int);
  289. break;
  290. }
  291. fmtint(sbuffer, buffer, &currlen, maxlen,
  292. value, 10, min, max, flags);
  293. break;
  294. case 'X':
  295. flags |= DP_F_UP;
  296. /* FALLTHROUGH */
  297. case 'x':
  298. case 'o':
  299. case 'u':
  300. flags |= DP_F_UNSIGNED;
  301. switch (cflags) {
  302. case DP_C_SHORT:
  303. value = (unsigned short int)va_arg(args, unsigned int);
  304. break;
  305. case DP_C_LONG:
  306. value = (LLONG) va_arg(args,
  307. unsigned long int);
  308. break;
  309. case DP_C_LLONG:
  310. value = va_arg(args, unsigned LLONG);
  311. break;
  312. default:
  313. value = (LLONG) va_arg(args,
  314. unsigned int);
  315. break;
  316. }
  317. fmtint(sbuffer, buffer, &currlen, maxlen, value,
  318. ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
  319. min, max, flags);
  320. break;
  321. case 'f':
  322. if (cflags == DP_C_LDOUBLE)
  323. fvalue = va_arg(args, LDOUBLE);
  324. else
  325. fvalue = va_arg(args, double);
  326. fmtfp(sbuffer, buffer, &currlen, maxlen,
  327. fvalue, min, max, flags);
  328. break;
  329. case 'E':
  330. flags |= DP_F_UP;
  331. case 'e':
  332. if (cflags == DP_C_LDOUBLE)
  333. fvalue = va_arg(args, LDOUBLE);
  334. else
  335. fvalue = va_arg(args, double);
  336. break;
  337. case 'G':
  338. flags |= DP_F_UP;
  339. case 'g':
  340. if (cflags == DP_C_LDOUBLE)
  341. fvalue = va_arg(args, LDOUBLE);
  342. else
  343. fvalue = va_arg(args, double);
  344. break;
  345. case 'c':
  346. doapr_outch(sbuffer, buffer, &currlen, maxlen,
  347. va_arg(args, int));
  348. break;
  349. case 's':
  350. strvalue = va_arg(args, char *);
  351. if (max < 0) {
  352. if (buffer)
  353. max = INT_MAX;
  354. else
  355. max = *maxlen;
  356. }
  357. fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
  358. flags, min, max);
  359. break;
  360. case 'p':
  361. value = (long)va_arg(args, void *);
  362. fmtint(sbuffer, buffer, &currlen, maxlen,
  363. value, 16, min, max, flags);
  364. break;
  365. case 'n': /* XXX */
  366. if (cflags == DP_C_SHORT) {
  367. short int *num;
  368. num = va_arg(args, short int *);
  369. *num = currlen;
  370. } else if (cflags == DP_C_LONG) { /* XXX */
  371. long int *num;
  372. num = va_arg(args, long int *);
  373. *num = (long int) currlen;
  374. } else if (cflags == DP_C_LLONG) { /* XXX */
  375. LLONG *num;
  376. num = va_arg(args, LLONG *);
  377. *num = (LLONG) currlen;
  378. } else {
  379. int *num;
  380. num = va_arg(args, int *);
  381. *num = currlen;
  382. }
  383. break;
  384. case '%':
  385. doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
  386. break;
  387. case 'w':
  388. /* not supported yet, treat as next char */
  389. ch = *format++;
  390. break;
  391. default:
  392. /* unknown, skip */
  393. break;
  394. }
  395. ch = *format++;
  396. state = DP_S_DEFAULT;
  397. flags = cflags = min = 0;
  398. max = -1;
  399. break;
  400. case DP_S_DONE:
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. *truncated = (currlen > *maxlen - 1);
  407. if (*truncated)
  408. currlen = *maxlen - 1;
  409. doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
  410. *retlen = currlen - 1;
  411. return;
  412. }
  413. static void
  414. fmtstr(
  415. char **sbuffer,
  416. char **buffer,
  417. size_t *currlen,
  418. size_t *maxlen,
  419. const char *value,
  420. int flags,
  421. int min,
  422. int max)
  423. {
  424. int padlen, strln;
  425. int cnt = 0;
  426. if (value == 0)
  427. value = "<NULL>";
  428. for (strln = 0; value[strln]; ++strln)
  429. ;
  430. padlen = min - strln;
  431. if (padlen < 0)
  432. padlen = 0;
  433. if (flags & DP_F_MINUS)
  434. padlen = -padlen;
  435. while ((padlen > 0) && (cnt < max)) {
  436. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  437. --padlen;
  438. ++cnt;
  439. }
  440. while (*value && (cnt < max)) {
  441. doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
  442. ++cnt;
  443. }
  444. while ((padlen < 0) && (cnt < max)) {
  445. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  446. ++padlen;
  447. ++cnt;
  448. }
  449. }
  450. static void
  451. fmtint(
  452. char **sbuffer,
  453. char **buffer,
  454. size_t *currlen,
  455. size_t *maxlen,
  456. LLONG value,
  457. int base,
  458. int min,
  459. int max,
  460. int flags)
  461. {
  462. int signvalue = 0;
  463. unsigned LLONG uvalue;
  464. char convert[20];
  465. int place = 0;
  466. int spadlen = 0;
  467. int zpadlen = 0;
  468. int caps = 0;
  469. if (max < 0)
  470. max = 0;
  471. uvalue = value;
  472. if (!(flags & DP_F_UNSIGNED)) {
  473. if (value < 0) {
  474. signvalue = '-';
  475. uvalue = -value;
  476. } else if (flags & DP_F_PLUS)
  477. signvalue = '+';
  478. else if (flags & DP_F_SPACE)
  479. signvalue = ' ';
  480. }
  481. if (flags & DP_F_UP)
  482. caps = 1;
  483. do {
  484. convert[place++] =
  485. (caps ? "0123456789ABCDEF" : "0123456789abcdef")
  486. [uvalue % (unsigned) base];
  487. uvalue = (uvalue / (unsigned) base);
  488. } while (uvalue && (place < 20));
  489. if (place == 20)
  490. place--;
  491. convert[place] = 0;
  492. zpadlen = max - place;
  493. spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0);
  494. if (zpadlen < 0)
  495. zpadlen = 0;
  496. if (spadlen < 0)
  497. spadlen = 0;
  498. if (flags & DP_F_ZERO) {
  499. zpadlen = OSSL_MAX(zpadlen, spadlen);
  500. spadlen = 0;
  501. }
  502. if (flags & DP_F_MINUS)
  503. spadlen = -spadlen;
  504. /* spaces */
  505. while (spadlen > 0) {
  506. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  507. --spadlen;
  508. }
  509. /* sign */
  510. if (signvalue)
  511. doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
  512. /* zeros */
  513. if (zpadlen > 0) {
  514. while (zpadlen > 0) {
  515. doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
  516. --zpadlen;
  517. }
  518. }
  519. /* digits */
  520. while (place > 0)
  521. doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
  522. /* left justified spaces */
  523. while (spadlen < 0) {
  524. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  525. ++spadlen;
  526. }
  527. return;
  528. }
  529. static LDOUBLE
  530. abs_val(LDOUBLE value)
  531. {
  532. LDOUBLE result = value;
  533. if (value < 0)
  534. result = -value;
  535. return result;
  536. }
  537. static LDOUBLE
  538. pow10(int exp)
  539. {
  540. LDOUBLE result = 1;
  541. while (exp) {
  542. result *= 10;
  543. exp--;
  544. }
  545. return result;
  546. }
  547. static long
  548. roundv(LDOUBLE value)
  549. {
  550. long intpart;
  551. intpart = (long) value;
  552. value = value - intpart;
  553. if (value >= 0.5)
  554. intpart++;
  555. return intpart;
  556. }
  557. static void
  558. fmtfp(
  559. char **sbuffer,
  560. char **buffer,
  561. size_t *currlen,
  562. size_t *maxlen,
  563. LDOUBLE fvalue,
  564. int min,
  565. int max,
  566. int flags)
  567. {
  568. int signvalue = 0;
  569. LDOUBLE ufvalue;
  570. char iconvert[20];
  571. char fconvert[20];
  572. int iplace = 0;
  573. int fplace = 0;
  574. int padlen = 0;
  575. int zpadlen = 0;
  576. int caps = 0;
  577. long intpart;
  578. long fracpart;
  579. if (max < 0)
  580. max = 6;
  581. ufvalue = abs_val(fvalue);
  582. if (fvalue < 0)
  583. signvalue = '-';
  584. else if (flags & DP_F_PLUS)
  585. signvalue = '+';
  586. else if (flags & DP_F_SPACE)
  587. signvalue = ' ';
  588. intpart = (long)ufvalue;
  589. /* sorry, we only support 9 digits past the decimal because of our
  590. conversion method */
  591. if (max > 9)
  592. max = 9;
  593. /* we "cheat" by converting the fractional part to integer by
  594. multiplying by a factor of 10 */
  595. fracpart = roundv((pow10(max)) * (ufvalue - intpart));
  596. if (fracpart >= pow10(max)) {
  597. intpart++;
  598. fracpart -= (long)pow10(max);
  599. }
  600. /* convert integer part */
  601. do {
  602. iconvert[iplace++] =
  603. (caps ? "0123456789ABCDEF"
  604. : "0123456789abcdef")[intpart % 10];
  605. intpart = (intpart / 10);
  606. } while (intpart && (iplace < 20));
  607. if (iplace == 20)
  608. iplace--;
  609. iconvert[iplace] = 0;
  610. /* convert fractional part */
  611. do {
  612. fconvert[fplace++] =
  613. (caps ? "0123456789ABCDEF"
  614. : "0123456789abcdef")[fracpart % 10];
  615. fracpart = (fracpart / 10);
  616. } while (fplace < max);
  617. if (fplace == 20)
  618. fplace--;
  619. fconvert[fplace] = 0;
  620. /* -1 for decimal point, another -1 if we are printing a sign */
  621. padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
  622. zpadlen = max - fplace;
  623. if (zpadlen < 0)
  624. zpadlen = 0;
  625. if (padlen < 0)
  626. padlen = 0;
  627. if (flags & DP_F_MINUS)
  628. padlen = -padlen;
  629. if ((flags & DP_F_ZERO) && (padlen > 0)) {
  630. if (signvalue) {
  631. doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
  632. --padlen;
  633. signvalue = 0;
  634. }
  635. while (padlen > 0) {
  636. doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
  637. --padlen;
  638. }
  639. }
  640. while (padlen > 0) {
  641. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  642. --padlen;
  643. }
  644. if (signvalue)
  645. doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
  646. while (iplace > 0)
  647. doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
  648. /*
  649. * Decimal point. This should probably use locale to find the correct
  650. * char to print out.
  651. */
  652. if (max > 0) {
  653. doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
  654. while (fplace > 0)
  655. doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
  656. }
  657. while (zpadlen > 0) {
  658. doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
  659. --zpadlen;
  660. }
  661. while (padlen < 0) {
  662. doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
  663. ++padlen;
  664. }
  665. }
  666. static void
  667. doapr_outch(
  668. char **sbuffer,
  669. char **buffer,
  670. size_t *currlen,
  671. size_t *maxlen,
  672. int c)
  673. {
  674. /* If we haven't at least one buffer, someone has doe a big booboo */
  675. assert(*sbuffer != NULL || buffer != NULL);
  676. if (buffer) {
  677. while (*currlen >= *maxlen) {
  678. if (*buffer == NULL) {
  679. if (*maxlen == 0)
  680. *maxlen = 1024;
  681. *buffer = OPENSSL_malloc(*maxlen);
  682. if (*currlen > 0) {
  683. assert(*sbuffer != NULL);
  684. memcpy(*buffer, *sbuffer, *currlen);
  685. }
  686. *sbuffer = NULL;
  687. } else {
  688. *maxlen += 1024;
  689. *buffer = OPENSSL_realloc(*buffer, *maxlen);
  690. }
  691. }
  692. /* What to do if *buffer is NULL? */
  693. assert(*sbuffer != NULL || *buffer != NULL);
  694. }
  695. if (*currlen < *maxlen) {
  696. if (*sbuffer)
  697. (*sbuffer)[(*currlen)++] = (char)c;
  698. else
  699. (*buffer)[(*currlen)++] = (char)c;
  700. }
  701. return;
  702. }
  703. /***************************************************************************/
  704. int BIO_printf (BIO *bio, const char *format, ...)
  705. {
  706. va_list args;
  707. int ret;
  708. va_start(args, format);
  709. ret = BIO_vprintf(bio, format, args);
  710. va_end(args);
  711. return(ret);
  712. }
  713. int BIO_vprintf (BIO *bio, const char *format, va_list args)
  714. {
  715. int ret;
  716. size_t retlen;
  717. char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
  718. in small-stack environments, like threads
  719. or DOS programs. */
  720. char *hugebufp = hugebuf;
  721. size_t hugebufsize = sizeof(hugebuf);
  722. char *dynbuf = NULL;
  723. int ignored;
  724. dynbuf = NULL;
  725. CRYPTO_push_info("doapr()");
  726. _dopr(&hugebufp, &dynbuf, &hugebufsize,
  727. &retlen, &ignored, format, args);
  728. if (dynbuf)
  729. {
  730. ret=BIO_write(bio, dynbuf, (int)retlen);
  731. OPENSSL_free(dynbuf);
  732. }
  733. else
  734. {
  735. ret=BIO_write(bio, hugebuf, (int)retlen);
  736. }
  737. CRYPTO_pop_info();
  738. return(ret);
  739. }
  740. /* As snprintf is not available everywhere, we provide our own implementation.
  741. * This function has nothing to do with BIOs, but it's closely related
  742. * to BIO_printf, and we need *some* name prefix ...
  743. * (XXX the function should be renamed, but to what?) */
  744. int BIO_snprintf(char *buf, size_t n, const char *format, ...)
  745. {
  746. va_list args;
  747. int ret;
  748. va_start(args, format);
  749. ret = BIO_vsnprintf(buf, n, format, args);
  750. va_end(args);
  751. return(ret);
  752. }
  753. int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
  754. {
  755. size_t retlen;
  756. int truncated;
  757. _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
  758. if (truncated)
  759. /* In case of truncation, return -1 like traditional snprintf.
  760. * (Current drafts for ISO/IEC 9899 say snprintf should return
  761. * the number of characters that would have been written,
  762. * had the buffer been large enough.) */
  763. return -1;
  764. else
  765. return (retlen <= INT_MAX) ? (int)retlen : -1;
  766. }