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