FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/avstring.c
Date: 2026-10-10 04:53:52
Exec Total Coverage
Lines: 221 250 88.4%
Functions: 19 20 95.0%
Branches: 165 210 78.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3 * Copyright (c) 2007 Mans Rullgard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "config.h"
29 #include "mem.h"
30 #include "avassert.h"
31 #include "avstring.h"
32 #include "bprint.h"
33 #include "error.h"
34 #include "macros.h"
35
36 203651936 int av_strstart(const char *str, const char *pfx, const char **ptr)
37 {
38
4/4
✓ Branch 0 taken 221465916 times.
✓ Branch 1 taken 1398396 times.
✓ Branch 2 taken 19212376 times.
✓ Branch 3 taken 202253540 times.
222864312 while (*pfx && *pfx == *str) {
39 19212376 pfx++;
40 19212376 str++;
41 }
42
4/4
✓ Branch 0 taken 1398396 times.
✓ Branch 1 taken 202253540 times.
✓ Branch 2 taken 1397383 times.
✓ Branch 3 taken 1013 times.
203651936 if (!*pfx && ptr)
43 1397383 *ptr = str;
44 203651936 return !*pfx;
45 }
46
47 14395 int av_stristart(const char *str, const char *pfx, const char **ptr)
48 {
49
4/4
✓ Branch 0 taken 15782 times.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 1692 times.
✓ Branch 3 taken 14090 times.
16087 while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
50 1692 pfx++;
51 1692 str++;
52 }
53
4/4
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 14090 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 301 times.
14395 if (!*pfx && ptr)
54 4 *ptr = str;
55 14395 return !*pfx;
56 }
57
58 841 char *av_stristr(const char *s1, const char *s2)
59 {
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 841 times.
841 if (!*s2)
61 ✗ return (char*)(intptr_t)s1;
62
63 do
64
2/2
✓ Branch 1 taken 301 times.
✓ Branch 2 taken 14075 times.
14376 if (av_stristart(s1, s2, NULL))
65 301 return (char*)(intptr_t)s1;
66
2/2
✓ Branch 0 taken 13535 times.
✓ Branch 1 taken 540 times.
14075 while (*s1++);
67
68 540 return NULL;
69 }
70
71 8 char *av_strnstr(const char *haystack, const char *needle, size_t hay_length)
72 {
73 8 size_t needle_len = strlen(needle);
74
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (!needle_len)
75 1 return (char*)haystack;
76
2/2
✓ Branch 0 taken 353 times.
✓ Branch 1 taken 1 times.
354 while (hay_length >= needle_len) {
77 353 hay_length--;
78
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 347 times.
353 if (!memcmp(haystack, needle, needle_len))
79 6 return (char*)haystack;
80 347 haystack++;
81 }
82 1 return NULL;
83 }
84
85 403960 size_t av_strlcpy(char *dst, const char *src, size_t size)
86 {
87 403960 size_t len = 0;
88
4/4
✓ Branch 0 taken 2499417 times.
✓ Branch 1 taken 135465 times.
✓ Branch 2 taken 2230922 times.
✓ Branch 3 taken 268495 times.
2634882 while (++len < size && *src)
89 2230922 *dst++ = *src++;
90
1/2
✓ Branch 0 taken 403960 times.
✗ Branch 1 not taken.
403960 if (len <= size)
91 403960 *dst = 0;
92 403960 return len + strlen(src) - 1;
93 }
94
95 67417 size_t av_strlcat(char *dst, const char *src, size_t size)
96 {
97 67417 size_t len = strlen(dst);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67417 times.
67417 if (size <= len + 1)
99 ✗ return len + strlen(src);
100 67417 return len + av_strlcpy(dst + len, src, size - len);
101 }
102
103 162461 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
104 {
105 162461 size_t len = strlen(dst);
106 va_list vl;
107
108 162461 va_start(vl, fmt);
109
1/2
✓ Branch 0 taken 162461 times.
✗ Branch 1 not taken.
162461 len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
110 162461 va_end(vl);
111
112 162461 return len;
113 }
114
115 18110 char *av_asprintf(const char *fmt, ...)
116 {
117 18110 char *p = NULL;
118 va_list va;
119 int len;
120
121 18110 va_start(va, fmt);
122 18110 len = vsnprintf(NULL, 0, fmt, va);
123 18110 va_end(va);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18110 times.
18110 if (len < 0)
125 ✗ goto end;
126
127 18110 p = av_malloc(len + 1);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18110 times.
18110 if (!p)
129 ✗ goto end;
130
131 18110 va_start(va, fmt);
132 18110 len = vsnprintf(p, len + 1, fmt, va);
133 18110 va_end(va);
134
1/2
✓ Branch 0 taken 18110 times.
✗ Branch 1 not taken.
18110 if (len < 0)
135 ✗ av_freep(&p);
136
137 18110 end:
138 18110 return p;
139 }
140
141 #define WHITESPACES " \n\t\r"
142
143 129495 char *av_get_token(const char **buf, const char *term)
144 {
145 129495 char *out = av_realloc(NULL, strlen(*buf) + 1);
146 129495 char *ret = out, *end = out;
147 129495 const char *p = *buf;
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129495 times.
129495 if (!out)
149 ✗ return NULL;
150 129495 p += strspn(p, WHITESPACES);
151
152
4/4
✓ Branch 0 taken 1318850 times.
✓ Branch 1 taken 47507 times.
✓ Branch 2 taken 1236862 times.
✓ Branch 3 taken 81988 times.
1366357 while (*p && !strspn(p, term)) {
153 1236862 char c = *p++;
154
4/4
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 1236636 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 2 times.
1236862 if (c == '\\' && *p) {
155 224 *out++ = *p++;
156 224 end = out;
157
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1236587 times.
1236638 } else if (c == '\'') {
158
4/4
✓ Branch 0 taken 920 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 873 times.
✓ Branch 3 taken 47 times.
924 while (*p && *p != '\'')
159 873 *out++ = *p++;
160
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 4 times.
51 if (*p) {
161 47 p++;
162 47 end = out;
163 }
164 } else {
165 1236587 *out++ = c;
166 }
167 }
168
169 do
170 129969 *out-- = 0;
171
4/4
✓ Branch 0 taken 128070 times.
✓ Branch 1 taken 1899 times.
✓ Branch 2 taken 474 times.
✓ Branch 3 taken 127596 times.
129969 while (out >= end && strspn(out, WHITESPACES));
172
173 129495 *buf = p;
174
175 129495 char *small_ret = av_realloc(ret, out - ret + 2);
176
1/2
✓ Branch 0 taken 129495 times.
✗ Branch 1 not taken.
129495 return small_ret ? small_ret : ret;
177 }
178
179 10804 char *av_strtok(char *s, const char *delim, char **saveptr)
180 {
181 char *tok;
182
183
4/4
✓ Branch 0 taken 9025 times.
✓ Branch 1 taken 1779 times.
✓ Branch 2 taken 505 times.
✓ Branch 3 taken 8520 times.
10804 if (!s && !(s = *saveptr))
184 505 return NULL;
185
186 /* skip leading delimiters */
187 10299 s += strspn(s, delim);
188
189 /* s now points to the first non delimiter char, or to the end of the string */
190
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 10270 times.
10299 if (!*s) {
191 29 *saveptr = NULL;
192 29 return NULL;
193 }
194 10270 tok = s++;
195
196 /* skip non delimiters */
197 10270 s += strcspn(s, delim);
198
2/2
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 1420 times.
10270 if (*s) {
199 8850 *s = 0;
200 8850 *saveptr = s+1;
201 } else {
202 1420 *saveptr = NULL;
203 }
204
205 10270 return tok;
206 }
207
208 697168 int av_strcasecmp(const char *a, const char *b)
209 {
210 uint8_t c1, c2;
211 do {
212 774255 c1 = av_tolower(*a++);
213 774255 c2 = av_tolower(*b++);
214
4/4
✓ Branch 0 taken 768660 times.
✓ Branch 1 taken 5595 times.
✓ Branch 2 taken 77087 times.
✓ Branch 3 taken 691573 times.
774255 } while (c1 && c1 == c2);
215 697168 return c1 - c2;
216 }
217
218 3888445 int av_strncasecmp(const char *a, const char *b, size_t n)
219 {
220 uint8_t c1, c2;
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3888445 times.
3888445 if (n <= 0)
222 ✗ return 0;
223 do {
224 4248747 c1 = av_tolower(*a++);
225 4248747 c2 = av_tolower(*b++);
226
6/6
✓ Branch 0 taken 4204267 times.
✓ Branch 1 taken 44480 times.
✓ Branch 2 taken 4202145 times.
✓ Branch 3 taken 2122 times.
✓ Branch 4 taken 360302 times.
✓ Branch 5 taken 3841843 times.
4248747 } while (--n && c1 && c1 == c2);
227 3888445 return c1 - c2;
228 }
229
230 6 char *av_strireplace(const char *str, const char *from, const char *to)
231 {
232 6 char *ret = NULL;
233 6 const char *pstr2, *pstr = str;
234 6 size_t tolen = strlen(to), fromlen = strlen(from);
235 AVBPrint pbuf;
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!fromlen)
238 ✗ return av_strdup(str);
239
240 6 av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
241
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
12 while ((pstr2 = av_stristr(pstr, from))) {
242 6 av_bprint_append_data(&pbuf, pstr, pstr2 - pstr);
243 6 pstr = pstr2 + fromlen;
244 6 av_bprint_append_data(&pbuf, to, tolen);
245 }
246 6 av_bprint_append_data(&pbuf, pstr, strlen(pstr));
247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!av_bprint_is_complete(&pbuf)) {
248 ✗ av_bprint_finalize(&pbuf, NULL);
249 } else {
250 6 av_bprint_finalize(&pbuf, &ret);
251 }
252
253 6 return ret;
254 }
255
256 196 const char *av_basename(const char *path)
257 {
258
2/4
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 196 times.
196 if (!path || *path == '\0')
259 ✗ return ".";
260
261 196 const char *p = strrchr(path, '/');
262 #if HAVE_DOS_PATHS
263 const char *q = strrchr(path, '\\');
264 const char *d = strchr(path, ':');
265 p = FFMAX3(p, q, d);
266 #endif
267
268
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 190 times.
196 if (!p)
269 6 return path;
270
271 190 return p + 1;
272 }
273
274 55 const char *av_dirname(char *path)
275 {
276
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 char *p = path ? strrchr(path, '/') : NULL;
277
278 #if HAVE_DOS_PATHS
279 char *q = path ? strrchr(path, '\\') : NULL;
280 char *d = path ? strchr(path, ':') : NULL;
281
282 d = d ? d + 1 : d;
283
284 p = FFMAX3(p, q, d);
285 #endif
286
287
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 51 times.
55 if (!p)
288 4 return ".";
289
290 51 *p = '\0';
291
292 51 return path;
293 }
294
295 9 char *av_append_path_component(const char *path, const char *component)
296 {
297 size_t p_len, c_len;
298 char *fullpath;
299
300
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!path)
301 2 return av_strdup(component);
302
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (!component)
303 1 return av_strdup(path);
304
305 6 p_len = strlen(path);
306 6 c_len = strlen(component);
307
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (p_len > SIZE_MAX - c_len || p_len + c_len > SIZE_MAX - 2)
308 ✗ return NULL;
309 6 fullpath = av_malloc(p_len + c_len + 2);
310
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (fullpath) {
311
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (p_len) {
312 6 av_strlcpy(fullpath, path, p_len + 1);
313
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (c_len) {
314
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
6 if (fullpath[p_len - 1] != '/' && component[0] != '/')
315 2 fullpath[p_len++] = '/';
316
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
4 else if (fullpath[p_len - 1] == '/' && component[0] == '/')
317 2 p_len--;
318 }
319 }
320 6 av_strlcpy(&fullpath[p_len], component, c_len + 1);
321 6 fullpath[p_len + c_len] = 0;
322 }
323 6 return fullpath;
324 }
325
326 ✗ int av_escape(char **dst, const char *src, const char *special_chars,
327 enum AVEscapeMode mode, int flags)
328 {
329 AVBPrint dstbuf;
330 int ret;
331
332 ✗ av_bprint_init(&dstbuf, 1, INT_MAX); /* (int)dstbuf.len must be >= 0 */
333 ✗ av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
334
335 ✗ if (!av_bprint_is_complete(&dstbuf)) {
336 ✗ av_bprint_finalize(&dstbuf, NULL);
337 ✗ return AVERROR(ENOMEM);
338 }
339 ✗ if ((ret = av_bprint_finalize(&dstbuf, dst)) < 0)
340 ✗ return ret;
341 ✗ return dstbuf.len;
342 }
343
344 9088941 int av_match_name(const char *name, const char *names)
345 {
346 const char *p;
347 size_t len, namelen;
348
349
4/4
✓ Branch 0 taken 6111942 times.
✓ Branch 1 taken 2976999 times.
✓ Branch 2 taken 2714248 times.
✓ Branch 3 taken 3397694 times.
9088941 if (!name || !names)
350 5691247 return 0;
351
352 3397694 namelen = strlen(name);
353
2/2
✓ Branch 0 taken 3869924 times.
✓ Branch 1 taken 3379931 times.
7249855 while (*names) {
354 3869924 int negate = '-' == *names;
355 3869924 p = strchr(names, ',');
356
2/2
✓ Branch 0 taken 3395784 times.
✓ Branch 1 taken 474140 times.
3869924 if (!p)
357 3395784 p = names + strlen(names);
358 3869924 names += negate;
359 3869924 len = FFMAX(p - names, namelen);
360
3/4
✓ Branch 1 taken 3852161 times.
✓ Branch 2 taken 17763 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3852161 times.
3869924 if (!av_strncasecmp(name, names, len) || !strncmp("ALL", names, FFMAX(3, p - names)))
361 17763 return !negate;
362 3852161 names = p + (*p == ',');
363 }
364 3379931 return 0;
365 }
366
367 259329 int av_utf8_decode(int32_t *codep, const uint8_t **bufp, const uint8_t *buf_end,
368 unsigned int flags)
369 {
370 259329 const uint8_t *p = *bufp;
371 uint32_t top;
372 uint64_t code;
373 259329 int ret = 0, tail_len;
374 259329 uint32_t overlong_encoding_mins[6] = {
375 0x00000000, 0x00000080, 0x00000800, 0x00010000, 0x00200000, 0x04000000,
376 };
377
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259329 times.
259329 if (p >= buf_end)
379 ✗ return 0;
380
381 259329 code = *p++;
382
383 /* first sequence byte starts with 10, or is 1111-1110 or 1111-1111,
384 which is not admitted */
385
4/4
✓ Branch 0 taken 259327 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 259326 times.
259329 if ((code & 0xc0) == 0x80 || code >= 0xFE) {
386 3 ret = AVERROR(EILSEQ);
387 3 goto end;
388 }
389 259326 top = (code & 128) >> 1;
390
391 259326 tail_len = 0;
392
2/2
✓ Branch 0 taken 791 times.
✓ Branch 1 taken 259324 times.
260115 while (code & top) {
393 int tmp;
394 791 tail_len++;
395
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 790 times.
791 if (p >= buf_end) {
396 1 (*bufp) ++;
397 1 return AVERROR(EILSEQ); /* incomplete sequence */
398 }
399
400 /* we assume the byte to be in the form 10xx-xxxx */
401 790 tmp = *p++ - 128; /* strip leading 1 */
402
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 789 times.
790 if (tmp>>6) {
403 1 (*bufp) ++;
404 1 return AVERROR(EILSEQ);
405 }
406 789 code = (code<<6) + tmp;
407 789 top <<= 5;
408 }
409 259324 code &= (top << 1) - 1;
410
411 /* check for overlong encodings */
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259324 times.
259324 av_assert0(tail_len <= 5);
413
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 259322 times.
259324 if (code < overlong_encoding_mins[tail_len]) {
414 2 ret = AVERROR(EILSEQ);
415 2 goto end;
416 }
417
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259322 times.
259322 if (code >= 1U<<31) {
419 ✗ ret = AVERROR(EILSEQ); /* out-of-range value */
420 ✗ goto end;
421 }
422
423 259322 *codep = code;
424
425
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 259320 times.
259322 if (code > 0x10FFFF &&
426
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(flags & AV_UTF8_FLAG_ACCEPT_INVALID_BIG_CODES))
427 2 ret = AVERROR(EILSEQ);
428
6/8
✓ Branch 0 taken 2017 times.
✓ Branch 1 taken 257305 times.
✓ Branch 2 taken 2017 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 91 times.
✓ Branch 5 taken 1926 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 91 times.
259322 if (code < 0x20 && code != 0x9 && code != 0xA && code != 0xD &&
429 ✗ flags & AV_UTF8_FLAG_EXCLUDE_XML_INVALID_CONTROL_CODES)
430 ✗ ret = AVERROR(EILSEQ);
431
4/4
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 259008 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 313 times.
259322 if (code >= 0xD800 && code <= 0xDFFF &&
432
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 !(flags & AV_UTF8_FLAG_ACCEPT_SURROGATES))
433 1 ret = AVERROR(EILSEQ);
434
3/4
✓ Branch 0 taken 259322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 259321 times.
✓ Branch 3 taken 1 times.
259322 if ((code == 0xFFFE || code == 0xFFFF) &&
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 !(flags & AV_UTF8_FLAG_ACCEPT_NON_CHARACTERS))
436 1 ret = AVERROR(EILSEQ);
437
438 259321 end:
439 259327 *bufp = p;
440 259327 return ret;
441 }
442
443 206 int av_match_list(const char *name, const char *list, char separator)
444 {
445 const char *p, *q;
446
447
2/4
✓ Branch 0 taken 206 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 206 times.
✗ Branch 3 not taken.
412 for (p = name; p && *p; ) {
448
2/4
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 212 times.
✗ Branch 3 not taken.
418 for (q = list; q && *q; ) {
449 int k;
450
5/6
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 212 times.
✓ Branch 2 taken 206 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 206 times.
✗ Branch 5 not taken.
1036 for (k = 0; p[k] == q[k] || (p[k]*q[k] == 0 && p[k]+q[k] == separator); k++)
451
5/6
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 206 times.
✓ Branch 2 taken 618 times.
✓ Branch 3 taken 206 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 618 times.
1030 if (k && (!p[k] || p[k] == separator))
452 206 return 1;
453 6 q = strchr(q, separator);
454
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if(q)
455 6 q++;
456 }
457 ✗ p = strchr(p, separator);
458 ✗ if (p)
459 ✗ p++;
460 }
461
462 ✗ return 0;
463 }
464