]> 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>
Wed, 26 Jun 2019 08:26:41 +0000 (10:26 +0200)
src/dvr/dvr_rec.c
src/tvh_string.h
src/utils.c

index a3291d6766a6202f667e640e8747d5cc0eb11b4c..4b97b0c1520746cedc687aa75d7e9b1590106cae 100644 (file)
@@ -326,8 +326,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 13ef2f308e06059d717e91a7533ca20d2f56af90..d194d8535e13ef79b447210750b0b7729fc73577 100644 (file)
@@ -98,5 +98,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_STRINGS_H */
index a92d4c7dabe76a9f8539172f9530cb23c5b0badd..7a4d0a2e82c5d1feeb7e5666730f6e868f001a7e 100644 (file)
@@ -318,6 +318,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;
+}
+
 /**
  *
  */