Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <string.h> |
19 | | #include <assert.h> |
20 | | #include <stdarg.h> |
21 | | #include <stdint.h> |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include "mpv_talloc.h" |
25 | | |
26 | | #include "common/common.h" |
27 | | #include "misc/ctype.h" |
28 | | #include "bstr.h" |
29 | | |
30 | | int bstrcmp(struct bstr str1, struct bstr str2) |
31 | 559M | { |
32 | 559M | int ret = 0; |
33 | 559M | if (str1.len && str2.len) |
34 | 557M | ret = memcmp(str1.start, str2.start, MPMIN(str1.len, str2.len)); |
35 | | |
36 | 559M | if (!ret) { |
37 | 94.0M | if (str1.len == str2.len) |
38 | 72.5M | return 0; |
39 | 21.5M | else if (str1.len > str2.len) |
40 | 2.19M | return 1; |
41 | 19.3M | else |
42 | 19.3M | return -1; |
43 | 94.0M | } |
44 | 465M | return ret; |
45 | 559M | } |
46 | | |
47 | | int bstrcasecmp(struct bstr str1, struct bstr str2) |
48 | 16.8M | { |
49 | 16.8M | int ret = 0; |
50 | 16.8M | if (str1.len && str2.len) |
51 | 14.0M | ret = strncasecmp(str1.start, str2.start, MPMIN(str1.len, str2.len)); |
52 | | |
53 | 16.8M | if (!ret) { |
54 | 5.69M | if (str1.len == str2.len) |
55 | 2.59M | return 0; |
56 | 3.10M | else if (str1.len > str2.len) |
57 | 48.6k | return 1; |
58 | 3.05M | else |
59 | 3.05M | return -1; |
60 | 5.69M | } |
61 | 11.1M | return ret; |
62 | 16.8M | } |
63 | | |
64 | | int bstrchr(struct bstr str, int c) |
65 | 371M | { |
66 | 371M | if (!str.len) |
67 | 35.9k | return -1; |
68 | 371M | unsigned char *pos = memchr(str.start, c, str.len); |
69 | 371M | return pos ? pos - str.start : -1; |
70 | 371M | } |
71 | | |
72 | | int bstrrchr(struct bstr str, int c) |
73 | 50.6M | { |
74 | 50.6M | if (!str.len) |
75 | 95.0k | return -1; |
76 | 50.5M | #if HAVE_MEMRCHR |
77 | 50.5M | unsigned char *pos = memrchr(str.start, c, str.len); |
78 | 50.5M | return pos ? pos - str.start : -1; |
79 | | #else |
80 | | for (int i = str.len - 1; i >= 0; i--) |
81 | | if (str.start[i] == c) |
82 | | return i; |
83 | | return -1; |
84 | | #endif |
85 | 50.6M | } |
86 | | |
87 | | int bstrcspn(struct bstr str, const char *reject) |
88 | 25.4M | { |
89 | 25.4M | int i; |
90 | 918M | for (i = 0; i < str.len; i++) |
91 | 913M | if (strchr(reject, str.start[i])) |
92 | 20.4M | break; |
93 | 25.4M | return i; |
94 | 25.4M | } |
95 | | |
96 | | int bstrspn(struct bstr str, const char *accept) |
97 | 2.72M | { |
98 | 2.72M | int i; |
99 | 11.1M | for (i = 0; i < str.len; i++) |
100 | 9.81M | if (!strchr(accept, str.start[i])) |
101 | 1.38M | break; |
102 | 2.72M | return i; |
103 | 2.72M | } |
104 | | |
105 | | int bstr_find(struct bstr haystack, struct bstr needle) |
106 | 65.5M | { |
107 | 393M | for (int i = 0; i < haystack.len; i++) |
108 | 381M | if (bstr_startswith(bstr_splice(haystack, i, haystack.len), needle)) |
109 | 53.3M | return i; |
110 | 12.1M | return -1; |
111 | 65.5M | } |
112 | | |
113 | | struct bstr bstr_lstrip(struct bstr str) |
114 | 140M | { |
115 | 145M | while (str.len && mp_isspace(*str.start)) { |
116 | 4.70M | str.start++; |
117 | 4.70M | str.len--; |
118 | 4.70M | } |
119 | 140M | return str; |
120 | 140M | } |
121 | | |
122 | | struct bstr bstr_strip(struct bstr str) |
123 | 24.8M | { |
124 | 24.8M | str = bstr_lstrip(str); |
125 | 44.7M | while (str.len && mp_isspace(str.start[str.len - 1])) |
126 | 19.9M | str.len--; |
127 | 24.8M | return str; |
128 | 24.8M | } |
129 | | |
130 | | struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest) |
131 | 3.26M | { |
132 | 3.26M | int start; |
133 | 3.27M | for (start = 0; start < str.len; start++) |
134 | 3.27M | if (!strchr(sep, str.start[start])) |
135 | 3.26M | break; |
136 | 3.26M | str = bstr_cut(str, start); |
137 | 3.26M | int end = bstrcspn(str, sep); |
138 | 3.26M | if (rest) { |
139 | 2.87M | *rest = bstr_cut(str, end); |
140 | 2.87M | } |
141 | 3.26M | return bstr_splice(str, 0, end); |
142 | 3.26M | } |
143 | | |
144 | | // Unlike with bstr_split(), tok is a string, and not a set of char. |
145 | | // If tok is in str, return true, and: concat(out_left, tok, out_right) == str |
146 | | // Otherwise, return false, and set out_left==str, out_right=="" |
147 | | bool bstr_split_tok(bstr str, const char *tok, bstr *out_left, bstr *out_right) |
148 | 18.2M | { |
149 | 18.2M | bstr bsep = bstr0(tok); |
150 | 18.2M | int pos = bstr_find(str, bsep); |
151 | 18.2M | if (pos < 0) |
152 | 10.2M | pos = str.len; |
153 | 18.2M | *out_left = bstr_splice(str, 0, pos); |
154 | 18.2M | *out_right = bstr_cut(str, pos + bsep.len); |
155 | 18.2M | return pos != str.len; |
156 | 18.2M | } |
157 | | |
158 | | struct bstr bstr_splice(struct bstr str, int start, int end) |
159 | 886M | { |
160 | 886M | if (!str.len) |
161 | 339k | return str; |
162 | | |
163 | 886M | if (start < 0) |
164 | 0 | start += str.len; |
165 | 886M | if (end < 0) |
166 | 1.89k | end += str.len; |
167 | 886M | end = MPMIN(end, str.len); |
168 | 886M | start = MPMAX(start, 0); |
169 | 886M | end = MPMAX(end, start); |
170 | 886M | str.start += start; |
171 | 886M | str.len = end - start; |
172 | 886M | return str; |
173 | 886M | } |
174 | | |
175 | | long long bstrtoll(struct bstr str, struct bstr *rest, int base) |
176 | 2.44M | { |
177 | 2.44M | str = bstr_lstrip(str); |
178 | 2.44M | char buf[51]; |
179 | 2.44M | int len = MPMIN(str.len, 50); |
180 | 2.44M | memcpy(buf, str.start, len); |
181 | 2.44M | buf[len] = 0; |
182 | 2.44M | char *endptr; |
183 | 2.44M | long long r = strtoll(buf, &endptr, base); |
184 | 2.44M | if (rest) |
185 | 2.44M | *rest = bstr_cut(str, endptr - buf); |
186 | 2.44M | return r; |
187 | 2.44M | } |
188 | | |
189 | | double bstrtod(struct bstr str, struct bstr *rest) |
190 | 3.64M | { |
191 | 3.64M | str = bstr_lstrip(str); |
192 | 3.64M | char buf[101]; |
193 | 3.64M | int len = MPMIN(str.len, 100); |
194 | 3.64M | memcpy(buf, str.start, len); |
195 | 3.64M | buf[len] = 0; |
196 | 3.64M | char *endptr; |
197 | 3.64M | double r = strtod(buf, &endptr); |
198 | 3.64M | if (rest) |
199 | 3.64M | *rest = bstr_cut(str, endptr - buf); |
200 | 3.64M | return r; |
201 | 3.64M | } |
202 | | |
203 | | struct bstr bstr_splitchar(struct bstr str, struct bstr *rest, const char c) |
204 | 340M | { |
205 | 340M | int pos = bstrchr(str, c); |
206 | 340M | if (pos < 0) |
207 | 22.4M | pos = str.len; |
208 | 340M | if (rest) |
209 | 340M | *rest = bstr_cut(str, pos + 1); |
210 | 340M | return bstr_splice(str, 0, pos + 1); |
211 | 340M | } |
212 | | |
213 | | struct bstr bstr_strip_linebreaks(struct bstr str) |
214 | 32.8M | { |
215 | 32.8M | if (bstr_endswith0(str, "\r\n")) { |
216 | 13.2k | str = bstr_splice(str, 0, str.len - 2); |
217 | 32.8M | } else if (bstr_endswith0(str, "\n")) { |
218 | 32.6M | str = bstr_splice(str, 0, str.len - 1); |
219 | 32.6M | } |
220 | 32.8M | return str; |
221 | 32.8M | } |
222 | | |
223 | | bool bstr_eatstart(struct bstr *s, struct bstr prefix) |
224 | 809M | { |
225 | 809M | if (!bstr_startswith(*s, prefix)) |
226 | 747M | return false; |
227 | 61.5M | *s = bstr_cut(*s, prefix.len); |
228 | 61.5M | return true; |
229 | 809M | } |
230 | | |
231 | | bool bstr_eatend(struct bstr *s, struct bstr prefix) |
232 | 3.52k | { |
233 | 3.52k | if (!bstr_endswith(*s, prefix)) |
234 | 1.86k | return false; |
235 | 1.66k | s->len -= prefix.len; |
236 | 1.66k | return true; |
237 | 3.52k | } |
238 | | |
239 | | void bstr_lower(struct bstr str) |
240 | 0 | { |
241 | 0 | for (int i = 0; i < str.len; i++) |
242 | 0 | str.start[i] = mp_tolower(str.start[i]); |
243 | 0 | } |
244 | | |
245 | | int bstr_sscanf(struct bstr str, const char *format, ...) |
246 | 202k | { |
247 | 202k | char *ptr = bstrdup0(NULL, str); |
248 | 202k | va_list va; |
249 | 202k | va_start(va, format); |
250 | 202k | int ret = vsscanf(ptr, format, va); |
251 | 202k | va_end(va); |
252 | 202k | talloc_free(ptr); |
253 | 202k | return ret; |
254 | 202k | } |
255 | | |
256 | | int bstr_parse_utf8_code_length(unsigned char b) |
257 | 15.3G | { |
258 | 15.3G | if (b < 128) |
259 | 14.2G | return 1; |
260 | 1.15G | int bytes = 7 - mp_log2(b ^ 255); |
261 | 1.15G | return (bytes >= 2 && bytes <= 4) ? bytes : -1; |
262 | 15.3G | } |
263 | | |
264 | | int bstr_decode_partial_utf8(struct bstr *s) |
265 | 359M | { |
266 | 359M | if (s->len == 0) |
267 | 1.37k | return -1; |
268 | 359M | unsigned int codepoint = s->start[0]; |
269 | 359M | s->start++; s->len--; |
270 | 359M | if (codepoint >= 128) { |
271 | 71.7M | int bytes = bstr_parse_utf8_code_length(codepoint); |
272 | 71.7M | if (bytes < 1) |
273 | 17.9M | return BSTR_DECODE_OUT_OF_RANGE; |
274 | 53.8M | codepoint &= 127 >> bytes; |
275 | 103M | for (int n = 1; n < bytes; n++) { |
276 | 63.3M | if (!s->len) |
277 | 67.6k | return BSTR_DECODE_TRUNCATED_SEQUENCE; |
278 | 63.3M | int tmp = (unsigned char)s->start[0]; |
279 | 63.3M | if ((tmp & 0xC0) != 0x80) |
280 | 13.8M | return BSTR_DECODE_TRUNCATED_SEQUENCE; |
281 | 49.5M | codepoint = (codepoint << 6) | (tmp & ~0xC0); |
282 | 49.5M | s->start++; s->len--; |
283 | 49.5M | } |
284 | 39.9M | if (codepoint > 0x10FFFF || (codepoint >= 0xD800 && codepoint <= 0xDFFF)) |
285 | 60.2k | return BSTR_DECODE_OUT_OF_RANGE; |
286 | | |
287 | | // Overlong sequences - check taken from libavcodec. |
288 | 39.9M | unsigned int min = bytes == 2 ? 0x80 : 1 << (5 * bytes - 4); |
289 | 39.9M | if (codepoint < min) |
290 | 26.9k | return BSTR_DECODE_OVERLONG_ENCODING; |
291 | 39.9M | } |
292 | 327M | return codepoint; |
293 | 359M | } |
294 | | |
295 | | int bstr_decode_utf8(struct bstr s, struct bstr *out_next) |
296 | 259M | { |
297 | 259M | int res = bstr_decode_partial_utf8(&s); |
298 | 259M | if (res >= 0 && out_next) |
299 | 228M | *out_next = s; |
300 | 259M | return res; |
301 | 259M | } |
302 | | |
303 | | struct bstr bstr_split_utf8(struct bstr str, struct bstr *out_next) |
304 | 0 | { |
305 | 0 | bstr rest; |
306 | 0 | int code = bstr_decode_utf8(str, &rest); |
307 | 0 | if (code < 0) |
308 | 0 | return (bstr){0}; |
309 | 0 | if (out_next) |
310 | 0 | *out_next = rest; |
311 | 0 | return bstr_splice(str, 0, str.len - rest.len); |
312 | 0 | } |
313 | | |
314 | | int bstr_validate_utf8(struct bstr s) |
315 | 2.46M | { |
316 | 104M | while (s.len) { |
317 | 103M | if (bstr_decode_utf8(s, &s) < 0) { |
318 | | // Try to guess whether the sequence was just cut-off. |
319 | 804k | unsigned int codepoint = (unsigned char)s.start[0]; |
320 | 804k | int bytes = bstr_parse_utf8_code_length(codepoint); |
321 | 804k | if (bytes > 1 && s.len < 6) { |
322 | | // Manually check validity of left bytes |
323 | 116k | for (int n = 1; n < bytes; n++) { |
324 | 106k | if (n >= s.len) { |
325 | | // Everything valid until now - just cut off. |
326 | 48.5k | return -(bytes - s.len); |
327 | 48.5k | } |
328 | 58.2k | int tmp = (unsigned char)s.start[n]; |
329 | 58.2k | if ((tmp & 0xC0) != 0x80) |
330 | 43.7k | break; |
331 | 58.2k | } |
332 | 101k | } |
333 | 755k | return -8; |
334 | 804k | } |
335 | 103M | } |
336 | 1.66M | return 0; |
337 | 2.46M | } |
338 | | |
339 | | struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s) |
340 | 150k | { |
341 | 150k | bstr new = {0}; |
342 | 150k | bstr left = s; |
343 | 150k | unsigned char *first_ok = s.start; |
344 | 154M | while (left.len) { |
345 | 154M | int r = bstr_decode_utf8(left, &left); |
346 | 154M | if (r < 0) { |
347 | 30.2M | bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok}); |
348 | 30.2M | mp_append_utf8_bstr(talloc_ctx, &new, (unsigned char)left.start[0]); |
349 | 30.2M | left.start += 1; |
350 | 30.2M | left.len -= 1; |
351 | 30.2M | first_ok = left.start; |
352 | 30.2M | } |
353 | 154M | } |
354 | 150k | if (!new.start) |
355 | 13.3k | return s; |
356 | 137k | if (first_ok != left.start) |
357 | 79.7k | bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok}); |
358 | 137k | return new; |
359 | 150k | } |
360 | | |
361 | | static void resize_append(void *talloc_ctx, bstr *s, size_t append_min) |
362 | 291M | { |
363 | 291M | size_t size = talloc_get_size(s->start); |
364 | 291M | mp_assert(s->len <= size); |
365 | 291M | if (append_min > size - s->len) { |
366 | 39.5M | if (append_min < size) |
367 | 14.0M | append_min = size; // preallocate in power of 2s |
368 | 39.5M | if (size >= SIZE_MAX / 2 || append_min >= SIZE_MAX / 2) |
369 | 0 | abort(); // oom |
370 | 39.5M | s->start = talloc_realloc_size(talloc_ctx, s->start, size + append_min); |
371 | 39.5M | } |
372 | 291M | } |
373 | | |
374 | | void bstr_xappend(void *talloc_ctx, bstr *s, bstr append) |
375 | 502M | { |
376 | 502M | if (!append.len) |
377 | 217M | return; |
378 | 284M | resize_append(talloc_ctx, s, append.len + 1); |
379 | 284M | memmove(s->start + s->len, append.start, append.len); |
380 | 284M | s->len += append.len; |
381 | 284M | s->start[s->len] = '\0'; |
382 | 284M | } |
383 | | |
384 | | int bstr_xappend_asprintf(void *talloc_ctx, bstr *s, const char *fmt, ...) |
385 | 228M | { |
386 | 228M | va_list ap; |
387 | 228M | va_start(ap, fmt); |
388 | 228M | int ret = bstr_xappend_vasprintf(talloc_ctx, s, fmt, ap); |
389 | 228M | va_end(ap); |
390 | 228M | return ret; |
391 | 228M | } |
392 | | |
393 | | int bstr_xappend_vasprintf(void *talloc_ctx, bstr *s, const char *fmt, |
394 | | va_list ap) |
395 | 525M | { |
396 | 525M | int size; |
397 | 525M | va_list copy; |
398 | 525M | va_copy(copy, ap); |
399 | 525M | size_t avail = talloc_get_size(s->start) - s->len; |
400 | 525M | char *dest = s->start ? s->start + s->len : NULL; |
401 | 525M | size = vsnprintf(dest, avail, fmt, copy); |
402 | 525M | va_end(copy); |
403 | | |
404 | 525M | if (size < 0) |
405 | 0 | return size; |
406 | | |
407 | 525M | if (avail < 1 || size + 1 > avail) { |
408 | 7.36M | resize_append(talloc_ctx, s, size + 1); |
409 | 7.36M | vsnprintf(s->start + s->len, size + 1, fmt, ap); |
410 | 7.36M | } |
411 | 525M | s->len += size; |
412 | 525M | return size; |
413 | 525M | } |
414 | | |
415 | | bool bstr_case_startswith(struct bstr s, struct bstr prefix) |
416 | 4.71M | { |
417 | 4.71M | struct bstr start = bstr_splice(s, 0, prefix.len); |
418 | 4.71M | return start.len == prefix.len && bstrcasecmp(start, prefix) == 0; |
419 | 4.71M | } |
420 | | |
421 | | bool bstr_case_endswith(struct bstr s, struct bstr suffix) |
422 | 0 | { |
423 | 0 | struct bstr end = bstr_cut(s, -suffix.len); |
424 | 0 | return end.len == suffix.len && bstrcasecmp(end, suffix) == 0; |
425 | 0 | } |
426 | | |
427 | | static int h_to_i(unsigned char c) |
428 | 57.5M | { |
429 | 57.5M | if (c >= '0' && c <= '9') |
430 | 44.7M | return c - '0'; |
431 | 12.7M | if (c >= 'a' && c <= 'f') |
432 | 11.5M | return c - 'a' + 10; |
433 | 1.24M | if (c >= 'A' && c <= 'F') |
434 | 1.24M | return c - 'A' + 10; |
435 | | |
436 | 1.13k | return -1; // invalid char |
437 | 1.24M | } |
438 | | |
439 | | bool bstr_decode_hex(void *talloc_ctx, struct bstr hex, struct bstr *out) |
440 | 12.9k | { |
441 | 12.9k | if (!out) |
442 | 0 | return false; |
443 | | |
444 | 12.9k | char *arr = talloc_array(talloc_ctx, char, hex.len / 2); |
445 | 12.9k | int len = 0; |
446 | | |
447 | 28.7M | while (hex.len >= 2) { |
448 | 28.7M | int a = h_to_i(hex.start[0]); |
449 | 28.7M | int b = h_to_i(hex.start[1]); |
450 | 28.7M | hex = bstr_splice(hex, 2, hex.len); |
451 | | |
452 | 28.7M | if (a < 0 || b < 0) { |
453 | 856 | talloc_free(arr); |
454 | 856 | return false; |
455 | 856 | } |
456 | | |
457 | 28.7M | arr[len++] = (a << 4) | b; |
458 | 28.7M | } |
459 | | |
460 | 12.0k | *out = (struct bstr){ .start = arr, .len = len }; |
461 | 12.0k | return true; |
462 | 12.9k | } |
463 | | |
464 | | int bstr_find_in_list0(struct bstr str, char **list, bool case_sensitive) |
465 | 1.59M | { |
466 | 5.84M | for (int n = 0; list && list[n]; n++) |
467 | 5.78M | if (case_sensitive ? !bstrcmp0(str, list[n]) : !bstrcasecmp0(str, list[n])) |
468 | 1.52M | return n; |
469 | | |
470 | 64.8k | return -1; |
471 | 1.59M | } |
472 | | |
473 | | bool bstr_in_list0(struct bstr str, char **list) |
474 | 1.58M | { |
475 | | return bstr_find_in_list0(str, list, false) != -1; |
476 | 1.58M | } |
477 | | |
478 | | #ifdef _WIN32 |
479 | | |
480 | | #include <windows.h> |
481 | | |
482 | | int bstr_to_wchar(void *talloc_ctx, struct bstr s, wchar_t **ret) |
483 | | { |
484 | | int count = MultiByteToWideChar(CP_UTF8, 0, s.start, s.len, NULL, 0); |
485 | | if (count <= 0) |
486 | | abort(); |
487 | | wchar_t *wbuf = *ret; |
488 | | if (!wbuf || ta_get_size(wbuf) < (count + 1) * sizeof(wchar_t)) |
489 | | wbuf = talloc_realloc(talloc_ctx, wbuf, wchar_t, count + 1); |
490 | | MultiByteToWideChar(CP_UTF8, 0, s.start, s.len, wbuf, count); |
491 | | wbuf[count] = L'\0'; |
492 | | *ret = wbuf; |
493 | | return count; |
494 | | } |
495 | | |
496 | | #endif |