]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
DVR: add utf8 validator for title/subtitle when cutted
authorJaroslav Kysela <perex@perex.cz>
Tue, 8 Jan 2019 14:48:33 +0000 (15:48 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 8 Jan 2019 14:48:33 +0000 (15:48 +0100)
src/dvr/dvr_rec.c
src/tvh_string.h
src/utils.c

index bfa34008c1c79fe7ba178978abe320e9063d4536..27932e2aefbef649ef326c8b44fce3f5925b9809 100644 (file)
@@ -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);
 }
index 62e25320071f356b33a4a36f70772e488ce853a0..707a01007fa7d179242c033b372daed21ceafd72 100644 (file)
@@ -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 */
index 0c73b2e4442f26a2de5b3e3b5c711e0ada2c1c88..5d2fb8deeb0af9411c79ed3381d85be1c75a0993 100644 (file)
@@ -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;
+}
+
 /**
  *
  */