FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bprint.c
Go to the documentation of this file.
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 <time.h>
25 #include "avassert.h"
26 #include "bprint.h"
27 #include "common.h"
28 #include "error.h"
29 #include "mem.h"
30 
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)
33 
34 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
35 {
36  char *old_str, *new_str;
37  unsigned min_size, new_size;
38 
39  if (buf->size == buf->size_max)
40  return AVERROR(EIO);
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);
49  if (!new_str)
50  return AVERROR(ENOMEM);
51  if (!old_str)
52  memcpy(new_str, buf->str, buf->len + 1);
53  buf->str = new_str;
54  buf->size = new_size;
55  return 0;
56 }
57 
58 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
59 {
60  /* arbitrary margin to avoid small overflows */
61  extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
62  buf->len += extra_len;
63  if (buf->size)
64  buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
65 }
66 
67 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
68 {
69  unsigned size_auto = (char *)buf + sizeof(*buf) -
70  buf->reserved_internal_buffer;
71 
72  if (size_max == 1)
73  size_max = size_auto;
74  buf->str = buf->reserved_internal_buffer;
75  buf->len = 0;
76  buf->size = FFMIN(size_auto, size_max);
77  buf->size_max = size_max;
78  *buf->str = 0;
79  if (size_init > buf->size)
80  av_bprint_alloc(buf, size_init - 1);
81 }
82 
83 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
84 {
85  buf->str = buffer;
86  buf->len = 0;
87  buf->size = size;
88  buf->size_max = size;
89  *buf->str = 0;
90 }
91 
92 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
93 {
94  unsigned room;
95  char *dst;
96  va_list vl;
97  int extra_len;
98 
99  while (1) {
100  room = av_bprint_room(buf);
101  dst = room ? buf->str + buf->len : NULL;
102  va_start(vl, fmt);
103  extra_len = vsnprintf(dst, room, fmt, vl);
104  va_end(vl);
105  if (extra_len <= 0)
106  return;
107  if (extra_len < room)
108  break;
109  if (av_bprint_alloc(buf, extra_len))
110  break;
111  }
112  av_bprint_grow(buf, extra_len);
113 }
114 
115 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
116 {
117  unsigned room, real_n;
118 
119  while (1) {
120  room = av_bprint_room(buf);
121  if (n < room)
122  break;
123  if (av_bprint_alloc(buf, n))
124  break;
125  }
126  if (room) {
127  real_n = FFMIN(n, room - 1);
128  memset(buf->str + buf->len, c, real_n);
129  }
130  av_bprint_grow(buf, n);
131 }
132 
133 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
134 {
135  unsigned room;
136  size_t l;
137 
138  if (!*fmt)
139  return;
140  while (1) {
141  room = av_bprint_room(buf);
142  if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
143  break;
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);
151  if (room < 1024) {
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 */
155  char buf2[1024];
156  if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
157  av_bprintf(buf, "%s", buf2);
158  return;
159  }
160  }
161  if (room) {
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 */
168  }
169  return;
170  }
171  }
172  av_bprint_grow(buf, l);
173 }
174 
175 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
176  unsigned char **mem, unsigned *actual_size)
177 {
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;
182 }
183 
185 {
186  if (buf->len) {
187  *buf->str = 0;
188  buf->len = 0;
189  }
190 }
191 
193 {
194  unsigned real_size = FFMIN(buf->len + 1, buf->size);
195  char *str;
196  int ret = 0;
197 
198  if (ret_str) {
199  if (av_bprint_is_allocated(buf)) {
200  str = av_realloc(buf->str, real_size);
201  if (!str)
202  str = buf->str;
203  buf->str = NULL;
204  } else {
205  str = av_malloc(real_size);
206  if (str)
207  memcpy(str, buf->str, real_size);
208  else
209  ret = AVERROR(ENOMEM);
210  }
211  *ret_str = str;
212  } else {
213  if (av_bprint_is_allocated(buf))
214  av_freep(&buf->str);
215  }
216  buf->size = real_size;
217  return ret;
218 }
219 
220 #ifdef TEST
221 
222 #undef printf
223 
224 static void bprint_pascal(AVBPrint *b, unsigned size)
225 {
226  unsigned i, j;
227  unsigned p[42];
228 
229  av_assert0(size < FF_ARRAY_ELEMS(p));
230 
231  p[0] = 1;
232  av_bprintf(b, "%8d\n", 1);
233  for (i = 1; i <= size; i++) {
234  p[i] = 1;
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]);
239  av_bprintf(b, "\n");
240  }
241 }
242 
243 int main(void)
244 {
245  AVBPrint b;
246  char buf[256];
247  struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
248 
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);
254 
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);
259 
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);
264 
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);
268 
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. */
273 
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);
277 
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);
281 
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);
286 
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);
290 
291  return 0;
292 }
293 
294 #endif