2 * Copyright (c) 2012 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
29 #include "compat/va_copy.h"
38 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
39 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
41 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
43 char *old_str, *new_str;
44 unsigned min_size, new_size;
46 if (buf->size == buf->size_max)
48 if (!av_bprint_is_complete(buf))
49 return AVERROR_INVALIDDATA; /* it is already truncated anyway */
50 min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
51 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
52 if (new_size < min_size)
53 new_size = FFMIN(buf->size_max, min_size);
54 old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
55 new_str = av_realloc(old_str, new_size);
57 return AVERROR(ENOMEM);
59 memcpy(new_str, buf->str, buf->len + 1);
65 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
67 /* arbitrary margin to avoid small overflows */
68 extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
69 buf->len += extra_len;
71 buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
74 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
76 unsigned size_auto = (char *)buf + sizeof(*buf) -
77 buf->reserved_internal_buffer;
81 buf->str = buf->reserved_internal_buffer;
83 buf->size = FFMIN(size_auto, size_max);
84 buf->size_max = size_max;
86 if (size_init > buf->size)
87 av_bprint_alloc(buf, size_init - 1);
90 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
99 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
107 room = av_bprint_room(buf);
108 dst = room ? buf->str + buf->len : NULL;
110 extra_len = vsnprintf(dst, room, fmt, vl);
114 if (extra_len < room)
116 if (av_bprint_alloc(buf, extra_len))
119 av_bprint_grow(buf, extra_len);
122 void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
130 room = av_bprint_room(buf);
131 dst = room ? buf->str + buf->len : NULL;
133 extra_len = vsnprintf(dst, room, fmt, vl);
137 if (extra_len < room)
139 if (av_bprint_alloc(buf, extra_len))
142 av_bprint_grow(buf, extra_len);
145 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
147 unsigned room, real_n;
150 room = av_bprint_room(buf);
153 if (av_bprint_alloc(buf, n))
157 real_n = FFMIN(n, room - 1);
158 memset(buf->str + buf->len, c, real_n);
160 av_bprint_grow(buf, n);
163 void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
165 unsigned room, real_n;
168 room = av_bprint_room(buf);
171 if (av_bprint_alloc(buf, size))
175 real_n = FFMIN(size, room - 1);
176 memcpy(buf->str + buf->len, data, real_n);
178 av_bprint_grow(buf, size);
181 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
189 room = av_bprint_room(buf);
190 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
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;
196 if (av_bprint_alloc(buf, room)) {
197 /* impossible to grow, try to manage something useful anyway */
198 room = av_bprint_room(buf);
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 */
204 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
205 av_bprintf(buf, "%s", buf2);
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));
215 av_bprint_grow(buf, room); /* force truncation */
220 av_bprint_grow(buf, l);
223 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
224 unsigned char **mem, unsigned *actual_size)
226 if (size > av_bprint_room(buf))
227 av_bprint_alloc(buf, size);
228 *actual_size = av_bprint_room(buf);
229 *mem = *actual_size ? buf->str + buf->len : NULL;
232 void av_bprint_clear(AVBPrint *buf)
240 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
242 unsigned real_size = FFMIN(buf->len + 1, buf->size);
247 if (av_bprint_is_allocated(buf)) {
248 str = av_realloc(buf->str, real_size);
253 str = av_malloc(real_size);
255 memcpy(str, buf->str, real_size);
257 ret = AVERROR(ENOMEM);
261 if (av_bprint_is_allocated(buf))
264 buf->size = real_size;
268 #define WHITESPACES " \n\t"
270 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
271 enum AVEscapeMode mode, int flags)
273 const char *src0 = src;
275 if (mode == AV_ESCAPE_MODE_AUTO)
276 mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
279 case AV_ESCAPE_MODE_QUOTE:
280 /* enclose the string between '' */
281 av_bprint_chars(dstbuf, '\'', 1);
282 for (; *src; src++) {
284 av_bprintf(dstbuf, "'\\''");
286 av_bprint_chars(dstbuf, *src, 1);
288 av_bprint_chars(dstbuf, '\'', 1);
291 /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
293 /* \-escape characters */
294 for (; *src; src++) {
295 int is_first_last = src == src0 || !*(src+1);
296 int is_ws = !!strchr(WHITESPACES, *src);
297 int is_strictly_special = special_chars && strchr(special_chars, *src);
299 is_strictly_special || strchr("'\\", *src) ||
300 (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
302 if (is_strictly_special ||
303 (!(flags & AV_ESCAPE_FLAG_STRICT) &&
304 (is_special || (is_ws && is_first_last))))
305 av_bprint_chars(dstbuf, '\\', 1);
306 av_bprint_chars(dstbuf, *src, 1);
312 int av_bprint_fd_contents(AVBPrint *pb, int fd)
317 ret = read(fd, buf, sizeof(buf));
321 return AVERROR(errno);
322 av_bprint_append_data(pb, buf, ret);
323 if (!av_bprint_is_complete(pb))
324 return AVERROR(ENOMEM);
332 static void bprint_pascal(AVBPrint *b, unsigned size)
337 av_assert0(size < FF_ARRAY_ELEMS(p));
340 av_bprintf(b, "%8d\n", 1);
341 for (i = 1; i <= size; i++) {
343 for (j = i - 1; j > 0; j--)
344 p[j] = p[j] + p[j - 1];
345 for (j = 0; j <= i; j++)
346 av_bprintf(b, "%8d", p[j]);
355 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
357 av_bprint_init(&b, 0, -1);
358 bprint_pascal(&b, 5);
359 printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
360 printf("%s\n", b.str);
361 av_bprint_finalize(&b, NULL);
363 av_bprint_init(&b, 0, -1);
364 bprint_pascal(&b, 25);
365 printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
366 av_bprint_finalize(&b, NULL);
368 av_bprint_init(&b, 0, 2048);
369 bprint_pascal(&b, 25);
370 printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
371 av_bprint_finalize(&b, NULL);
373 av_bprint_init(&b, 0, 1);
374 bprint_pascal(&b, 5);
375 printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
377 av_bprint_init(&b, 0, 1);
378 bprint_pascal(&b, 25);
379 printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
380 /* Note that the size of the automatic buffer is arch-dependent. */
382 av_bprint_init(&b, 0, 0);
383 bprint_pascal(&b, 25);
384 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
386 av_bprint_init_for_buffer(&b, buf, sizeof(buf));
387 bprint_pascal(&b, 25);
388 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
390 av_bprint_init(&b, 0, -1);
391 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
392 printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
393 av_bprint_finalize(&b, NULL);
395 av_bprint_init(&b, 0, 8);
396 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
397 printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);