From: Jaroslav Kysela Date: Tue, 8 Jan 2019 14:48:33 +0000 (+0100) Subject: DVR: add utf8 validator for title/subtitle when cutted X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e8925fe785064be3947e11888638f20e9e7ab50;p=thirdparty%2Ftvheadend.git DVR: add utf8 validator for title/subtitle when cutted --- diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index bfa34008c..27932e2ae 100644 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -341,8 +341,10 @@ dvr_do_prefix(const char *id, const char *fmt, const char *s, char *tmp, size_t tmp[0] = '\0'; } else if (s[0] && !isalpha(id[0])) { snprintf(tmp, tmplen, "%c%s", id[0], s); + utf8_validate_inplace(tmp); } else { strlcpy(tmp, s, tmplen); + utf8_validate_inplace(tmp); } return dvr_clean_directory_separator(tmp, tmp, tmplen); } diff --git a/src/tvh_string.h b/src/tvh_string.h index 62e253200..707a01007 100644 --- a/src/tvh_string.h +++ b/src/tvh_string.h @@ -129,5 +129,6 @@ static inline unsigned int tvh_strhash(const char *s, unsigned int mod) int put_utf8(char *out, int c); char *utf8_lowercase_inplace(char *s); +char *utf8_validate_inplace(char *s); #endif /* TVHEADEND_STRING_H */ diff --git a/src/utils.c b/src/utils.c index 0c73b2e44..5d2fb8dee 100644 --- a/src/utils.c +++ b/src/utils.c @@ -319,6 +319,40 @@ char *utf8_lowercase_inplace(char *s) return r; } +static int utf8_len(char first) +{ + if ((first & 0xe0) == 0xc0) return 2; + if ((first & 0xf0) == 0xe0) return 3; + if ((first & 0xf8) == 0xf0) return 4; + if ((first & 0xfc) == 0xf8) return 5; + if ((first & 0xfe) == 0xfc) return 6; + assert(0); + return 1; +} + +/* + * Remove the partial utf8 character at the end of the string + */ +char *utf8_validate_inplace(char *s) +{ + if (s == NULL) return NULL; + size_t i, l = strlen(s); + if (l < 1) return s; + for (i = l; i > 0; i--) { + char c = s[i-1]; + if ((c & 0x80) == 0) { + if (l != i) s[i] = '\0'; + break; + } + if ((c & 0xc0) == 0xc0) { + if (1 + l - i != utf8_len(c)) + s[i-1] = '\0'; + break; + } + } + return s; +} + /** * */