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