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
31 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
32 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
34 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
36 char *old_str, *new_str;
37 unsigned min_size, new_size;
39 if (buf->size == buf->size_max)
41 if (!av_bprint_is_complete(buf))
42 return AVERROR_INVALIDDATA; /* it is already truncated anyway */
43 min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
44 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
45 if (new_size < min_size)
46 new_size = FFMIN(buf->size_max, min_size);
47 old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
48 new_str = av_realloc(old_str, new_size);
50 return AVERROR(ENOMEM);
52 memcpy(new_str, buf->str, buf->len + 1);
58 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
60 /* arbitrary margin to avoid small overflows */
61 extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
62 buf->len += extra_len;
64 buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
67 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
69 unsigned size_auto = (char *)buf + sizeof(*buf) -
70 buf->reserved_internal_buffer;
74 buf->str = buf->reserved_internal_buffer;
76 buf->size = FFMIN(size_auto, size_max);
77 buf->size_max = size_max;
79 if (size_init > buf->size)
80 av_bprint_alloc(buf, size_init - 1);
83 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
92 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
100 room = av_bprint_room(buf);
101 dst = room ? buf->str + buf->len : NULL;
103 extra_len = vsnprintf(dst, room, fmt, vl);
107 if (extra_len < room)
109 if (av_bprint_alloc(buf, extra_len))
112 av_bprint_grow(buf, extra_len);
115 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
117 unsigned room, real_n;
120 room = av_bprint_room(buf);
123 if (av_bprint_alloc(buf, n))
127 real_n = FFMIN(n, room - 1);
128 memset(buf->str + buf->len, c, real_n);
130 av_bprint_grow(buf, n);
133 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
141 room = av_bprint_room(buf);
142 if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
144 /* strftime does not tell us how much room it would need: let us
145 retry with twice as much until the buffer is large enough */
146 room = !room ? strlen(fmt) + 1 :
147 room <= INT_MAX / 2 ? room * 2 : INT_MAX;
148 if (av_bprint_alloc(buf, room)) {
149 /* impossible to grow, try to manage something useful anyway */
150 room = av_bprint_room(buf);
152 /* if strftime fails because the buffer has (almost) reached
153 its maximum size, let us try in a local buffer; 1k should
154 be enough to format any real date+time string */
156 if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
157 av_bprintf(buf, "%s", buf2);
162 /* if anything else failed and the buffer is not already
163 truncated, let us add a stock string and force truncation */
164 static const char txt[] = "[truncated strftime output]";
165 memset(buf->str + buf->len, '!', room);
166 memcpy(buf->str + buf->len, txt, FFMIN(sizeof(txt) - 1, room));
167 av_bprint_grow(buf, room); /* force truncation */
172 av_bprint_grow(buf, l);
175 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
176 unsigned char **mem, unsigned *actual_size)
178 if (size > av_bprint_room(buf))
179 av_bprint_alloc(buf, size);
180 *actual_size = av_bprint_room(buf);
181 *mem = *actual_size ? buf->str + buf->len : NULL;
184 void av_bprint_clear(AVBPrint *buf)
192 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
194 unsigned real_size = FFMIN(buf->len + 1, buf->size);
199 if (av_bprint_is_allocated(buf)) {
200 str = av_realloc(buf->str, real_size);
205 str = av_malloc(real_size);
207 memcpy(str, buf->str, real_size);
209 ret = AVERROR(ENOMEM);
213 if (av_bprint_is_allocated(buf))
216 buf->size = real_size;
224 static void bprint_pascal(AVBPrint *b, unsigned size)
229 av_assert0(size < FF_ARRAY_ELEMS(p));
232 av_bprintf(b, "%8d\n", 1);
233 for (i = 1; i <= size; i++) {
235 for (j = i - 1; j > 0; j--)
236 p[j] = p[j] + p[j - 1];
237 for (j = 0; j <= i; j++)
238 av_bprintf(b, "%8d", p[j]);
247 struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
249 av_bprint_init(&b, 0, -1);
250 bprint_pascal(&b, 5);
251 printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
252 printf("%s\n", b.str);
253 av_bprint_finalize(&b, NULL);
255 av_bprint_init(&b, 0, -1);
256 bprint_pascal(&b, 25);
257 printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
258 av_bprint_finalize(&b, NULL);
260 av_bprint_init(&b, 0, 2048);
261 bprint_pascal(&b, 25);
262 printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
263 av_bprint_finalize(&b, NULL);
265 av_bprint_init(&b, 0, 1);
266 bprint_pascal(&b, 5);
267 printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
269 av_bprint_init(&b, 0, 1);
270 bprint_pascal(&b, 25);
271 printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
272 /* Note that the size of the automatic buffer is arch-dependant. */
274 av_bprint_init(&b, 0, 0);
275 bprint_pascal(&b, 25);
276 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
278 av_bprint_init_for_buffer(&b, buf, sizeof(buf));
279 bprint_pascal(&b, 25);
280 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
282 av_bprint_init(&b, 0, -1);
283 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
284 printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
285 av_bprint_finalize(&b, NULL);
287 av_bprint_init(&b, 0, 8);
288 av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
289 printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);