1 /*
2 * Copyright (c) 2012 Nicolas George
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
32
33 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
34 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
35
37 {
38 char *old_str, *new_str;
39 unsigned min_size, new_size;
40
41 if (buf->size == buf->size_max)
45 min_size = buf->len + 1 +
FFMIN(UINT_MAX - buf->len - 1, room);
46 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
47 if (new_size < min_size)
48 new_size =
FFMIN(buf->size_max, min_size);
51 if (!new_str)
53 if (!old_str)
54 memcpy(new_str, buf->str, buf->len + 1);
55 buf->str = new_str;
56 buf->size = new_size;
57 return 0;
58 }
59
61 {
62 /* arbitrary margin to avoid small overflows */
63 extra_len =
FFMIN(extra_len, UINT_MAX - 5 - buf->len);
64 buf->len += extra_len;
65 if (buf->size)
66 buf->str[
FFMIN(buf->len, buf->size - 1)] = 0;
67 }
68
70 {
71 unsigned size_auto = (char *)buf + sizeof(*buf) -
72 buf->reserved_internal_buffer;
73
76 buf->str = buf->reserved_internal_buffer;
77 buf->len = 0;
80 *buf->str = 0;
83 }
84
86 {
89 return;
90 }
91
93 buf->len = 0;
96 *buf->str = 0;
97 }
98
100 {
101 unsigned room;
102 char *dst;
103 va_list vl;
104 int extra_len;
105
106 while (1) {
108 dst = room ? buf->str + buf->len :
NULL;
109 va_start(vl, fmt);
110 extra_len =
vsnprintf(dst, room, fmt, vl);
111 va_end(vl);
112 if (extra_len <= 0)
113 return;
114 if (extra_len < room)
115 break;
117 break;
118 }
120 }
121
123 {
124 unsigned room;
125 char *dst;
126 int extra_len;
127 va_list vl;
128
129 while (1) {
131 dst = room ? buf->str + buf->len :
NULL;
133 extra_len =
vsnprintf(dst, room, fmt, vl);
134 va_end(vl);
135 if (extra_len <= 0)
136 return;
137 if (extra_len < room)
138 break;
140 break;
141 }
143 }
144
146 {
147 unsigned room, real_n;
148
149 while (1) {
151 if (n < room)
152 break;
154 break;
155 }
156 if (room) {
157 real_n =
FFMIN(n, room - 1);
158 memset(buf->str + buf->len,
c, real_n);
159 }
161 }
162
164 {
165 unsigned room, real_n;
166
167 while (1) {
170 break;
172 break;
173 }
174 if (room) {
176 memcpy(buf->str + buf->len,
data, real_n);
177 }
179 }
180
182 {
183 unsigned room;
184 size_t l;
185
186 if (!*fmt)
187 return;
188 while (1) {
190 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
191 break;
192 /* strftime does not tell us how much room it would need: let us
193 retry with twice as much until the buffer is large enough */
194 room = !room ? strlen(fmt) + 1 :
195 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
197 /* impossible to grow, try to manage something useful anyway */
199 if (room < 1024) {
200 /* if strftime fails because the buffer has (almost) reached
201 its maximum size, let us try in a local buffer; 1k should
202 be enough to format any real date+time string */
203 char buf2[1024];
204 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
206 return;
207 }
208 }
209 if (room) {
210 /* if anything else failed and the buffer is not already
211 truncated, let us add a stock string and force truncation */
212 static const char txt[] = "[truncated strftime output]";
213 memset(buf->str + buf->len, '!', room);
214 memcpy(buf->str + buf->len, txt,
FFMIN(
sizeof(txt) - 1, room));
216 }
217 return;
218 }
219 }
221 }
222
224 unsigned char **mem, unsigned *actual_size)
225 {
229 *mem = *actual_size ? buf->str + buf->len :
NULL;
230 }
231
233 {
234 if (buf->len) {
235 *buf->str = 0;
236 buf->len = 0;
237 }
238 }
239
241 {
242 unsigned real_size =
FFMIN(buf->len + 1, buf->size);
243 char *str;
245
249 if (!str)
250 str = buf->str;
252 } else {
254 if (!str)
256 }
258 } else {
261 }
262 buf->size = real_size;
264 }
265
266 #define WHITESPACES " \n\t\r"
267
270 {
272
275
278 /* enclose the string between '' */
283 else
285 }
287 break;
288
290 /* escape XML non-markup character data as per 2.4 by default: */
291 /* [^<&]* - ([^<&]* ']]>' [^<&]*) */
292
293 /* additionally, given one of the AV_ESCAPE_FLAG_XML_* flags, */
294 /* escape those specific characters as required. */
297 case '&' :
av_bprintf(dstbuf,
"%s",
"&");
break;
298 case '<' :
av_bprintf(dstbuf,
"%s",
"<");
break;
299 case '>' :
av_bprintf(dstbuf,
"%s",
">");
break;
300 case '\'':
302 goto XML_DEFAULT_HANDLING;
303
305 break;
306 case '"' :
308 goto XML_DEFAULT_HANDLING;
309
311 break;
312 XML_DEFAULT_HANDLING:
314 }
315 }
316 break;
317
318 /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
319 default:
320 /* \-escape characters */
324 int is_strictly_special = special_chars && strchr(special_chars, *
src);
325 int is_special =
326 is_strictly_special || strchr(
"'\\", *
src) ||
328
329 if (is_strictly_special ||
331 (is_special || (is_ws && is_first_last))))
334 }
335 break;
336 }
337 }