1 | /*
|
---|
2 | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <string.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include "crypto/ctype.h"
|
---|
14 | #include "internal/numbers.h"
|
---|
15 | #include <openssl/bio.h>
|
---|
16 | #include <openssl/configuration.h>
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * Copyright Patrick Powell 1995
|
---|
20 | * This code is based on code written by Patrick Powell <[email protected]>
|
---|
21 | * It may be used for any purpose as long as this notice remains intact
|
---|
22 | * on all source code distributions.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #ifdef HAVE_LONG_DOUBLE
|
---|
26 | # define LDOUBLE long double
|
---|
27 | #else
|
---|
28 | # define LDOUBLE double
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | static int fmtstr(char **, char **, size_t *, size_t *,
|
---|
32 | const char *, int, int, int);
|
---|
33 | static int fmtint(char **, char **, size_t *, size_t *,
|
---|
34 | int64_t, int, int, int, int);
|
---|
35 | #ifndef OPENSSL_SYS_UEFI
|
---|
36 | static int fmtfp(char **, char **, size_t *, size_t *,
|
---|
37 | LDOUBLE, int, int, int, int);
|
---|
38 | #endif
|
---|
39 | static int doapr_outch(char **, char **, size_t *, size_t *, int);
|
---|
40 | static int _dopr(char **sbuffer, char **buffer,
|
---|
41 | size_t *maxlen, size_t *retlen, int *truncated,
|
---|
42 | const char *format, va_list args);
|
---|
43 |
|
---|
44 | /* format read states */
|
---|
45 | #define DP_S_DEFAULT 0
|
---|
46 | #define DP_S_FLAGS 1
|
---|
47 | #define DP_S_MIN 2
|
---|
48 | #define DP_S_DOT 3
|
---|
49 | #define DP_S_MAX 4
|
---|
50 | #define DP_S_MOD 5
|
---|
51 | #define DP_S_CONV 6
|
---|
52 | #define DP_S_DONE 7
|
---|
53 |
|
---|
54 | /* format flags - Bits */
|
---|
55 | /* left-aligned padding */
|
---|
56 | #define DP_F_MINUS (1 << 0)
|
---|
57 | /* print an explicit '+' for a value with positive sign */
|
---|
58 | #define DP_F_PLUS (1 << 1)
|
---|
59 | /* print an explicit ' ' for a value with positive sign */
|
---|
60 | #define DP_F_SPACE (1 << 2)
|
---|
61 | /* print 0/0x prefix for octal/hex and decimal point for floating point */
|
---|
62 | #define DP_F_NUM (1 << 3)
|
---|
63 | /* print leading zeroes */
|
---|
64 | #define DP_F_ZERO (1 << 4)
|
---|
65 | /* print HEX in UPPPERcase */
|
---|
66 | #define DP_F_UP (1 << 5)
|
---|
67 | /* treat value as unsigned */
|
---|
68 | #define DP_F_UNSIGNED (1 << 6)
|
---|
69 |
|
---|
70 | /* conversion flags */
|
---|
71 | #define DP_C_SHORT 1
|
---|
72 | #define DP_C_LONG 2
|
---|
73 | #define DP_C_LDOUBLE 3
|
---|
74 | #define DP_C_LLONG 4
|
---|
75 | #define DP_C_SIZE 5
|
---|
76 |
|
---|
77 | /* Floating point formats */
|
---|
78 | #define F_FORMAT 0
|
---|
79 | #define E_FORMAT 1
|
---|
80 | #define G_FORMAT 2
|
---|
81 |
|
---|
82 | /* some handy macros */
|
---|
83 | #define char_to_int(p) (p - '0')
|
---|
84 | #define OSSL_MAX(p,q) ((p >= q) ? p : q)
|
---|
85 |
|
---|
86 | static int
|
---|
87 | _dopr(char **sbuffer,
|
---|
88 | char **buffer,
|
---|
89 | size_t *maxlen,
|
---|
90 | size_t *retlen, int *truncated, const char *format, va_list args)
|
---|
91 | {
|
---|
92 | char ch;
|
---|
93 | int64_t value;
|
---|
94 | #ifndef OPENSSL_SYS_UEFI
|
---|
95 | LDOUBLE fvalue;
|
---|
96 | #endif
|
---|
97 | char *strvalue;
|
---|
98 | int min;
|
---|
99 | int max;
|
---|
100 | int state;
|
---|
101 | int flags;
|
---|
102 | int cflags;
|
---|
103 | size_t currlen;
|
---|
104 |
|
---|
105 | state = DP_S_DEFAULT;
|
---|
106 | flags = currlen = cflags = min = 0;
|
---|
107 | max = -1;
|
---|
108 | ch = *format++;
|
---|
109 |
|
---|
110 | while (state != DP_S_DONE) {
|
---|
111 | if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
|
---|
112 | state = DP_S_DONE;
|
---|
113 |
|
---|
114 | switch (state) {
|
---|
115 | case DP_S_DEFAULT:
|
---|
116 | if (ch == '%')
|
---|
117 | state = DP_S_FLAGS;
|
---|
118 | else
|
---|
119 | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
|
---|
120 | return 0;
|
---|
121 | ch = *format++;
|
---|
122 | break;
|
---|
123 | case DP_S_FLAGS:
|
---|
124 | switch (ch) {
|
---|
125 | case '-':
|
---|
126 | flags |= DP_F_MINUS;
|
---|
127 | ch = *format++;
|
---|
128 | break;
|
---|
129 | case '+':
|
---|
130 | flags |= DP_F_PLUS;
|
---|
131 | ch = *format++;
|
---|
132 | break;
|
---|
133 | case ' ':
|
---|
134 | flags |= DP_F_SPACE;
|
---|
135 | ch = *format++;
|
---|
136 | break;
|
---|
137 | case '#':
|
---|
138 | flags |= DP_F_NUM;
|
---|
139 | ch = *format++;
|
---|
140 | break;
|
---|
141 | case '0':
|
---|
142 | flags |= DP_F_ZERO;
|
---|
143 | ch = *format++;
|
---|
144 | break;
|
---|
145 | default:
|
---|
146 | state = DP_S_MIN;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | break;
|
---|
150 | case DP_S_MIN:
|
---|
151 | if (ossl_isdigit(ch)) {
|
---|
152 | min = 10 * min + char_to_int(ch);
|
---|
153 | ch = *format++;
|
---|
154 | } else if (ch == '*') {
|
---|
155 | min = va_arg(args, int);
|
---|
156 | ch = *format++;
|
---|
157 | state = DP_S_DOT;
|
---|
158 | } else
|
---|
159 | state = DP_S_DOT;
|
---|
160 | break;
|
---|
161 | case DP_S_DOT:
|
---|
162 | if (ch == '.') {
|
---|
163 | state = DP_S_MAX;
|
---|
164 | ch = *format++;
|
---|
165 | } else
|
---|
166 | state = DP_S_MOD;
|
---|
167 | break;
|
---|
168 | case DP_S_MAX:
|
---|
169 | if (ossl_isdigit(ch)) {
|
---|
170 | if (max < 0)
|
---|
171 | max = 0;
|
---|
172 | max = 10 * max + char_to_int(ch);
|
---|
173 | ch = *format++;
|
---|
174 | } else if (ch == '*') {
|
---|
175 | max = va_arg(args, int);
|
---|
176 | ch = *format++;
|
---|
177 | state = DP_S_MOD;
|
---|
178 | } else
|
---|
179 | state = DP_S_MOD;
|
---|
180 | break;
|
---|
181 | case DP_S_MOD:
|
---|
182 | switch (ch) {
|
---|
183 | case 'h':
|
---|
184 | cflags = DP_C_SHORT;
|
---|
185 | ch = *format++;
|
---|
186 | break;
|
---|
187 | case 'l':
|
---|
188 | if (*format == 'l') {
|
---|
189 | cflags = DP_C_LLONG;
|
---|
190 | format++;
|
---|
191 | } else
|
---|
192 | cflags = DP_C_LONG;
|
---|
193 | ch = *format++;
|
---|
194 | break;
|
---|
195 | case 'q':
|
---|
196 | case 'j':
|
---|
197 | cflags = DP_C_LLONG;
|
---|
198 | ch = *format++;
|
---|
199 | break;
|
---|
200 | case 'L':
|
---|
201 | cflags = DP_C_LDOUBLE;
|
---|
202 | ch = *format++;
|
---|
203 | break;
|
---|
204 | case 'z':
|
---|
205 | cflags = DP_C_SIZE;
|
---|
206 | ch = *format++;
|
---|
207 | break;
|
---|
208 | default:
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | state = DP_S_CONV;
|
---|
212 | break;
|
---|
213 | case DP_S_CONV:
|
---|
214 | switch (ch) {
|
---|
215 | case 'd':
|
---|
216 | case 'i':
|
---|
217 | switch (cflags) {
|
---|
218 | case DP_C_SHORT:
|
---|
219 | value = (short int)va_arg(args, int);
|
---|
220 | break;
|
---|
221 | case DP_C_LONG:
|
---|
222 | value = va_arg(args, long int);
|
---|
223 | break;
|
---|
224 | case DP_C_LLONG:
|
---|
225 | value = va_arg(args, int64_t);
|
---|
226 | break;
|
---|
227 | case DP_C_SIZE:
|
---|
228 | value = va_arg(args, ossl_ssize_t);
|
---|
229 | break;
|
---|
230 | default:
|
---|
231 | value = va_arg(args, int);
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
|
---|
235 | max, flags))
|
---|
236 | return 0;
|
---|
237 | break;
|
---|
238 | case 'X':
|
---|
239 | flags |= DP_F_UP;
|
---|
240 | /* FALLTHROUGH */
|
---|
241 | case 'x':
|
---|
242 | case 'o':
|
---|
243 | case 'u':
|
---|
244 | flags |= DP_F_UNSIGNED;
|
---|
245 | switch (cflags) {
|
---|
246 | case DP_C_SHORT:
|
---|
247 | value = (unsigned short int)va_arg(args, unsigned int);
|
---|
248 | break;
|
---|
249 | case DP_C_LONG:
|
---|
250 | value = va_arg(args, unsigned long int);
|
---|
251 | break;
|
---|
252 | case DP_C_LLONG:
|
---|
253 | value = va_arg(args, uint64_t);
|
---|
254 | break;
|
---|
255 | case DP_C_SIZE:
|
---|
256 | value = va_arg(args, size_t);
|
---|
257 | break;
|
---|
258 | default:
|
---|
259 | value = va_arg(args, unsigned int);
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
|
---|
263 | ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
|
---|
264 | min, max, flags))
|
---|
265 | return 0;
|
---|
266 | break;
|
---|
267 | #ifndef OPENSSL_SYS_UEFI
|
---|
268 | case 'f':
|
---|
269 | if (cflags == DP_C_LDOUBLE)
|
---|
270 | fvalue = va_arg(args, LDOUBLE);
|
---|
271 | else
|
---|
272 | fvalue = va_arg(args, double);
|
---|
273 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
|
---|
274 | flags, F_FORMAT))
|
---|
275 | return 0;
|
---|
276 | break;
|
---|
277 | case 'E':
|
---|
278 | flags |= DP_F_UP;
|
---|
279 | /* fall through */
|
---|
280 | case 'e':
|
---|
281 | if (cflags == DP_C_LDOUBLE)
|
---|
282 | fvalue = va_arg(args, LDOUBLE);
|
---|
283 | else
|
---|
284 | fvalue = va_arg(args, double);
|
---|
285 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
|
---|
286 | flags, E_FORMAT))
|
---|
287 | return 0;
|
---|
288 | break;
|
---|
289 | case 'G':
|
---|
290 | flags |= DP_F_UP;
|
---|
291 | /* fall through */
|
---|
292 | case 'g':
|
---|
293 | if (cflags == DP_C_LDOUBLE)
|
---|
294 | fvalue = va_arg(args, LDOUBLE);
|
---|
295 | else
|
---|
296 | fvalue = va_arg(args, double);
|
---|
297 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
|
---|
298 | flags, G_FORMAT))
|
---|
299 | return 0;
|
---|
300 | break;
|
---|
301 | #else
|
---|
302 | case 'f':
|
---|
303 | case 'E':
|
---|
304 | case 'e':
|
---|
305 | case 'G':
|
---|
306 | case 'g':
|
---|
307 | /* not implemented for UEFI */
|
---|
308 | ERR_raise(ERR_LIB_BIO, ERR_R_UNSUPPORTED);
|
---|
309 | return 0;
|
---|
310 | #endif
|
---|
311 | case 'c':
|
---|
312 | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
|
---|
313 | va_arg(args, int)))
|
---|
314 | return 0;
|
---|
315 | break;
|
---|
316 | case 's':
|
---|
317 | strvalue = va_arg(args, char *);
|
---|
318 | if (max < 0) {
|
---|
319 | if (buffer)
|
---|
320 | max = INT_MAX;
|
---|
321 | else
|
---|
322 | max = *maxlen;
|
---|
323 | }
|
---|
324 | if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
|
---|
325 | flags, min, max))
|
---|
326 | return 0;
|
---|
327 | break;
|
---|
328 | case 'p':
|
---|
329 | value = (size_t)va_arg(args, void *);
|
---|
330 | if (!fmtint(sbuffer, buffer, &currlen, maxlen,
|
---|
331 | value, 16, min, max, flags | DP_F_NUM))
|
---|
332 | return 0;
|
---|
333 | break;
|
---|
334 | case 'n':
|
---|
335 | {
|
---|
336 | int *num;
|
---|
337 | num = va_arg(args, int *);
|
---|
338 | *num = currlen;
|
---|
339 | }
|
---|
340 | break;
|
---|
341 | case '%':
|
---|
342 | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
|
---|
343 | return 0;
|
---|
344 | break;
|
---|
345 | case 'w':
|
---|
346 | /* not supported yet, treat as next char */
|
---|
347 | format++;
|
---|
348 | break;
|
---|
349 | default:
|
---|
350 | /* unknown, skip */
|
---|
351 | break;
|
---|
352 | }
|
---|
353 | ch = *format++;
|
---|
354 | state = DP_S_DEFAULT;
|
---|
355 | flags = cflags = min = 0;
|
---|
356 | max = -1;
|
---|
357 | break;
|
---|
358 | case DP_S_DONE:
|
---|
359 | break;
|
---|
360 | default:
|
---|
361 | break;
|
---|
362 | }
|
---|
363 | }
|
---|
364 | /*
|
---|
365 | * We have to truncate if there is no dynamic buffer and we have filled the
|
---|
366 | * static buffer.
|
---|
367 | */
|
---|
368 | if (buffer == NULL) {
|
---|
369 | *truncated = (currlen > *maxlen - 1);
|
---|
370 | if (*truncated)
|
---|
371 | currlen = *maxlen - 1;
|
---|
372 | }
|
---|
373 | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
|
---|
374 | return 0;
|
---|
375 | *retlen = currlen - 1;
|
---|
376 | return 1;
|
---|
377 | }
|
---|
378 |
|
---|
379 | static int
|
---|
380 | fmtstr(char **sbuffer,
|
---|
381 | char **buffer,
|
---|
382 | size_t *currlen,
|
---|
383 | size_t *maxlen, const char *value, int flags, int min, int max)
|
---|
384 | {
|
---|
385 | int padlen;
|
---|
386 | size_t strln;
|
---|
387 | int cnt = 0;
|
---|
388 |
|
---|
389 | if (value == 0)
|
---|
390 | value = "<NULL>";
|
---|
391 |
|
---|
392 | strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
|
---|
393 |
|
---|
394 | padlen = min - strln;
|
---|
395 | if (min < 0 || padlen < 0)
|
---|
396 | padlen = 0;
|
---|
397 | if (max >= 0) {
|
---|
398 | /*
|
---|
399 | * Calculate the maximum output including padding.
|
---|
400 | * Make sure max doesn't overflow into negativity
|
---|
401 | */
|
---|
402 | if (max < INT_MAX - padlen)
|
---|
403 | max += padlen;
|
---|
404 | else
|
---|
405 | max = INT_MAX;
|
---|
406 | }
|
---|
407 | if (flags & DP_F_MINUS)
|
---|
408 | padlen = -padlen;
|
---|
409 |
|
---|
410 | while ((padlen > 0) && (max < 0 || cnt < max)) {
|
---|
411 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
412 | return 0;
|
---|
413 | --padlen;
|
---|
414 | ++cnt;
|
---|
415 | }
|
---|
416 | while (strln > 0 && (max < 0 || cnt < max)) {
|
---|
417 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
|
---|
418 | return 0;
|
---|
419 | --strln;
|
---|
420 | ++cnt;
|
---|
421 | }
|
---|
422 | while ((padlen < 0) && (max < 0 || cnt < max)) {
|
---|
423 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
424 | return 0;
|
---|
425 | ++padlen;
|
---|
426 | ++cnt;
|
---|
427 | }
|
---|
428 | return 1;
|
---|
429 | }
|
---|
430 |
|
---|
431 | static int
|
---|
432 | fmtint(char **sbuffer,
|
---|
433 | char **buffer,
|
---|
434 | size_t *currlen,
|
---|
435 | size_t *maxlen, int64_t value, int base, int min, int max, int flags)
|
---|
436 | {
|
---|
437 | int signvalue = 0;
|
---|
438 | const char *prefix = "";
|
---|
439 | uint64_t uvalue;
|
---|
440 | char convert[DECIMAL_SIZE(value) + 3];
|
---|
441 | int place = 0;
|
---|
442 | int spadlen = 0;
|
---|
443 | int zpadlen = 0;
|
---|
444 | int caps = 0;
|
---|
445 |
|
---|
446 | if (max < 0)
|
---|
447 | max = 0;
|
---|
448 | uvalue = value;
|
---|
449 | if (!(flags & DP_F_UNSIGNED)) {
|
---|
450 | if (value < 0) {
|
---|
451 | signvalue = '-';
|
---|
452 | uvalue = 0 - (uint64_t)value;
|
---|
453 | } else if (flags & DP_F_PLUS)
|
---|
454 | signvalue = '+';
|
---|
455 | else if (flags & DP_F_SPACE)
|
---|
456 | signvalue = ' ';
|
---|
457 | }
|
---|
458 | if (flags & DP_F_NUM) {
|
---|
459 | if (base == 8)
|
---|
460 | prefix = "0";
|
---|
461 | if (base == 16)
|
---|
462 | prefix = "0x";
|
---|
463 | }
|
---|
464 | if (flags & DP_F_UP)
|
---|
465 | caps = 1;
|
---|
466 | do {
|
---|
467 | convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
|
---|
468 | [uvalue % (unsigned)base];
|
---|
469 | uvalue = (uvalue / (unsigned)base);
|
---|
470 | } while (uvalue && (place < (int)sizeof(convert)));
|
---|
471 | if (place == sizeof(convert))
|
---|
472 | place--;
|
---|
473 | convert[place] = 0;
|
---|
474 |
|
---|
475 | zpadlen = max - place;
|
---|
476 | spadlen =
|
---|
477 | min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
|
---|
478 | if (zpadlen < 0)
|
---|
479 | zpadlen = 0;
|
---|
480 | if (spadlen < 0)
|
---|
481 | spadlen = 0;
|
---|
482 | if (flags & DP_F_ZERO) {
|
---|
483 | zpadlen = OSSL_MAX(zpadlen, spadlen);
|
---|
484 | spadlen = 0;
|
---|
485 | }
|
---|
486 | if (flags & DP_F_MINUS)
|
---|
487 | spadlen = -spadlen;
|
---|
488 |
|
---|
489 | /* spaces */
|
---|
490 | while (spadlen > 0) {
|
---|
491 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
492 | return 0;
|
---|
493 | --spadlen;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* sign */
|
---|
497 | if (signvalue)
|
---|
498 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
|
---|
499 | return 0;
|
---|
500 |
|
---|
501 | /* prefix */
|
---|
502 | while (*prefix) {
|
---|
503 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
|
---|
504 | return 0;
|
---|
505 | prefix++;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /* zeros */
|
---|
509 | if (zpadlen > 0) {
|
---|
510 | while (zpadlen > 0) {
|
---|
511 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
|
---|
512 | return 0;
|
---|
513 | --zpadlen;
|
---|
514 | }
|
---|
515 | }
|
---|
516 | /* digits */
|
---|
517 | while (place > 0) {
|
---|
518 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
|
---|
519 | return 0;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /* left justified spaces */
|
---|
523 | while (spadlen < 0) {
|
---|
524 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
525 | return 0;
|
---|
526 | ++spadlen;
|
---|
527 | }
|
---|
528 | return 1;
|
---|
529 | }
|
---|
530 |
|
---|
531 | #ifndef OPENSSL_SYS_UEFI
|
---|
532 |
|
---|
533 | static LDOUBLE abs_val(LDOUBLE value)
|
---|
534 | {
|
---|
535 | LDOUBLE result = value;
|
---|
536 | if (value < 0)
|
---|
537 | result = -value;
|
---|
538 | return result;
|
---|
539 | }
|
---|
540 |
|
---|
541 | static LDOUBLE pow_10(int in_exp)
|
---|
542 | {
|
---|
543 | LDOUBLE result = 1;
|
---|
544 | while (in_exp) {
|
---|
545 | result *= 10;
|
---|
546 | in_exp--;
|
---|
547 | }
|
---|
548 | return result;
|
---|
549 | }
|
---|
550 |
|
---|
551 | static long roundv(LDOUBLE value)
|
---|
552 | {
|
---|
553 | long intpart;
|
---|
554 | intpart = (long)value;
|
---|
555 | value = value - intpart;
|
---|
556 | if (value >= 0.5)
|
---|
557 | intpart++;
|
---|
558 | return intpart;
|
---|
559 | }
|
---|
560 |
|
---|
561 | static int
|
---|
562 | fmtfp(char **sbuffer,
|
---|
563 | char **buffer,
|
---|
564 | size_t *currlen,
|
---|
565 | size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
|
---|
566 | {
|
---|
567 | int signvalue = 0;
|
---|
568 | LDOUBLE ufvalue;
|
---|
569 | LDOUBLE tmpvalue;
|
---|
570 | char iconvert[20];
|
---|
571 | char fconvert[20];
|
---|
572 | char econvert[20];
|
---|
573 | int iplace = 0;
|
---|
574 | int fplace = 0;
|
---|
575 | int eplace = 0;
|
---|
576 | int padlen = 0;
|
---|
577 | int zpadlen = 0;
|
---|
578 | long exp = 0;
|
---|
579 | unsigned long intpart;
|
---|
580 | unsigned long fracpart;
|
---|
581 | unsigned long max10;
|
---|
582 | int realstyle;
|
---|
583 |
|
---|
584 | if (max < 0)
|
---|
585 | max = 6;
|
---|
586 |
|
---|
587 | if (fvalue < 0)
|
---|
588 | signvalue = '-';
|
---|
589 | else if (flags & DP_F_PLUS)
|
---|
590 | signvalue = '+';
|
---|
591 | else if (flags & DP_F_SPACE)
|
---|
592 | signvalue = ' ';
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
|
---|
596 | * depending on the number to be printed. Work out which one it is and use
|
---|
597 | * that from here on.
|
---|
598 | */
|
---|
599 | if (style == G_FORMAT) {
|
---|
600 | if (fvalue == 0.0) {
|
---|
601 | realstyle = F_FORMAT;
|
---|
602 | } else if (fvalue < 0.0001) {
|
---|
603 | realstyle = E_FORMAT;
|
---|
604 | } else if ((max == 0 && fvalue >= 10)
|
---|
605 | || (max > 0 && fvalue >= pow_10(max))) {
|
---|
606 | realstyle = E_FORMAT;
|
---|
607 | } else {
|
---|
608 | realstyle = F_FORMAT;
|
---|
609 | }
|
---|
610 | } else {
|
---|
611 | realstyle = style;
|
---|
612 | }
|
---|
613 |
|
---|
614 | if (style != F_FORMAT) {
|
---|
615 | tmpvalue = fvalue;
|
---|
616 | /* Calculate the exponent */
|
---|
617 | if (fvalue != 0.0) {
|
---|
618 | while (tmpvalue < 1) {
|
---|
619 | tmpvalue *= 10;
|
---|
620 | exp--;
|
---|
621 | }
|
---|
622 | while (tmpvalue > 10) {
|
---|
623 | tmpvalue /= 10;
|
---|
624 | exp++;
|
---|
625 | }
|
---|
626 | }
|
---|
627 | if (style == G_FORMAT) {
|
---|
628 | /*
|
---|
629 | * In G_FORMAT the "precision" represents significant digits. We
|
---|
630 | * always have at least 1 significant digit.
|
---|
631 | */
|
---|
632 | if (max == 0)
|
---|
633 | max = 1;
|
---|
634 | /* Now convert significant digits to decimal places */
|
---|
635 | if (realstyle == F_FORMAT) {
|
---|
636 | max -= (exp + 1);
|
---|
637 | if (max < 0) {
|
---|
638 | /*
|
---|
639 | * Should not happen. If we're in F_FORMAT then exp < max?
|
---|
640 | */
|
---|
641 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
|
---|
642 | return 0;
|
---|
643 | }
|
---|
644 | } else {
|
---|
645 | /*
|
---|
646 | * In E_FORMAT there is always one significant digit in front
|
---|
647 | * of the decimal point, so:
|
---|
648 | * significant digits == 1 + decimal places
|
---|
649 | */
|
---|
650 | max--;
|
---|
651 | }
|
---|
652 | }
|
---|
653 | if (realstyle == E_FORMAT)
|
---|
654 | fvalue = tmpvalue;
|
---|
655 | }
|
---|
656 | ufvalue = abs_val(fvalue);
|
---|
657 | /*
|
---|
658 | * By subtracting 65535 (2^16-1) we cancel the low order 15 bits
|
---|
659 | * of ULONG_MAX to avoid using imprecise floating point values.
|
---|
660 | */
|
---|
661 | if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
|
---|
662 | /* Number too big */
|
---|
663 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
|
---|
664 | return 0;
|
---|
665 | }
|
---|
666 | intpart = (unsigned long)ufvalue;
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * sorry, we only support 9 digits past the decimal because of our
|
---|
670 | * conversion method
|
---|
671 | */
|
---|
672 | if (max > 9)
|
---|
673 | max = 9;
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * we "cheat" by converting the fractional part to integer by multiplying
|
---|
677 | * by a factor of 10
|
---|
678 | */
|
---|
679 | max10 = roundv(pow_10(max));
|
---|
680 | fracpart = roundv(pow_10(max) * (ufvalue - intpart));
|
---|
681 |
|
---|
682 | if (fracpart >= max10) {
|
---|
683 | intpart++;
|
---|
684 | fracpart -= max10;
|
---|
685 | }
|
---|
686 |
|
---|
687 | /* convert integer part */
|
---|
688 | do {
|
---|
689 | iconvert[iplace++] = "0123456789"[intpart % 10];
|
---|
690 | intpart = (intpart / 10);
|
---|
691 | } while (intpart && (iplace < (int)sizeof(iconvert)));
|
---|
692 | if (iplace == sizeof(iconvert))
|
---|
693 | iplace--;
|
---|
694 | iconvert[iplace] = 0;
|
---|
695 |
|
---|
696 | /* convert fractional part */
|
---|
697 | while (fplace < max) {
|
---|
698 | if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
|
---|
699 | /* We strip trailing zeros in G_FORMAT */
|
---|
700 | max--;
|
---|
701 | fracpart = fracpart / 10;
|
---|
702 | if (fplace < max)
|
---|
703 | continue;
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | fconvert[fplace++] = "0123456789"[fracpart % 10];
|
---|
707 | fracpart = (fracpart / 10);
|
---|
708 | }
|
---|
709 |
|
---|
710 | if (fplace == sizeof(fconvert))
|
---|
711 | fplace--;
|
---|
712 | fconvert[fplace] = 0;
|
---|
713 |
|
---|
714 | /* convert exponent part */
|
---|
715 | if (realstyle == E_FORMAT) {
|
---|
716 | int tmpexp;
|
---|
717 | if (exp < 0)
|
---|
718 | tmpexp = -exp;
|
---|
719 | else
|
---|
720 | tmpexp = exp;
|
---|
721 |
|
---|
722 | do {
|
---|
723 | econvert[eplace++] = "0123456789"[tmpexp % 10];
|
---|
724 | tmpexp = (tmpexp / 10);
|
---|
725 | } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
|
---|
726 | /* Exponent is huge!! Too big to print */
|
---|
727 | if (tmpexp > 0) {
|
---|
728 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
|
---|
729 | return 0;
|
---|
730 | }
|
---|
731 | /* Add a leading 0 for single digit exponents */
|
---|
732 | if (eplace == 1)
|
---|
733 | econvert[eplace++] = '0';
|
---|
734 | }
|
---|
735 |
|
---|
736 | /*
|
---|
737 | * -1 for decimal point (if we have one, i.e. max > 0),
|
---|
738 | * another -1 if we are printing a sign
|
---|
739 | */
|
---|
740 | padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
|
---|
741 | /* Take some off for exponent prefix "+e" and exponent */
|
---|
742 | if (realstyle == E_FORMAT)
|
---|
743 | padlen -= 2 + eplace;
|
---|
744 | zpadlen = max - fplace;
|
---|
745 | if (zpadlen < 0)
|
---|
746 | zpadlen = 0;
|
---|
747 | if (padlen < 0)
|
---|
748 | padlen = 0;
|
---|
749 | if (flags & DP_F_MINUS)
|
---|
750 | padlen = -padlen;
|
---|
751 |
|
---|
752 | if ((flags & DP_F_ZERO) && (padlen > 0)) {
|
---|
753 | if (signvalue) {
|
---|
754 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
|
---|
755 | return 0;
|
---|
756 | --padlen;
|
---|
757 | signvalue = 0;
|
---|
758 | }
|
---|
759 | while (padlen > 0) {
|
---|
760 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
|
---|
761 | return 0;
|
---|
762 | --padlen;
|
---|
763 | }
|
---|
764 | }
|
---|
765 | while (padlen > 0) {
|
---|
766 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
767 | return 0;
|
---|
768 | --padlen;
|
---|
769 | }
|
---|
770 | if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
|
---|
771 | return 0;
|
---|
772 |
|
---|
773 | while (iplace > 0) {
|
---|
774 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
|
---|
775 | return 0;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Decimal point. This should probably use locale to find the correct
|
---|
780 | * char to print out.
|
---|
781 | */
|
---|
782 | if (max > 0 || (flags & DP_F_NUM)) {
|
---|
783 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
|
---|
784 | return 0;
|
---|
785 |
|
---|
786 | while (fplace > 0) {
|
---|
787 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
|
---|
788 | fconvert[--fplace]))
|
---|
789 | return 0;
|
---|
790 | }
|
---|
791 | }
|
---|
792 | while (zpadlen > 0) {
|
---|
793 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
|
---|
794 | return 0;
|
---|
795 | --zpadlen;
|
---|
796 | }
|
---|
797 | if (realstyle == E_FORMAT) {
|
---|
798 | char ech;
|
---|
799 |
|
---|
800 | if ((flags & DP_F_UP) == 0)
|
---|
801 | ech = 'e';
|
---|
802 | else
|
---|
803 | ech = 'E';
|
---|
804 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
|
---|
805 | return 0;
|
---|
806 | if (exp < 0) {
|
---|
807 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
|
---|
808 | return 0;
|
---|
809 | } else {
|
---|
810 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
|
---|
811 | return 0;
|
---|
812 | }
|
---|
813 | while (eplace > 0) {
|
---|
814 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
|
---|
815 | econvert[--eplace]))
|
---|
816 | return 0;
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | while (padlen < 0) {
|
---|
821 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
|
---|
822 | return 0;
|
---|
823 | ++padlen;
|
---|
824 | }
|
---|
825 | return 1;
|
---|
826 | }
|
---|
827 |
|
---|
828 | #endif /* OPENSSL_SYS_UEFI */
|
---|
829 |
|
---|
830 | #define BUFFER_INC 1024
|
---|
831 |
|
---|
832 | static int
|
---|
833 | doapr_outch(char **sbuffer,
|
---|
834 | char **buffer, size_t *currlen, size_t *maxlen, int c)
|
---|
835 | {
|
---|
836 | /* If we haven't at least one buffer, someone has done a big booboo */
|
---|
837 | if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
|
---|
838 | return 0;
|
---|
839 |
|
---|
840 | /* |currlen| must always be <= |*maxlen| */
|
---|
841 | if (!ossl_assert(*currlen <= *maxlen))
|
---|
842 | return 0;
|
---|
843 |
|
---|
844 | if (buffer && *currlen == *maxlen) {
|
---|
845 | if (*maxlen > INT_MAX - BUFFER_INC)
|
---|
846 | return 0;
|
---|
847 |
|
---|
848 | *maxlen += BUFFER_INC;
|
---|
849 | if (*buffer == NULL) {
|
---|
850 | if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
|
---|
851 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
|
---|
852 | return 0;
|
---|
853 | }
|
---|
854 | if (*currlen > 0) {
|
---|
855 | if (!ossl_assert(*sbuffer != NULL))
|
---|
856 | return 0;
|
---|
857 | memcpy(*buffer, *sbuffer, *currlen);
|
---|
858 | }
|
---|
859 | *sbuffer = NULL;
|
---|
860 | } else {
|
---|
861 | char *tmpbuf;
|
---|
862 |
|
---|
863 | tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
|
---|
864 | if (tmpbuf == NULL) {
|
---|
865 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
|
---|
866 | return 0;
|
---|
867 | }
|
---|
868 | *buffer = tmpbuf;
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (*currlen < *maxlen) {
|
---|
873 | if (*sbuffer)
|
---|
874 | (*sbuffer)[(*currlen)++] = (char)c;
|
---|
875 | else
|
---|
876 | (*buffer)[(*currlen)++] = (char)c;
|
---|
877 | }
|
---|
878 |
|
---|
879 | return 1;
|
---|
880 | }
|
---|
881 |
|
---|
882 | /***************************************************************************/
|
---|
883 |
|
---|
884 | int BIO_printf(BIO *bio, const char *format, ...)
|
---|
885 | {
|
---|
886 | va_list args;
|
---|
887 | int ret;
|
---|
888 |
|
---|
889 | va_start(args, format);
|
---|
890 |
|
---|
891 | ret = BIO_vprintf(bio, format, args);
|
---|
892 |
|
---|
893 | va_end(args);
|
---|
894 | return ret;
|
---|
895 | }
|
---|
896 |
|
---|
897 | int BIO_vprintf(BIO *bio, const char *format, va_list args)
|
---|
898 | {
|
---|
899 | int ret;
|
---|
900 | size_t retlen;
|
---|
901 | char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
|
---|
902 | * in small-stack environments, like threads
|
---|
903 | * or DOS programs. */
|
---|
904 | char *hugebufp = hugebuf;
|
---|
905 | size_t hugebufsize = sizeof(hugebuf);
|
---|
906 | char *dynbuf = NULL;
|
---|
907 | int ignored;
|
---|
908 |
|
---|
909 | dynbuf = NULL;
|
---|
910 | if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
|
---|
911 | args)) {
|
---|
912 | OPENSSL_free(dynbuf);
|
---|
913 | return -1;
|
---|
914 | }
|
---|
915 | if (dynbuf) {
|
---|
916 | ret = BIO_write(bio, dynbuf, (int)retlen);
|
---|
917 | OPENSSL_free(dynbuf);
|
---|
918 | } else {
|
---|
919 | ret = BIO_write(bio, hugebuf, (int)retlen);
|
---|
920 | }
|
---|
921 | return ret;
|
---|
922 | }
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * As snprintf is not available everywhere, we provide our own
|
---|
926 | * implementation. This function has nothing to do with BIOs, but it's
|
---|
927 | * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
|
---|
928 | * function should be renamed, but to what?)
|
---|
929 | */
|
---|
930 | int BIO_snprintf(char *buf, size_t n, const char *format, ...)
|
---|
931 | {
|
---|
932 | va_list args;
|
---|
933 | int ret;
|
---|
934 |
|
---|
935 | va_start(args, format);
|
---|
936 |
|
---|
937 | ret = BIO_vsnprintf(buf, n, format, args);
|
---|
938 |
|
---|
939 | va_end(args);
|
---|
940 | return ret;
|
---|
941 | }
|
---|
942 |
|
---|
943 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
|
---|
944 | {
|
---|
945 | size_t retlen;
|
---|
946 | int truncated;
|
---|
947 |
|
---|
948 | if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
|
---|
949 | return -1;
|
---|
950 |
|
---|
951 | if (truncated)
|
---|
952 | /*
|
---|
953 | * In case of truncation, return -1 like traditional snprintf.
|
---|
954 | * (Current drafts for ISO/IEC 9899 say snprintf should return the
|
---|
955 | * number of characters that would have been written, had the buffer
|
---|
956 | * been large enough.)
|
---|
957 | */
|
---|
958 | return -1;
|
---|
959 | return (retlen <= INT_MAX) ? (int)retlen : -1;
|
---|
960 | }
|
---|