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 static int vsnprintf_fixed(char *s, size_t n, const char *format, va_list va)
37 r = vsnprintf(s, n, format, va2);
40 r = _vscprintf(format, va);
44 #define vsnprintf vsnprintf_fixed
48 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
49 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
51 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
53 char *old_str, *new_str;
54 unsigned min_size, new_size;
56 if (buf->size == buf->size_max)
58 if (!av_bprint_is_complete(buf))
59 return AVERROR_INVALIDDATA; /* it is already truncated anyway */
60 min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
61 new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
62 if (new_size < min_size)
63 new_size = FFMIN(buf->size_max, min_size);
64 old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
65 new_str = av_realloc(old_str, new_size);
67 return AVERROR(ENOMEM);
69 memcpy(new_str, buf->str, buf->len + 1);
75 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
77 /* arbitrary margin to avoid small overflows */
78 extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
79 buf->len += extra_len;
81 buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
84 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
86 unsigned size_auto = (char *)buf + sizeof(*buf) -
87 buf->reserved_internal_buffer;
91 buf->str = buf->reserved_internal_buffer;
93 buf->size = FFMIN(size_auto, size_max);
94 buf->size_max = size_max;
96 if (size_init > buf->size)
97 av_bprint_alloc(buf, size_init - 1);
100 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
105 buf->size_max = size;
109 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
117 room = av_bprint_room(buf);
118 dst = room ? buf->str + buf->len : NULL;
120 extra_len = vsnprintf(dst, room, fmt, vl);
124 if (extra_len < room)
126 if (av_bprint_alloc(buf, extra_len))
129 av_bprint_grow(buf, extra_len);
132 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
134 unsigned room, real_n;
137 room = av_bprint_room(buf);
140 if (av_bprint_alloc(buf, n))
144 real_n = FFMIN(n, room - 1);
145 memset(buf->str + buf->len, c, real_n);
147 av_bprint_grow(buf, n);
150 void av_bprint_clear(AVBPrint *buf)
158 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
160 unsigned real_size = FFMIN(buf->len + 1, buf->size);
165 if (av_bprint_is_allocated(buf)) {
166 str = av_realloc(buf->str, real_size);
171 str = av_malloc(real_size);
173 memcpy(str, buf->str, real_size);
175 ret = AVERROR(ENOMEM);
179 if (av_bprint_is_allocated(buf))
182 buf->size = real_size;
190 static void bprint_pascal(AVBPrint *b, unsigned size)
192 unsigned p[size + 1], i, j;
195 av_bprintf(b, "%8d\n", 1);
196 for (i = 1; i <= size; i++) {
198 for (j = i - 1; j > 0; j--)
199 p[j] = p[j] + p[j - 1];
200 for (j = 0; j <= i; j++)
201 av_bprintf(b, "%8d", p[j]);
211 av_bprint_init(&b, 0, -1);
212 bprint_pascal(&b, 5);
213 printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
214 printf("%s\n", b.str);
215 av_bprint_finalize(&b, NULL);
217 av_bprint_init(&b, 0, -1);
218 bprint_pascal(&b, 25);
219 printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
220 av_bprint_finalize(&b, NULL);
222 av_bprint_init(&b, 0, 2048);
223 bprint_pascal(&b, 25);
224 printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
225 av_bprint_finalize(&b, NULL);
227 av_bprint_init(&b, 0, 1);
228 bprint_pascal(&b, 5);
229 printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
231 av_bprint_init(&b, 0, 1);
232 bprint_pascal(&b, 25);
233 printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
234 /* Note that the size of the automatic buffer is arch-dependant. */
236 av_bprint_init(&b, 0, 0);
237 bprint_pascal(&b, 25);
238 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
240 av_bprint_init_for_buffer(&b, buf, sizeof(buf));
241 bprint_pascal(&b, 25);
242 printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);