/src/openssl35/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 | 337M | #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 | 1.96G | #define DP_S_DEFAULT 0 |
46 | 1.49G | #define DP_S_FLAGS 1 |
47 | 1.52G | #define DP_S_MIN 2 |
48 | 1.24G | #define DP_S_DOT 3 |
49 | 527k | #define DP_S_MAX 4 |
50 | 1.24G | #define DP_S_MOD 5 |
51 | 1.24G | #define DP_S_CONV 6 |
52 | 5.99G | #define DP_S_DONE 7 |
53 | | |
54 | | /* format flags - Bits */ |
55 | | /* left-aligned padding */ |
56 | 481M | #define DP_F_MINUS (1 << 0) |
57 | | /* print an explicit '+' for a value with positive sign */ |
58 | 42.5M | #define DP_F_PLUS (1 << 1) |
59 | | /* print an explicit ' ' for a value with positive sign */ |
60 | 42.5M | #define DP_F_SPACE (1 << 2) |
61 | | /* print 0/0x prefix for octal/hex and decimal point for floating point */ |
62 | 282M | #define DP_F_NUM (1 << 3) |
63 | | /* print leading zeroes */ |
64 | 509M | #define DP_F_ZERO (1 << 4) |
65 | | /* print HEX in UPPERcase */ |
66 | 363M | #define DP_F_UP (1 << 5) |
67 | | /* treat value as unsigned */ |
68 | 522M | #define DP_F_UNSIGNED (1 << 6) |
69 | | |
70 | | /* conversion flags */ |
71 | 0 | #define DP_C_SHORT 1 |
72 | 81.8M | #define DP_C_LONG 2 |
73 | 0 | #define DP_C_LDOUBLE 3 |
74 | 119k | #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 | 276M | #define char_to_int(p) (p - '0') |
84 | 509M | #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 | 337M | { |
92 | 337M | char ch; |
93 | 337M | int64_t value; |
94 | 337M | #ifndef OPENSSL_SYS_UEFI |
95 | 337M | LDOUBLE fvalue; |
96 | 337M | #endif |
97 | 337M | char *strvalue; |
98 | 337M | int min; |
99 | 337M | int max; |
100 | 337M | int state; |
101 | 337M | int flags; |
102 | 337M | int cflags; |
103 | 337M | size_t currlen; |
104 | | |
105 | 337M | state = DP_S_DEFAULT; |
106 | 337M | flags = currlen = cflags = min = 0; |
107 | 337M | max = -1; |
108 | 337M | ch = *format++; |
109 | | |
110 | 5.32G | while (state != DP_S_DONE) { |
111 | 4.98G | if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) |
112 | 337M | state = DP_S_DONE; |
113 | | |
114 | 4.98G | switch (state) { |
115 | 1.00G | case DP_S_DEFAULT: |
116 | 1.00G | if (ch == '%') |
117 | 623M | state = DP_S_FLAGS; |
118 | 384M | else if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) |
119 | 0 | return 0; |
120 | 1.00G | ch = *format++; |
121 | 1.00G | break; |
122 | 870M | case DP_S_FLAGS: |
123 | 870M | switch (ch) { |
124 | 19.8M | case '-': |
125 | 19.8M | flags |= DP_F_MINUS; |
126 | 19.8M | ch = *format++; |
127 | 19.8M | break; |
128 | 1.53k | case '+': |
129 | 1.53k | flags |= DP_F_PLUS; |
130 | 1.53k | ch = *format++; |
131 | 1.53k | 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 | 227M | case '0': |
141 | 227M | flags |= DP_F_ZERO; |
142 | 227M | ch = *format++; |
143 | 227M | break; |
144 | 623M | default: |
145 | 623M | state = DP_S_MIN; |
146 | 623M | break; |
147 | 870M | } |
148 | 870M | break; |
149 | 899M | case DP_S_MIN: |
150 | 899M | if (ossl_isdigit(ch)) { |
151 | 275M | min = 10 * min + char_to_int(ch); |
152 | 275M | ch = *format++; |
153 | 623M | } else if (ch == '*') { |
154 | 25.4M | min = va_arg(args, int); |
155 | 25.4M | ch = *format++; |
156 | 25.4M | state = DP_S_DOT; |
157 | 25.4M | } else |
158 | 597M | state = DP_S_DOT; |
159 | 899M | break; |
160 | 623M | case DP_S_DOT: |
161 | 623M | if (ch == '.') { |
162 | 186k | state = DP_S_MAX; |
163 | 186k | ch = *format++; |
164 | 186k | } else |
165 | 623M | state = DP_S_MOD; |
166 | 623M | break; |
167 | 341k | case DP_S_MAX: |
168 | 341k | if (ossl_isdigit(ch)) { |
169 | 154k | if (max < 0) |
170 | 77.1k | max = 0; |
171 | 154k | max = 10 * max + char_to_int(ch); |
172 | 154k | ch = *format++; |
173 | 186k | } else if (ch == '*') { |
174 | 109k | max = va_arg(args, int); |
175 | 109k | ch = *format++; |
176 | 109k | state = DP_S_MOD; |
177 | 109k | } else |
178 | 77.1k | state = DP_S_MOD; |
179 | 341k | break; |
180 | 623M | case DP_S_MOD: |
181 | 623M | switch (ch) { |
182 | 0 | case 'h': |
183 | 0 | cflags = DP_C_SHORT; |
184 | 0 | ch = *format++; |
185 | 0 | break; |
186 | 40.9M | case 'l': |
187 | 40.9M | if (*format == 'l') { |
188 | 42.2k | cflags = DP_C_LLONG; |
189 | 42.2k | format++; |
190 | 42.2k | } else |
191 | 40.9M | cflags = DP_C_LONG; |
192 | 40.9M | ch = *format++; |
193 | 40.9M | break; |
194 | 0 | case 'q': |
195 | 17.3k | case 'j': |
196 | 17.3k | cflags = DP_C_LLONG; |
197 | 17.3k | ch = *format++; |
198 | 17.3k | 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 | 582M | default: |
208 | 582M | break; |
209 | 623M | } |
210 | 623M | state = DP_S_CONV; |
211 | 623M | break; |
212 | 623M | case DP_S_CONV: |
213 | 623M | switch (ch) { |
214 | 42.5M | case 'd': |
215 | 42.5M | case 'i': |
216 | 42.5M | switch (cflags) { |
217 | 0 | case DP_C_SHORT: |
218 | 0 | value = (short int)va_arg(args, int); |
219 | 0 | break; |
220 | 28.3M | case DP_C_LONG: |
221 | 28.3M | value = va_arg(args, long int); |
222 | 28.3M | break; |
223 | 8.21k | case DP_C_LLONG: |
224 | 8.21k | value = va_arg(args, int64_t); |
225 | 8.21k | break; |
226 | 0 | case DP_C_SIZE: |
227 | 0 | value = va_arg(args, ossl_ssize_t); |
228 | 0 | break; |
229 | 14.2M | default: |
230 | 14.2M | value = va_arg(args, int); |
231 | 14.2M | break; |
232 | 42.5M | } |
233 | 42.5M | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min, |
234 | 42.5M | max, flags)) |
235 | 0 | return 0; |
236 | 42.5M | break; |
237 | 80.3M | case 'X': |
238 | 80.3M | flags |= DP_F_UP; |
239 | | /* FALLTHROUGH */ |
240 | 228M | case 'x': |
241 | 228M | case 'o': |
242 | 240M | case 'u': |
243 | 240M | flags |= DP_F_UNSIGNED; |
244 | 240M | switch (cflags) { |
245 | 0 | case DP_C_SHORT: |
246 | 0 | value = (unsigned short int)va_arg(args, unsigned int); |
247 | 0 | break; |
248 | 12.5M | case DP_C_LONG: |
249 | 12.5M | value = va_arg(args, unsigned long int); |
250 | 12.5M | break; |
251 | 51.4k | case DP_C_LLONG: |
252 | 51.4k | value = va_arg(args, uint64_t); |
253 | 51.4k | break; |
254 | 0 | case DP_C_SIZE: |
255 | 0 | value = va_arg(args, size_t); |
256 | 0 | break; |
257 | 227M | default: |
258 | 227M | value = va_arg(args, unsigned int); |
259 | 227M | break; |
260 | 240M | } |
261 | 240M | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, |
262 | 240M | ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), |
263 | 240M | min, max, flags)) |
264 | 0 | return 0; |
265 | 240M | break; |
266 | 240M | #ifndef OPENSSL_SYS_UEFI |
267 | 240M | 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 | 161M | case 'c': |
311 | 161M | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, |
312 | 161M | va_arg(args, int))) |
313 | 0 | return 0; |
314 | 161M | break; |
315 | 179M | case 's': |
316 | 179M | strvalue = va_arg(args, char *); |
317 | 179M | if (max < 0) { |
318 | 178M | if (buffer) |
319 | 156M | max = INT_MAX; |
320 | 22.8M | else |
321 | 22.8M | max = *maxlen; |
322 | 178M | } |
323 | 179M | if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, |
324 | 179M | flags, min, max)) |
325 | 0 | return 0; |
326 | 179M | break; |
327 | 179M | 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 | 623M | } |
350 | 623M | ch = *format++; |
351 | 623M | state = DP_S_DEFAULT; |
352 | 623M | flags = cflags = min = 0; |
353 | 623M | max = -1; |
354 | 623M | break; |
355 | 337M | case DP_S_DONE: |
356 | 337M | break; |
357 | 0 | default: |
358 | 0 | break; |
359 | 4.98G | } |
360 | 4.98G | } |
361 | | /* |
362 | | * We have to truncate if there is no dynamic buffer and we have filled the |
363 | | * static buffer. |
364 | | */ |
365 | 337M | if (buffer == NULL) { |
366 | 103M | *truncated = (currlen > *maxlen - 1); |
367 | 103M | if (*truncated) |
368 | 8.36k | currlen = *maxlen - 1; |
369 | 103M | } |
370 | 337M | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) |
371 | 0 | return 0; |
372 | 337M | *retlen = currlen - 1; |
373 | 337M | return 1; |
374 | 337M | } |
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 | 179M | { |
382 | 179M | int padlen; |
383 | 179M | size_t strln; |
384 | 179M | int cnt = 0; |
385 | | |
386 | 179M | if (value == 0) |
387 | 34.8k | value = "<NULL>"; |
388 | | |
389 | 179M | strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max); |
390 | | |
391 | 179M | padlen = min - strln; |
392 | 179M | if (min < 0 || padlen < 0) |
393 | 137M | padlen = 0; |
394 | 179M | if (max >= 0) { |
395 | | /* |
396 | | * Calculate the maximum output including padding. |
397 | | * Make sure max doesn't overflow into negativity |
398 | | */ |
399 | 179M | if (max < INT_MAX - padlen) |
400 | 23.0M | max += padlen; |
401 | 156M | else |
402 | 156M | max = INT_MAX; |
403 | 179M | } |
404 | 179M | if (flags & DP_F_MINUS) |
405 | 9.90M | padlen = -padlen; |
406 | | |
407 | 358M | while ((padlen > 0) && (max < 0 || cnt < max)) { |
408 | 179M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
409 | 0 | return 0; |
410 | 179M | --padlen; |
411 | 179M | ++cnt; |
412 | 179M | } |
413 | 612M | while (strln > 0 && (max < 0 || cnt < max)) { |
414 | 433M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++)) |
415 | 0 | return 0; |
416 | 433M | --strln; |
417 | 433M | ++cnt; |
418 | 433M | } |
419 | 290M | while ((padlen < 0) && (max < 0 || cnt < max)) { |
420 | 111M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
421 | 0 | return 0; |
422 | 111M | ++padlen; |
423 | 111M | ++cnt; |
424 | 111M | } |
425 | 179M | return 1; |
426 | 179M | } |
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 | 282M | { |
434 | 282M | int signvalue = 0; |
435 | 282M | const char *prefix = ""; |
436 | 282M | uint64_t uvalue; |
437 | 282M | char convert[DECIMAL_SIZE(value) + 3]; |
438 | 282M | int place = 0; |
439 | 282M | int spadlen = 0; |
440 | 282M | int zpadlen = 0; |
441 | 282M | int caps = 0; |
442 | | |
443 | 282M | if (max < 0) |
444 | 282M | max = 0; |
445 | 282M | uvalue = value; |
446 | 282M | if (!(flags & DP_F_UNSIGNED)) { |
447 | 42.5M | if (value < 0) { |
448 | 58.7k | signvalue = '-'; |
449 | 58.7k | uvalue = 0 - (uint64_t)value; |
450 | 42.5M | } else if (flags & DP_F_PLUS) |
451 | 1.44k | signvalue = '+'; |
452 | 42.5M | else if (flags & DP_F_SPACE) |
453 | 0 | signvalue = ' '; |
454 | 42.5M | } |
455 | 282M | 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 | 282M | if (flags & DP_F_UP) |
462 | 80.3M | caps = 1; |
463 | 515M | do { |
464 | 515M | convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef") |
465 | 515M | [uvalue % (unsigned)base]; |
466 | 515M | uvalue = (uvalue / (unsigned)base); |
467 | 515M | } while (uvalue && (place < (int)sizeof(convert))); |
468 | 282M | if (place == sizeof(convert)) |
469 | 0 | place--; |
470 | 282M | convert[place] = 0; |
471 | | |
472 | 282M | zpadlen = max - place; |
473 | 282M | spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix); |
474 | 282M | if (zpadlen < 0) |
475 | 282M | zpadlen = 0; |
476 | 282M | if (spadlen < 0) |
477 | 30.4M | spadlen = 0; |
478 | 282M | if (flags & DP_F_ZERO) { |
479 | 227M | zpadlen = OSSL_MAX(zpadlen, spadlen); |
480 | 227M | spadlen = 0; |
481 | 227M | } |
482 | 282M | if (flags & DP_F_MINUS) |
483 | 9.90M | spadlen = -spadlen; |
484 | | |
485 | | /* spaces */ |
486 | 325M | while (spadlen > 0) { |
487 | 42.7M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
488 | 0 | return 0; |
489 | 42.7M | --spadlen; |
490 | 42.7M | } |
491 | | |
492 | | /* sign */ |
493 | 282M | if (signvalue) |
494 | 60.2k | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
495 | 0 | return 0; |
496 | | |
497 | | /* prefix */ |
498 | 282M | 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 | 282M | if (zpadlen > 0) { |
506 | 127M | while (zpadlen > 0) { |
507 | 64.0M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
508 | 0 | return 0; |
509 | 64.0M | --zpadlen; |
510 | 64.0M | } |
511 | 63.4M | } |
512 | | /* digits */ |
513 | 798M | while (place > 0) { |
514 | 515M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place])) |
515 | 0 | return 0; |
516 | 515M | } |
517 | | |
518 | | /* left justified spaces */ |
519 | 292M | while (spadlen < 0) { |
520 | 9.41M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
521 | 0 | return 0; |
522 | 9.41M | ++spadlen; |
523 | 9.41M | } |
524 | 282M | return 1; |
525 | 282M | } |
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 | 58.3k | #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.23G | { |
837 | | /* If we haven't at least one buffer, someone has done a big booboo */ |
838 | 2.23G | if (!ossl_assert(*sbuffer != NULL || buffer != NULL)) |
839 | 0 | return 0; |
840 | | |
841 | | /* |currlen| must always be <= |*maxlen| */ |
842 | 2.23G | if (!ossl_assert(*currlen <= *maxlen)) |
843 | 0 | return 0; |
844 | | |
845 | 2.23G | if (buffer && *currlen == *maxlen) { |
846 | 29.1k | if (*maxlen > INT_MAX - BUFFER_INC) |
847 | 0 | return 0; |
848 | | |
849 | 29.1k | *maxlen += BUFFER_INC; |
850 | 29.1k | if (*buffer == NULL) { |
851 | 666 | if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) |
852 | 0 | return 0; |
853 | 666 | if (*currlen > 0) { |
854 | 666 | if (!ossl_assert(*sbuffer != NULL)) |
855 | 0 | return 0; |
856 | 666 | memcpy(*buffer, *sbuffer, *currlen); |
857 | 666 | } |
858 | 666 | *sbuffer = NULL; |
859 | 28.4k | } else { |
860 | 28.4k | char *tmpbuf; |
861 | | |
862 | 28.4k | tmpbuf = OPENSSL_realloc(*buffer, *maxlen); |
863 | 28.4k | if (tmpbuf == NULL) |
864 | 0 | return 0; |
865 | 28.4k | *buffer = tmpbuf; |
866 | 28.4k | } |
867 | 29.1k | } |
868 | | |
869 | 2.23G | if (*currlen < *maxlen) { |
870 | 2.23G | if (*sbuffer) |
871 | 2.20G | (*sbuffer)[(*currlen)++] = (char)c; |
872 | 29.3M | else |
873 | 29.3M | (*buffer)[(*currlen)++] = (char)c; |
874 | 2.23G | } |
875 | | |
876 | 2.23G | return 1; |
877 | 2.23G | } |
878 | | |
879 | | /***************************************************************************/ |
880 | | |
881 | | int BIO_printf(BIO *bio, const char *format, ...) |
882 | 344M | { |
883 | 344M | va_list args; |
884 | 344M | int ret; |
885 | | |
886 | 344M | va_start(args, format); |
887 | | |
888 | 344M | ret = BIO_vprintf(bio, format, args); |
889 | | |
890 | 344M | va_end(args); |
891 | 344M | return ret; |
892 | 344M | } |
893 | | |
894 | | int BIO_vprintf(BIO *bio, const char *format, va_list args) |
895 | 275M | { |
896 | 275M | int ret; |
897 | 275M | size_t retlen; |
898 | 275M | char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable |
899 | | * in small-stack environments, like threads |
900 | | * or DOS programs. */ |
901 | 275M | char *hugebufp = hugebuf; |
902 | 275M | size_t hugebufsize = sizeof(hugebuf); |
903 | 275M | char *dynbuf = NULL; |
904 | 275M | int ignored; |
905 | | |
906 | 275M | dynbuf = NULL; |
907 | 275M | if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, |
908 | 275M | args)) { |
909 | 0 | OPENSSL_free(dynbuf); |
910 | 0 | return -1; |
911 | 0 | } |
912 | 275M | if (dynbuf) { |
913 | 720 | ret = BIO_write(bio, dynbuf, (int)retlen); |
914 | 720 | OPENSSL_free(dynbuf); |
915 | 275M | } else { |
916 | 275M | ret = BIO_write(bio, hugebuf, (int)retlen); |
917 | 275M | } |
918 | 275M | return ret; |
919 | 275M | } |
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 | 121M | { |
929 | 121M | va_list args; |
930 | 121M | int ret; |
931 | | |
932 | 121M | va_start(args, format); |
933 | | |
934 | 121M | ret = BIO_vsnprintf(buf, n, format, args); |
935 | | |
936 | 121M | va_end(args); |
937 | 121M | return ret; |
938 | 121M | } |
939 | | |
940 | | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) |
941 | 124M | { |
942 | 124M | size_t retlen; |
943 | 124M | int truncated; |
944 | | |
945 | 124M | if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args)) |
946 | 0 | return -1; |
947 | | |
948 | 124M | 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 | 8.83k | return -1; |
956 | 124M | return (retlen <= INT_MAX) ? (int)retlen : -1; |
957 | 124M | } |