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 {
88 buf->len = 0;
91 *buf->str = 0;
92 }
93
95 {
96 unsigned room;
97 char *dst;
98 va_list vl;
99 int extra_len;
100
101 while (1) {
103 dst = room ? buf->str + buf->len :
NULL;
104 va_start(vl, fmt);
105 extra_len =
vsnprintf(dst, room, fmt, vl);
106 va_end(vl);
107 if (extra_len <= 0)
108 return;
109 if (extra_len < room)
110 break;
112 break;
113 }
115 }
116
118 {
119 unsigned room;
120 char *dst;
121 int extra_len;
122 va_list vl;
123
124 while (1) {
126 dst = room ? buf->str + buf->len :
NULL;
128 extra_len =
vsnprintf(dst, room, fmt, vl);
129 va_end(vl);
130 if (extra_len <= 0)
131 return;
132 if (extra_len < room)
133 break;
135 break;
136 }
138 }
139
141 {
142 unsigned room, real_n;
143
144 while (1) {
146 if (n < room)
147 break;
149 break;
150 }
151 if (room) {
152 real_n =
FFMIN(n, room - 1);
153 memset(buf->str + buf->len,
c, real_n);
154 }
156 }
157
159 {
160 unsigned room, real_n;
161
162 while (1) {
165 break;
167 break;
168 }
169 if (room) {
171 memcpy(buf->str + buf->len,
data, real_n);
172 }
174 }
175
177 {
178 unsigned room;
179 size_t l;
180
181 if (!*fmt)
182 return;
183 while (1) {
185 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
186 break;
187 /* strftime does not tell us how much room it would need: let us
188 retry with twice as much until the buffer is large enough */
189 room = !room ? strlen(fmt) + 1 :
190 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
192 /* impossible to grow, try to manage something useful anyway */
194 if (room < 1024) {
195 /* if strftime fails because the buffer has (almost) reached
196 its maximum size, let us try in a local buffer; 1k should
197 be enough to format any real date+time string */
198 char buf2[1024];
199 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
201 return;
202 }
203 }
204 if (room) {
205 /* if anything else failed and the buffer is not already
206 truncated, let us add a stock string and force truncation */
207 static const char txt[] = "[truncated strftime output]";
208 memset(buf->str + buf->len, '!', room);
209 memcpy(buf->str + buf->len, txt,
FFMIN(
sizeof(txt) - 1, room));
211 }
212 return;
213 }
214 }
216 }
217
219 unsigned char **mem, unsigned *actual_size)
220 {
224 *mem = *actual_size ? buf->str + buf->len :
NULL;
225 }
226
228 {
229 if (buf->len) {
230 *buf->str = 0;
231 buf->len = 0;
232 }
233 }
234
236 {
237 unsigned real_size =
FFMIN(buf->len + 1, buf->size);
240
247 } else {
251 }
253 } else {
256 }
257 buf->size = real_size;
259 }
260
261 #define WHITESPACES " \n\t\r"
262
265 {
267
270
273 /* enclose the string between '' */
278 else
280 }
282 break;
283
285 /* escape XML non-markup character data as per 2.4 by default: */
286 /* [^<&]* - ([^<&]* ']]>' [^<&]*) */
287
288 /* additionally, given one of the AV_ESCAPE_FLAG_XML_* flags, */
289 /* escape those specific characters as required. */
292 case '&' :
av_bprintf(dstbuf,
"%s",
"&");
break;
293 case '<' :
av_bprintf(dstbuf,
"%s",
"<");
break;
294 case '>' :
av_bprintf(dstbuf,
"%s",
">");
break;
295 case '\'':
297 goto XML_DEFAULT_HANDLING;
298
300 break;
301 case '"' :
303 goto XML_DEFAULT_HANDLING;
304
306 break;
307 XML_DEFAULT_HANDLING:
309 }
310 }
311 break;
312
313 /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
314 default:
315 /* \-escape characters */
319 int is_strictly_special = special_chars && strchr(special_chars, *
src);
320 int is_special =
321 is_strictly_special || strchr(
"'\\", *
src) ||
323
324 if (is_strictly_special ||
326 (is_special || (is_ws && is_first_last))))
329 }
330 break;
331 }
332 }