2010-05-20 20:50:18 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2010-05-20 20:50:18 +00:00
|
|
|
*
|
2016-12-11 17:09:44 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-05-20 20:50:18 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-05-20 20:50:18 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-12-11 17:09:44 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-05-20 20:50:18 +00:00
|
|
|
*
|
2016-12-11 17:09:44 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-20 20:50:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2014-07-10 06:28:03 +00:00
|
|
|
#include <strings.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
#include <assert.h>
|
2011-07-28 08:07:47 +00:00
|
|
|
#include <stdarg.h>
|
2014-01-02 23:34:15 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2011-02-23 14:18:09 +00:00
|
|
|
|
2013-12-30 21:45:40 +00:00
|
|
|
#include "common/common.h"
|
2014-07-01 21:10:38 +00:00
|
|
|
#include "misc/ctype.h"
|
2014-08-29 10:01:40 +00:00
|
|
|
#include "bstr.h"
|
2010-05-20 20:50:18 +00:00
|
|
|
|
|
|
|
int bstrcmp(struct bstr str1, struct bstr str2)
|
|
|
|
{
|
2014-10-07 04:42:30 +00:00
|
|
|
int ret = 0;
|
|
|
|
if (str1.len && str2.len)
|
2019-10-31 10:24:20 +00:00
|
|
|
ret = memcmp(str1.start, str2.start, MPMIN(str1.len, str2.len));
|
2010-05-20 20:50:18 +00:00
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
if (str1.len == str2.len)
|
|
|
|
return 0;
|
|
|
|
else if (str1.len > str2.len)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bstrcasecmp(struct bstr str1, struct bstr str2)
|
|
|
|
{
|
2014-10-07 04:42:30 +00:00
|
|
|
int ret = 0;
|
|
|
|
if (str1.len && str2.len)
|
2019-10-31 10:24:20 +00:00
|
|
|
ret = strncasecmp(str1.start, str2.start, MPMIN(str1.len, str2.len));
|
2010-05-20 20:50:18 +00:00
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
if (str1.len == str2.len)
|
|
|
|
return 0;
|
|
|
|
else if (str1.len > str2.len)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-23 14:18:09 +00:00
|
|
|
|
|
|
|
int bstrchr(struct bstr str, int c)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < str.len; i++)
|
|
|
|
if (str.start[i] == c)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-04-09 02:14:55 +00:00
|
|
|
int bstrrchr(struct bstr str, int c)
|
|
|
|
{
|
|
|
|
for (int i = str.len - 1; i >= 0; i--)
|
|
|
|
if (str.start[i] == c)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
int bstrcspn(struct bstr str, const char *reject)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < str.len; i++)
|
|
|
|
if (strchr(reject, str.start[i]))
|
|
|
|
break;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-04-20 23:03:46 +00:00
|
|
|
int bstrspn(struct bstr str, const char *accept)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < str.len; i++)
|
|
|
|
if (!strchr(accept, str.start[i]))
|
|
|
|
break;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
int bstr_find(struct bstr haystack, struct bstr needle)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < haystack.len; i++)
|
|
|
|
if (bstr_startswith(bstr_splice(haystack, i, haystack.len), needle))
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:41:51 +00:00
|
|
|
struct bstr bstr_lstrip(struct bstr str)
|
2011-02-23 14:18:09 +00:00
|
|
|
{
|
2014-07-01 21:10:38 +00:00
|
|
|
while (str.len && mp_isspace(*str.start)) {
|
2011-02-23 14:18:09 +00:00
|
|
|
str.start++;
|
|
|
|
str.len--;
|
|
|
|
}
|
2011-07-20 01:41:51 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr bstr_strip(struct bstr str)
|
|
|
|
{
|
|
|
|
str = bstr_lstrip(str);
|
2014-07-01 21:10:38 +00:00
|
|
|
while (str.len && mp_isspace(str.start[str.len - 1]))
|
2011-02-23 14:18:09 +00:00
|
|
|
str.len--;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest)
|
2011-02-23 14:18:09 +00:00
|
|
|
{
|
2011-07-28 08:07:47 +00:00
|
|
|
int start;
|
2011-02-23 14:18:09 +00:00
|
|
|
for (start = 0; start < str.len; start++)
|
|
|
|
if (!strchr(sep, str.start[start]))
|
|
|
|
break;
|
2011-07-28 08:07:47 +00:00
|
|
|
str = bstr_cut(str, start);
|
|
|
|
int end = bstrcspn(str, sep);
|
2011-02-23 14:18:09 +00:00
|
|
|
if (rest) {
|
|
|
|
*rest = bstr_cut(str, end);
|
|
|
|
}
|
2011-07-28 08:07:47 +00:00
|
|
|
return bstr_splice(str, 0, end);
|
2011-02-23 14:18:09 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 15:13:13 +00:00
|
|
|
// Unlike with bstr_split(), tok is a string, and not a set of char.
|
|
|
|
// If tok is in str, return true, and: concat(out_left, tok, out_right) == str
|
|
|
|
// Otherwise, return false, and set out_left==str, out_right==""
|
|
|
|
bool bstr_split_tok(bstr str, const char *tok, bstr *out_left, bstr *out_right)
|
|
|
|
{
|
|
|
|
bstr bsep = bstr0(tok);
|
|
|
|
int pos = bstr_find(str, bsep);
|
|
|
|
if (pos < 0)
|
|
|
|
pos = str.len;
|
|
|
|
*out_left = bstr_splice(str, 0, pos);
|
|
|
|
*out_right = bstr_cut(str, pos + bsep.len);
|
|
|
|
return pos != str.len;
|
|
|
|
}
|
2011-02-23 14:18:09 +00:00
|
|
|
|
|
|
|
struct bstr bstr_splice(struct bstr str, int start, int end)
|
|
|
|
{
|
|
|
|
if (start < 0)
|
|
|
|
start += str.len;
|
|
|
|
if (end < 0)
|
|
|
|
end += str.len;
|
2019-10-31 10:24:20 +00:00
|
|
|
end = MPMIN(end, str.len);
|
|
|
|
start = MPMAX(start, 0);
|
|
|
|
end = MPMAX(end, start);
|
2011-02-23 14:18:09 +00:00
|
|
|
str.start += start;
|
|
|
|
str.len = end - start;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long bstrtoll(struct bstr str, struct bstr *rest, int base)
|
|
|
|
{
|
2011-07-28 08:07:47 +00:00
|
|
|
str = bstr_lstrip(str);
|
2011-02-23 14:18:09 +00:00
|
|
|
char buf[51];
|
2019-10-31 10:24:20 +00:00
|
|
|
int len = MPMIN(str.len, 50);
|
2011-02-23 14:18:09 +00:00
|
|
|
memcpy(buf, str.start, len);
|
|
|
|
buf[len] = 0;
|
|
|
|
char *endptr;
|
|
|
|
long long r = strtoll(buf, &endptr, base);
|
|
|
|
if (rest)
|
|
|
|
*rest = bstr_cut(str, endptr - buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
double bstrtod(struct bstr str, struct bstr *rest)
|
|
|
|
{
|
|
|
|
str = bstr_lstrip(str);
|
|
|
|
char buf[101];
|
2019-10-31 10:24:20 +00:00
|
|
|
int len = MPMIN(str.len, 100);
|
2011-07-28 08:07:47 +00:00
|
|
|
memcpy(buf, str.start, len);
|
|
|
|
buf[len] = 0;
|
|
|
|
char *endptr;
|
|
|
|
double r = strtod(buf, &endptr);
|
|
|
|
if (rest)
|
|
|
|
*rest = bstr_cut(str, endptr - buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-02-23 14:18:09 +00:00
|
|
|
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
|
|
|
|
{
|
|
|
|
if (str.len == 0)
|
|
|
|
return NULL;
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < str.len; i++)
|
|
|
|
if (str.start[i] == '\n')
|
|
|
|
count++;
|
|
|
|
if (str.start[str.len - 1] != '\n')
|
|
|
|
count++;
|
|
|
|
struct bstr *r = talloc_array_ptrtype(talloc_ctx, r, count);
|
|
|
|
unsigned char *p = str.start;
|
|
|
|
for (int i = 0; i < count - 1; i++) {
|
|
|
|
r[i].start = p;
|
|
|
|
while (*p++ != '\n');
|
|
|
|
r[i].len = p - r[i].start;
|
|
|
|
}
|
|
|
|
r[count - 1].start = p;
|
|
|
|
r[count - 1].len = str.start + str.len - p;
|
|
|
|
return r;
|
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
|
2016-05-12 01:34:47 +00:00
|
|
|
struct bstr bstr_splitchar(struct bstr str, struct bstr *rest, const char c)
|
2012-01-01 16:28:01 +00:00
|
|
|
{
|
2016-05-12 01:34:47 +00:00
|
|
|
int pos = bstrchr(str, c);
|
2012-01-01 16:28:01 +00:00
|
|
|
if (pos < 0)
|
|
|
|
pos = str.len;
|
|
|
|
if (rest)
|
|
|
|
*rest = bstr_cut(str, pos + 1);
|
2012-07-01 23:35:57 +00:00
|
|
|
return bstr_splice(str, 0, pos + 1);
|
|
|
|
}
|
|
|
|
|
2012-07-28 15:07:49 +00:00
|
|
|
struct bstr bstr_strip_linebreaks(struct bstr str)
|
|
|
|
{
|
|
|
|
if (bstr_endswith0(str, "\r\n")) {
|
|
|
|
str = bstr_splice(str, 0, str.len - 2);
|
|
|
|
} else if (bstr_endswith0(str, "\n")) {
|
|
|
|
str = bstr_splice(str, 0, str.len - 1);
|
|
|
|
}
|
2012-01-01 16:28:01 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-02-09 11:43:33 +00:00
|
|
|
bool bstr_eatstart(struct bstr *s, struct bstr prefix)
|
|
|
|
{
|
|
|
|
if (!bstr_startswith(*s, prefix))
|
|
|
|
return false;
|
|
|
|
*s = bstr_cut(*s, prefix.len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-12 01:34:47 +00:00
|
|
|
bool bstr_eatend(struct bstr *s, struct bstr prefix)
|
|
|
|
{
|
|
|
|
if (!bstr_endswith(*s, prefix))
|
|
|
|
return false;
|
|
|
|
s->len -= prefix.len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
void bstr_lower(struct bstr str)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < str.len; i++)
|
2014-07-01 21:10:38 +00:00
|
|
|
str.start[i] = mp_tolower(str.start[i]);
|
2011-04-19 03:53:56 +00:00
|
|
|
}
|
2011-07-28 08:07:47 +00:00
|
|
|
|
|
|
|
int bstr_sscanf(struct bstr str, const char *format, ...)
|
|
|
|
{
|
|
|
|
char *ptr = bstrdup0(NULL, str);
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
int ret = vsscanf(ptr, format, va);
|
|
|
|
va_end(va);
|
|
|
|
talloc_free(ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-01-13 06:38:40 +00:00
|
|
|
|
|
|
|
int bstr_parse_utf8_code_length(unsigned char b)
|
|
|
|
{
|
|
|
|
if (b < 128)
|
|
|
|
return 1;
|
2019-10-31 12:17:18 +00:00
|
|
|
int bytes = 7 - mp_log2(b ^ 255);
|
2012-01-13 06:38:40 +00:00
|
|
|
return (bytes >= 2 && bytes <= 4) ? bytes : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bstr_decode_utf8(struct bstr s, struct bstr *out_next)
|
|
|
|
{
|
|
|
|
if (s.len == 0)
|
|
|
|
return -1;
|
|
|
|
unsigned int codepoint = s.start[0];
|
|
|
|
s.start++; s.len--;
|
|
|
|
if (codepoint >= 128) {
|
|
|
|
int bytes = bstr_parse_utf8_code_length(codepoint);
|
2014-11-21 08:59:58 +00:00
|
|
|
if (bytes < 1 || s.len < bytes - 1)
|
2012-01-13 06:38:40 +00:00
|
|
|
return -1;
|
|
|
|
codepoint &= 127 >> bytes;
|
|
|
|
for (int n = 1; n < bytes; n++) {
|
2013-08-15 16:48:05 +00:00
|
|
|
int tmp = (unsigned char)s.start[0];
|
2012-01-13 06:38:40 +00:00
|
|
|
if ((tmp & 0xC0) != 0x80)
|
|
|
|
return -1;
|
|
|
|
codepoint = (codepoint << 6) | (tmp & ~0xC0);
|
|
|
|
s.start++; s.len--;
|
|
|
|
}
|
2013-08-15 19:14:00 +00:00
|
|
|
if (codepoint > 0x10FFFF || (codepoint >= 0xD800 && codepoint <= 0xDFFF))
|
|
|
|
return -1;
|
|
|
|
// Overlong sequences - check taken from libavcodec.
|
|
|
|
// (The only reason we even bother with this is to make libavcodec's
|
|
|
|
// retarded subtitle utf-8 check happy.)
|
|
|
|
unsigned int min = bytes == 2 ? 0x80 : 1 << (5 * bytes - 4);
|
|
|
|
if (codepoint < min)
|
|
|
|
return -1;
|
2012-01-13 06:38:40 +00:00
|
|
|
}
|
|
|
|
if (out_next)
|
|
|
|
*out_next = s;
|
|
|
|
return codepoint;
|
|
|
|
}
|
2012-01-01 16:28:01 +00:00
|
|
|
|
2014-01-15 15:13:07 +00:00
|
|
|
struct bstr bstr_split_utf8(struct bstr str, struct bstr *out_next)
|
|
|
|
{
|
|
|
|
bstr rest;
|
|
|
|
int code = bstr_decode_utf8(str, &rest);
|
|
|
|
if (code < 0)
|
|
|
|
return (bstr){0};
|
|
|
|
if (out_next)
|
|
|
|
*out_next = rest;
|
|
|
|
return bstr_splice(str, 0, str.len - rest.len);
|
|
|
|
}
|
|
|
|
|
2013-08-15 16:48:05 +00:00
|
|
|
int bstr_validate_utf8(struct bstr s)
|
|
|
|
{
|
|
|
|
while (s.len) {
|
|
|
|
if (bstr_decode_utf8(s, &s) < 0) {
|
|
|
|
// Try to guess whether the sequence was just cut-off.
|
|
|
|
unsigned int codepoint = (unsigned char)s.start[0];
|
|
|
|
int bytes = bstr_parse_utf8_code_length(codepoint);
|
|
|
|
if (bytes > 1 && s.len < 6) {
|
|
|
|
// Manually check validity of left bytes
|
|
|
|
for (int n = 1; n < bytes; n++) {
|
|
|
|
if (n >= s.len) {
|
|
|
|
// Everything valid until now - just cut off.
|
|
|
|
return -(bytes - s.len);
|
|
|
|
}
|
|
|
|
int tmp = (unsigned char)s.start[n];
|
|
|
|
if ((tmp & 0xC0) != 0x80)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s)
|
|
|
|
{
|
|
|
|
bstr new = {0};
|
|
|
|
bstr left = s;
|
|
|
|
unsigned char *first_ok = s.start;
|
|
|
|
while (left.len) {
|
|
|
|
int r = bstr_decode_utf8(left, &left);
|
|
|
|
if (r < 0) {
|
2013-12-30 21:45:40 +00:00
|
|
|
bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok});
|
|
|
|
mp_append_utf8_bstr(talloc_ctx, &new, (unsigned char)left.start[0]);
|
2013-08-15 16:48:05 +00:00
|
|
|
left.start += 1;
|
|
|
|
left.len -= 1;
|
|
|
|
first_ok = left.start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!new.start)
|
|
|
|
return s;
|
|
|
|
if (first_ok != left.start)
|
2013-12-30 21:45:40 +00:00
|
|
|
bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok});
|
2013-08-15 16:48:05 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2013-12-30 19:20:28 +00:00
|
|
|
static void resize_append(void *talloc_ctx, bstr *s, size_t append_min)
|
|
|
|
{
|
|
|
|
size_t size = talloc_get_size(s->start);
|
|
|
|
assert(s->len <= size);
|
|
|
|
if (append_min > size - s->len) {
|
|
|
|
if (append_min < size)
|
|
|
|
append_min = size; // preallocate in power of 2s
|
2014-01-02 23:34:15 +00:00
|
|
|
if (size >= SIZE_MAX / 2 || append_min >= SIZE_MAX / 2)
|
|
|
|
abort(); // oom
|
2013-12-30 19:20:28 +00:00
|
|
|
s->start = talloc_realloc_size(talloc_ctx, s->start, size + append_min);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the string, so that *s = *s + append. s->start is expected to be
|
|
|
|
// a talloc allocation (which can be realloced) or NULL.
|
|
|
|
// This function will always implicitly append a \0 after the new string for
|
|
|
|
// convenience.
|
|
|
|
// talloc_ctx will be used as parent context, if s->start is NULL.
|
|
|
|
void bstr_xappend(void *talloc_ctx, bstr *s, bstr append)
|
|
|
|
{
|
2014-11-21 04:13:40 +00:00
|
|
|
if (!append.len)
|
|
|
|
return;
|
2013-12-30 19:20:28 +00:00
|
|
|
resize_append(talloc_ctx, s, append.len + 1);
|
|
|
|
memcpy(s->start + s->len, append.start, append.len);
|
|
|
|
s->len += append.len;
|
|
|
|
s->start[s->len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void bstr_xappend_asprintf(void *talloc_ctx, bstr *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
bstr_xappend_vasprintf(talloc_ctx, s, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exactly as bstr_xappend(), but with a formatted string.
|
|
|
|
void bstr_xappend_vasprintf(void *talloc_ctx, bstr *s, const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
va_list copy;
|
|
|
|
va_copy(copy, ap);
|
2016-03-23 21:00:16 +00:00
|
|
|
size_t avail = talloc_get_size(s->start) - s->len;
|
|
|
|
char *dest = s->start ? s->start + s->len : NULL;
|
2013-12-30 19:20:28 +00:00
|
|
|
char c;
|
2016-03-23 21:00:16 +00:00
|
|
|
if (avail < 1)
|
|
|
|
dest = &c;
|
|
|
|
size = vsnprintf(dest, MPMAX(avail, 1), fmt, copy);
|
2013-12-30 19:20:28 +00:00
|
|
|
va_end(copy);
|
|
|
|
|
|
|
|
if (size < 0)
|
|
|
|
abort();
|
|
|
|
|
2016-03-23 21:00:16 +00:00
|
|
|
if (avail < 1 || size + 1 > avail) {
|
|
|
|
resize_append(talloc_ctx, s, size + 1);
|
|
|
|
vsnprintf(s->start + s->len, size + 1, fmt, ap);
|
|
|
|
}
|
2013-12-30 19:20:28 +00:00
|
|
|
s->len += size;
|
|
|
|
}
|
|
|
|
|
2012-01-01 16:28:01 +00:00
|
|
|
bool bstr_case_startswith(struct bstr s, struct bstr prefix)
|
|
|
|
{
|
|
|
|
struct bstr start = bstr_splice(s, 0, prefix.len);
|
|
|
|
return start.len == prefix.len && bstrcasecmp(start, prefix) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bstr_case_endswith(struct bstr s, struct bstr suffix)
|
|
|
|
{
|
|
|
|
struct bstr end = bstr_cut(s, -suffix.len);
|
|
|
|
return end.len == suffix.len && bstrcasecmp(end, suffix) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr bstr_strip_ext(struct bstr str)
|
|
|
|
{
|
|
|
|
int dotpos = bstrrchr(str, '.');
|
|
|
|
if (dotpos < 0)
|
|
|
|
return str;
|
|
|
|
return (struct bstr){str.start, dotpos};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr bstr_get_ext(struct bstr s)
|
|
|
|
{
|
|
|
|
int dotpos = bstrrchr(s, '.');
|
|
|
|
if (dotpos < 0)
|
|
|
|
return (struct bstr){NULL, 0};
|
|
|
|
return bstr_splice(s, dotpos + 1, s.len);
|
|
|
|
}
|
2017-07-10 23:59:21 +00:00
|
|
|
|
|
|
|
static int h_to_i(unsigned char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
|
|
|
|
return -1; // invalid char
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bstr_decode_hex(void *talloc_ctx, struct bstr hex, struct bstr *out)
|
|
|
|
{
|
|
|
|
if (!out)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *arr = talloc_array(talloc_ctx, char, hex.len / 2);
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while (hex.len >= 2) {
|
|
|
|
int a = h_to_i(hex.start[0]);
|
|
|
|
int b = h_to_i(hex.start[1]);
|
|
|
|
hex = bstr_splice(hex, 2, hex.len);
|
|
|
|
|
|
|
|
if (a < 0 || b < 0) {
|
|
|
|
talloc_free(arr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
arr[len++] = (a << 4) | b;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = (struct bstr){ .start = arr, .len = len };
|
|
|
|
return true;
|
|
|
|
}
|