]> git.sesse.net Git - ffmpeg/blob - libavutil/bprint.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavutil / bprint.c
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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "avassert.h"
25 #include "bprint.h"
26 #include "common.h"
27 #include "error.h"
28 #include "mem.h"
29
30 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
31 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
32
33 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
34 {
35     char *old_str, *new_str;
36     unsigned min_size, new_size;
37
38     if (buf->size == buf->size_max)
39         return AVERROR(EIO);
40     if (!av_bprint_is_complete(buf))
41         return AVERROR_INVALIDDATA; /* it is already truncated anyway */
42     min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
43     new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
44     if (new_size < min_size)
45         new_size = FFMIN(buf->size_max, min_size);
46     old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
47     new_str = av_realloc(old_str, new_size);
48     if (!new_str)
49         return AVERROR(ENOMEM);
50     if (!old_str)
51         memcpy(new_str, buf->str, buf->len + 1);
52     buf->str  = new_str;
53     buf->size = new_size;
54     return 0;
55 }
56
57 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
58 {
59     /* arbitrary margin to avoid small overflows */
60     extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
61     buf->len += extra_len;
62     if (buf->size)
63         buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
64 }
65
66 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
67 {
68     unsigned size_auto = (char *)buf + sizeof(*buf) -
69                          buf->reserved_internal_buffer;
70
71     if (size_max == 1)
72         size_max = size_auto;
73     buf->str      = buf->reserved_internal_buffer;
74     buf->len      = 0;
75     buf->size     = FFMIN(size_auto, size_max);
76     buf->size_max = size_max;
77     *buf->str = 0;
78     if (size_init > buf->size)
79         av_bprint_alloc(buf, size_init - 1);
80 }
81
82 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
83 {
84     buf->str      = buffer;
85     buf->len      = 0;
86     buf->size     = size;
87     buf->size_max = size;
88     *buf->str = 0;
89 }
90
91 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
92 {
93     unsigned room;
94     char *dst;
95     va_list vl;
96     int extra_len;
97
98     while (1) {
99         room = av_bprint_room(buf);
100         dst = room ? buf->str + buf->len : NULL;
101         va_start(vl, fmt);
102         extra_len = vsnprintf(dst, room, fmt, vl);
103         va_end(vl);
104         if (extra_len <= 0)
105             return;
106         if (extra_len < room)
107             break;
108         if (av_bprint_alloc(buf, extra_len))
109             break;
110     }
111     av_bprint_grow(buf, extra_len);
112 }
113
114 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
115 {
116     unsigned room, real_n;
117
118     while (1) {
119         room = av_bprint_room(buf);
120         if (n < room)
121             break;
122         if (av_bprint_alloc(buf, n))
123             break;
124     }
125     if (room) {
126         real_n = FFMIN(n, room - 1);
127         memset(buf->str + buf->len, c, real_n);
128     }
129     av_bprint_grow(buf, n);
130 }
131
132 void av_bprint_clear(AVBPrint *buf)
133 {
134     if (buf->len) {
135         *buf->str = 0;
136         buf->len  = 0;
137     }
138 }
139
140 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
141 {
142     unsigned real_size = FFMIN(buf->len + 1, buf->size);
143     char *str;
144     int ret = 0;
145
146     if (ret_str) {
147         if (av_bprint_is_allocated(buf)) {
148             str = av_realloc(buf->str, real_size);
149             if (!str)
150                 str = buf->str;
151             buf->str = NULL;
152         } else {
153             str = av_malloc(real_size);
154             if (str)
155                 memcpy(str, buf->str, real_size);
156             else
157                 ret = AVERROR(ENOMEM);
158         }
159         *ret_str = str;
160     } else {
161         if (av_bprint_is_allocated(buf))
162             av_freep(&buf->str);
163     }
164     buf->size = real_size;
165     return ret;
166 }
167
168 #ifdef TEST
169
170 #undef printf
171
172 static void bprint_pascal(AVBPrint *b, unsigned size)
173 {
174     unsigned i, j;
175     unsigned p[42];
176
177     av_assert0(size < FF_ARRAY_ELEMS(p));
178
179     p[0] = 1;
180     av_bprintf(b, "%8d\n", 1);
181     for (i = 1; i <= size; i++) {
182         p[i] = 1;
183         for (j = i - 1; j > 0; j--)
184             p[j] = p[j] + p[j - 1];
185         for (j = 0; j <= i; j++)
186             av_bprintf(b, "%8d", p[j]);
187         av_bprintf(b, "\n");
188     }
189 }
190
191 int main(void)
192 {
193     AVBPrint b;
194     char buf[256];
195
196     av_bprint_init(&b, 0, -1);
197     bprint_pascal(&b, 5);
198     printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
199     printf("%s\n", b.str);
200     av_bprint_finalize(&b, NULL);
201
202     av_bprint_init(&b, 0, -1);
203     bprint_pascal(&b, 25);
204     printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
205     av_bprint_finalize(&b, NULL);
206
207     av_bprint_init(&b, 0, 2048);
208     bprint_pascal(&b, 25);
209     printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
210     av_bprint_finalize(&b, NULL);
211
212     av_bprint_init(&b, 0, 1);
213     bprint_pascal(&b, 5);
214     printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
215
216     av_bprint_init(&b, 0, 1);
217     bprint_pascal(&b, 25);
218     printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
219     /* Note that the size of the automatic buffer is arch-dependant. */
220
221     av_bprint_init(&b, 0, 0);
222     bprint_pascal(&b, 25);
223     printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
224
225     av_bprint_init_for_buffer(&b, buf, sizeof(buf));
226     bprint_pascal(&b, 25);
227     printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
228
229     return 0;
230 }
231
232 #endif