blob: a8c5dc9979803f771329738ae34b4636382fd6f0 [file] [log] [blame]
The Android Open Source Project656d9c72009-03-03 19:30:25 -08001/* crypto/bio/b_print.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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/* 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>
81
82#if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
83# ifndef HAVE_LONG_LONG
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
Brian Carlstrom221304e2010-04-15 14:22:36 -0700118#ifdef HAVE_LONG_LONG
119# if defined(_WIN32) && !defined(__GNUC__)
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800120# define LLONG __int64
121# else
122# define LLONG long long
123# endif
124#else
125#define LLONG long
126#endif
127
Matt Caswell4eca81a2016-02-25 06:09:46 -0700128static int fmtstr(char **, char **, size_t *, size_t *,
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800129 const char *, int, int, int);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700130static int fmtint(char **, char **, size_t *, size_t *,
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800131 LLONG, int, int, int, int);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700132static int fmtfp(char **, char **, size_t *, size_t *,
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800133 LDOUBLE, int, int, int);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700134static int doapr_outch(char **, char **, size_t *, size_t *, int);
135static int _dopr(char **sbuffer, char **buffer,
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800136 size_t *maxlen, size_t *retlen, int *truncated,
137 const char *format, va_list args);
138
139/* format read states */
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
148
149/* format flags - Bits */
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 */
165#define char_to_int(p) (p - '0')
166#define OSSL_MAX(p,q) ((p >= q) ? p : q)
167
Matt Caswell4eca81a2016-02-25 06:09:46 -0700168static int
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800169_dopr(
170 char **sbuffer,
171 char **buffer,
172 size_t *maxlen,
173 size_t *retlen,
174 int *truncated,
175 const char *format,
176 va_list args)
177{
178 char ch;
179 LLONG value;
180 LDOUBLE fvalue;
181 char *strvalue;
182 int min;
183 int max;
184 int state;
185 int flags;
186 int cflags;
187 size_t currlen;
188
189 state = DP_S_DEFAULT;
190 flags = currlen = cflags = min = 0;
191 max = -1;
192 ch = *format++;
193
194 while (state != DP_S_DONE) {
195 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
196 state = DP_S_DONE;
197
198 switch (state) {
199 case DP_S_DEFAULT:
200 if (ch == '%')
201 state = DP_S_FLAGS;
202 else
Matt Caswell4eca81a2016-02-25 06:09:46 -0700203 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
204 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800205 ch = *format++;
206 break;
207 case DP_S_FLAGS:
208 switch (ch) {
209 case '-':
210 flags |= DP_F_MINUS;
211 ch = *format++;
212 break;
213 case '+':
214 flags |= DP_F_PLUS;
215 ch = *format++;
216 break;
217 case ' ':
218 flags |= DP_F_SPACE;
219 ch = *format++;
220 break;
221 case '#':
222 flags |= DP_F_NUM;
223 ch = *format++;
224 break;
225 case '0':
226 flags |= DP_F_ZERO;
227 ch = *format++;
228 break;
229 default:
230 state = DP_S_MIN;
231 break;
232 }
233 break;
234 case DP_S_MIN:
235 if (isdigit((unsigned char)ch)) {
236 min = 10 * min + char_to_int(ch);
237 ch = *format++;
238 } else if (ch == '*') {
239 min = va_arg(args, int);
240 ch = *format++;
241 state = DP_S_DOT;
242 } else
243 state = DP_S_DOT;
244 break;
245 case DP_S_DOT:
246 if (ch == '.') {
247 state = DP_S_MAX;
248 ch = *format++;
249 } else
250 state = DP_S_MOD;
251 break;
252 case DP_S_MAX:
253 if (isdigit((unsigned char)ch)) {
254 if (max < 0)
255 max = 0;
256 max = 10 * max + char_to_int(ch);
257 ch = *format++;
258 } else if (ch == '*') {
259 max = va_arg(args, int);
260 ch = *format++;
261 state = DP_S_MOD;
262 } else
263 state = DP_S_MOD;
264 break;
265 case DP_S_MOD:
266 switch (ch) {
267 case 'h':
268 cflags = DP_C_SHORT;
269 ch = *format++;
270 break;
271 case 'l':
272 if (*format == 'l') {
273 cflags = DP_C_LLONG;
274 format++;
275 } else
276 cflags = DP_C_LONG;
277 ch = *format++;
278 break;
279 case 'q':
280 cflags = DP_C_LLONG;
281 ch = *format++;
282 break;
283 case 'L':
284 cflags = DP_C_LDOUBLE;
285 ch = *format++;
286 break;
287 default:
288 break;
289 }
290 state = DP_S_CONV;
291 break;
292 case DP_S_CONV:
293 switch (ch) {
294 case 'd':
295 case 'i':
296 switch (cflags) {
297 case DP_C_SHORT:
298 value = (short int)va_arg(args, int);
299 break;
300 case DP_C_LONG:
301 value = va_arg(args, long int);
302 break;
303 case DP_C_LLONG:
304 value = va_arg(args, LLONG);
305 break;
306 default:
307 value = va_arg(args, int);
308 break;
309 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700310 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
311 max, flags))
312 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800313 break;
314 case 'X':
315 flags |= DP_F_UP;
316 /* FALLTHROUGH */
317 case 'x':
318 case 'o':
319 case 'u':
320 flags |= DP_F_UNSIGNED;
321 switch (cflags) {
322 case DP_C_SHORT:
323 value = (unsigned short int)va_arg(args, unsigned int);
324 break;
325 case DP_C_LONG:
326 value = (LLONG) va_arg(args,
327 unsigned long int);
328 break;
329 case DP_C_LLONG:
330 value = va_arg(args, unsigned LLONG);
331 break;
332 default:
333 value = (LLONG) va_arg(args,
334 unsigned int);
335 break;
336 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700337 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800338 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
Matt Caswell4eca81a2016-02-25 06:09:46 -0700339 min, max, flags))
340 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800341 break;
342 case 'f':
343 if (cflags == DP_C_LDOUBLE)
344 fvalue = va_arg(args, LDOUBLE);
345 else
346 fvalue = va_arg(args, double);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700347 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
348 flags))
349 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800350 break;
351 case 'E':
352 flags |= DP_F_UP;
353 case 'e':
354 if (cflags == DP_C_LDOUBLE)
355 fvalue = va_arg(args, LDOUBLE);
356 else
357 fvalue = va_arg(args, double);
358 break;
359 case 'G':
360 flags |= DP_F_UP;
361 case 'g':
362 if (cflags == DP_C_LDOUBLE)
363 fvalue = va_arg(args, LDOUBLE);
364 else
365 fvalue = va_arg(args, double);
366 break;
367 case 'c':
Matt Caswell4eca81a2016-02-25 06:09:46 -0700368 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
369 va_arg(args, int)))
370 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800371 break;
372 case 's':
373 strvalue = va_arg(args, char *);
374 if (max < 0) {
375 if (buffer)
376 max = INT_MAX;
377 else
378 max = *maxlen;
379 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700380 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
381 flags, min, max))
382 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800383 break;
384 case 'p':
385 value = (long)va_arg(args, void *);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700386 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
387 value, 16, min, max, flags | DP_F_NUM))
388 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800389 break;
390 case 'n': /* XXX */
391 if (cflags == DP_C_SHORT) {
392 short int *num;
393 num = va_arg(args, short int *);
394 *num = currlen;
395 } else if (cflags == DP_C_LONG) { /* XXX */
396 long int *num;
397 num = va_arg(args, long int *);
398 *num = (long int) currlen;
399 } else if (cflags == DP_C_LLONG) { /* XXX */
400 LLONG *num;
401 num = va_arg(args, LLONG *);
402 *num = (LLONG) currlen;
403 } else {
404 int *num;
405 num = va_arg(args, int *);
406 *num = currlen;
407 }
408 break;
409 case '%':
Matt Caswell4eca81a2016-02-25 06:09:46 -0700410 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
411 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800412 break;
413 case 'w':
414 /* not supported yet, treat as next char */
415 ch = *format++;
416 break;
417 default:
418 /* unknown, skip */
419 break;
420 }
421 ch = *format++;
422 state = DP_S_DEFAULT;
423 flags = cflags = min = 0;
424 max = -1;
425 break;
426 case DP_S_DONE:
427 break;
428 default:
429 break;
430 }
431 }
432 *truncated = (currlen > *maxlen - 1);
433 if (*truncated)
434 currlen = *maxlen - 1;
Matt Caswell4eca81a2016-02-25 06:09:46 -0700435 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
436 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800437 *retlen = currlen - 1;
Matt Caswell4eca81a2016-02-25 06:09:46 -0700438 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800439}
440
Matt Caswell4eca81a2016-02-25 06:09:46 -0700441static int
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800442fmtstr(
443 char **sbuffer,
444 char **buffer,
445 size_t *currlen,
446 size_t *maxlen,
447 const char *value,
448 int flags,
449 int min,
450 int max)
451{
Matt Caswell4eca81a2016-02-25 06:09:46 -0700452 int padlen;
453 size_t strln;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800454 int cnt = 0;
455
456 if (value == 0)
457 value = "<NULL>";
Matt Caswell4eca81a2016-02-25 06:09:46 -0700458
459 strln = strlen(value);
460 if (strln > INT_MAX)
461 strln = INT_MAX;
462
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800463 padlen = min - strln;
Matt Caswell4eca81a2016-02-25 06:09:46 -0700464 if (min < 0 || padlen < 0)
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800465 padlen = 0;
466 if (flags & DP_F_MINUS)
467 padlen = -padlen;
468
469 while ((padlen > 0) && (cnt < max)) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700470 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
471 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800472 --padlen;
473 ++cnt;
474 }
475 while (*value && (cnt < max)) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700476 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
477 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800478 ++cnt;
479 }
480 while ((padlen < 0) && (cnt < max)) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700481 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
482 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800483 ++padlen;
484 ++cnt;
485 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700486 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800487}
488
Matt Caswell4eca81a2016-02-25 06:09:46 -0700489static int
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800490fmtint(
491 char **sbuffer,
492 char **buffer,
493 size_t *currlen,
494 size_t *maxlen,
495 LLONG value,
496 int base,
497 int min,
498 int max,
499 int flags)
500{
501 int signvalue = 0;
502 const char *prefix = "";
503 unsigned LLONG uvalue;
504 char convert[DECIMAL_SIZE(value)+3];
505 int place = 0;
506 int spadlen = 0;
507 int zpadlen = 0;
508 int caps = 0;
509
510 if (max < 0)
511 max = 0;
512 uvalue = value;
513 if (!(flags & DP_F_UNSIGNED)) {
514 if (value < 0) {
515 signvalue = '-';
516 uvalue = -value;
517 } else if (flags & DP_F_PLUS)
518 signvalue = '+';
519 else if (flags & DP_F_SPACE)
520 signvalue = ' ';
521 }
522 if (flags & DP_F_NUM) {
523 if (base == 8) prefix = "0";
524 if (base == 16) prefix = "0x";
525 }
526 if (flags & DP_F_UP)
527 caps = 1;
528 do {
529 convert[place++] =
530 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
531 [uvalue % (unsigned) base];
532 uvalue = (uvalue / (unsigned) base);
533 } while (uvalue && (place < (int)sizeof(convert)));
534 if (place == sizeof(convert))
535 place--;
536 convert[place] = 0;
537
538 zpadlen = max - place;
539 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
540 if (zpadlen < 0)
541 zpadlen = 0;
542 if (spadlen < 0)
543 spadlen = 0;
544 if (flags & DP_F_ZERO) {
545 zpadlen = OSSL_MAX(zpadlen, spadlen);
546 spadlen = 0;
547 }
548 if (flags & DP_F_MINUS)
549 spadlen = -spadlen;
550
551 /* spaces */
552 while (spadlen > 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700553 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
554 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800555 --spadlen;
556 }
557
558 /* sign */
559 if (signvalue)
Matt Caswell4eca81a2016-02-25 06:09:46 -0700560 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
561 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800562
563 /* prefix */
564 while (*prefix) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700565 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
566 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800567 prefix++;
568 }
569
570 /* zeros */
571 if (zpadlen > 0) {
572 while (zpadlen > 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700573 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
574 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800575 --zpadlen;
576 }
577 }
578 /* digits */
Matt Caswell4eca81a2016-02-25 06:09:46 -0700579 while (place > 0) {
580 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
581 return 0;
582 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800583
584 /* left justified spaces */
585 while (spadlen < 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700586 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
587 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800588 ++spadlen;
589 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700590 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800591}
592
593static LDOUBLE
594abs_val(LDOUBLE value)
595{
596 LDOUBLE result = value;
597 if (value < 0)
598 result = -value;
599 return result;
600}
601
602static LDOUBLE
603pow_10(int in_exp)
604{
605 LDOUBLE result = 1;
606 while (in_exp) {
607 result *= 10;
608 in_exp--;
609 }
610 return result;
611}
612
613static long
614roundv(LDOUBLE value)
615{
616 long intpart;
617 intpart = (long) value;
618 value = value - intpart;
619 if (value >= 0.5)
620 intpart++;
621 return intpart;
622}
623
Matt Caswell4eca81a2016-02-25 06:09:46 -0700624static int
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800625fmtfp(
626 char **sbuffer,
627 char **buffer,
628 size_t *currlen,
629 size_t *maxlen,
630 LDOUBLE fvalue,
631 int min,
632 int max,
633 int flags)
634{
635 int signvalue = 0;
636 LDOUBLE ufvalue;
637 char iconvert[20];
638 char fconvert[20];
639 int iplace = 0;
640 int fplace = 0;
641 int padlen = 0;
642 int zpadlen = 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800643 long intpart;
644 long fracpart;
645 long max10;
646
647 if (max < 0)
648 max = 6;
649 ufvalue = abs_val(fvalue);
650 if (fvalue < 0)
651 signvalue = '-';
652 else if (flags & DP_F_PLUS)
653 signvalue = '+';
654 else if (flags & DP_F_SPACE)
655 signvalue = ' ';
656
657 intpart = (long)ufvalue;
658
659 /* sorry, we only support 9 digits past the decimal because of our
660 conversion method */
661 if (max > 9)
662 max = 9;
663
664 /* we "cheat" by converting the fractional part to integer by
665 multiplying by a factor of 10 */
666 max10 = roundv(pow_10(max));
667 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
668
669 if (fracpart >= max10) {
670 intpart++;
671 fracpart -= max10;
672 }
673
674 /* convert integer part */
675 do {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700676 iconvert[iplace++] = "0123456789"[intpart % 10];
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800677 intpart = (intpart / 10);
678 } while (intpart && (iplace < (int)sizeof(iconvert)));
679 if (iplace == sizeof iconvert)
680 iplace--;
681 iconvert[iplace] = 0;
682
683 /* convert fractional part */
684 do {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700685 fconvert[fplace++] = "0123456789"[fracpart % 10];
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800686 fracpart = (fracpart / 10);
687 } while (fplace < max);
688 if (fplace == sizeof fconvert)
689 fplace--;
690 fconvert[fplace] = 0;
691
692 /* -1 for decimal point, another -1 if we are printing a sign */
693 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
694 zpadlen = max - fplace;
695 if (zpadlen < 0)
696 zpadlen = 0;
697 if (padlen < 0)
698 padlen = 0;
699 if (flags & DP_F_MINUS)
700 padlen = -padlen;
701
702 if ((flags & DP_F_ZERO) && (padlen > 0)) {
703 if (signvalue) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700704 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
705 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800706 --padlen;
707 signvalue = 0;
708 }
709 while (padlen > 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700710 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
711 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800712 --padlen;
713 }
714 }
715 while (padlen > 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700716 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
717 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800718 --padlen;
719 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700720 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
721 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800722
Matt Caswell4eca81a2016-02-25 06:09:46 -0700723 while (iplace > 0) {
724 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
725 return 0;
726 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800727
728 /*
729 * Decimal point. This should probably use locale to find the correct
730 * char to print out.
731 */
732 if (max > 0 || (flags & DP_F_NUM)) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700733 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
734 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800735
Matt Caswell4eca81a2016-02-25 06:09:46 -0700736 while (fplace > 0) {
737 if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
738 fconvert[--fplace]))
739 return 0;
740 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800741 }
742 while (zpadlen > 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700743 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
744 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800745 --zpadlen;
746 }
747
748 while (padlen < 0) {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700749 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
750 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800751 ++padlen;
752 }
Matt Caswell4eca81a2016-02-25 06:09:46 -0700753 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800754}
755
Matt Caswell4eca81a2016-02-25 06:09:46 -0700756#define BUFFER_INC 1024
757
758static int
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800759doapr_outch(
760 char **sbuffer,
761 char **buffer,
762 size_t *currlen,
763 size_t *maxlen,
764 int c)
765{
766 /* If we haven't at least one buffer, someone has doe a big booboo */
767 assert(*sbuffer != NULL || buffer != NULL);
768
Matt Caswell4eca81a2016-02-25 06:09:46 -0700769 /* |currlen| must always be <= |*maxlen| */
770 assert(*currlen <= *maxlen);
771
772 if (buffer && *currlen == *maxlen) {
773 if (*maxlen > INT_MAX - BUFFER_INC)
774 return 0;
775
776 *maxlen += BUFFER_INC;
777 if (*buffer == NULL) {
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800778 *buffer = OPENSSL_malloc(*maxlen);
Matt Caswell4eca81a2016-02-25 06:09:46 -0700779 if (*buffer == NULL)
780 return 0;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800781 if (*currlen > 0) {
782 assert(*sbuffer != NULL);
783 memcpy(*buffer, *sbuffer, *currlen);
784 }
785 *sbuffer = NULL;
Matt Caswell4eca81a2016-02-25 06:09:46 -0700786 } else {
787 char *tmpbuf;
788 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
789 if (tmpbuf == NULL)
790 return 0;
791 *buffer = tmpbuf;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800792 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800793 }
794
795 if (*currlen < *maxlen) {
796 if (*sbuffer)
797 (*sbuffer)[(*currlen)++] = (char)c;
798 else
799 (*buffer)[(*currlen)++] = (char)c;
800 }
801
Matt Caswell4eca81a2016-02-25 06:09:46 -0700802 return 1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800803}
804
805/***************************************************************************/
806
807int BIO_printf (BIO *bio, const char *format, ...)
808 {
809 va_list args;
810 int ret;
811
812 va_start(args, format);
813
814 ret = BIO_vprintf(bio, format, args);
815
816 va_end(args);
817 return(ret);
818 }
819
820int BIO_vprintf (BIO *bio, const char *format, va_list args)
821 {
822 int ret;
823 size_t retlen;
824 char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
825 in small-stack environments, like threads
826 or DOS programs. */
827 char *hugebufp = hugebuf;
828 size_t hugebufsize = sizeof(hugebuf);
829 char *dynbuf = NULL;
830 int ignored;
831
832 dynbuf = NULL;
833 CRYPTO_push_info("doapr()");
Matt Caswell4eca81a2016-02-25 06:09:46 -0700834 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
835 args))
836 {
837 OPENSSL_free(dynbuf);
838 return -1;
839 }
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800840 if (dynbuf)
841 {
Matt Caswell4eca81a2016-02-25 06:09:46 -0700842 ret = BIO_write(bio, dynbuf, (int)retlen);
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800843 OPENSSL_free(dynbuf);
844 }
845 else
846 {
847 ret=BIO_write(bio, hugebuf, (int)retlen);
848 }
849 CRYPTO_pop_info();
850 return(ret);
851 }
852
853/* As snprintf is not available everywhere, we provide our own implementation.
854 * This function has nothing to do with BIOs, but it's closely related
855 * to BIO_printf, and we need *some* name prefix ...
856 * (XXX the function should be renamed, but to what?) */
857int BIO_snprintf(char *buf, size_t n, const char *format, ...)
858 {
859 va_list args;
860 int ret;
861
862 va_start(args, format);
863
864 ret = BIO_vsnprintf(buf, n, format, args);
865
866 va_end(args);
867 return(ret);
868 }
869
870int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
871 {
872 size_t retlen;
873 int truncated;
874
Matt Caswell4eca81a2016-02-25 06:09:46 -0700875 if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
876 return -1;
The Android Open Source Project656d9c72009-03-03 19:30:25 -0800877
878 if (truncated)
879 /* In case of truncation, return -1 like traditional snprintf.
880 * (Current drafts for ISO/IEC 9899 say snprintf should return
881 * the number of characters that would have been written,
882 * had the buffer been large enough.) */
883 return -1;
884 else
885 return (retlen <= INT_MAX) ? (int)retlen : -1;
886 }