summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>2014-04-21 11:18:34 +0000
committerderaadt <>2014-04-21 11:18:34 +0000
commit8e35014f168891bed7866c3c08a7cd4efaaf8c90 (patch)
tree182c3a6725f3631931b08a9e3c6bcb3bfff15d5d
parentc7b0a4eec03bf03a5c2c668a4b732ecd9a91fb31 (diff)
downloadopenbsd-8e35014f168891bed7866c3c08a7cd4efaaf8c90.tar.gz
openbsd-8e35014f168891bed7866c3c08a7cd4efaaf8c90.tar.bz2
openbsd-8e35014f168891bed7866c3c08a7cd4efaaf8c90.zip
Replace entire printf-like guts with calls to libc snprintf.
funopen(3) is used to interface to BIO descriptors. ok guenther
-rw-r--r--src/lib/libcrypto/bio/b_print.c815
-rw-r--r--src/lib/libssl/src/crypto/bio/b_print.c815
2 files changed, 92 insertions, 1538 deletions
diff --git a/src/lib/libcrypto/bio/b_print.c b/src/lib/libcrypto/bio/b_print.c
index ff0089e82e..3a8fdcc821 100644
--- a/src/lib/libcrypto/bio/b_print.c
+++ b/src/lib/libcrypto/bio/b_print.c
@@ -1,781 +1,63 @@
1/* crypto/bio/b_print.c */ 1/* $OpenBSD: b_print.c,v 1.22 2014/04/21 11:18:34 deraadt Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Theo de Raadt places this file in the public domain. */
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 3
59/* disable assert() unless BIO_DEBUG has been defined */
60#ifndef BIO_DEBUG
61# ifndef NDEBUG
62# define NDEBUG
63# endif
64#endif
65
66/*
67 * Stolen from tjh's ssl/ssl_trc.c stuff.
68 */
69
70#include <stdio.h>
71#include <string.h>
72#include <ctype.h>
73#include <assert.h>
74#include <limits.h>
75#include "cryptlib.h"
76#ifndef NO_SYS_TYPES_H
77#include <sys/types.h>
78#endif
79#include <openssl/bn.h> /* To get BN_LLONG properly defined */
80#include <openssl/bio.h> 4#include <openssl/bio.h>
81 5
82#if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT) 6static int
83# ifndef HAVE_LONG_LONG 7_BIO_write(void *cookie, const char *buf, int nbytes)
84# define HAVE_LONG_LONG 1
85# endif
86#endif
87
88/***************************************************************************/
89
90/*
91 * Copyright Patrick Powell 1995
92 * This code is based on code written by Patrick Powell <papowell@astart.com>
93 * It may be used for any purpose as long as this notice remains intact
94 * on all source code distributions.
95 */
96
97/*
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)
109 * o ... (for OpenSSL)
110 */
111
112#ifdef HAVE_LONG_DOUBLE
113#define LDOUBLE long double
114#else
115#define LDOUBLE double
116#endif
117
118#ifdef HAVE_LONG_LONG
119# if defined(_WIN32) && !defined(__GNUC__)
120# define LLONG __int64
121# else
122# define LLONG long long
123# endif
124#else
125#define LLONG long
126#endif
127
128static void fmtstr(char **, char **, size_t *, size_t *, const char *,
129 int, int, int);
130static void fmtint(char **, char **, size_t *, size_t *, LLONG, int,
131 int, int, int);
132static void fmtfp(char **, char **, size_t *, size_t *, LDOUBLE,
133 int, int, int);
134static void doapr_outch(char **, char **, size_t *, size_t *, int);
135static void _dopr(char **sbuffer, char **buffer, size_t *maxlen,
136 size_t *retlen, int *truncated, const char *format, va_list args);
137
138/* format read states */
139#define DP_S_DEFAULT 0
140#define DP_S_FLAGS 1
141#define DP_S_MIN 2
142#define DP_S_DOT 3
143#define DP_S_MAX 4
144#define DP_S_MOD 5
145#define DP_S_CONV 6
146#define DP_S_DONE 7
147
148/* format flags - Bits */
149#define DP_F_MINUS (1 << 0)
150#define DP_F_PLUS (1 << 1)
151#define DP_F_SPACE (1 << 2)
152#define DP_F_NUM (1 << 3)
153#define DP_F_ZERO (1 << 4)
154#define DP_F_UP (1 << 5)
155#define DP_F_UNSIGNED (1 << 6)
156
157/* conversion flags */
158#define DP_C_SHORT 1
159#define DP_C_LONG 2
160#define DP_C_LDOUBLE 3
161#define DP_C_LLONG 4
162
163/* some handy macros */
164#define char_to_int(p) (p - '0')
165#define OSSL_MAX(p,q) ((p >= q) ? p : q)
166
167static void
168_dopr(char **sbuffer, char **buffer, size_t *maxlen, size_t *retlen,
169 int *truncated, const char *format, va_list args)
170{
171 char ch;
172 LLONG value;
173 LDOUBLE fvalue;
174 char *strvalue;
175 int min;
176 int max;
177 int state;
178 int flags;
179 int cflags;
180 size_t currlen;
181
182 state = DP_S_DEFAULT;
183 flags = currlen = cflags = min = 0;
184 max = -1;
185 ch = *format++;
186
187 while (state != DP_S_DONE) {
188 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
189 state = DP_S_DONE;
190
191 switch (state) {
192 case DP_S_DEFAULT:
193 if (ch == '%')
194 state = DP_S_FLAGS;
195 else
196 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
197 ch = *format++;
198 break;
199 case DP_S_FLAGS:
200 switch (ch) {
201 case '-':
202 flags |= DP_F_MINUS;
203 ch = *format++;
204 break;
205 case '+':
206 flags |= DP_F_PLUS;
207 ch = *format++;
208 break;
209 case ' ':
210 flags |= DP_F_SPACE;
211 ch = *format++;
212 break;
213 case '#':
214 flags |= DP_F_NUM;
215 ch = *format++;
216 break;
217 case '0':
218 flags |= DP_F_ZERO;
219 ch = *format++;
220 break;
221 default:
222 state = DP_S_MIN;
223 break;
224 }
225 break;
226 case DP_S_MIN:
227 if (isdigit((unsigned char)ch)) {
228 min = 10 * min + char_to_int(ch);
229 ch = *format++;
230 } else if (ch == '*') {
231 min = va_arg(args, int);
232 ch = *format++;
233 state = DP_S_DOT;
234 } else
235 state = DP_S_DOT;
236 break;
237 case DP_S_DOT:
238 if (ch == '.') {
239 state = DP_S_MAX;
240 ch = *format++;
241 } else
242 state = DP_S_MOD;
243 break;
244 case DP_S_MAX:
245 if (isdigit((unsigned char)ch)) {
246 if (max < 0)
247 max = 0;
248 max = 10 * max + char_to_int(ch);
249 ch = *format++;
250 } else if (ch == '*') {
251 max = va_arg(args, int);
252 ch = *format++;
253 state = DP_S_MOD;
254 } else
255 state = DP_S_MOD;
256 break;
257 case DP_S_MOD:
258 switch (ch) {
259 case 'h':
260 cflags = DP_C_SHORT;
261 ch = *format++;
262 break;
263 case 'l':
264 if (*format == 'l') {
265 cflags = DP_C_LLONG;
266 format++;
267 } else
268 cflags = DP_C_LONG;
269 ch = *format++;
270 break;
271 case 'q':
272 cflags = DP_C_LLONG;
273 ch = *format++;
274 break;
275 case 'L':
276 cflags = DP_C_LDOUBLE;
277 ch = *format++;
278 break;
279 default:
280 break;
281 }
282 state = DP_S_CONV;
283 break;
284 case DP_S_CONV:
285 switch (ch) {
286 case 'd':
287 case 'i':
288 switch (cflags) {
289 case DP_C_SHORT:
290 value = (short int)va_arg(args, int);
291 break;
292 case DP_C_LONG:
293 value = va_arg(args, long int);
294 break;
295 case DP_C_LLONG:
296 value = va_arg(args, LLONG);
297 break;
298 default:
299 value = va_arg(args, int);
300 break;
301 }
302 fmtint(sbuffer, buffer, &currlen, maxlen,
303 value, 10, min, max, flags);
304 break;
305 case 'X':
306 flags |= DP_F_UP;
307 /* FALLTHROUGH */
308 case 'x':
309 case 'o':
310 case 'u':
311 flags |= DP_F_UNSIGNED;
312 switch (cflags) {
313 case DP_C_SHORT:
314 value = (unsigned short int)va_arg(
315 args, unsigned int);
316 break;
317 case DP_C_LONG:
318 value = (LLONG)va_arg(args,
319 unsigned long int);
320 break;
321 case DP_C_LLONG:
322 value = va_arg(args, unsigned LLONG);
323 break;
324 default:
325 value = (LLONG)va_arg(args,
326 unsigned int);
327 break;
328 }
329 fmtint(sbuffer, buffer, &currlen, maxlen, value,
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);
338 fmtfp(sbuffer, buffer, &currlen, maxlen,
339 fvalue, min, max, flags);
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':
358 doapr_outch(sbuffer, buffer, &currlen, maxlen,
359 va_arg(args, int));
360 break;
361 case 's':
362 strvalue = va_arg(args, char *);
363 if (max < 0) {
364 if (buffer)
365 max = INT_MAX;
366 else
367 max = *maxlen;
368 }
369 fmtstr(sbuffer, buffer, &currlen, maxlen,
370 strvalue, flags, min, max);
371 break;
372 case 'p':
373 value = (long)va_arg(args, void *);
374 fmtint(sbuffer, buffer, &currlen, maxlen,
375 value, 16, min, max, flags|DP_F_NUM);
376 break;
377 case 'n': /* XXX */
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 *);
385 *num = (long int) currlen;
386 } else if (cflags == DP_C_LLONG) { /* XXX */
387 LLONG *num;
388 num = va_arg(args, LLONG *);
389 *num = (LLONG) currlen;
390 } else {
391 int *num;
392 num = va_arg(args, int *);
393 *num = currlen;
394 }
395 break;
396 case '%':
397 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
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;
416 }
417 }
418 *truncated = (currlen > *maxlen - 1);
419 if (*truncated)
420 currlen = *maxlen - 1;
421 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
422 *retlen = currlen - 1;
423 return;
424}
425
426static void
427fmtstr(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
428 const char *value, int flags, int min, int max)
429{
430 int padlen, strln;
431 int cnt = 0;
432
433 if (value == 0)
434 value = "<NULL>";
435 for (strln = 0; value[strln]; ++strln)
436 ;
437 padlen = min - strln;
438 if (padlen < 0)
439 padlen = 0;
440 if (flags & DP_F_MINUS)
441 padlen = -padlen;
442
443 while ((padlen > 0) && (cnt < max)) {
444 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
445 --padlen;
446 ++cnt;
447 }
448 while (*value && (cnt < max)) {
449 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
450 ++cnt;
451 }
452 while ((padlen < 0) && (cnt < max)) {
453 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
454 ++padlen;
455 ++cnt;
456 }
457}
458
459static void
460fmtint(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
461 LLONG value, int base, int min, int max, int flags)
462{ 8{
463 int signvalue = 0; 9 return BIO_write(cookie, buf, nbytes);
464 const char *prefix = "";
465 unsigned LLONG uvalue;
466 char convert[DECIMAL_SIZE(value) + 3];
467 int place = 0;
468 int spadlen = 0;
469 int zpadlen = 0;
470 int caps = 0;
471
472 if (max < 0)
473 max = 0;
474 uvalue = value;
475 if (!(flags & DP_F_UNSIGNED)) {
476 if (value < 0) {
477 signvalue = '-';
478 uvalue = -value;
479 } else if (flags & DP_F_PLUS)
480 signvalue = '+';
481 else if (flags & DP_F_SPACE)
482 signvalue = ' ';
483 }
484 if (flags & DP_F_NUM) {
485 if (base == 8)
486 prefix = "0";
487 if (base == 16)
488 prefix = "0x";
489 }
490 if (flags & DP_F_UP)
491 caps = 1;
492 do {
493 convert[place++] = (caps ? "0123456789ABCDEF" :
494 "0123456789abcdef")[uvalue % (unsigned) base];
495 uvalue = (uvalue / (unsigned) base);
496 } while (uvalue && (place < (int)sizeof(convert)));
497 if (place == sizeof(convert))
498 place--;
499 convert[place] = 0;
500
501 zpadlen = max - place;
502 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) -
503 strlen(prefix);
504 if (zpadlen < 0)
505 zpadlen = 0;
506 if (spadlen < 0)
507 spadlen = 0;
508 if (flags & DP_F_ZERO) {
509 zpadlen = OSSL_MAX(zpadlen, spadlen);
510 spadlen = 0;
511 }
512 if (flags & DP_F_MINUS)
513 spadlen = -spadlen;
514
515 /* spaces */
516 while (spadlen > 0) {
517 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
518 --spadlen;
519 }
520
521 /* sign */
522 if (signvalue)
523 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
524
525 /* prefix */
526 while (*prefix) {
527 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
528 prefix++;
529 }
530
531 /* zeros */
532 if (zpadlen > 0) {
533 while (zpadlen > 0) {
534 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
535 --zpadlen;
536 }
537 }
538 /* digits */
539 while (place > 0)
540 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
541
542 /* left justified spaces */
543 while (spadlen < 0) {
544 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
545 ++spadlen;
546 }
547 return;
548} 10}
549 11
550static LDOUBLE 12int
551abs_val(LDOUBLE value) 13BIO_printf(BIO *bio, const char *format, ...)
552{
553 LDOUBLE result = value;
554 if (value < 0)
555 result = -value;
556 return result;
557}
558
559static LDOUBLE
560pow_10(int in_exp)
561{
562 LDOUBLE result = 1;
563 while (in_exp) {
564 result *= 10;
565 in_exp--;
566 }
567 return result;
568}
569
570static long
571roundv(LDOUBLE value)
572{
573 long intpart;
574 intpart = (long) value;
575 value = value - intpart;
576 if (value >= 0.5)
577 intpart++;
578 return intpart;
579}
580
581static void
582fmtfp(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
583 LDOUBLE fvalue, int min, int max, int flags)
584{
585 int signvalue = 0;
586 LDOUBLE ufvalue;
587 char iconvert[20];
588 char fconvert[20];
589 int iplace = 0;
590 int fplace = 0;
591 int padlen = 0;
592 int zpadlen = 0;
593 int caps = 0;
594 long intpart;
595 long fracpart;
596 long max10;
597
598 if (max < 0)
599 max = 6;
600 ufvalue = abs_val(fvalue);
601 if (fvalue < 0)
602 signvalue = '-';
603 else if (flags & DP_F_PLUS)
604 signvalue = '+';
605 else if (flags & DP_F_SPACE)
606 signvalue = ' ';
607
608 intpart = (long)ufvalue;
609
610 /* sorry, we only support 9 digits past the decimal because of our
611 conversion method */
612 if (max > 9)
613 max = 9;
614
615 /* we "cheat" by converting the fractional part to integer by
616 multiplying by a factor of 10 */
617 max10 = roundv(pow_10(max));
618 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
619
620 if (fracpart >= max10) {
621 intpart++;
622 fracpart -= max10;
623 }
624
625 /* convert integer part */
626 do {
627 iconvert[iplace++] = (caps ? "0123456789ABCDEF" :
628 "0123456789abcdef")[intpart % 10];
629 intpart = (intpart / 10);
630 } while (intpart && (iplace < (int)sizeof(iconvert)));
631 if (iplace == sizeof iconvert)
632 iplace--;
633 iconvert[iplace] = 0;
634
635 /* convert fractional part */
636 do {
637 fconvert[fplace++] = (caps ? "0123456789ABCDEF" :
638 "0123456789abcdef")[fracpart % 10];
639 fracpart = (fracpart / 10);
640 } while (fplace < max);
641 if (fplace == sizeof fconvert)
642 fplace--;
643 fconvert[fplace] = 0;
644
645 /* -1 for decimal point, another -1 if we are printing a sign */
646 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
647 zpadlen = max - fplace;
648 if (zpadlen < 0)
649 zpadlen = 0;
650 if (padlen < 0)
651 padlen = 0;
652 if (flags & DP_F_MINUS)
653 padlen = -padlen;
654
655 if ((flags & DP_F_ZERO) && (padlen > 0)) {
656 if (signvalue) {
657 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
658 --padlen;
659 signvalue = 0;
660 }
661 while (padlen > 0) {
662 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
663 --padlen;
664 }
665 }
666 while (padlen > 0) {
667 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
668 --padlen;
669 }
670 if (signvalue)
671 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
672
673 while (iplace > 0)
674 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
675
676 /*
677 * Decimal point. This should probably use locale to find the correct
678 * char to print out.
679 */
680 if (max > 0 || (flags & DP_F_NUM)) {
681 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
682
683 while (fplace > 0)
684 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
685 }
686 while (zpadlen > 0) {
687 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
688 --zpadlen;
689 }
690
691 while (padlen < 0) {
692 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
693 ++padlen;
694 }
695}
696
697static void
698doapr_outch(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
699 int c)
700{
701 /* If we haven't at least one buffer, someone has doe a big booboo */
702 assert(*sbuffer != NULL || buffer != NULL);
703
704 if (buffer) {
705 while (*currlen >= *maxlen) {
706 if (*buffer == NULL) {
707 if (*maxlen == 0)
708 *maxlen = 1024;
709 *buffer = malloc(*maxlen);
710 if (*currlen > 0) {
711 assert(*sbuffer != NULL);
712 memcpy(*buffer, *sbuffer, *currlen);
713 }
714 *sbuffer = NULL;
715 } else {
716 *maxlen += 1024;
717 *buffer = realloc(*buffer, *maxlen);
718 }
719 }
720 /* What to do if *buffer is NULL? */
721 assert(*sbuffer != NULL || *buffer != NULL);
722 }
723
724 if (*currlen < *maxlen) {
725 if (*sbuffer)
726 (*sbuffer)[(*currlen)++] = (char)c;
727 else
728 (*buffer)[(*currlen)++] = (char)c;
729 }
730
731 return;
732}
733
734/***************************************************************************/
735
736int BIO_printf (BIO *bio, const char *format, ...)
737{ 14{
738 va_list args; 15 va_list args;
739 int ret; 16 int ret;
740 17
741 va_start(args, format); 18 va_start(args, format);
742
743 ret = BIO_vprintf(bio, format, args); 19 ret = BIO_vprintf(bio, format, args);
744
745 va_end(args); 20 va_end(args);
746 return (ret); 21 return (ret);
747} 22}
748 23
749int BIO_vprintf (BIO *bio, const char *format, va_list args) 24int
25BIO_vprintf(BIO *bio, const char *format, va_list args)
750{ 26{
27 FILE *fp;
751 int ret; 28 int ret;
752 size_t retlen;
753 char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
754 in small - stack environments, like threads
755 or DOS programs. */
756 char *hugebufp = hugebuf;
757 size_t hugebufsize = sizeof(hugebuf);
758 char *dynbuf = NULL;
759 int ignored;
760 29
761 dynbuf = NULL; 30 fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
762 CRYPTO_push_info("doapr()"); 31 if (fp == NULL) {
763 _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, 32 ret = -1;
764 format, args); 33 goto fail;
765 if (dynbuf) {
766 ret = BIO_write(bio, dynbuf, (int)retlen);
767 free(dynbuf);
768 } else {
769 ret = BIO_write(bio, hugebuf, (int)retlen);
770 } 34 }
771 CRYPTO_pop_info(); 35 ret = vfprintf(fp, format, args);
36 fclose(fp);
37fail:
772 return (ret); 38 return (ret);
773} 39}
774 40
775/* As snprintf is not available everywhere, we provide our own implementation. 41/*
776 * This function has nothing to do with BIOs, but it's closely related 42 * BIO_snprintf and BIO_vsnprintf return -1 for overflow,
777 * to BIO_printf, and we need *some* name prefix ... 43 * due to the history of this API. Justification:
778 * (XXX the function should be renamed, but to what?) */ 44 *
45 * Traditional snprintf surfaced in 4.4BSD, and returned
46 * "number of bytes wanted". Solaris and Windows opted to
47 * return -1. A draft standard was written which returned -1.
48 * Due to the large volume of code already using the first
49 * semantics, the draft was repaired before standardization to
50 * specify "number of bytes wanted" plus "-1 for character conversion
51 * style errors". Solaris adapted to this rule, but Windows stuck
52 * with -1.
53 *
54 * Original OpenSSL comment which is full of lies:
55 *
56 * "In case of truncation, return -1 like traditional snprintf.
57 * (Current drafts for ISO/IEC 9899 say snprintf should return
58 * the number of characters that would have been written,
59 * had the buffer been large enough.)"
60 */
779int 61int
780BIO_snprintf(char *buf, size_t n, const char *format, ...) 62BIO_snprintf(char *buf, size_t n, const char *format, ...)
781{ 63{
@@ -783,27 +65,22 @@ BIO_snprintf(char *buf, size_t n, const char *format, ...)
783 int ret; 65 int ret;
784 66
785 va_start(args, format); 67 va_start(args, format);
786 68 ret = vsnprintf(buf, n, format, args);
787 ret = BIO_vsnprintf(buf, n, format, args);
788
789 va_end(args); 69 va_end(args);
70
71 if (ret >= n || ret == -1)
72 return (-1);
790 return (ret); 73 return (ret);
791} 74}
792 75
793int 76int
794BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) 77BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
795{ 78{
796 size_t retlen; 79 int ret;
797 int truncated;
798 80
799 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args); 81 ret = vsnprintf(buf, n, format, args);
800 82
801 if (truncated) 83 if (ret >= n || ret == -1)
802 /* In case of truncation, return -1 like traditional snprintf. 84 return (-1);
803 * (Current drafts for ISO/IEC 9899 say snprintf should return 85 return (ret);
804 * the number of characters that would have been written,
805 * had the buffer been large enough.) */
806 return -1;
807 else
808 return (retlen <= INT_MAX) ? (int)retlen : -1;
809} 86}
diff --git a/src/lib/libssl/src/crypto/bio/b_print.c b/src/lib/libssl/src/crypto/bio/b_print.c
index ff0089e82e..3a8fdcc821 100644
--- a/src/lib/libssl/src/crypto/bio/b_print.c
+++ b/src/lib/libssl/src/crypto/bio/b_print.c
@@ -1,781 +1,63 @@
1/* crypto/bio/b_print.c */ 1/* $OpenBSD: b_print.c,v 1.22 2014/04/21 11:18:34 deraadt Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Theo de Raadt places this file in the public domain. */
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 3
59/* disable assert() unless BIO_DEBUG has been defined */
60#ifndef BIO_DEBUG
61# ifndef NDEBUG
62# define NDEBUG
63# endif
64#endif
65
66/*
67 * Stolen from tjh's ssl/ssl_trc.c stuff.
68 */
69
70#include <stdio.h>
71#include <string.h>
72#include <ctype.h>
73#include <assert.h>
74#include <limits.h>
75#include "cryptlib.h"
76#ifndef NO_SYS_TYPES_H
77#include <sys/types.h>
78#endif
79#include <openssl/bn.h> /* To get BN_LLONG properly defined */
80#include <openssl/bio.h> 4#include <openssl/bio.h>
81 5
82#if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT) 6static int
83# ifndef HAVE_LONG_LONG 7_BIO_write(void *cookie, const char *buf, int nbytes)
84# define HAVE_LONG_LONG 1
85# endif
86#endif
87
88/***************************************************************************/
89
90/*
91 * Copyright Patrick Powell 1995
92 * This code is based on code written by Patrick Powell <papowell@astart.com>
93 * It may be used for any purpose as long as this notice remains intact
94 * on all source code distributions.
95 */
96
97/*
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)
109 * o ... (for OpenSSL)
110 */
111
112#ifdef HAVE_LONG_DOUBLE
113#define LDOUBLE long double
114#else
115#define LDOUBLE double
116#endif
117
118#ifdef HAVE_LONG_LONG
119# if defined(_WIN32) && !defined(__GNUC__)
120# define LLONG __int64
121# else
122# define LLONG long long
123# endif
124#else
125#define LLONG long
126#endif
127
128static void fmtstr(char **, char **, size_t *, size_t *, const char *,
129 int, int, int);
130static void fmtint(char **, char **, size_t *, size_t *, LLONG, int,
131 int, int, int);
132static void fmtfp(char **, char **, size_t *, size_t *, LDOUBLE,
133 int, int, int);
134static void doapr_outch(char **, char **, size_t *, size_t *, int);
135static void _dopr(char **sbuffer, char **buffer, size_t *maxlen,
136 size_t *retlen, int *truncated, const char *format, va_list args);
137
138/* format read states */
139#define DP_S_DEFAULT 0
140#define DP_S_FLAGS 1
141#define DP_S_MIN 2
142#define DP_S_DOT 3
143#define DP_S_MAX 4
144#define DP_S_MOD 5
145#define DP_S_CONV 6
146#define DP_S_DONE 7
147
148/* format flags - Bits */
149#define DP_F_MINUS (1 << 0)
150#define DP_F_PLUS (1 << 1)
151#define DP_F_SPACE (1 << 2)
152#define DP_F_NUM (1 << 3)
153#define DP_F_ZERO (1 << 4)
154#define DP_F_UP (1 << 5)
155#define DP_F_UNSIGNED (1 << 6)
156
157/* conversion flags */
158#define DP_C_SHORT 1
159#define DP_C_LONG 2
160#define DP_C_LDOUBLE 3
161#define DP_C_LLONG 4
162
163/* some handy macros */
164#define char_to_int(p) (p - '0')
165#define OSSL_MAX(p,q) ((p >= q) ? p : q)
166
167static void
168_dopr(char **sbuffer, char **buffer, size_t *maxlen, size_t *retlen,
169 int *truncated, const char *format, va_list args)
170{
171 char ch;
172 LLONG value;
173 LDOUBLE fvalue;
174 char *strvalue;
175 int min;
176 int max;
177 int state;
178 int flags;
179 int cflags;
180 size_t currlen;
181
182 state = DP_S_DEFAULT;
183 flags = currlen = cflags = min = 0;
184 max = -1;
185 ch = *format++;
186
187 while (state != DP_S_DONE) {
188 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
189 state = DP_S_DONE;
190
191 switch (state) {
192 case DP_S_DEFAULT:
193 if (ch == '%')
194 state = DP_S_FLAGS;
195 else
196 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
197 ch = *format++;
198 break;
199 case DP_S_FLAGS:
200 switch (ch) {
201 case '-':
202 flags |= DP_F_MINUS;
203 ch = *format++;
204 break;
205 case '+':
206 flags |= DP_F_PLUS;
207 ch = *format++;
208 break;
209 case ' ':
210 flags |= DP_F_SPACE;
211 ch = *format++;
212 break;
213 case '#':
214 flags |= DP_F_NUM;
215 ch = *format++;
216 break;
217 case '0':
218 flags |= DP_F_ZERO;
219 ch = *format++;
220 break;
221 default:
222 state = DP_S_MIN;
223 break;
224 }
225 break;
226 case DP_S_MIN:
227 if (isdigit((unsigned char)ch)) {
228 min = 10 * min + char_to_int(ch);
229 ch = *format++;
230 } else if (ch == '*') {
231 min = va_arg(args, int);
232 ch = *format++;
233 state = DP_S_DOT;
234 } else
235 state = DP_S_DOT;
236 break;
237 case DP_S_DOT:
238 if (ch == '.') {
239 state = DP_S_MAX;
240 ch = *format++;
241 } else
242 state = DP_S_MOD;
243 break;
244 case DP_S_MAX:
245 if (isdigit((unsigned char)ch)) {
246 if (max < 0)
247 max = 0;
248 max = 10 * max + char_to_int(ch);
249 ch = *format++;
250 } else if (ch == '*') {
251 max = va_arg(args, int);
252 ch = *format++;
253 state = DP_S_MOD;
254 } else
255 state = DP_S_MOD;
256 break;
257 case DP_S_MOD:
258 switch (ch) {
259 case 'h':
260 cflags = DP_C_SHORT;
261 ch = *format++;
262 break;
263 case 'l':
264 if (*format == 'l') {
265 cflags = DP_C_LLONG;
266 format++;
267 } else
268 cflags = DP_C_LONG;
269 ch = *format++;
270 break;
271 case 'q':
272 cflags = DP_C_LLONG;
273 ch = *format++;
274 break;
275 case 'L':
276 cflags = DP_C_LDOUBLE;
277 ch = *format++;
278 break;
279 default:
280 break;
281 }
282 state = DP_S_CONV;
283 break;
284 case DP_S_CONV:
285 switch (ch) {
286 case 'd':
287 case 'i':
288 switch (cflags) {
289 case DP_C_SHORT:
290 value = (short int)va_arg(args, int);
291 break;
292 case DP_C_LONG:
293 value = va_arg(args, long int);
294 break;
295 case DP_C_LLONG:
296 value = va_arg(args, LLONG);
297 break;
298 default:
299 value = va_arg(args, int);
300 break;
301 }
302 fmtint(sbuffer, buffer, &currlen, maxlen,
303 value, 10, min, max, flags);
304 break;
305 case 'X':
306 flags |= DP_F_UP;
307 /* FALLTHROUGH */
308 case 'x':
309 case 'o':
310 case 'u':
311 flags |= DP_F_UNSIGNED;
312 switch (cflags) {
313 case DP_C_SHORT:
314 value = (unsigned short int)va_arg(
315 args, unsigned int);
316 break;
317 case DP_C_LONG:
318 value = (LLONG)va_arg(args,
319 unsigned long int);
320 break;
321 case DP_C_LLONG:
322 value = va_arg(args, unsigned LLONG);
323 break;
324 default:
325 value = (LLONG)va_arg(args,
326 unsigned int);
327 break;
328 }
329 fmtint(sbuffer, buffer, &currlen, maxlen, value,
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);
338 fmtfp(sbuffer, buffer, &currlen, maxlen,
339 fvalue, min, max, flags);
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':
358 doapr_outch(sbuffer, buffer, &currlen, maxlen,
359 va_arg(args, int));
360 break;
361 case 's':
362 strvalue = va_arg(args, char *);
363 if (max < 0) {
364 if (buffer)
365 max = INT_MAX;
366 else
367 max = *maxlen;
368 }
369 fmtstr(sbuffer, buffer, &currlen, maxlen,
370 strvalue, flags, min, max);
371 break;
372 case 'p':
373 value = (long)va_arg(args, void *);
374 fmtint(sbuffer, buffer, &currlen, maxlen,
375 value, 16, min, max, flags|DP_F_NUM);
376 break;
377 case 'n': /* XXX */
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 *);
385 *num = (long int) currlen;
386 } else if (cflags == DP_C_LLONG) { /* XXX */
387 LLONG *num;
388 num = va_arg(args, LLONG *);
389 *num = (LLONG) currlen;
390 } else {
391 int *num;
392 num = va_arg(args, int *);
393 *num = currlen;
394 }
395 break;
396 case '%':
397 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
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;
416 }
417 }
418 *truncated = (currlen > *maxlen - 1);
419 if (*truncated)
420 currlen = *maxlen - 1;
421 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
422 *retlen = currlen - 1;
423 return;
424}
425
426static void
427fmtstr(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
428 const char *value, int flags, int min, int max)
429{
430 int padlen, strln;
431 int cnt = 0;
432
433 if (value == 0)
434 value = "<NULL>";
435 for (strln = 0; value[strln]; ++strln)
436 ;
437 padlen = min - strln;
438 if (padlen < 0)
439 padlen = 0;
440 if (flags & DP_F_MINUS)
441 padlen = -padlen;
442
443 while ((padlen > 0) && (cnt < max)) {
444 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
445 --padlen;
446 ++cnt;
447 }
448 while (*value && (cnt < max)) {
449 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
450 ++cnt;
451 }
452 while ((padlen < 0) && (cnt < max)) {
453 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
454 ++padlen;
455 ++cnt;
456 }
457}
458
459static void
460fmtint(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
461 LLONG value, int base, int min, int max, int flags)
462{ 8{
463 int signvalue = 0; 9 return BIO_write(cookie, buf, nbytes);
464 const char *prefix = "";
465 unsigned LLONG uvalue;
466 char convert[DECIMAL_SIZE(value) + 3];
467 int place = 0;
468 int spadlen = 0;
469 int zpadlen = 0;
470 int caps = 0;
471
472 if (max < 0)
473 max = 0;
474 uvalue = value;
475 if (!(flags & DP_F_UNSIGNED)) {
476 if (value < 0) {
477 signvalue = '-';
478 uvalue = -value;
479 } else if (flags & DP_F_PLUS)
480 signvalue = '+';
481 else if (flags & DP_F_SPACE)
482 signvalue = ' ';
483 }
484 if (flags & DP_F_NUM) {
485 if (base == 8)
486 prefix = "0";
487 if (base == 16)
488 prefix = "0x";
489 }
490 if (flags & DP_F_UP)
491 caps = 1;
492 do {
493 convert[place++] = (caps ? "0123456789ABCDEF" :
494 "0123456789abcdef")[uvalue % (unsigned) base];
495 uvalue = (uvalue / (unsigned) base);
496 } while (uvalue && (place < (int)sizeof(convert)));
497 if (place == sizeof(convert))
498 place--;
499 convert[place] = 0;
500
501 zpadlen = max - place;
502 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) -
503 strlen(prefix);
504 if (zpadlen < 0)
505 zpadlen = 0;
506 if (spadlen < 0)
507 spadlen = 0;
508 if (flags & DP_F_ZERO) {
509 zpadlen = OSSL_MAX(zpadlen, spadlen);
510 spadlen = 0;
511 }
512 if (flags & DP_F_MINUS)
513 spadlen = -spadlen;
514
515 /* spaces */
516 while (spadlen > 0) {
517 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
518 --spadlen;
519 }
520
521 /* sign */
522 if (signvalue)
523 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
524
525 /* prefix */
526 while (*prefix) {
527 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
528 prefix++;
529 }
530
531 /* zeros */
532 if (zpadlen > 0) {
533 while (zpadlen > 0) {
534 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
535 --zpadlen;
536 }
537 }
538 /* digits */
539 while (place > 0)
540 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
541
542 /* left justified spaces */
543 while (spadlen < 0) {
544 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
545 ++spadlen;
546 }
547 return;
548} 10}
549 11
550static LDOUBLE 12int
551abs_val(LDOUBLE value) 13BIO_printf(BIO *bio, const char *format, ...)
552{
553 LDOUBLE result = value;
554 if (value < 0)
555 result = -value;
556 return result;
557}
558
559static LDOUBLE
560pow_10(int in_exp)
561{
562 LDOUBLE result = 1;
563 while (in_exp) {
564 result *= 10;
565 in_exp--;
566 }
567 return result;
568}
569
570static long
571roundv(LDOUBLE value)
572{
573 long intpart;
574 intpart = (long) value;
575 value = value - intpart;
576 if (value >= 0.5)
577 intpart++;
578 return intpart;
579}
580
581static void
582fmtfp(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
583 LDOUBLE fvalue, int min, int max, int flags)
584{
585 int signvalue = 0;
586 LDOUBLE ufvalue;
587 char iconvert[20];
588 char fconvert[20];
589 int iplace = 0;
590 int fplace = 0;
591 int padlen = 0;
592 int zpadlen = 0;
593 int caps = 0;
594 long intpart;
595 long fracpart;
596 long max10;
597
598 if (max < 0)
599 max = 6;
600 ufvalue = abs_val(fvalue);
601 if (fvalue < 0)
602 signvalue = '-';
603 else if (flags & DP_F_PLUS)
604 signvalue = '+';
605 else if (flags & DP_F_SPACE)
606 signvalue = ' ';
607
608 intpart = (long)ufvalue;
609
610 /* sorry, we only support 9 digits past the decimal because of our
611 conversion method */
612 if (max > 9)
613 max = 9;
614
615 /* we "cheat" by converting the fractional part to integer by
616 multiplying by a factor of 10 */
617 max10 = roundv(pow_10(max));
618 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
619
620 if (fracpart >= max10) {
621 intpart++;
622 fracpart -= max10;
623 }
624
625 /* convert integer part */
626 do {
627 iconvert[iplace++] = (caps ? "0123456789ABCDEF" :
628 "0123456789abcdef")[intpart % 10];
629 intpart = (intpart / 10);
630 } while (intpart && (iplace < (int)sizeof(iconvert)));
631 if (iplace == sizeof iconvert)
632 iplace--;
633 iconvert[iplace] = 0;
634
635 /* convert fractional part */
636 do {
637 fconvert[fplace++] = (caps ? "0123456789ABCDEF" :
638 "0123456789abcdef")[fracpart % 10];
639 fracpart = (fracpart / 10);
640 } while (fplace < max);
641 if (fplace == sizeof fconvert)
642 fplace--;
643 fconvert[fplace] = 0;
644
645 /* -1 for decimal point, another -1 if we are printing a sign */
646 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
647 zpadlen = max - fplace;
648 if (zpadlen < 0)
649 zpadlen = 0;
650 if (padlen < 0)
651 padlen = 0;
652 if (flags & DP_F_MINUS)
653 padlen = -padlen;
654
655 if ((flags & DP_F_ZERO) && (padlen > 0)) {
656 if (signvalue) {
657 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
658 --padlen;
659 signvalue = 0;
660 }
661 while (padlen > 0) {
662 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
663 --padlen;
664 }
665 }
666 while (padlen > 0) {
667 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
668 --padlen;
669 }
670 if (signvalue)
671 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
672
673 while (iplace > 0)
674 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
675
676 /*
677 * Decimal point. This should probably use locale to find the correct
678 * char to print out.
679 */
680 if (max > 0 || (flags & DP_F_NUM)) {
681 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
682
683 while (fplace > 0)
684 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
685 }
686 while (zpadlen > 0) {
687 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
688 --zpadlen;
689 }
690
691 while (padlen < 0) {
692 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
693 ++padlen;
694 }
695}
696
697static void
698doapr_outch(char **sbuffer, char **buffer, size_t *currlen, size_t *maxlen,
699 int c)
700{
701 /* If we haven't at least one buffer, someone has doe a big booboo */
702 assert(*sbuffer != NULL || buffer != NULL);
703
704 if (buffer) {
705 while (*currlen >= *maxlen) {
706 if (*buffer == NULL) {
707 if (*maxlen == 0)
708 *maxlen = 1024;
709 *buffer = malloc(*maxlen);
710 if (*currlen > 0) {
711 assert(*sbuffer != NULL);
712 memcpy(*buffer, *sbuffer, *currlen);
713 }
714 *sbuffer = NULL;
715 } else {
716 *maxlen += 1024;
717 *buffer = realloc(*buffer, *maxlen);
718 }
719 }
720 /* What to do if *buffer is NULL? */
721 assert(*sbuffer != NULL || *buffer != NULL);
722 }
723
724 if (*currlen < *maxlen) {
725 if (*sbuffer)
726 (*sbuffer)[(*currlen)++] = (char)c;
727 else
728 (*buffer)[(*currlen)++] = (char)c;
729 }
730
731 return;
732}
733
734/***************************************************************************/
735
736int BIO_printf (BIO *bio, const char *format, ...)
737{ 14{
738 va_list args; 15 va_list args;
739 int ret; 16 int ret;
740 17
741 va_start(args, format); 18 va_start(args, format);
742
743 ret = BIO_vprintf(bio, format, args); 19 ret = BIO_vprintf(bio, format, args);
744
745 va_end(args); 20 va_end(args);
746 return (ret); 21 return (ret);
747} 22}
748 23
749int BIO_vprintf (BIO *bio, const char *format, va_list args) 24int
25BIO_vprintf(BIO *bio, const char *format, va_list args)
750{ 26{
27 FILE *fp;
751 int ret; 28 int ret;
752 size_t retlen;
753 char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
754 in small - stack environments, like threads
755 or DOS programs. */
756 char *hugebufp = hugebuf;
757 size_t hugebufsize = sizeof(hugebuf);
758 char *dynbuf = NULL;
759 int ignored;
760 29
761 dynbuf = NULL; 30 fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
762 CRYPTO_push_info("doapr()"); 31 if (fp == NULL) {
763 _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, 32 ret = -1;
764 format, args); 33 goto fail;
765 if (dynbuf) {
766 ret = BIO_write(bio, dynbuf, (int)retlen);
767 free(dynbuf);
768 } else {
769 ret = BIO_write(bio, hugebuf, (int)retlen);
770 } 34 }
771 CRYPTO_pop_info(); 35 ret = vfprintf(fp, format, args);
36 fclose(fp);
37fail:
772 return (ret); 38 return (ret);
773} 39}
774 40
775/* As snprintf is not available everywhere, we provide our own implementation. 41/*
776 * This function has nothing to do with BIOs, but it's closely related 42 * BIO_snprintf and BIO_vsnprintf return -1 for overflow,
777 * to BIO_printf, and we need *some* name prefix ... 43 * due to the history of this API. Justification:
778 * (XXX the function should be renamed, but to what?) */ 44 *
45 * Traditional snprintf surfaced in 4.4BSD, and returned
46 * "number of bytes wanted". Solaris and Windows opted to
47 * return -1. A draft standard was written which returned -1.
48 * Due to the large volume of code already using the first
49 * semantics, the draft was repaired before standardization to
50 * specify "number of bytes wanted" plus "-1 for character conversion
51 * style errors". Solaris adapted to this rule, but Windows stuck
52 * with -1.
53 *
54 * Original OpenSSL comment which is full of lies:
55 *
56 * "In case of truncation, return -1 like traditional snprintf.
57 * (Current drafts for ISO/IEC 9899 say snprintf should return
58 * the number of characters that would have been written,
59 * had the buffer been large enough.)"
60 */
779int 61int
780BIO_snprintf(char *buf, size_t n, const char *format, ...) 62BIO_snprintf(char *buf, size_t n, const char *format, ...)
781{ 63{
@@ -783,27 +65,22 @@ BIO_snprintf(char *buf, size_t n, const char *format, ...)
783 int ret; 65 int ret;
784 66
785 va_start(args, format); 67 va_start(args, format);
786 68 ret = vsnprintf(buf, n, format, args);
787 ret = BIO_vsnprintf(buf, n, format, args);
788
789 va_end(args); 69 va_end(args);
70
71 if (ret >= n || ret == -1)
72 return (-1);
790 return (ret); 73 return (ret);
791} 74}
792 75
793int 76int
794BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) 77BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
795{ 78{
796 size_t retlen; 79 int ret;
797 int truncated;
798 80
799 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args); 81 ret = vsnprintf(buf, n, format, args);
800 82
801 if (truncated) 83 if (ret >= n || ret == -1)
802 /* In case of truncation, return -1 like traditional snprintf. 84 return (-1);
803 * (Current drafts for ISO/IEC 9899 say snprintf should return 85 return (ret);
804 * the number of characters that would have been written,
805 * had the buffer been large enough.) */
806 return -1;
807 else
808 return (retlen <= INT_MAX) ? (int)retlen : -1;
809} 86}