1/* Estimate the length of the string generated by a vprintf-like
2 function. Used by vasprintf and xvasprintf.
3 Copyright (C) 1994-2024 Free Software Foundation, Inc.
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If not, write
18to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
19Floor, Boston, MA 02110-1301, USA. */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24#include <ansidecl.h>
25#include <stdarg.h>
26#if !defined (va_copy) && defined (__va_copy)
27# define va_copy(d,s) __va_copy((d),(s))
28#endif
29#include <stdio.h>
30#ifdef HAVE_STRING_H
31#include <string.h>
32#endif
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#else
36extern unsigned long strtoul ();
37#endif
38#include "libiberty.h"
39
40int
41libiberty_vprintf_buffer_size (const char *format, va_list args)
42{
43 const char *p = format;
44 /* Add one to make sure that it is never zero, which might cause malloc
45 to return NULL. */
46 int total_width = strlen (s: format) + 1;
47 va_list ap;
48
49#ifdef va_copy
50 va_copy (ap, args);
51#else
52 memcpy ((void *) &ap, (void *) &args, sizeof (va_list));
53#endif
54
55 while (*p != '\0')
56 {
57 if (*p++ == '%')
58 {
59 int prec = 0;
60 while (strchr (s: "-+ #0", c: *p))
61 ++p;
62 if (*p == '*')
63 {
64 ++p;
65 total_width += abs (va_arg (ap, int));
66 }
67 else
68 total_width += strtoul (nptr: p, endptr: (char **) &p, base: 10);
69 if (*p == '.')
70 {
71 ++p;
72 if (*p == '*')
73 {
74 ++p;
75 total_width += abs (va_arg (ap, int));
76 }
77 else
78 total_width += strtoul (nptr: p, endptr: (char **) &p, base: 10);
79 }
80 do
81 {
82 switch (*p)
83 {
84 case 'h':
85 ++p;
86 continue;
87 case 'l':
88 case 'L':
89 ++prec;
90 ++p;
91 continue;
92 case 'z':
93 prec = 3;
94 ++p;
95 continue;
96 case 't':
97 prec = 4;
98 ++p;
99 continue;
100#ifdef _WIN32
101 case 'I':
102 if (p[1] == '6' && p[2] == '4')
103 {
104 prec = 2;
105 p += 3;
106 continue;
107 }
108 break;
109#endif
110 default:
111 break;
112 }
113 break;
114 }
115 while (1);
116
117 /* Should be big enough for any format specifier except %s and floats. */
118 total_width += 30;
119 switch (*p)
120 {
121 case 'd':
122 case 'i':
123 case 'o':
124 case 'u':
125 case 'x':
126 case 'X':
127 switch (prec)
128 {
129 case 0: (void) va_arg (ap, int); break;
130 case 1: (void) va_arg (ap, long int); break;
131 case 2: (void) va_arg (ap, long long int); break;
132 case 3: (void) va_arg (ap, size_t); break;
133 case 4: (void) va_arg (ap, ptrdiff_t); break;
134 }
135 break;
136 case 'c':
137 (void) va_arg (ap, int);
138 break;
139 case 'f':
140 case 'e':
141 case 'E':
142 case 'g':
143 case 'G':
144 if (!prec)
145 {
146 (void) va_arg (ap, double);
147 /* Since an ieee double can have an exponent of 308, we'll
148 make the buffer wide enough to cover the gross case. */
149 total_width += 308;
150 }
151 else
152 {
153 (void) va_arg (ap, long double);
154 total_width += 4932;
155 }
156 break;
157 case 's':
158 total_width += strlen (va_arg (ap, char *));
159 break;
160 case 'p':
161 case 'n':
162 (void) va_arg (ap, char *);
163 break;
164 }
165 p++;
166 }
167 }
168#ifdef va_copy
169 va_end (ap);
170#endif
171 return total_width;
172}
173

source code of libiberty/vprintf-support.c