/src/openssl31/crypto/bio/bio_print.c
Line | Count | Source (jump to first uncovered line) |
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 <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 | 376M | # 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.14G | #define DP_S_DEFAULT 0 |
46 | 1.67G | #define DP_S_FLAGS 1 |
47 | 1.70G | #define DP_S_MIN 2 |
48 | 1.37G | #define DP_S_DOT 3 |
49 | 350k | #define DP_S_MAX 4 |
50 | 1.37G | #define DP_S_MOD 5 |
51 | 1.37G | #define DP_S_CONV 6 |
52 | 6.65G | #define DP_S_DONE 7 |
53 | | |
54 | | /* format flags - Bits */ |
55 | | /* left-aligned padding */ |
56 | 525M | #define DP_F_MINUS (1 << 0) |
57 | | /* print an explicit '+' for a value with positive sign */ |
58 | 36.3M | #define DP_F_PLUS (1 << 1) |
59 | | /* print an explicit ' ' for a value with positive sign */ |
60 | 36.3M | #define DP_F_SPACE (1 << 2) |
61 | | /* print 0/0x prefix for octal/hex and decimal point for floating point */ |
62 | 333M | #define DP_F_NUM (1 << 3) |
63 | | /* print leading zeroes */ |
64 | 616M | #define DP_F_ZERO (1 << 4) |
65 | | /* print HEX in UPPPERcase */ |
66 | 419M | #define DP_F_UP (1 << 5) |
67 | | /* treat value as unsigned */ |
68 | 630M | #define DP_F_UNSIGNED (1 << 6) |
69 | | |
70 | | /* conversion flags */ |
71 | 0 | #define DP_C_SHORT 1 |
72 | 72.2M | #define DP_C_LONG 2 |
73 | 0 | #define DP_C_LDOUBLE 3 |
74 | 109k | #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 | 325M | #define char_to_int(p) (p - '0') |
84 | 616M | #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 | 376M | { |
92 | 376M | char ch; |
93 | 376M | int64_t value; |
94 | 376M | #ifndef OPENSSL_SYS_UEFI |
95 | 376M | LDOUBLE fvalue; |
96 | 376M | #endif |
97 | 376M | char *strvalue; |
98 | 376M | int min; |
99 | 376M | int max; |
100 | 376M | int state; |
101 | 376M | int flags; |
102 | 376M | int cflags; |
103 | 376M | size_t currlen; |
104 | | |
105 | 376M | state = DP_S_DEFAULT; |
106 | 376M | flags = currlen = cflags = min = 0; |
107 | 376M | max = -1; |
108 | 376M | ch = *format++; |
109 | | |
110 | 5.89G | while (state != DP_S_DONE) { |
111 | 5.52G | if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) |
112 | 376M | state = DP_S_DONE; |
113 | | |
114 | 5.52G | switch (state) { |
115 | 1.07G | case DP_S_DEFAULT: |
116 | 1.07G | if (ch == '%') |
117 | 688M | state = DP_S_FLAGS; |
118 | 387M | else |
119 | 387M | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) |
120 | 0 | return 0; |
121 | 1.07G | ch = *format++; |
122 | 1.07G | break; |
123 | 988M | case DP_S_FLAGS: |
124 | 988M | switch (ch) { |
125 | 16.8M | case '-': |
126 | 16.8M | flags |= DP_F_MINUS; |
127 | 16.8M | ch = *format++; |
128 | 16.8M | break; |
129 | 141 | case '+': |
130 | 141 | flags |= DP_F_PLUS; |
131 | 141 | ch = *format++; |
132 | 141 | break; |
133 | 0 | case ' ': |
134 | 0 | flags |= DP_F_SPACE; |
135 | 0 | ch = *format++; |
136 | 0 | break; |
137 | 0 | case '#': |
138 | 0 | flags |= DP_F_NUM; |
139 | 0 | ch = *format++; |
140 | 0 | break; |
141 | 283M | case '0': |
142 | 283M | flags |= DP_F_ZERO; |
143 | 283M | ch = *format++; |
144 | 283M | break; |
145 | 688M | default: |
146 | 688M | state = DP_S_MIN; |
147 | 688M | break; |
148 | 988M | } |
149 | 988M | break; |
150 | 1.01G | case DP_S_MIN: |
151 | 1.01G | if (ossl_isdigit(ch)) { |
152 | 325M | min = 10 * min + char_to_int(ch); |
153 | 325M | ch = *format++; |
154 | 688M | } else if (ch == '*') { |
155 | 24.1M | min = va_arg(args, int); |
156 | 24.1M | ch = *format++; |
157 | 24.1M | state = DP_S_DOT; |
158 | 24.1M | } else |
159 | 664M | state = DP_S_DOT; |
160 | 1.01G | break; |
161 | 688M | case DP_S_DOT: |
162 | 688M | if (ch == '.') { |
163 | 123k | state = DP_S_MAX; |
164 | 123k | ch = *format++; |
165 | 123k | } else |
166 | 688M | state = DP_S_MOD; |
167 | 688M | break; |
168 | 226k | case DP_S_MAX: |
169 | 226k | if (ossl_isdigit(ch)) { |
170 | 102k | if (max < 0) |
171 | 51.1k | max = 0; |
172 | 102k | max = 10 * max + char_to_int(ch); |
173 | 102k | ch = *format++; |
174 | 123k | } else if (ch == '*') { |
175 | 72.7k | max = va_arg(args, int); |
176 | 72.7k | ch = *format++; |
177 | 72.7k | state = DP_S_MOD; |
178 | 72.7k | } else |
179 | 51.1k | state = DP_S_MOD; |
180 | 226k | break; |
181 | 688M | case DP_S_MOD: |
182 | 688M | switch (ch) { |
183 | 0 | case 'h': |
184 | 0 | cflags = DP_C_SHORT; |
185 | 0 | ch = *format++; |
186 | 0 | break; |
187 | 36.1M | case 'l': |
188 | 36.1M | if (*format == 'l') { |
189 | 34.4k | cflags = DP_C_LLONG; |
190 | 34.4k | format++; |
191 | 34.4k | } else |
192 | 36.1M | cflags = DP_C_LONG; |
193 | 36.1M | ch = *format++; |
194 | 36.1M | break; |
195 | 0 | case 'q': |
196 | 20.2k | case 'j': |
197 | 20.2k | cflags = DP_C_LLONG; |
198 | 20.2k | ch = *format++; |
199 | 20.2k | break; |
200 | 0 | case 'L': |
201 | 0 | cflags = DP_C_LDOUBLE; |
202 | 0 | ch = *format++; |
203 | 0 | break; |
204 | 0 | case 'z': |
205 | 0 | cflags = DP_C_SIZE; |
206 | 0 | ch = *format++; |
207 | 0 | break; |
208 | 652M | default: |
209 | 652M | break; |
210 | 688M | } |
211 | 688M | state = DP_S_CONV; |
212 | 688M | break; |
213 | 688M | case DP_S_CONV: |
214 | 688M | switch (ch) { |
215 | 36.3M | case 'd': |
216 | 36.3M | case 'i': |
217 | 36.3M | switch (cflags) { |
218 | 0 | case DP_C_SHORT: |
219 | 0 | value = (short int)va_arg(args, int); |
220 | 0 | break; |
221 | 24.2M | case DP_C_LONG: |
222 | 24.2M | value = va_arg(args, long int); |
223 | 24.2M | break; |
224 | 9.76k | case DP_C_LLONG: |
225 | 9.76k | value = va_arg(args, int64_t); |
226 | 9.76k | break; |
227 | 0 | case DP_C_SIZE: |
228 | 0 | value = va_arg(args, ossl_ssize_t); |
229 | 0 | break; |
230 | 12.1M | default: |
231 | 12.1M | value = va_arg(args, int); |
232 | 12.1M | break; |
233 | 36.3M | } |
234 | 36.3M | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min, |
235 | 36.3M | max, flags)) |
236 | 0 | return 0; |
237 | 36.3M | break; |
238 | 86.3M | case 'X': |
239 | 86.3M | flags |= DP_F_UP; |
240 | | /* FALLTHROUGH */ |
241 | 285M | case 'x': |
242 | 285M | case 'o': |
243 | 296M | case 'u': |
244 | 296M | flags |= DP_F_UNSIGNED; |
245 | 296M | switch (cflags) { |
246 | 0 | case DP_C_SHORT: |
247 | 0 | value = (unsigned short int)va_arg(args, unsigned int); |
248 | 0 | break; |
249 | 11.9M | case DP_C_LONG: |
250 | 11.9M | value = va_arg(args, unsigned long int); |
251 | 11.9M | break; |
252 | 44.9k | case DP_C_LLONG: |
253 | 44.9k | value = va_arg(args, uint64_t); |
254 | 44.9k | break; |
255 | 0 | case DP_C_SIZE: |
256 | 0 | value = va_arg(args, size_t); |
257 | 0 | break; |
258 | 284M | default: |
259 | 284M | value = va_arg(args, unsigned int); |
260 | 284M | break; |
261 | 296M | } |
262 | 296M | if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, |
263 | 296M | ch == 'o' ? 8 : (ch == 'u' ? 10 : 16), |
264 | 296M | min, max, flags)) |
265 | 0 | return 0; |
266 | 296M | break; |
267 | 296M | #ifndef OPENSSL_SYS_UEFI |
268 | 296M | case 'f': |
269 | 0 | if (cflags == DP_C_LDOUBLE) |
270 | 0 | fvalue = va_arg(args, LDOUBLE); |
271 | 0 | else |
272 | 0 | fvalue = va_arg(args, double); |
273 | 0 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, |
274 | 0 | flags, F_FORMAT)) |
275 | 0 | return 0; |
276 | 0 | break; |
277 | 0 | case 'E': |
278 | 0 | flags |= DP_F_UP; |
279 | | /* fall through */ |
280 | 0 | case 'e': |
281 | 0 | if (cflags == DP_C_LDOUBLE) |
282 | 0 | fvalue = va_arg(args, LDOUBLE); |
283 | 0 | else |
284 | 0 | fvalue = va_arg(args, double); |
285 | 0 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, |
286 | 0 | flags, E_FORMAT)) |
287 | 0 | return 0; |
288 | 0 | break; |
289 | 0 | case 'G': |
290 | 0 | flags |= DP_F_UP; |
291 | | /* fall through */ |
292 | 0 | case 'g': |
293 | 0 | if (cflags == DP_C_LDOUBLE) |
294 | 0 | fvalue = va_arg(args, LDOUBLE); |
295 | 0 | else |
296 | 0 | fvalue = va_arg(args, double); |
297 | 0 | if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max, |
298 | 0 | flags, G_FORMAT)) |
299 | 0 | return 0; |
300 | 0 | 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 | 179M | case 'c': |
312 | 179M | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, |
313 | 179M | va_arg(args, int))) |
314 | 0 | return 0; |
315 | 179M | break; |
316 | 179M | case 's': |
317 | 175M | strvalue = va_arg(args, char *); |
318 | 175M | if (max < 0) { |
319 | 175M | if (buffer) |
320 | 152M | max = INT_MAX; |
321 | 22.9M | else |
322 | 22.9M | max = *maxlen; |
323 | 175M | } |
324 | 175M | if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue, |
325 | 175M | flags, min, max)) |
326 | 0 | return 0; |
327 | 175M | break; |
328 | 175M | case 'p': |
329 | 0 | value = (size_t)va_arg(args, void *); |
330 | 0 | if (!fmtint(sbuffer, buffer, &currlen, maxlen, |
331 | 0 | value, 16, min, max, flags | DP_F_NUM)) |
332 | 0 | return 0; |
333 | 0 | break; |
334 | 0 | case 'n': |
335 | 0 | { |
336 | 0 | int *num; |
337 | 0 | num = va_arg(args, int *); |
338 | 0 | *num = currlen; |
339 | 0 | } |
340 | 0 | break; |
341 | 0 | case '%': |
342 | 0 | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch)) |
343 | 0 | return 0; |
344 | 0 | break; |
345 | 0 | case 'w': |
346 | | /* not supported yet, treat as next char */ |
347 | 0 | format++; |
348 | 0 | break; |
349 | 0 | default: |
350 | | /* unknown, skip */ |
351 | 0 | break; |
352 | 688M | } |
353 | 688M | ch = *format++; |
354 | 688M | state = DP_S_DEFAULT; |
355 | 688M | flags = cflags = min = 0; |
356 | 688M | max = -1; |
357 | 688M | break; |
358 | 376M | case DP_S_DONE: |
359 | 376M | break; |
360 | 0 | default: |
361 | 0 | break; |
362 | 5.52G | } |
363 | 5.52G | } |
364 | | /* |
365 | | * We have to truncate if there is no dynamic buffer and we have filled the |
366 | | * static buffer. |
367 | | */ |
368 | 376M | if (buffer == NULL) { |
369 | 139M | *truncated = (currlen > *maxlen - 1); |
370 | 139M | if (*truncated) |
371 | 10.8k | currlen = *maxlen - 1; |
372 | 139M | } |
373 | 376M | if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0')) |
374 | 0 | return 0; |
375 | 376M | *retlen = currlen - 1; |
376 | 376M | return 1; |
377 | 376M | } |
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 | 175M | { |
385 | 175M | int padlen; |
386 | 175M | size_t strln; |
387 | 175M | int cnt = 0; |
388 | | |
389 | 175M | if (value == 0) |
390 | 19.2k | value = "<NULL>"; |
391 | | |
392 | 175M | strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max); |
393 | | |
394 | 175M | padlen = min - strln; |
395 | 175M | if (min < 0 || padlen < 0) |
396 | 137M | padlen = 0; |
397 | 175M | if (max >= 0) { |
398 | | /* |
399 | | * Calculate the maximum output including padding. |
400 | | * Make sure max doesn't overflow into negativity |
401 | | */ |
402 | 175M | if (max < INT_MAX - padlen) |
403 | 23.0M | max += padlen; |
404 | 152M | else |
405 | 152M | max = INT_MAX; |
406 | 175M | } |
407 | 175M | if (flags & DP_F_MINUS) |
408 | 8.42M | padlen = -padlen; |
409 | | |
410 | 337M | while ((padlen > 0) && (max < 0 || cnt < max)) { |
411 | 161M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
412 | 0 | return 0; |
413 | 161M | --padlen; |
414 | 161M | ++cnt; |
415 | 161M | } |
416 | 605M | while (strln > 0 && (max < 0 || cnt < max)) { |
417 | 429M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++)) |
418 | 0 | return 0; |
419 | 429M | --strln; |
420 | 429M | ++cnt; |
421 | 429M | } |
422 | 266M | while ((padlen < 0) && (max < 0 || cnt < max)) { |
423 | 91.1M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
424 | 0 | return 0; |
425 | 91.1M | ++padlen; |
426 | 91.1M | ++cnt; |
427 | 91.1M | } |
428 | 175M | return 1; |
429 | 175M | } |
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 | 333M | { |
437 | 333M | int signvalue = 0; |
438 | 333M | const char *prefix = ""; |
439 | 333M | uint64_t uvalue; |
440 | 333M | char convert[DECIMAL_SIZE(value) + 3]; |
441 | 333M | int place = 0; |
442 | 333M | int spadlen = 0; |
443 | 333M | int zpadlen = 0; |
444 | 333M | int caps = 0; |
445 | | |
446 | 333M | if (max < 0) |
447 | 333M | max = 0; |
448 | 333M | uvalue = value; |
449 | 333M | if (!(flags & DP_F_UNSIGNED)) { |
450 | 36.3M | if (value < 0) { |
451 | 31.7k | signvalue = '-'; |
452 | 31.7k | uvalue = 0 - (uint64_t)value; |
453 | 36.3M | } else if (flags & DP_F_PLUS) |
454 | 112 | signvalue = '+'; |
455 | 36.3M | else if (flags & DP_F_SPACE) |
456 | 0 | signvalue = ' '; |
457 | 36.3M | } |
458 | 333M | if (flags & DP_F_NUM) { |
459 | 0 | if (base == 8) |
460 | 0 | prefix = "0"; |
461 | 0 | if (base == 16) |
462 | 0 | prefix = "0x"; |
463 | 0 | } |
464 | 333M | if (flags & DP_F_UP) |
465 | 86.3M | caps = 1; |
466 | 589M | do { |
467 | 589M | convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef") |
468 | 589M | [uvalue % (unsigned)base]; |
469 | 589M | uvalue = (uvalue / (unsigned)base); |
470 | 589M | } while (uvalue && (place < (int)sizeof(convert))); |
471 | 333M | if (place == sizeof(convert)) |
472 | 0 | place--; |
473 | 333M | convert[place] = 0; |
474 | | |
475 | 333M | zpadlen = max - place; |
476 | 333M | spadlen = |
477 | 333M | min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix); |
478 | 333M | if (zpadlen < 0) |
479 | 333M | zpadlen = 0; |
480 | 333M | if (spadlen < 0) |
481 | 30.6M | spadlen = 0; |
482 | 333M | if (flags & DP_F_ZERO) { |
483 | 283M | zpadlen = OSSL_MAX(zpadlen, spadlen); |
484 | 283M | spadlen = 0; |
485 | 283M | } |
486 | 333M | if (flags & DP_F_MINUS) |
487 | 8.42M | spadlen = -spadlen; |
488 | | |
489 | | /* spaces */ |
490 | 370M | while (spadlen > 0) { |
491 | 37.0M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
492 | 0 | return 0; |
493 | 37.0M | --spadlen; |
494 | 37.0M | } |
495 | | |
496 | | /* sign */ |
497 | 333M | if (signvalue) |
498 | 31.8k | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
499 | 0 | return 0; |
500 | | |
501 | | /* prefix */ |
502 | 333M | while (*prefix) { |
503 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix)) |
504 | 0 | return 0; |
505 | 0 | prefix++; |
506 | 0 | } |
507 | | |
508 | | /* zeros */ |
509 | 333M | if (zpadlen > 0) { |
510 | 177M | while (zpadlen > 0) { |
511 | 88.9M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
512 | 0 | return 0; |
513 | 88.9M | --zpadlen; |
514 | 88.9M | } |
515 | 88.3M | } |
516 | | /* digits */ |
517 | 922M | while (place > 0) { |
518 | 589M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place])) |
519 | 0 | return 0; |
520 | 589M | } |
521 | | |
522 | | /* left justified spaces */ |
523 | 341M | while (spadlen < 0) { |
524 | 8.12M | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
525 | 0 | return 0; |
526 | 8.12M | ++spadlen; |
527 | 8.12M | } |
528 | 333M | return 1; |
529 | 333M | } |
530 | | |
531 | | #ifndef OPENSSL_SYS_UEFI |
532 | | |
533 | | static LDOUBLE abs_val(LDOUBLE value) |
534 | 0 | { |
535 | 0 | LDOUBLE result = value; |
536 | 0 | if (value < 0) |
537 | 0 | result = -value; |
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 | | |
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 | 0 | if (style == G_FORMAT) { |
600 | 0 | if (fvalue == 0.0) { |
601 | 0 | realstyle = F_FORMAT; |
602 | 0 | } else if (fvalue < 0.0001) { |
603 | 0 | realstyle = E_FORMAT; |
604 | 0 | } else if ((max == 0 && fvalue >= 10) |
605 | 0 | || (max > 0 && fvalue >= pow_10(max))) { |
606 | 0 | realstyle = E_FORMAT; |
607 | 0 | } else { |
608 | 0 | realstyle = F_FORMAT; |
609 | 0 | } |
610 | 0 | } else { |
611 | 0 | realstyle = style; |
612 | 0 | } |
613 | |
|
614 | 0 | if (style != F_FORMAT) { |
615 | 0 | tmpvalue = fvalue; |
616 | | /* Calculate the exponent */ |
617 | 0 | if (fvalue != 0.0) { |
618 | 0 | while (tmpvalue < 1) { |
619 | 0 | tmpvalue *= 10; |
620 | 0 | exp--; |
621 | 0 | } |
622 | 0 | while (tmpvalue > 10) { |
623 | 0 | tmpvalue /= 10; |
624 | 0 | exp++; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | 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 | 0 | if (max == 0) |
633 | 0 | max = 1; |
634 | | /* Now convert significant digits to decimal places */ |
635 | 0 | if (realstyle == F_FORMAT) { |
636 | 0 | max -= (exp + 1); |
637 | 0 | if (max < 0) { |
638 | | /* |
639 | | * Should not happen. If we're in F_FORMAT then exp < max? |
640 | | */ |
641 | 0 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0'); |
642 | 0 | return 0; |
643 | 0 | } |
644 | 0 | } 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 | 0 | max--; |
651 | 0 | } |
652 | 0 | } |
653 | 0 | if (realstyle == E_FORMAT) |
654 | 0 | fvalue = tmpvalue; |
655 | 0 | } |
656 | 0 | 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 | 0 | if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) { |
662 | | /* Number too big */ |
663 | 0 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0'); |
664 | 0 | return 0; |
665 | 0 | } |
666 | 0 | intpart = (unsigned long)ufvalue; |
667 | | |
668 | | /* |
669 | | * sorry, we only support 9 digits past the decimal because of our |
670 | | * conversion method |
671 | | */ |
672 | 0 | if (max > 9) |
673 | 0 | max = 9; |
674 | | |
675 | | /* |
676 | | * we "cheat" by converting the fractional part to integer by multiplying |
677 | | * by a factor of 10 |
678 | | */ |
679 | 0 | max10 = roundv(pow_10(max)); |
680 | 0 | fracpart = roundv(pow_10(max) * (ufvalue - intpart)); |
681 | |
|
682 | 0 | if (fracpart >= max10) { |
683 | 0 | intpart++; |
684 | 0 | fracpart -= max10; |
685 | 0 | } |
686 | | |
687 | | /* convert integer part */ |
688 | 0 | do { |
689 | 0 | iconvert[iplace++] = "0123456789"[intpart % 10]; |
690 | 0 | intpart = (intpart / 10); |
691 | 0 | } while (intpart && (iplace < (int)sizeof(iconvert))); |
692 | 0 | if (iplace == sizeof(iconvert)) |
693 | 0 | iplace--; |
694 | 0 | iconvert[iplace] = 0; |
695 | | |
696 | | /* convert fractional part */ |
697 | 0 | while (fplace < max) { |
698 | 0 | if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) { |
699 | | /* We strip trailing zeros in G_FORMAT */ |
700 | 0 | max--; |
701 | 0 | fracpart = fracpart / 10; |
702 | 0 | if (fplace < max) |
703 | 0 | continue; |
704 | 0 | break; |
705 | 0 | } |
706 | 0 | fconvert[fplace++] = "0123456789"[fracpart % 10]; |
707 | 0 | fracpart = (fracpart / 10); |
708 | 0 | } |
709 | |
|
710 | 0 | if (fplace == sizeof(fconvert)) |
711 | 0 | fplace--; |
712 | 0 | fconvert[fplace] = 0; |
713 | | |
714 | | /* convert exponent part */ |
715 | 0 | if (realstyle == E_FORMAT) { |
716 | 0 | int tmpexp; |
717 | 0 | if (exp < 0) |
718 | 0 | tmpexp = -exp; |
719 | 0 | else |
720 | 0 | tmpexp = exp; |
721 | |
|
722 | 0 | do { |
723 | 0 | econvert[eplace++] = "0123456789"[tmpexp % 10]; |
724 | 0 | tmpexp = (tmpexp / 10); |
725 | 0 | } while (tmpexp > 0 && eplace < (int)sizeof(econvert)); |
726 | | /* Exponent is huge!! Too big to print */ |
727 | 0 | if (tmpexp > 0) { |
728 | 0 | (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0'); |
729 | 0 | return 0; |
730 | 0 | } |
731 | | /* Add a leading 0 for single digit exponents */ |
732 | 0 | if (eplace == 1) |
733 | 0 | econvert[eplace++] = '0'; |
734 | 0 | } |
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 | 0 | padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0); |
741 | | /* Take some off for exponent prefix "+e" and exponent */ |
742 | 0 | if (realstyle == E_FORMAT) |
743 | 0 | padlen -= 2 + eplace; |
744 | 0 | zpadlen = max - fplace; |
745 | 0 | if (zpadlen < 0) |
746 | 0 | zpadlen = 0; |
747 | 0 | if (padlen < 0) |
748 | 0 | padlen = 0; |
749 | 0 | if (flags & DP_F_MINUS) |
750 | 0 | padlen = -padlen; |
751 | |
|
752 | 0 | if ((flags & DP_F_ZERO) && (padlen > 0)) { |
753 | 0 | if (signvalue) { |
754 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
755 | 0 | return 0; |
756 | 0 | --padlen; |
757 | 0 | signvalue = 0; |
758 | 0 | } |
759 | 0 | while (padlen > 0) { |
760 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
761 | 0 | return 0; |
762 | 0 | --padlen; |
763 | 0 | } |
764 | 0 | } |
765 | 0 | while (padlen > 0) { |
766 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
767 | 0 | return 0; |
768 | 0 | --padlen; |
769 | 0 | } |
770 | 0 | if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue)) |
771 | 0 | return 0; |
772 | | |
773 | 0 | while (iplace > 0) { |
774 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace])) |
775 | 0 | return 0; |
776 | 0 | } |
777 | | |
778 | | /* |
779 | | * Decimal point. This should probably use locale to find the correct |
780 | | * char to print out. |
781 | | */ |
782 | 0 | if (max > 0 || (flags & DP_F_NUM)) { |
783 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.')) |
784 | 0 | return 0; |
785 | | |
786 | 0 | while (fplace > 0) { |
787 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, |
788 | 0 | fconvert[--fplace])) |
789 | 0 | return 0; |
790 | 0 | } |
791 | 0 | } |
792 | 0 | while (zpadlen > 0) { |
793 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0')) |
794 | 0 | return 0; |
795 | 0 | --zpadlen; |
796 | 0 | } |
797 | 0 | if (realstyle == E_FORMAT) { |
798 | 0 | char ech; |
799 | |
|
800 | 0 | if ((flags & DP_F_UP) == 0) |
801 | 0 | ech = 'e'; |
802 | 0 | else |
803 | 0 | ech = 'E'; |
804 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech)) |
805 | 0 | return 0; |
806 | 0 | if (exp < 0) { |
807 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-')) |
808 | 0 | return 0; |
809 | 0 | } else { |
810 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+')) |
811 | 0 | return 0; |
812 | 0 | } |
813 | 0 | while (eplace > 0) { |
814 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, |
815 | 0 | econvert[--eplace])) |
816 | 0 | return 0; |
817 | 0 | } |
818 | 0 | } |
819 | | |
820 | 0 | while (padlen < 0) { |
821 | 0 | if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' ')) |
822 | 0 | return 0; |
823 | 0 | ++padlen; |
824 | 0 | } |
825 | 0 | return 1; |
826 | 0 | } |
827 | | |
828 | | #endif /* OPENSSL_SYS_UEFI */ |
829 | | |
830 | 107k | #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 | 2.35G | { |
836 | | /* If we haven't at least one buffer, someone has done a big booboo */ |
837 | 2.35G | if (!ossl_assert(*sbuffer != NULL || buffer != NULL)) |
838 | 0 | return 0; |
839 | | |
840 | | /* |currlen| must always be <= |*maxlen| */ |
841 | 2.35G | if (!ossl_assert(*currlen <= *maxlen)) |
842 | 0 | return 0; |
843 | | |
844 | 2.35G | if (buffer && *currlen == *maxlen) { |
845 | 53.5k | if (*maxlen > INT_MAX - BUFFER_INC) |
846 | 0 | return 0; |
847 | | |
848 | 53.5k | *maxlen += BUFFER_INC; |
849 | 53.5k | if (*buffer == NULL) { |
850 | 924 | if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) { |
851 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
852 | 0 | return 0; |
853 | 0 | } |
854 | 924 | if (*currlen > 0) { |
855 | 924 | if (!ossl_assert(*sbuffer != NULL)) |
856 | 0 | return 0; |
857 | 924 | memcpy(*buffer, *sbuffer, *currlen); |
858 | 924 | } |
859 | 924 | *sbuffer = NULL; |
860 | 52.6k | } else { |
861 | 52.6k | char *tmpbuf; |
862 | | |
863 | 52.6k | tmpbuf = OPENSSL_realloc(*buffer, *maxlen); |
864 | 52.6k | if (tmpbuf == NULL) { |
865 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
866 | 0 | return 0; |
867 | 0 | } |
868 | 52.6k | *buffer = tmpbuf; |
869 | 52.6k | } |
870 | 53.5k | } |
871 | | |
872 | 2.35G | if (*currlen < *maxlen) { |
873 | 2.34G | if (*sbuffer) |
874 | 2.29G | (*sbuffer)[(*currlen)++] = (char)c; |
875 | 54.0M | else |
876 | 54.0M | (*buffer)[(*currlen)++] = (char)c; |
877 | 2.34G | } |
878 | | |
879 | 2.35G | return 1; |
880 | 2.35G | } |
881 | | |
882 | | /***************************************************************************/ |
883 | | |
884 | | int BIO_printf(BIO *bio, const char *format, ...) |
885 | 237M | { |
886 | 237M | va_list args; |
887 | 237M | int ret; |
888 | | |
889 | 237M | va_start(args, format); |
890 | | |
891 | 237M | ret = BIO_vprintf(bio, format, args); |
892 | | |
893 | 237M | va_end(args); |
894 | 237M | return ret; |
895 | 237M | } |
896 | | |
897 | | int BIO_vprintf(BIO *bio, const char *format, va_list args) |
898 | 237M | { |
899 | 237M | int ret; |
900 | 237M | size_t retlen; |
901 | 237M | char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable |
902 | | * in small-stack environments, like threads |
903 | | * or DOS programs. */ |
904 | 237M | char *hugebufp = hugebuf; |
905 | 237M | size_t hugebufsize = sizeof(hugebuf); |
906 | 237M | char *dynbuf = NULL; |
907 | 237M | int ignored; |
908 | | |
909 | 237M | dynbuf = NULL; |
910 | 237M | if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, |
911 | 237M | args)) { |
912 | 0 | OPENSSL_free(dynbuf); |
913 | 0 | return -1; |
914 | 0 | } |
915 | 237M | if (dynbuf) { |
916 | 924 | ret = BIO_write(bio, dynbuf, (int)retlen); |
917 | 924 | OPENSSL_free(dynbuf); |
918 | 237M | } else { |
919 | 237M | ret = BIO_write(bio, hugebuf, (int)retlen); |
920 | 237M | } |
921 | 237M | return ret; |
922 | 237M | } |
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 | 137M | { |
932 | 137M | va_list args; |
933 | 137M | int ret; |
934 | | |
935 | 137M | va_start(args, format); |
936 | | |
937 | 137M | ret = BIO_vsnprintf(buf, n, format, args); |
938 | | |
939 | 137M | va_end(args); |
940 | 137M | return ret; |
941 | 137M | } |
942 | | |
943 | | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) |
944 | 139M | { |
945 | 139M | size_t retlen; |
946 | 139M | int truncated; |
947 | | |
948 | 139M | if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args)) |
949 | 0 | return -1; |
950 | | |
951 | 139M | 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 | 10.8k | return -1; |
959 | 139M | return (retlen <= INT_MAX) ? (int)retlen : -1; |
960 | 139M | } |