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