]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/b_print.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / crypto / bio / b_print.c
CommitLineData
d02b48c6 1/* crypto/bio/b_print.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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.
40720ce3 8 *
d02b48c6
RE
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).
40720ce3 15 *
d02b48c6
RE
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.
40720ce3 22 *
d02b48c6
RE
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 :-).
40720ce3 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40720ce3 40 *
d02b48c6
RE
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.
40720ce3 52 *
d02b48c6
RE
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
60e5f36d
BM
59/* disable assert() unless BIO_DEBUG has been defined */
60#ifndef BIO_DEBUG
61# ifndef NDEBUG
62# define NDEBUG
63# endif
64#endif
65
40720ce3 66/*
d02b48c6
RE
67 * Stolen from tjh's ssl/ssl_trc.c stuff.
68 */
69
70#include <stdio.h>
b478e91f
UM
71#include <string.h>
72#include <ctype.h>
5ee0d9c4
RL
73#include <assert.h>
74#include <limits.h>
d02b48c6 75#include "cryptlib.h"
a1990dd7 76#ifndef NO_SYS_TYPES_H
40720ce3 77# include <sys/types.h>
a1990dd7 78#endif
347177e0 79#include <openssl/bn.h> /* To get BN_LLONG properly defined */
ec577822 80#include <openssl/bio.h>
d02b48c6 81
80ed5f84 82#if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
b478e91f 83# ifndef HAVE_LONG_LONG
96723a3a 84# define HAVE_LONG_LONG 1
b478e91f
UM
85# endif
86#endif
87
e5c84d51 88/***************************************************************************/
d02b48c6 89
9fd4ee5d
UM
90/*
91 * Copyright Patrick Powell 1995
b478e91f 92 * This code is based on code written by Patrick Powell <papowell@astart.com>
9fd4ee5d 93 * It may be used for any purpose as long as this notice remains intact
b478e91f 94 * on all source code distributions.
9fd4ee5d
UM
95 */
96
b558c8d5 97/*-
b478e91f
UM
98 * This code contains numerious changes and enhancements which were
99 * made by lots of contributors over the last years to Patrick Powell's
100 * original code:
101 *
102 * o Patrick Powell <papowell@astart.com> (1995)
103 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
104 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
105 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
106 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
107 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
108 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
e5c84d51 109 * o ... (for OpenSSL)
9fd4ee5d
UM
110 */
111
ba8fb521 112#ifdef HAVE_LONG_DOUBLE
40720ce3 113# define LDOUBLE long double
b478e91f 114#else
40720ce3 115# define LDOUBLE double
b478e91f
UM
116#endif
117
118#if HAVE_LONG_LONG
c2e45f6d 119# if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
40720ce3 120# define LLONG __int64
c2e45f6d 121# else
40720ce3 122# define LLONG long long
c2e45f6d 123# endif
b478e91f 124#else
40720ce3 125# define LLONG long
b478e91f
UM
126#endif
127
40720ce3
MC
128static void fmtstr(char **, char **, size_t *, size_t *,
129 const char *, int, int, int);
130static void fmtint(char **, char **, size_t *, size_t *,
131 LLONG, int, int, int, int);
132static void fmtfp(char **, char **, size_t *, size_t *,
133 LDOUBLE, int, int, int);
134static void doapr_outch(char **, char **, size_t *, size_t *, int);
827dbcb1 135static void _dopr(char **sbuffer, char **buffer,
40720ce3
MC
136 size_t *maxlen, size_t *retlen, int *truncated,
137 const char *format, va_list args);
b478e91f 138
9fd4ee5d 139/* format read states */
b478e91f
UM
140#define DP_S_DEFAULT 0
141#define DP_S_FLAGS 1
142#define DP_S_MIN 2
143#define DP_S_DOT 3
144#define DP_S_MAX 4
145#define DP_S_MOD 5
146#define DP_S_CONV 6
147#define DP_S_DONE 7
9fd4ee5d
UM
148
149/* format flags - Bits */
b478e91f
UM
150#define DP_F_MINUS (1 << 0)
151#define DP_F_PLUS (1 << 1)
152#define DP_F_SPACE (1 << 2)
153#define DP_F_NUM (1 << 3)
154#define DP_F_ZERO (1 << 4)
155#define DP_F_UP (1 << 5)
156#define DP_F_UNSIGNED (1 << 6)
157
158/* conversion flags */
159#define DP_C_SHORT 1
160#define DP_C_LONG 2
161#define DP_C_LDOUBLE 3
162#define DP_C_LLONG 4
163
164/* some handy macros */
9fd4ee5d 165#define char_to_int(p) (p - '0')
99b1aaf2 166#define OSSL_MAX(p,q) ((p >= q) ? p : q)
9fd4ee5d 167
5ee0d9c4 168static void
40720ce3
MC
169_dopr(char **sbuffer,
170 char **buffer,
171 size_t *maxlen,
172 size_t *retlen, int *truncated, const char *format, va_list args)
9fd4ee5d 173{
b478e91f
UM
174 char ch;
175 LLONG value;
176 LDOUBLE fvalue;
177 char *strvalue;
178 int min;
179 int max;
180 int state;
181 int flags;
182 int cflags;
183 size_t currlen;
184
185 state = DP_S_DEFAULT;
186 flags = currlen = cflags = min = 0;
187 max = -1;
188 ch = *format++;
189
190 while (state != DP_S_DONE) {
827dbcb1 191 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
b478e91f
UM
192 state = DP_S_DONE;
193
194 switch (state) {
195 case DP_S_DEFAULT:
196 if (ch == '%')
197 state = DP_S_FLAGS;
198 else
40720ce3 199 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
b478e91f
UM
200 ch = *format++;
201 break;
202 case DP_S_FLAGS:
203 switch (ch) {
204 case '-':
205 flags |= DP_F_MINUS;
206 ch = *format++;
207 break;
208 case '+':
209 flags |= DP_F_PLUS;
210 ch = *format++;
211 break;
212 case ' ':
213 flags |= DP_F_SPACE;
214 ch = *format++;
215 break;
216 case '#':
217 flags |= DP_F_NUM;
218 ch = *format++;
219 break;
220 case '0':
221 flags |= DP_F_ZERO;
222 ch = *format++;
223 break;
224 default:
225 state = DP_S_MIN;
226 break;
227 }
228 break;
229 case DP_S_MIN:
230 if (isdigit((unsigned char)ch)) {
231 min = 10 * min + char_to_int(ch);
232 ch = *format++;
233 } else if (ch == '*') {
234 min = va_arg(args, int);
235 ch = *format++;
236 state = DP_S_DOT;
237 } else
238 state = DP_S_DOT;
239 break;
240 case DP_S_DOT:
241 if (ch == '.') {
242 state = DP_S_MAX;
243 ch = *format++;
244 } else
245 state = DP_S_MOD;
246 break;
247 case DP_S_MAX:
248 if (isdigit((unsigned char)ch)) {
249 if (max < 0)
250 max = 0;
251 max = 10 * max + char_to_int(ch);
252 ch = *format++;
253 } else if (ch == '*') {
254 max = va_arg(args, int);
255 ch = *format++;
256 state = DP_S_MOD;
257 } else
258 state = DP_S_MOD;
259 break;
260 case DP_S_MOD:
261 switch (ch) {
262 case 'h':
263 cflags = DP_C_SHORT;
264 ch = *format++;
265 break;
266 case 'l':
267 if (*format == 'l') {
268 cflags = DP_C_LLONG;
269 format++;
270 } else
271 cflags = DP_C_LONG;
272 ch = *format++;
273 break;
274 case 'q':
275 cflags = DP_C_LLONG;
276 ch = *format++;
277 break;
278 case 'L':
279 cflags = DP_C_LDOUBLE;
280 ch = *format++;
281 break;
282 default:
283 break;
284 }
285 state = DP_S_CONV;
286 break;
287 case DP_S_CONV:
288 switch (ch) {
289 case 'd':
290 case 'i':
291 switch (cflags) {
292 case DP_C_SHORT:
1b7aee1d 293 value = (short int)va_arg(args, int);
b478e91f
UM
294 break;
295 case DP_C_LONG:
296 value = va_arg(args, long int);
297 break;
298 case DP_C_LLONG:
299 value = va_arg(args, LLONG);
300 break;
301 default:
302 value = va_arg(args, int);
303 break;
304 }
827dbcb1 305 fmtint(sbuffer, buffer, &currlen, maxlen,
e5c84d51 306 value, 10, min, max, flags);
b478e91f
UM
307 break;
308 case 'X':
309 flags |= DP_F_UP;
310 /* FALLTHROUGH */
311 case 'x':
312 case 'o':
313 case 'u':
314 flags |= DP_F_UNSIGNED;
315 switch (cflags) {
316 case DP_C_SHORT:
42a9af38 317 value = (unsigned short int)va_arg(args, unsigned int);
b478e91f
UM
318 break;
319 case DP_C_LONG:
40720ce3 320 value = (LLONG) va_arg(args, unsigned long int);
b478e91f
UM
321 break;
322 case DP_C_LLONG:
323 value = va_arg(args, unsigned LLONG);
324 break;
325 default:
40720ce3 326 value = (LLONG) va_arg(args, unsigned int);
b478e91f
UM
327 break;
328 }
827dbcb1 329 fmtint(sbuffer, buffer, &currlen, maxlen, value,
b478e91f
UM
330 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
331 min, max, flags);
332 break;
333 case 'f':
334 if (cflags == DP_C_LDOUBLE)
335 fvalue = va_arg(args, LDOUBLE);
336 else
337 fvalue = va_arg(args, double);
827dbcb1 338 fmtfp(sbuffer, buffer, &currlen, maxlen,
e5c84d51 339 fvalue, min, max, flags);
b478e91f
UM
340 break;
341 case 'E':
342 flags |= DP_F_UP;
343 case 'e':
344 if (cflags == DP_C_LDOUBLE)
345 fvalue = va_arg(args, LDOUBLE);
346 else
347 fvalue = va_arg(args, double);
348 break;
349 case 'G':
350 flags |= DP_F_UP;
351 case 'g':
352 if (cflags == DP_C_LDOUBLE)
353 fvalue = va_arg(args, LDOUBLE);
354 else
355 fvalue = va_arg(args, double);
356 break;
357 case 'c':
827dbcb1 358 doapr_outch(sbuffer, buffer, &currlen, maxlen,
40720ce3 359 va_arg(args, int));
b478e91f
UM
360 break;
361 case 's':
362 strvalue = va_arg(args, char *);
827dbcb1 363 if (max < 0) {
40720ce3
MC
364 if (buffer)
365 max = INT_MAX;
366 else
367 max = *maxlen;
368 }
827dbcb1 369 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
e5c84d51 370 flags, min, max);
b478e91f
UM
371 break;
372 case 'p':
373 value = (long)va_arg(args, void *);
827dbcb1 374 fmtint(sbuffer, buffer, &currlen, maxlen,
40720ce3 375 value, 16, min, max, flags | DP_F_NUM);
b478e91f 376 break;
40720ce3 377 case 'n': /* XXX */
b478e91f
UM
378 if (cflags == DP_C_SHORT) {
379 short int *num;
380 num = va_arg(args, short int *);
381 *num = currlen;
382 } else if (cflags == DP_C_LONG) { /* XXX */
383 long int *num;
384 num = va_arg(args, long int *);
40720ce3 385 *num = (long int)currlen;
b478e91f
UM
386 } else if (cflags == DP_C_LLONG) { /* XXX */
387 LLONG *num;
388 num = va_arg(args, LLONG *);
389 *num = (LLONG) currlen;
390 } else {
40720ce3 391 int *num;
b478e91f
UM
392 num = va_arg(args, int *);
393 *num = currlen;
394 }
395 break;
396 case '%':
827dbcb1 397 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
b478e91f
UM
398 break;
399 case 'w':
400 /* not supported yet, treat as next char */
401 ch = *format++;
402 break;
403 default:
404 /* unknown, skip */
405 break;
406 }
407 ch = *format++;
408 state = DP_S_DEFAULT;
409 flags = cflags = min = 0;
410 max = -1;
411 break;
412 case DP_S_DONE:
413 break;
414 default:
415 break;
9fd4ee5d 416 }
9fd4ee5d 417 }
e5c84d51
BM
418 *truncated = (currlen > *maxlen - 1);
419 if (*truncated)
5ee0d9c4 420 currlen = *maxlen - 1;
827dbcb1
RL
421 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
422 *retlen = currlen - 1;
b478e91f 423 return;
9fd4ee5d
UM
424}
425
b478e91f 426static void
40720ce3
MC
427fmtstr(char **sbuffer,
428 char **buffer,
429 size_t *currlen,
430 size_t *maxlen, const char *value, int flags, int min, int max)
9fd4ee5d 431{
b478e91f
UM
432 int padlen, strln;
433 int cnt = 0;
434
435 if (value == 0)
436 value = "<NULL>";
40720ce3 437 for (strln = 0; value[strln]; ++strln) ;
b478e91f
UM
438 padlen = min - strln;
439 if (padlen < 0)
440 padlen = 0;
441 if (flags & DP_F_MINUS)
442 padlen = -padlen;
443
444 while ((padlen > 0) && (cnt < max)) {
827dbcb1 445 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f
UM
446 --padlen;
447 ++cnt;
448 }
449 while (*value && (cnt < max)) {
827dbcb1 450 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
b478e91f
UM
451 ++cnt;
452 }
453 while ((padlen < 0) && (cnt < max)) {
827dbcb1 454 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f
UM
455 ++padlen;
456 ++cnt;
457 }
9fd4ee5d
UM
458}
459
b478e91f 460static void
40720ce3
MC
461fmtint(char **sbuffer,
462 char **buffer,
463 size_t *currlen,
464 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
9fd4ee5d 465{
b478e91f 466 int signvalue = 0;
70f34a58 467 const char *prefix = "";
b478e91f 468 unsigned LLONG uvalue;
40720ce3 469 char convert[DECIMAL_SIZE(value) + 3];
b478e91f
UM
470 int place = 0;
471 int spadlen = 0;
472 int zpadlen = 0;
473 int caps = 0;
474
475 if (max < 0)
476 max = 0;
477 uvalue = value;
478 if (!(flags & DP_F_UNSIGNED)) {
479 if (value < 0) {
480 signvalue = '-';
481 uvalue = -value;
482 } else if (flags & DP_F_PLUS)
483 signvalue = '+';
484 else if (flags & DP_F_SPACE)
485 signvalue = ' ';
9fd4ee5d 486 }
c433d725 487 if (flags & DP_F_NUM) {
40720ce3
MC
488 if (base == 8)
489 prefix = "0";
490 if (base == 16)
491 prefix = "0x";
c433d725 492 }
b478e91f
UM
493 if (flags & DP_F_UP)
494 caps = 1;
495 do {
40720ce3
MC
496 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
497 [uvalue % (unsigned)base];
498 uvalue = (uvalue / (unsigned)base);
8a66d178
GT
499 } while (uvalue && (place < (int)sizeof(convert)));
500 if (place == sizeof(convert))
b478e91f
UM
501 place--;
502 convert[place] = 0;
503
504 zpadlen = max - place;
40720ce3
MC
505 spadlen =
506 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
b478e91f
UM
507 if (zpadlen < 0)
508 zpadlen = 0;
509 if (spadlen < 0)
510 spadlen = 0;
511 if (flags & DP_F_ZERO) {
99b1aaf2 512 zpadlen = OSSL_MAX(zpadlen, spadlen);
b478e91f 513 spadlen = 0;
9fd4ee5d 514 }
b478e91f
UM
515 if (flags & DP_F_MINUS)
516 spadlen = -spadlen;
9fd4ee5d 517
b478e91f
UM
518 /* spaces */
519 while (spadlen > 0) {
827dbcb1 520 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f
UM
521 --spadlen;
522 }
9fd4ee5d 523
b478e91f
UM
524 /* sign */
525 if (signvalue)
827dbcb1 526 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
9fd4ee5d 527
c433d725
RL
528 /* prefix */
529 while (*prefix) {
40720ce3
MC
530 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
531 prefix++;
c433d725
RL
532 }
533
b478e91f
UM
534 /* zeros */
535 if (zpadlen > 0) {
536 while (zpadlen > 0) {
827dbcb1 537 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
b478e91f
UM
538 --zpadlen;
539 }
540 }
541 /* digits */
542 while (place > 0)
827dbcb1 543 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
b478e91f
UM
544
545 /* left justified spaces */
546 while (spadlen < 0) {
827dbcb1 547 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f
UM
548 ++spadlen;
549 }
550 return;
9fd4ee5d
UM
551}
552
40720ce3 553static LDOUBLE abs_val(LDOUBLE value)
9fd4ee5d 554{
b478e91f
UM
555 LDOUBLE result = value;
556 if (value < 0)
557 result = -value;
558 return result;
9fd4ee5d
UM
559}
560
40720ce3 561static LDOUBLE pow_10(int in_exp)
9fd4ee5d 562{
b478e91f 563 LDOUBLE result = 1;
d8ec0dcf 564 while (in_exp) {
b478e91f 565 result *= 10;
d8ec0dcf 566 in_exp--;
b478e91f
UM
567 }
568 return result;
9fd4ee5d
UM
569}
570
40720ce3 571static long roundv(LDOUBLE value)
9fd4ee5d 572{
b478e91f 573 long intpart;
40720ce3 574 intpart = (long)value;
b478e91f
UM
575 value = value - intpart;
576 if (value >= 0.5)
577 intpart++;
578 return intpart;
579}
9fd4ee5d 580
b478e91f 581static void
40720ce3
MC
582fmtfp(char **sbuffer,
583 char **buffer,
584 size_t *currlen,
585 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
b478e91f
UM
586{
587 int signvalue = 0;
588 LDOUBLE ufvalue;
589 char iconvert[20];
590 char fconvert[20];
591 int iplace = 0;
592 int fplace = 0;
593 int padlen = 0;
594 int zpadlen = 0;
595 int caps = 0;
596 long intpart;
597 long fracpart;
032c3ecb 598 long max10;
b478e91f
UM
599
600 if (max < 0)
601 max = 6;
602 ufvalue = abs_val(fvalue);
603 if (fvalue < 0)
604 signvalue = '-';
605 else if (flags & DP_F_PLUS)
606 signvalue = '+';
607 else if (flags & DP_F_SPACE)
608 signvalue = ' ';
609
610 intpart = (long)ufvalue;
611
40720ce3
MC
612 /*
613 * sorry, we only support 9 digits past the decimal because of our
614 * conversion method
615 */
b478e91f
UM
616 if (max > 9)
617 max = 9;
618
40720ce3
MC
619 /*
620 * we "cheat" by converting the fractional part to integer by multiplying
621 * by a factor of 10
622 */
2c288b2a
NL
623 max10 = roundv(pow_10(max));
624 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
b478e91f 625
032c3ecb 626 if (fracpart >= max10) {
b478e91f 627 intpart++;
032c3ecb 628 fracpart -= max10;
b478e91f 629 }
9fd4ee5d 630
b478e91f
UM
631 /* convert integer part */
632 do {
633 iconvert[iplace++] =
40720ce3 634 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[intpart % 10];
b478e91f 635 intpart = (intpart / 10);
a2b0de98
RL
636 } while (intpart && (iplace < (int)sizeof(iconvert)));
637 if (iplace == sizeof iconvert)
b478e91f
UM
638 iplace--;
639 iconvert[iplace] = 0;
640
641 /* convert fractional part */
642 do {
643 fconvert[fplace++] =
40720ce3 644 (caps ? "0123456789ABCDEF" : "0123456789abcdef")[fracpart % 10];
b478e91f 645 fracpart = (fracpart / 10);
bf401a2a 646 } while (fplace < max);
a2b0de98 647 if (fplace == sizeof fconvert)
b478e91f
UM
648 fplace--;
649 fconvert[fplace] = 0;
650
651 /* -1 for decimal point, another -1 if we are printing a sign */
652 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
653 zpadlen = max - fplace;
654 if (zpadlen < 0)
655 zpadlen = 0;
656 if (padlen < 0)
657 padlen = 0;
658 if (flags & DP_F_MINUS)
659 padlen = -padlen;
660
661 if ((flags & DP_F_ZERO) && (padlen > 0)) {
662 if (signvalue) {
827dbcb1 663 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
b478e91f
UM
664 --padlen;
665 signvalue = 0;
666 }
667 while (padlen > 0) {
827dbcb1 668 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
b478e91f
UM
669 --padlen;
670 }
671 }
672 while (padlen > 0) {
827dbcb1 673 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f
UM
674 --padlen;
675 }
676 if (signvalue)
827dbcb1 677 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
9fd4ee5d 678
b478e91f 679 while (iplace > 0)
827dbcb1 680 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
9fd4ee5d 681
b478e91f
UM
682 /*
683 * Decimal point. This should probably use locale to find the correct
684 * char to print out.
685 */
c433d725 686 if (max > 0 || (flags & DP_F_NUM)) {
827dbcb1 687 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
9fd4ee5d 688
b478e91f 689 while (fplace > 0)
827dbcb1 690 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
b478e91f
UM
691 }
692 while (zpadlen > 0) {
827dbcb1 693 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
b478e91f 694 --zpadlen;
9fd4ee5d 695 }
b478e91f
UM
696
697 while (padlen < 0) {
827dbcb1 698 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
b478e91f 699 ++padlen;
9fd4ee5d 700 }
9fd4ee5d
UM
701}
702
5ee0d9c4 703static void
40720ce3
MC
704doapr_outch(char **sbuffer,
705 char **buffer, size_t *currlen, size_t *maxlen, int c)
9fd4ee5d 706{
827dbcb1
RL
707 /* If we haven't at least one buffer, someone has doe a big booboo */
708 assert(*sbuffer != NULL || buffer != NULL);
709
710 if (buffer) {
40720ce3
MC
711 while (*currlen >= *maxlen) {
712 if (*buffer == NULL) {
713 if (*maxlen == 0)
714 *maxlen = 1024;
715 *buffer = OPENSSL_malloc(*maxlen);
716 if (*currlen > 0) {
717 assert(*sbuffer != NULL);
718 memcpy(*buffer, *sbuffer, *currlen);
719 }
720 *sbuffer = NULL;
721 } else {
722 *maxlen += 1024;
723 *buffer = OPENSSL_realloc(*buffer, *maxlen);
724 }
725 }
726 /* What to do if *buffer is NULL? */
727 assert(*sbuffer != NULL || *buffer != NULL);
5ee0d9c4 728 }
827dbcb1
RL
729
730 if (*currlen < *maxlen) {
40720ce3
MC
731 if (*sbuffer)
732 (*sbuffer)[(*currlen)++] = (char)c;
733 else
734 (*buffer)[(*currlen)++] = (char)c;
5ee0d9c4 735 }
5ee0d9c4 736
b478e91f 737 return;
9fd4ee5d 738}
e5c84d51
BM
739
740/***************************************************************************/
741
40720ce3
MC
742int BIO_printf(BIO *bio, const char *format, ...)
743{
744 va_list args;
745 int ret;
746
747 va_start(args, format);
748
749 ret = BIO_vprintf(bio, format, args);
750
751 va_end(args);
752 return (ret);
753}
754
755int BIO_vprintf(BIO *bio, const char *format, va_list args)
756{
757 int ret;
758 size_t retlen;
759 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
760 * in small-stack environments, like threads
761 * or DOS programs. */
762 char *hugebufp = hugebuf;
763 size_t hugebufsize = sizeof(hugebuf);
764 char *dynbuf = NULL;
765 int ignored;
766
767 dynbuf = NULL;
768 CRYPTO_push_info("doapr()");
769 _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, args);
770 if (dynbuf) {
771 ret = BIO_write(bio, dynbuf, (int)retlen);
772 OPENSSL_free(dynbuf);
773 } else {
774 ret = BIO_write(bio, hugebuf, (int)retlen);
775 }
776 CRYPTO_pop_info();
777 return (ret);
778}
779
780/*
781 * As snprintf is not available everywhere, we provide our own
782 * implementation. This function has nothing to do with BIOs, but it's
783 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
784 * function should be renamed, but to what?)
785 */
e5c84d51 786int BIO_snprintf(char *buf, size_t n, const char *format, ...)
40720ce3
MC
787{
788 va_list args;
789 int ret;
e6629837 790
40720ce3 791 va_start(args, format);
e6629837 792
40720ce3 793 ret = BIO_vsnprintf(buf, n, format, args);
e6629837 794
40720ce3
MC
795 va_end(args);
796 return (ret);
797}
e6629837
RL
798
799int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
40720ce3
MC
800{
801 size_t retlen;
802 int truncated;
803
804 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
805
806 if (truncated)
807 /*
808 * In case of truncation, return -1 like traditional snprintf.
809 * (Current drafts for ISO/IEC 9899 say snprintf should return the
810 * number of characters that would have been written, had the buffer
811 * been large enough.)
812 */
813 return -1;
814 else
815 return (retlen <= INT_MAX) ? (int)retlen : -1;
816}