summaryrefslogtreecommitdiff
path: root/nncmpp.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-10-10 08:39:31 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2016-10-10 10:16:28 +0200
commit29bc035ecfed9e37ce63b42de982b95569753463 (patch)
tree877faaeb0e78b80207082a8028d8e7e29d061bb1 /nncmpp.c
parent44e19d68f0fbbc5ddda9a780647bf4838d30d342 (diff)
downloadnncmpp-29bc035ecfed9e37ce63b42de982b95569753463.tar.gz
nncmpp-29bc035ecfed9e37ce63b42de982b95569753463.tar.xz
nncmpp-29bc035ecfed9e37ce63b42de982b95569753463.zip
Ensure UTF-8 when parsing playlists
Diffstat (limited to 'nncmpp.c')
-rw-r--r--nncmpp.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/nncmpp.c b/nncmpp.c
index cf54119..29a82b3 100644
--- a/nncmpp.c
+++ b/nncmpp.c
@@ -142,6 +142,25 @@ clock_msec (clockid_t clock)
return (int64_t) tp.tv_sec * 1000 + (int64_t) tp.tv_nsec / 1000000;
}
+static char *
+latin1_to_utf8 (const char *latin1)
+{
+ struct str converted;
+ str_init (&converted);
+ while (*latin1)
+ {
+ uint8_t c = *latin1++;
+ if (c < 0x80)
+ str_append_c (&converted, c);
+ else
+ {
+ str_append_c (&converted, 0xC0 | (c >> 6));
+ str_append_c (&converted, 0x80 | (c & 0x3F));
+ }
+ }
+ return str_steal (&converted);
+}
+
// --- cURL async wrapper ------------------------------------------------------
// You are meant to subclass this structure, no user_data pointers needed
@@ -1888,11 +1907,18 @@ parse_playlist (const char *playlist, const char *content_type,
regmatch_t groups[2];
for (size_t i = 0; i < lines.len; i++)
- {
if (regexec (re, lines.vector[i], 2, groups, 0) != REG_NOMATCH)
- str_vector_add (out, xstrndup (lines.vector[i] + groups[1].rm_so,
- groups[1].rm_eo - groups[1].rm_so));
- }
+ {
+ char *target = xstrndup (lines.vector[i] + groups[1].rm_so,
+ groups[1].rm_eo - groups[1].rm_so);
+ if (utf8_validate (target, strlen (target)))
+ str_vector_add_owned (out, target);
+ else
+ {
+ str_vector_add_owned (out, latin1_to_utf8 (target));
+ free (target);
+ }
+ }
regex_free (re);
str_vector_free (&lines);
}