]> 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 "bprint.h"
25 #include "common.h"
26 #include "error.h"
27 #include "mem.h"
28
29 #if defined(_WIN32)
30
31 static int vsnprintf_fixed(char *s, size_t n, const char *format, va_list va)
32 {
33     va_list va2;
34     int r;
35
36     va_copy(va2, va);
37     r = vsnprintf(s, n, format, va2);
38     va_end(va2);
39     if (r == -1)
40         r = _vscprintf(format, va);
41     return r;
42 }
43
44 #define vsnprintf vsnprintf_fixed
45
46 #endif
47
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)
50
51 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
52 {
53     char *old_str, *new_str;
54     unsigned min_size, new_size;
55
56     if (buf->size == buf->size_max)
57         return AVERROR(EIO);
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);
66     if (!new_str)
67         return AVERROR(ENOMEM);
68     if (!old_str)
69         memcpy(new_str, buf->str, buf->len + 1);
70     buf->str  = new_str;
71     buf->size = new_size;
72     return 0;
73 }
74
75 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
76 {
77     /* arbitrary margin to avoid small overflows */
78     extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
79     buf->len += extra_len;
80     if (buf->size)
81         buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
82 }
83
84 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
85 {
86     unsigned size_auto = (char *)buf + sizeof(*buf) -
87                          buf->reserved_internal_buffer;
88
89     if (size_max == 1)
90         size_max = size_auto;
91     buf->str      = buf->reserved_internal_buffer;
92     buf->len      = 0;
93     buf->size     = FFMIN(size_auto, size_max);
94     buf->size_max = size_max;
95     *buf->str = 0;
96     if (size_init > buf->size)
97         av_bprint_alloc(buf, size_init - 1);
98 }
99
100 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
101 {
102     buf->str      = buffer;
103     buf->len      = 0;
104     buf->size     = size;
105     buf->size_max = size;
106     *buf->str = 0;
107 }
108
109 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
110 {
111     unsigned room;
112     char *dst;
113     va_list vl;
114     int extra_len;
115
116     while (1) {
117         room = av_bprint_room(buf);
118         dst = room ? buf->str + buf->len : NULL;
119         va_start(vl, fmt);
120         extra_len = vsnprintf(dst, room, fmt, vl);
121         va_end(vl);
122         if (extra_len <= 0)
123             return;
124         if (extra_len < room)
125             break;
126         if (av_bprint_alloc(buf, extra_len))
127             break;
128     }
129     av_bprint_grow(buf, extra_len);
130 }
131
132 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
133 {
134     unsigned room, real_n;
135
136     while (1) {
137         room = av_bprint_room(buf);
138         if (n < room)
139             break;
140         if (av_bprint_alloc(buf, n))
141             break;
142     }
143     if (room) {
144         real_n = FFMIN(n, room - 1);
145         memset(buf->str + buf->len, c, real_n);
146     }
147     av_bprint_grow(buf, n);
148 }
149
150 void av_bprint_clear(AVBPrint *buf)
151 {
152     if (buf->len) {
153         *buf->str = 0;
154         buf->len  = 0;
155     }
156 }
157
158 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
159 {
160     unsigned real_size = FFMIN(buf->len + 1, buf->size);
161     char *str;
162     int ret = 0;
163
164     if (ret_str) {
165         if (av_bprint_is_allocated(buf)) {
166             str = av_realloc(buf->str, real_size);
167             if (!str)
168                 str = buf->str;
169             buf->str = NULL;
170         } else {
171             str = av_malloc(real_size);
172             if (str)
173                 memcpy(str, buf->str, real_size);
174             else
175                 ret = AVERROR(ENOMEM);
176         }
177         *ret_str = str;
178     } else {
179         if (av_bprint_is_allocated(buf))
180             av_freep(&buf->str);
181     }
182     buf->size = real_size;
183     return ret;
184 }
185
186 #ifdef TEST
187
188 #undef printf
189
190 static void bprint_pascal(AVBPrint *b, unsigned size)
191 {
192     unsigned p[size + 1], i, j;
193
194     p[0] = 1;
195     av_bprintf(b, "%8d\n", 1);
196     for (i = 1; i <= size; i++) {
197         p[i] = 1;
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]);
202         av_bprintf(b, "\n");
203     }
204 }
205
206 int main(void)
207 {
208     AVBPrint b;
209     char buf[256];
210
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);
216
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);
221
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);
226
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);
230
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. */
235
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);
239
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);
243
244     return 0;
245 }
246
247 #endif