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