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