| 1 |
/* crypto/bio/b_print.c */ |
| 2 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* This package is an SSL implementation written |
| 6 |
* by Eric Young ([email protected]). |
| 7 |
* The implementation was written so as to conform with Netscapes SSL. |
| 8 |
* |
| 9 |
* This library is free for commercial and non-commercial use as long as |
| 10 |
* the following conditions are aheared to. The following conditions |
| 11 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 |
* included with this distribution is covered by the same copyright terms |
| 14 |
* except that the holder is Tim Hudson ([email protected]). |
| 15 |
* |
| 16 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 |
* the code are not to be removed. |
| 18 |
* If this package is used in a product, Eric Young should be given attribution |
| 19 |
* as the author of the parts of the library used. |
| 20 |
* This can be in the form of a textual message at program startup or |
| 21 |
* in documentation (online or textual) provided with the package. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. All advertising materials mentioning features or use of this software |
| 32 |
* must display the following acknowledgement: |
| 33 |
* "This product includes cryptographic software written by |
| 34 |
* Eric Young ([email protected])" |
| 35 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 36 |
* being used are not cryptographic related :-). |
| 37 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 |
* the apps directory (application code) you must include an acknowledgement: |
| 39 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 40 |
* |
| 41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 |
* SUCH DAMAGE. |
| 52 |
* |
| 53 |
* The licence and distribution terms for any publically available version or |
| 54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 |
* copied and put under another distribution licence |
| 56 |
* [including the GNU Public Licence.] |
| 57 |
*/ |
| 58 |
|
| 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 <[email protected]> |
| 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 <[email protected]> (1995) |
| 103 |
* o Brandon Long <[email protected]> (1996, for Mutt) |
| 104 |
* o Thomas Roessler <[email protected]> (1998, for Mutt) |
| 105 |
* o Michael Elkins <[email protected]> (1998, for Mutt) |
| 106 |
* o Andrew Tridgell <[email protected]> (1998, for Samba) |
| 107 |
* o Luke Mewburn <[email protected]> (1999, for LukemFTP) |
| 108 |
* o Ralf S. Engelschall <[email protected]> (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 |
|
| 128 |
static int fmtstr(char **, char **, size_t *, size_t *, |
| 129 |
const char *, int, int, int); |
| 130 |
static int fmtint(char **, char **, size_t *, size_t *, |
| 131 |
LLONG, int, int, int, int); |
| 132 |
#ifndef OPENSSL_SYS_UEFI |
| 133 |
static int fmtfp(char **, char **, size_t *, size_t *, |
| 134 |
LDOUBLE, int, int, int); |
| 135 |
#endif |
| 136 |
static int doapr_outch(char **, char **, size_t *, size_t *, int); |
| 137 |
static int EFIAPI _dopr(char **sbuffer, char **buffer, |
| 138 |
size_t *maxlen, size_t *retlen, int *truncated, |
| 139 |
const char *format, ms_va_list args); |
| 140 |
|
| 141 |
/* format read states */ |
| 142 |
#define DP_S_DEFAULT 0 |
| 143 |
#define DP_S_FLAGS 1 |
| 144 |
#define DP_S_MIN 2 |
| 145 |
#define DP_S_DOT 3 |
| 146 |
#define DP_S_MAX 4 |
| 147 |
#define DP_S_MOD 5 |
| 148 |
#define DP_S_CONV 6 |
| 149 |
#define DP_S_DONE 7 |
| 150 |
|
| 151 |
/* format flags - Bits */ |
| 152 |
#define DP_F_MINUS (1 << 0) |
| 153 |
#define DP_F_PLUS (1 << 1) |
| 154 |
#define DP_F_SPACE (1 << 2) |
| 155 |
#define DP_F_NUM (1 << 3) |
| 156 |
#define DP_F_ZERO (1 << 4) |
| 157 |
#define DP_F_UP (1 << 5) |
| 158 |
#define DP_F_UNSIGNED (1 << 6) |
| 159 |
|
| 160 |
/* conversion flags */ |
| 161 |
#define DP_C_SHORT 1 |
| 162 |
#define DP_C_LONG 2 |
| 163 |
#define DP_C_LDOUBLE 3 |
| 164 |
#define DP_C_LLONG 4 |
| 165 |
|
| 166 |
/* some handy macros */ |
| 167 |
#define char_to_int(p) (p - '0') |
| 168 |
#define OSSL_MAX(p,q) ((p >= q) ? p : q) |
| 169 |
|
| 170 |
static int EFIAPI |
| 171 |
_dopr(char **sbuffer, |
| 172 |
char **buffer, |
| 173 |
size_t *maxlen, |
| 174 |
size_t *retlen, int *truncated, const char *format, ms_va_list args) |
| 175 |
{ |
| 176 |
char ch; |
| 177 |
LLONG value; |
| 178 |
#ifndef OPENSSL_SYS_UEFI |
| 179 |
LDOUBLE fvalue; |
| 180 |
#endif |
| 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 |
| 203 |
if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) |
| 204 |
return 0; |
| 205 |
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 = ms_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 = ms_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 |
#ifndef OPENSSL_SYS_UEFI |
| 284 |
case 'L': |
| 285 |
cflags = DP_C_LDOUBLE; |
| 286 |
ch = *format++; |
| 287 |
break; |
| 288 |
#endif |
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
state = DP_S_CONV; |
| 293 |
break; |
| 294 |
case DP_S_CONV: |
| 295 |
switch (ch) { |
| 296 |
case 'd': |
| 297 |
case 'i': |
| 298 |
switch (cflags) { |
| 299 |
case DP_C_SHORT: |
| 300 |
value = (short int)ms_va_arg(args, int); |
| 301 |
break; |
| 302 |
case DP_C_LONG: |
| 303 |
value = ms_va_arg(args, long int); |
| 304 |
break; |
| 305 |
case DP_C_LLONG: |
| 306 |
value = ms_va_arg(args, LLONG); |
| 307 |
break; |
| 308 |
default: |
| 309 |
value = ms_va_arg(args, int); |
| 310 |
break; |
| 311 |
} |
| 312 |
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min, |
| 313 |
max, flags)) |
| 314 |
return 0; |
| 315 |
break; |
| 316 |
case 'X': |
| 317 |
flags |= DP_F_UP; |
| 318 |
/* FALLTHROUGH */ |
| 319 |
case 'x': |
| 320 |
case 'o': |
| 321 |
case 'u': |
| 322 |
flags |= DP_F_UNSIGNED; |
| 323 |
switch (cflags) { |
| 324 |
case DP_C_SHORT: |
| 325 |
value = (unsigned short int)ms_va_arg(args, unsigned int); |
| 326 |
break; |
| 327 |
case DP_C_LONG: |
| 328 |
value = (LLONG) ms_va_arg(args, unsigned long int); |
| 329 |
break; |
| 330 |
case DP_C_LLONG: |
| 331 |
value = ms_va_arg(args, unsigned LLONG); |
| 332 |
break; |
| 333 |
default: |
| 334 |
value = (LLONG) ms_va_arg(args, unsigned int); |
| 335 |
break; |
| 336 |
} |
| 337 |
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, |
| 338 |
ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), |
| 339 |
min, max, flags)) |
| 340 |
return 0; |
| 341 |
break; |
| 342 |
#ifndef OPENSSL_SYS_UEFI |
| 343 |
case 'f': |
| 344 |
if (cflags == DP_C_LDOUBLE) |
| 345 |
fvalue = ms_va_arg(args, LDOUBLE); |
| 346 |
else |
| 347 |
fvalue = ms_va_arg(args, double); |
| 348 |
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, |
| 349 |
flags)) |
| 350 |
return 0; |
| 351 |
break; |
| 352 |
case 'E': |
| 353 |
flags |= DP_F_UP; |
| 354 |
case 'e': |
| 355 |
if (cflags == DP_C_LDOUBLE) |
| 356 |
fvalue = ms_va_arg(args, LDOUBLE); |
| 357 |
else |
| 358 |
fvalue = ms_va_arg(args, double); |
| 359 |
break; |
| 360 |
case 'G': |
| 361 |
flags |= DP_F_UP; |
| 362 |
case 'g': |
| 363 |
if (cflags == DP_C_LDOUBLE) |
| 364 |
fvalue = ms_va_arg(args, LDOUBLE); |
| 365 |
else |
| 366 |
fvalue = ms_va_arg(args, double); |
| 367 |
break; |
| 368 |
#endif |
| 369 |
case 'c': |
| 370 |
if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, |
| 371 |
ms_va_arg(args, int))) |
| 372 |
return 0; |
| 373 |
break; |
| 374 |
case 's': |
| 375 |
strvalue = ms_va_arg(args, char *); |
| 376 |
if (max < 0) { |
| 377 |
if (buffer) |
| 378 |
max = INT_MAX; |
| 379 |
else |
| 380 |
max = *maxlen; |
| 381 |
} |
| 382 |
if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, |
| 383 |
flags, min, max)) |
| 384 |
return 0; |
| 385 |
break; |
| 386 |
case 'p': |
| 387 |
value = (long)ms_va_arg(args, void *); |
| 388 |
if (!fmtint(sbuffer, buffer, &currlen, maxlen, |
| 389 |
value, 16, min, max, flags | DP_F_NUM)) |
| 390 |
return 0; |
| 391 |
break; |
| 392 |
case 'n': /* XXX */ |
| 393 |
if (cflags == DP_C_SHORT) { |
| 394 |
short int *num; |
| 395 |
num = ms_va_arg(args, short int *); |
| 396 |
*num = currlen; |
| 397 |
} else if (cflags == DP_C_LONG) { /* XXX */ |
| 398 |
long int *num; |
| 399 |
num = ms_va_arg(args, long int *); |
| 400 |
*num = (long int)currlen; |
| 401 |
} else if (cflags == DP_C_LLONG) { /* XXX */ |
| 402 |
LLONG *num; |
| 403 |
num = ms_va_arg(args, LLONG *); |
| 404 |
*num = (LLONG) currlen; |
| 405 |
} else { |
| 406 |
int *num; |
| 407 |
num = ms_va_arg(args, int *); |
| 408 |
*num = currlen; |
| 409 |
} |
| 410 |
break; |
| 411 |
case '%': |
| 412 |
if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) |
| 413 |
return 0; |
| 414 |
break; |
| 415 |
case 'w': |
| 416 |
/* not supported yet, treat as next char */ |
| 417 |
ch = *format++; |
| 418 |
break; |
| 419 |
default: |
| 420 |
/* unknown, skip */ |
| 421 |
break; |
| 422 |
} |
| 423 |
ch = *format++; |
| 424 |
state = DP_S_DEFAULT; |
| 425 |
flags = cflags = min = 0; |
| 426 |
max = -1; |
| 427 |
break; |
| 428 |
case DP_S_DONE: |
| 429 |
break; |
| 430 |
default: |
| 431 |
break; |
| 432 |
} |
| 433 |
} |
| 434 |
/* |
| 435 |
* We have to truncate if there is no dynamic buffer and we have filled the |
| 436 |
* static buffer. |
| 437 |
*/ |
| 438 |
if (buffer == NULL) { |
| 439 |
*truncated = (currlen > *maxlen - 1); |
| 440 |
if (*truncated) |
| 441 |
currlen = *maxlen - 1; |
| 442 |
} |
| 443 |
if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) |
| 444 |
return 0; |
| 445 |
*retlen = currlen - 1; |
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
static int |
| 450 |
fmtstr(char **sbuffer, |
| 451 |
char **buffer, |
| 452 |
size_t *currlen, |
| 453 |
size_t *maxlen, const char *value, int flags, int min, int max) |
| 454 |
{ |
| 455 |
int padlen; |
| 456 |
size_t strln; |
| 457 |
int cnt = 0; |
| 458 |
|
| 459 |
if (value == 0) |
| 460 |
value = "<NULL>"; |
| 461 |
|
| 462 |
strln = strlen(value); |
| 463 |
if (strln > INT_MAX) |
| 464 |
strln = INT_MAX; |
| 465 |
|
| 466 |
padlen = min - strln; |
| 467 |
if (min < 0 || padlen < 0) |
| 468 |
padlen = 0; |
| 469 |
if (flags & DP_F_MINUS) |
| 470 |
padlen = -padlen; |
| 471 |
|
| 472 |
while ((padlen > 0) && (cnt < max)) { |
| 473 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 474 |
return 0; |
| 475 |
--padlen; |
| 476 |
++cnt; |
| 477 |
} |
| 478 |
while (*value && (cnt < max)) { |
| 479 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++)) |
| 480 |
return 0; |
| 481 |
++cnt; |
| 482 |
} |
| 483 |
while ((padlen < 0) && (cnt < max)) { |
| 484 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 485 |
return 0; |
| 486 |
++padlen; |
| 487 |
++cnt; |
| 488 |
} |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
static int |
| 493 |
fmtint(char **sbuffer, |
| 494 |
char **buffer, |
| 495 |
size_t *currlen, |
| 496 |
size_t *maxlen, LLONG value, int base, int min, int max, int flags) |
| 497 |
{ |
| 498 |
int signvalue = 0; |
| 499 |
const char *prefix = ""; |
| 500 |
unsigned LLONG uvalue; |
| 501 |
char convert[DECIMAL_SIZE(value) + 3]; |
| 502 |
int place = 0; |
| 503 |
int spadlen = 0; |
| 504 |
int zpadlen = 0; |
| 505 |
int caps = 0; |
| 506 |
|
| 507 |
if (max < 0) |
| 508 |
max = 0; |
| 509 |
uvalue = value; |
| 510 |
if (!(flags & DP_F_UNSIGNED)) { |
| 511 |
if (value < 0) { |
| 512 |
signvalue = '-'; |
| 513 |
uvalue = -value; |
| 514 |
} else if (flags & DP_F_PLUS) |
| 515 |
signvalue = '+'; |
| 516 |
else if (flags & DP_F_SPACE) |
| 517 |
signvalue = ' '; |
| 518 |
} |
| 519 |
if (flags & DP_F_NUM) { |
| 520 |
if (base == 8) |
| 521 |
prefix = "0"; |
| 522 |
if (base == 16) |
| 523 |
prefix = "0x"; |
| 524 |
} |
| 525 |
if (flags & DP_F_UP) |
| 526 |
caps = 1; |
| 527 |
do { |
| 528 |
convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef") |
| 529 |
[uvalue % (unsigned)base]; |
| 530 |
uvalue = (uvalue / (unsigned)base); |
| 531 |
} while (uvalue && (place < (int)sizeof(convert))); |
| 532 |
if (place == sizeof(convert)) |
| 533 |
place--; |
| 534 |
convert[place] = 0; |
| 535 |
|
| 536 |
zpadlen = max - place; |
| 537 |
spadlen = |
| 538 |
min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix); |
| 539 |
if (zpadlen < 0) |
| 540 |
zpadlen = 0; |
| 541 |
if (spadlen < 0) |
| 542 |
spadlen = 0; |
| 543 |
if (flags & DP_F_ZERO) { |
| 544 |
zpadlen = OSSL_MAX(zpadlen, spadlen); |
| 545 |
spadlen = 0; |
| 546 |
} |
| 547 |
if (flags & DP_F_MINUS) |
| 548 |
spadlen = -spadlen; |
| 549 |
|
| 550 |
/* spaces */ |
| 551 |
while (spadlen > 0) { |
| 552 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 553 |
return 0; |
| 554 |
--spadlen; |
| 555 |
} |
| 556 |
|
| 557 |
/* sign */ |
| 558 |
if (signvalue) |
| 559 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
| 560 |
return 0; |
| 561 |
|
| 562 |
/* prefix */ |
| 563 |
while (*prefix) { |
| 564 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix)) |
| 565 |
return 0; |
| 566 |
prefix++; |
| 567 |
} |
| 568 |
|
| 569 |
/* zeros */ |
| 570 |
if (zpadlen > 0) { |
| 571 |
while (zpadlen > 0) { |
| 572 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
| 573 |
return 0; |
| 574 |
--zpadlen; |
| 575 |
} |
| 576 |
} |
| 577 |
/* digits */ |
| 578 |
while (place > 0) { |
| 579 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place])) |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
/* left justified spaces */ |
| 584 |
while (spadlen < 0) { |
| 585 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 586 |
return 0; |
| 587 |
++spadlen; |
| 588 |
} |
| 589 |
return 1; |
| 590 |
} |
| 591 |
|
| 592 |
#ifndef OPENSSL_SYS_UEFI |
| 593 |
static LDOUBLE abs_val(LDOUBLE value) |
| 594 |
{ |
| 595 |
LDOUBLE result = value; |
| 596 |
if (value < 0) |
| 597 |
result = -value; |
| 598 |
return result; |
| 599 |
} |
| 600 |
|
| 601 |
static LDOUBLE pow_10(int in_exp) |
| 602 |
{ |
| 603 |
LDOUBLE result = 1; |
| 604 |
while (in_exp) { |
| 605 |
result *= 10; |
| 606 |
in_exp--; |
| 607 |
} |
| 608 |
return result; |
| 609 |
} |
| 610 |
|
| 611 |
static long roundv(LDOUBLE value) |
| 612 |
{ |
| 613 |
long intpart; |
| 614 |
intpart = (long)value; |
| 615 |
value = value - intpart; |
| 616 |
if (value >= 0.5) |
| 617 |
intpart++; |
| 618 |
return intpart; |
| 619 |
} |
| 620 |
|
| 621 |
static int |
| 622 |
fmtfp(char **sbuffer, |
| 623 |
char **buffer, |
| 624 |
size_t *currlen, |
| 625 |
size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags) |
| 626 |
{ |
| 627 |
int signvalue = 0; |
| 628 |
LDOUBLE ufvalue; |
| 629 |
char iconvert[20]; |
| 630 |
char fconvert[20]; |
| 631 |
int iplace = 0; |
| 632 |
int fplace = 0; |
| 633 |
int padlen = 0; |
| 634 |
int zpadlen = 0; |
| 635 |
long intpart; |
| 636 |
long fracpart; |
| 637 |
long max10; |
| 638 |
|
| 639 |
if (max < 0) |
| 640 |
max = 6; |
| 641 |
ufvalue = abs_val(fvalue); |
| 642 |
if (fvalue < 0) |
| 643 |
signvalue = '-'; |
| 644 |
else if (flags & DP_F_PLUS) |
| 645 |
signvalue = '+'; |
| 646 |
else if (flags & DP_F_SPACE) |
| 647 |
signvalue = ' '; |
| 648 |
|
| 649 |
intpart = (long)ufvalue; |
| 650 |
|
| 651 |
/* |
| 652 |
* sorry, we only support 9 digits past the decimal because of our |
| 653 |
* conversion method |
| 654 |
*/ |
| 655 |
if (max > 9) |
| 656 |
max = 9; |
| 657 |
|
| 658 |
/* |
| 659 |
* we "cheat" by converting the fractional part to integer by multiplying |
| 660 |
* by a factor of 10 |
| 661 |
*/ |
| 662 |
max10 = roundv(pow_10(max)); |
| 663 |
fracpart = roundv(pow_10(max) * (ufvalue - intpart)); |
| 664 |
|
| 665 |
if (fracpart >= max10) { |
| 666 |
intpart++; |
| 667 |
fracpart -= max10; |
| 668 |
} |
| 669 |
|
| 670 |
/* convert integer part */ |
| 671 |
do { |
| 672 |
iconvert[iplace++] = "0123456789"[intpart % 10]; |
| 673 |
intpart = (intpart / 10); |
| 674 |
} while (intpart && (iplace < (int)sizeof(iconvert))); |
| 675 |
if (iplace == sizeof iconvert) |
| 676 |
iplace--; |
| 677 |
iconvert[iplace] = 0; |
| 678 |
|
| 679 |
/* convert fractional part */ |
| 680 |
do { |
| 681 |
fconvert[fplace++] = "0123456789"[fracpart % 10]; |
| 682 |
fracpart = (fracpart / 10); |
| 683 |
} while (fplace < max); |
| 684 |
if (fplace == sizeof fconvert) |
| 685 |
fplace--; |
| 686 |
fconvert[fplace] = 0; |
| 687 |
|
| 688 |
/* -1 for decimal point, another -1 if we are printing a sign */ |
| 689 |
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); |
| 690 |
zpadlen = max - fplace; |
| 691 |
if (zpadlen < 0) |
| 692 |
zpadlen = 0; |
| 693 |
if (padlen < 0) |
| 694 |
padlen = 0; |
| 695 |
if (flags & DP_F_MINUS) |
| 696 |
padlen = -padlen; |
| 697 |
|
| 698 |
if ((flags & DP_F_ZERO) && (padlen > 0)) { |
| 699 |
if (signvalue) { |
| 700 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
| 701 |
return 0; |
| 702 |
--padlen; |
| 703 |
signvalue = 0; |
| 704 |
} |
| 705 |
while (padlen > 0) { |
| 706 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
| 707 |
return 0; |
| 708 |
--padlen; |
| 709 |
} |
| 710 |
} |
| 711 |
while (padlen > 0) { |
| 712 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 713 |
return 0; |
| 714 |
--padlen; |
| 715 |
} |
| 716 |
if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
| 717 |
return 0; |
| 718 |
|
| 719 |
while (iplace > 0) { |
| 720 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace])) |
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
/* |
| 725 |
* Decimal point. This should probably use locale to find the correct |
| 726 |
* char to print out. |
| 727 |
*/ |
| 728 |
if (max > 0 || (flags & DP_F_NUM)) { |
| 729 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.')) |
| 730 |
return 0; |
| 731 |
|
| 732 |
while (fplace > 0) { |
| 733 |
if(!doapr_outch(sbuffer, buffer, currlen, maxlen, |
| 734 |
fconvert[--fplace])) |
| 735 |
return 0; |
| 736 |
} |
| 737 |
} |
| 738 |
while (zpadlen > 0) { |
| 739 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
| 740 |
return 0; |
| 741 |
--zpadlen; |
| 742 |
} |
| 743 |
|
| 744 |
while (padlen < 0) { |
| 745 |
if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
| 746 |
return 0; |
| 747 |
++padlen; |
| 748 |
} |
| 749 |
return 1; |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
#define BUFFER_INC 1024 |
| 754 |
|
| 755 |
static int |
| 756 |
doapr_outch(char **sbuffer, |
| 757 |
char **buffer, size_t *currlen, size_t *maxlen, int c) |
| 758 |
{ |
| 759 |
/* If we haven't at least one buffer, someone has doe a big booboo */ |
| 760 |
assert(*sbuffer != NULL || buffer != NULL); |
| 761 |
|
| 762 |
/* |currlen| must always be <= |*maxlen| */ |
| 763 |
assert(*currlen <= *maxlen); |
| 764 |
|
| 765 |
if (buffer && *currlen == *maxlen) { |
| 766 |
if (*maxlen > INT_MAX - BUFFER_INC) |
| 767 |
return 0; |
| 768 |
|
| 769 |
*maxlen += BUFFER_INC; |
| 770 |
if (*buffer == NULL) { |
| 771 |
*buffer = OPENSSL_malloc(*maxlen); |
| 772 |
if (*buffer == NULL) |
| 773 |
return 0; |
| 774 |
if (*currlen > 0) { |
| 775 |
assert(*sbuffer != NULL); |
| 776 |
memcpy(*buffer, *sbuffer, *currlen); |
| 777 |
} |
| 778 |
*sbuffer = NULL; |
| 779 |
} else { |
| 780 |
char *tmpbuf; |
| 781 |
tmpbuf = OPENSSL_realloc(*buffer, *maxlen); |
| 782 |
if (tmpbuf == NULL) |
| 783 |
return 0; |
| 784 |
*buffer = tmpbuf; |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
if (*currlen < *maxlen) { |
| 789 |
if (*sbuffer) |
| 790 |
(*sbuffer)[(*currlen)++] = (char)c; |
| 791 |
else |
| 792 |
(*buffer)[(*currlen)++] = (char)c; |
| 793 |
} |
| 794 |
|
| 795 |
return 1; |
| 796 |
} |
| 797 |
|
| 798 |
/***************************************************************************/ |
| 799 |
|
| 800 |
int EFIAPI BIO_printf(BIO *bio, const char *format, ...) |
| 801 |
{ |
| 802 |
ms_va_list args; |
| 803 |
int ret; |
| 804 |
|
| 805 |
ms_va_start(args, format); |
| 806 |
|
| 807 |
ret = BIO_vprintf(bio, format, args); |
| 808 |
|
| 809 |
ms_va_end(args); |
| 810 |
return (ret); |
| 811 |
} |
| 812 |
|
| 813 |
int EFIAPI BIO_vprintf(BIO *bio, const char *format, ms_va_list args) |
| 814 |
{ |
| 815 |
int ret; |
| 816 |
size_t retlen; |
| 817 |
char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable |
| 818 |
* in small-stack environments, like threads |
| 819 |
* or DOS programs. */ |
| 820 |
char *hugebufp = hugebuf; |
| 821 |
size_t hugebufsize = sizeof(hugebuf); |
| 822 |
char *dynbuf = NULL; |
| 823 |
int ignored; |
| 824 |
|
| 825 |
dynbuf = NULL; |
| 826 |
CRYPTO_push_info("doapr()"); |
| 827 |
if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, |
| 828 |
args)) { |
| 829 |
OPENSSL_free(dynbuf); |
| 830 |
return -1; |
| 831 |
} |
| 832 |
if (dynbuf) { |
| 833 |
ret = BIO_write(bio, dynbuf, (int)retlen); |
| 834 |
OPENSSL_free(dynbuf); |
| 835 |
} else { |
| 836 |
ret = BIO_write(bio, hugebuf, (int)retlen); |
| 837 |
} |
| 838 |
CRYPTO_pop_info(); |
| 839 |
return (ret); |
| 840 |
} |
| 841 |
|
| 842 |
/* |
| 843 |
* As snprintf is not available everywhere, we provide our own |
| 844 |
* implementation. This function has nothing to do with BIOs, but it's |
| 845 |
* closely related to BIO_printf, and we need *some* name prefix ... (XXX the |
| 846 |
* function should be renamed, but to what?) |
| 847 |
*/ |
| 848 |
int EFIAPI BIO_snprintf(char *buf, size_t n, const char *format, ...) |
| 849 |
{ |
| 850 |
ms_va_list args; |
| 851 |
int ret; |
| 852 |
|
| 853 |
ms_va_start(args, format); |
| 854 |
|
| 855 |
ret = BIO_vsnprintf(buf, n, format, args); |
| 856 |
|
| 857 |
ms_va_end(args); |
| 858 |
return (ret); |
| 859 |
} |
| 860 |
|
| 861 |
int EFIAPI BIO_vsnprintf(char *buf, size_t n, const char *format, ms_va_list args) |
| 862 |
{ |
| 863 |
size_t retlen; |
| 864 |
int truncated; |
| 865 |
|
| 866 |
if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args)) |
| 867 |
return -1; |
| 868 |
|
| 869 |
if (truncated) |
| 870 |
/* |
| 871 |
* In case of truncation, return -1 like traditional snprintf. |
| 872 |
* (Current drafts for ISO/IEC 9899 say snprintf should return the |
| 873 |
* number of characters that would have been written, had the buffer |
| 874 |
* been large enough.) |
| 875 |
*/ |
| 876 |
return -1; |
| 877 |
else |
| 878 |
return (retlen <= INT_MAX) ? (int)retlen : -1; |
| 879 |
} |