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
32 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
33 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
35 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
37 char *old_str, *new_str;
38 unsigned min_size, new_size;
40 if (buf->size == buf->size_max)
42 if (!av_bprint_is_complete(buf))
43 return AVERROR_INVALIDDATA; /* it is already truncated anyway */
44 min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
45 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
46 if (new_size < min_size)
47 new_size = FFMIN(buf->size_max, min_size);
48 old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
49 new_str = av_realloc(old_str, new_size);
51 return AVERROR(ENOMEM);
53 memcpy(new_str, buf->str, buf->len + 1);
59 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
61 /* arbitrary margin to avoid small overflows */
62 extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
63 buf->len += extra_len;
65 buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
68 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
70 unsigned size_auto = (char *)buf + sizeof(*buf) -
71 buf->reserved_internal_buffer;
75 buf->str = buf->reserved_internal_buffer;
77 buf->size = FFMIN(size_auto, size_max);
78 buf->size_max = size_max;
80 if (size_init > buf->size)
81 av_bprint_alloc(buf, size_init - 1);
84 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
93 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
101 room = av_bprint_room(buf);
102 dst = room ? buf->str + buf->len : NULL;
104 extra_len = vsnprintf(dst, room, fmt, vl);
108 if (extra_len < room)
110 if (av_bprint_alloc(buf, extra_len))
113 av_bprint_grow(buf, extra_len);
116 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
118 unsigned room, real_n;
121 room = av_bprint_room(buf);
124 if (av_bprint_alloc(buf, n))
128 real_n = FFMIN(n, room - 1);
129 memset(buf->str + buf->len, c, real_n);
131 av_bprint_grow(buf, n);
134 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
142 room = av_bprint_room(buf);
143 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
145 /* strftime does not tell us how much room it would need: let us
146 retry with twice as much until the buffer is large enough */
147 room = !room ? strlen(fmt) + 1 :
148 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
149 if (av_bprint_alloc(buf, room)) {
150 /* impossible to grow, try to manage something useful anyway */
151 room = av_bprint_room(buf);
153 /* if strftime fails because the buffer has (almost) reached
154 its maximum size, let us try in a local buffer; 1k should
155 be enough to format any real date+time string */
157 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
158 av_bprintf(buf, "%s", buf2);
163 /* if anything else failed and the buffer is not already
164 truncated, let us add a stock string and force truncation */
165 static const char txt[] = "[truncated strftime output]";
166 memset(buf->str + buf->len, '!', room);
167 memcpy(buf->str + buf->len, txt, FFMIN(sizeof(txt) - 1, room));
168 av_bprint_grow(buf, room); /* force truncation */
173 av_bprint_grow(buf, l);
176 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
177 unsigned char **mem, unsigned *actual_size)
179 if (size > av_bprint_room(buf))
180 av_bprint_alloc(buf, size);
181 *actual_size = av_bprint_room(buf);
182 *mem = *actual_size ? buf->str + buf->len : NULL;
185 void av_bprint_clear(AVBPrint *buf)
193 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
195 unsigned real_size = FFMIN(buf->len + 1, buf->size);
200 if (av_bprint_is_allocated(buf)) {
201 str = av_realloc(buf->str, real_size);
206 str = av_malloc(real_size);
208 memcpy(str, buf->str, real_size);
210 ret = AVERROR(ENOMEM);
214 if (av_bprint_is_allocated(buf))
217 buf->size = real_size;
221 #define WHITESPACES " \n\t"
223 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
224 enum AVEscapeMode mode, int flags)
226 const char *src0 = src;
228 if (mode == AV_ESCAPE_MODE_AUTO)
229 mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
232 case AV_ESCAPE_MODE_QUOTE:
233 /* enclose the string between '' */
234 av_bprint_chars(dstbuf, '\'', 1);
235 for (; *src; src++) {
237 av_bprintf(dstbuf, "'\\''");
239 av_bprint_chars(dstbuf, *src, 1);
241 av_bprint_chars(dstbuf, '\'', 1);
244 /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
246 /* \-escape characters */
247 for (; *src; src++) {
248 int is_first_last = src == src0 || !*(src+1);
249 int is_ws = !!strchr(WHITESPACES, *src);
250 int is_strictly_special = special_chars && strchr(special_chars, *src);
252 is_strictly_special || strchr("'\\", *src) ||
253 (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
255 if (is_strictly_special ||
256 (!(flags & AV_ESCAPE_FLAG_STRICT) &&
257 (is_special || (is_ws && is_first_last))))
258 av_bprint_chars(dstbuf, '\\', 1);
259 av_bprint_chars(dstbuf, *src, 1);
269 static void bprint_pascal(AVBPrint *b, unsigned size)
274 av_assert0(size < FF_ARRAY_ELEMS(p));
277 av_bprintf(b, "%8d\n", 1);
278 for (i = 1; i <= size; i++) {
280 for (j = i - 1; j > 0; j--)
281 p[j] = p[j] + p[j - 1];
282 for (j = 0; j <= i; j++)
283 av_bprintf(b, "%8d", p[j]);
292 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
294 av_bprint_init(&b, 0, -1);
295 bprint_pascal(&b, 5);
296 printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
297 printf("%s\n", b.str);
298 av_bprint_finalize(&b, NULL);
300 av_bprint_init(&b, 0, -1);
301 bprint_pascal(&b, 25);
302 printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
303 av_bprint_finalize(&b, NULL);
305 av_bprint_init(&b, 0, 2048);
306 bprint_pascal(&b, 25);
307 printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
308 av_bprint_finalize(&b, NULL);
310 av_bprint_init(&b, 0, 1);
311 bprint_pascal(&b, 5);
312 printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
314 av_bprint_init(&b, 0, 1);
315 bprint_pascal(&b, 25);
316 printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
317 /* Note that the size of the automatic buffer is arch-dependant. */
319 av_bprint_init(&b, 0, 0);
320 bprint_pascal(&b, 25);
321 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
323 av_bprint_init_for_buffer(&b, buf, sizeof(buf));
324 bprint_pascal(&b, 25);
325 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
327 av_bprint_init(&b, 0, -1);
328 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
329 printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
330 av_bprint_finalize(&b, NULL);
332 av_bprint_init(&b, 0, 8);
333 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
334 printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);