b_print.c 24 KB

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