FFmpeg  4.4.9
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 "avstring.h"
27 #include "bprint.h"
28 #include "common.h"
29 #include "compat/va_copy.h"
30 #include "error.h"
31 #include "mem.h"
32 
33 #define av_bprint_room(buf) ((buf)->size - FFMIN((buf)->len, (buf)->size))
34 #define av_bprint_is_allocated(buf) ((buf)->str != (buf)->reserved_internal_buffer)
35 
36 static int av_bprint_alloc(AVBPrint *buf, unsigned room)
37 {
38  char *old_str, *new_str;
39  unsigned min_size, new_size;
40 
41  if (buf->size == buf->size_max)
42  return AVERROR(EIO);
43  if (!av_bprint_is_complete(buf))
44  return AVERROR_INVALIDDATA; /* it is already truncated anyway */
45  min_size = buf->len + 1 + FFMIN(UINT_MAX - buf->len - 1, room);
46  new_size = buf->size > buf->size_max / 2 ? buf->size_max : buf->size * 2;
47  if (new_size < min_size)
48  new_size = FFMIN(buf->size_max, min_size);
49  old_str = av_bprint_is_allocated(buf) ? buf->str : NULL;
50  new_str = av_realloc(old_str, new_size);
51  if (!new_str)
52  return AVERROR(ENOMEM);
53  if (!old_str)
54  memcpy(new_str, buf->str, buf->len + 1);
55  buf->str = new_str;
56  buf->size = new_size;
57  return 0;
58 }
59 
60 static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
61 {
62  /* arbitrary margin to avoid small overflows */
63  extra_len = FFMIN(extra_len, UINT_MAX - 5 - buf->len);
64  buf->len += extra_len;
65  if (buf->size)
66  buf->str[FFMIN(buf->len, buf->size - 1)] = 0;
67 }
68 
69 void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
70 {
71  unsigned size_auto = (char *)buf + sizeof(*buf) -
72  buf->reserved_internal_buffer;
73 
74  if (size_max == 1)
75  size_max = size_auto;
76  buf->str = buf->reserved_internal_buffer;
77  buf->len = 0;
78  buf->size = FFMIN(size_auto, size_max);
79  buf->size_max = size_max;
80  *buf->str = 0;
81  if (size_init > buf->size)
82  av_bprint_alloc(buf, size_init - 1);
83 }
84 
85 void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
86 {
87  buf->str = buffer;
88  buf->len = 0;
89  buf->size = size;
90  buf->size_max = size;
91  *buf->str = 0;
92 }
93 
94 void av_bprintf(AVBPrint *buf, const char *fmt, ...)
95 {
96  unsigned room;
97  char *dst;
98  va_list vl;
99  int extra_len;
100 
101  while (1) {
102  room = av_bprint_room(buf);
103  dst = room ? buf->str + buf->len : NULL;
104  va_start(vl, fmt);
105  extra_len = vsnprintf(dst, room, fmt, vl);
106  va_end(vl);
107  if (extra_len <= 0)
108  return;
109  if (extra_len < room)
110  break;
111  if (av_bprint_alloc(buf, extra_len))
112  break;
113  }
114  av_bprint_grow(buf, extra_len);
115 }
116 
117 void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
118 {
119  unsigned room;
120  char *dst;
121  int extra_len;
122  va_list vl;
123 
124  while (1) {
125  room = av_bprint_room(buf);
126  dst = room ? buf->str + buf->len : NULL;
127  va_copy(vl, vl_arg);
128  extra_len = vsnprintf(dst, room, fmt, vl);
129  va_end(vl);
130  if (extra_len <= 0)
131  return;
132  if (extra_len < room)
133  break;
134  if (av_bprint_alloc(buf, extra_len))
135  break;
136  }
137  av_bprint_grow(buf, extra_len);
138 }
139 
140 void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
141 {
142  unsigned room, real_n;
143 
144  while (1) {
145  room = av_bprint_room(buf);
146  if (n < room)
147  break;
148  if (av_bprint_alloc(buf, n))
149  break;
150  }
151  if (room) {
152  real_n = FFMIN(n, room - 1);
153  memset(buf->str + buf->len, c, real_n);
154  }
155  av_bprint_grow(buf, n);
156 }
157 
158 void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
159 {
160  unsigned room, real_n;
161 
162  while (1) {
163  room = av_bprint_room(buf);
164  if (size < room)
165  break;
166  if (av_bprint_alloc(buf, size))
167  break;
168  }
169  if (room) {
170  real_n = FFMIN(size, room - 1);
171  memcpy(buf->str + buf->len, data, real_n);
172  }
173  av_bprint_grow(buf, size);
174 }
175 
176 void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
177 {
178  unsigned room;
179  size_t l;
180  size_t fmt_len = strlen(fmt);
181 
182  if (!*fmt)
183  return;
184  while (1) {
185  room = av_bprint_room(buf);
186  if (room && (l = strftime(buf->str + buf->len, room, fmt, tm)))
187  break;
188 
189  /* Due to the limitations of strftime() it is not possible to know if
190  * the output buffer is too small or the output is empty.
191  * However, a 256x output space requirement compared to the format
192  * string length is so unlikely we can safely assume empty output. This
193  * allows supporting possibly empty format strings like "%p". */
194  if (room >> 8 > fmt_len)
195  break;
196 
197  /* strftime does not tell us how much room it would need: let us
198  retry with twice as much until the buffer is large enough */
199  room = !room ? fmt_len + 1 :
200  room <= INT_MAX / 2 ? room * 2 : INT_MAX;
201  if (av_bprint_alloc(buf, room)) {
202  /* impossible to grow, try to manage something useful anyway */
203  room = av_bprint_room(buf);
204  if (room < 1024) {
205  /* if strftime fails because the buffer has (almost) reached
206  its maximum size, let us try in a local buffer; 1k should
207  be enough to format any real date+time string */
208  char buf2[1024];
209  if ((l = strftime(buf2, sizeof(buf2), fmt, tm))) {
210  av_bprintf(buf, "%s", buf2);
211  return;
212  }
213  }
214  if (room) {
215  /* if anything else failed and the buffer is not already
216  truncated, let us add a stock string and force truncation */
217  static const char txt[] = "[truncated strftime output]";
218  memset(buf->str + buf->len, '!', room);
219  memcpy(buf->str + buf->len, txt, FFMIN(sizeof(txt) - 1, room));
220  av_bprint_grow(buf, room); /* force truncation */
221  }
222  return;
223  }
224  }
225  av_bprint_grow(buf, l);
226 }
227 
228 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
229  unsigned char **mem, unsigned *actual_size)
230 {
231  if (size > av_bprint_room(buf))
232  av_bprint_alloc(buf, size);
233  *actual_size = av_bprint_room(buf);
234  *mem = *actual_size ? buf->str + buf->len : NULL;
235 }
236 
237 void av_bprint_clear(AVBPrint *buf)
238 {
239  if (buf->len) {
240  *buf->str = 0;
241  buf->len = 0;
242  }
243 }
244 
245 int av_bprint_finalize(AVBPrint *buf, char **ret_str)
246 {
247  unsigned real_size = FFMIN(buf->len + 1, buf->size);
248  char *str;
249  int ret = 0;
250 
251  if (ret_str) {
252  if (av_bprint_is_allocated(buf)) {
253  str = av_realloc(buf->str, real_size);
254  if (!str)
255  str = buf->str;
256  buf->str = NULL;
257  } else {
258  str = av_malloc(real_size);
259  if (str)
260  memcpy(str, buf->str, real_size);
261  else
262  ret = AVERROR(ENOMEM);
263  }
264  *ret_str = str;
265  } else {
266  if (av_bprint_is_allocated(buf))
267  av_freep(&buf->str);
268  }
269  buf->size = real_size;
270  return ret;
271 }
272 
273 #define WHITESPACES " \n\t\r"
274 
275 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
276  enum AVEscapeMode mode, int flags)
277 {
278  const char *src0 = src;
279 
280  if (mode == AV_ESCAPE_MODE_AUTO)
281  mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
282 
283  switch (mode) {
285  /* enclose the string between '' */
286  av_bprint_chars(dstbuf, '\'', 1);
287  for (; *src; src++) {
288  if (*src == '\'')
289  av_bprintf(dstbuf, "'\\''");
290  else
291  av_bprint_chars(dstbuf, *src, 1);
292  }
293  av_bprint_chars(dstbuf, '\'', 1);
294  break;
295 
296  case AV_ESCAPE_MODE_XML:
297  /* escape XML non-markup character data as per 2.4 by default: */
298  /* [^<&]* - ([^<&]* ']]>' [^<&]*) */
299 
300  /* additionally, given one of the AV_ESCAPE_FLAG_XML_* flags, */
301  /* escape those specific characters as required. */
302  for (; *src; src++) {
303  switch (*src) {
304  case '&' : av_bprintf(dstbuf, "%s", "&amp;"); break;
305  case '<' : av_bprintf(dstbuf, "%s", "&lt;"); break;
306  case '>' : av_bprintf(dstbuf, "%s", "&gt;"); break;
307  case '\'':
309  goto XML_DEFAULT_HANDLING;
310 
311  av_bprintf(dstbuf, "%s", "&apos;");
312  break;
313  case '"' :
315  goto XML_DEFAULT_HANDLING;
316 
317  av_bprintf(dstbuf, "%s", "&quot;");
318  break;
319 XML_DEFAULT_HANDLING:
320  default: av_bprint_chars(dstbuf, *src, 1);
321  }
322  }
323  break;
324 
325  /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
326  default:
327  /* \-escape characters */
328  for (; *src; src++) {
329  int is_first_last = src == src0 || !*(src+1);
330  int is_ws = !!strchr(WHITESPACES, *src);
331  int is_strictly_special = special_chars && strchr(special_chars, *src);
332  int is_special =
333  is_strictly_special || strchr("'\\", *src) ||
334  (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
335 
336  if (is_strictly_special ||
338  (is_special || (is_ws && is_first_last))))
339  av_bprint_chars(dstbuf, '\\', 1);
340  av_bprint_chars(dstbuf, *src, 1);
341  }
342  break;
343  }
344 }
simple assert() macros that are a bit more flexible than ISO C assert().
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape the content in src and append it to dstbuf.
Definition: bprint.c:275
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
void av_bprint_get_buffer(AVBPrint *buf, unsigned size, unsigned char **mem, unsigned *actual_size)
Allocate bytes in the buffer for external use.
Definition: bprint.c:228
#define av_bprint_is_allocated(buf)
Definition: bprint.c:34
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:245
void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
Append a formatted date and time to a print buffer.
Definition: bprint.c:176
#define av_bprint_room(buf)
Definition: bprint.c:33
static int av_bprint_alloc(AVBPrint *buf, unsigned room)
Definition: bprint.c:36
static void av_bprint_grow(AVBPrint *buf, unsigned extra_len)
Definition: bprint.c:60
void av_vbprintf(AVBPrint *buf, const char *fmt, va_list vl_arg)
Append a formatted string to a print buffer.
Definition: bprint.c:117
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:158
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:237
#define WHITESPACES
Definition: bprint.c:273
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
Definition: bprint.c:85
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
unsigned size_init
Definition: bprint.h:111
unsigned unsigned size_max
Definition: bprint.h:111
#define flags(name, subs,...)
Definition: cbs_av1.c:572
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
error code definitions
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
#define AV_ESCAPE_FLAG_STRICT
Escape only specified special characters.
Definition: avstring.h:345
#define AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES
Within AV_ESCAPE_MODE_XML, additionally escape double quotes for double quoted attributes.
Definition: avstring.h:357
#define AV_ESCAPE_FLAG_WHITESPACE
Consider spaces special and escape them even in the middle of the string.
Definition: avstring.h:338
#define AV_ESCAPE_FLAG_XML_SINGLE_QUOTES
Within AV_ESCAPE_MODE_XML, additionally escape single quotes for single quoted attributes.
Definition: avstring.h:351
AVEscapeMode
Definition: avstring.h:323
@ AV_ESCAPE_MODE_AUTO
Use auto-selected escaping mode.
Definition: avstring.h:324
@ AV_ESCAPE_MODE_XML
Use XML non-markup character data escaping.
Definition: avstring.h:327
@ AV_ESCAPE_MODE_QUOTE
Use single-quote escaping.
Definition: avstring.h:326
@ AV_ESCAPE_MODE_BACKSLASH
Use backslash escaping.
Definition: avstring.h:325
Memory handling functions.
const char data[16]
Definition: mxf.c:142
static const char * ret_str(int v)
Definition: seek.c:34
static char buffer[20]
Definition: seek.c:32
#define vsnprintf
Definition: snprintf.h:36
#define av_freep(p)
#define av_malloc(s)
#define src0
Definition: h264pred.c:139
#define src
Definition: vp8dsp.c:255
int size
#define va_copy(dst, src)
Definition: va_copy.h:31
static double c[64]