b_print.c 24 KB

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