]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
cwc: trim username and hostname string on load (config/webui), fixes #4135
authorJaroslav Kysela <perex@perex.cz>
Wed, 14 Dec 2016 10:21:47 +0000 (11:21 +0100)
committerJaroslav Kysela <perex@perex.cz>
Wed, 14 Dec 2016 10:21:47 +0000 (11:21 +0100)
src/descrambler/cwc.c
src/prop.c
src/prop.h

index 6e1bb17212a002b3076a537d46b7d5f2c196c503..35d5fc9e5e92b9766aa6f8ee2335cb11baac85b4 100644 (file)
@@ -1833,6 +1833,7 @@ const idclass_t caclient_cwc_class =
       .name     = N_("Username"),
       .desc     = N_("Login username."),
       .off      = offsetof(cwc_t, cwc_username),
+      .opts     = PO_TRIM,
     },
     {
       .type     = PT_STR,
@@ -1849,6 +1850,7 @@ const idclass_t caclient_cwc_class =
       .desc     = N_("Hostname (or IP) of the server."),
       .off      = offsetof(cwc_t, cwc_hostname),
       .def.s    = "localhost",
+      .opts     = PO_TRIM,
     },
     {
       .type     = PT_INT,
index bc6a2795dc00cb4a51e8042f5223395d960b5727..fb33d128ad8da81d1dc5cc967bf4d93fd5f0e3fa 100644 (file)
@@ -75,18 +75,19 @@ prop_write_values
   htsmsg_field_t *f;
   const property_t *p;
   void *cur;
-  const void *new;
+  const void *snew;
+  void *dnew;
   double dbl;
-  int i;
+   int i;
   int64_t s64;
-  uint32_t u32;
+  uint32_t u32, opts;
   uint16_t u16;
   time_t tm;
 #define PROP_UPDATE(v, t)\
-  new = &v;\
-  if (!p->set && (*((t*)cur) != *((t*)new))) {\
+  snew = &v;\
+  if (!p->set && (*((t*)cur) != *((t*)snew))) {\
     save = 1;\
-    *((t*)cur) = *((t*)new);\
+    *((t*)cur) = *((t*)snew);\
   } (void)0
 
   if (!pl) return 0;
@@ -99,8 +100,8 @@ prop_write_values
     if (!f) continue;
 
     /* Ignore */
-    u32 = p->get_opts ? p->get_opts(obj) : p->opts;
-    if(u32 & optmask) continue;
+    opts = p->get_opts ? p->get_opts(obj) : p->opts;
+    if(opts & optmask) continue;
 
     /* Sanity check */
     assert(p->set || p->off);
@@ -108,11 +109,11 @@ prop_write_values
     /* Write */
     save = 0;
     cur  = obj + p->off;
-    new  = NULL;
+    snew = dnew = NULL;
 
     /* List */
     if (p->islist)
-      new = (f->hmf_type == HMF_MAP) ?
+      snew = (f->hmf_type == HMF_MAP) ?
               htsmsg_field_get_map(f) :
               htsmsg_field_get_list(f);
 
@@ -142,10 +143,10 @@ prop_write_values
       case PT_U32: {
         if (p->intextra && INTEXTRA_IS_SPLIT(p->intextra)) {
           char *s;
-          if (!(new = htsmsg_field_get_str(f)))
+          if (!(snew = htsmsg_field_get_str(f)))
             continue;
-          u32 = atol(new) * p->intextra;
-          if ((s = strchr(new, '.')) != NULL)
+          u32 = atol(snew) * p->intextra;
+          if ((s = strchr(snew, '.')) != NULL)
             u32 += (atol(s + 1) % p->intextra);
         } else {
           if (htsmsg_field_get_u32(f, &u32))
@@ -156,9 +157,9 @@ prop_write_values
       }
       case PT_S64: {
         if (p->intextra && INTEXTRA_IS_SPLIT(p->intextra)) {
-          if (!(new = htsmsg_field_get_str(f)))
+          if (!(snew = htsmsg_field_get_str(f)))
             continue;
-          s64 = prop_intsplit_from_str(new, p->intextra);
+          s64 = prop_intsplit_from_str(snew, p->intextra);
         } else {
           if (htsmsg_field_get_s64(f, &s64))
             continue;
@@ -176,12 +177,28 @@ prop_write_values
       }
       case PT_STR: {
         char **str = cur;
-        if (!(new = htsmsg_field_get_str(f)))
+        if (!(snew = htsmsg_field_get_str(f)))
           continue;
-        if (!p->set && strcmp((*str) ?: "", new)) {
+        if (opts & PO_TRIM) {
+          if (*(char *)snew <= ' ' || ((char *)snew)[strlen(snew)-1] <= ' ') {
+            const char *x = snew;
+            char *y;
+            while (*x && *x <= ' ') x++;
+            snew = dnew = strdup(x);
+            if (*x) {
+              y = dnew + strlen(dnew);
+              while (y != x) {
+                y--;
+                if (*y <= ' ') break;
+              }
+              *y = '\0';
+            }
+          }
+        }
+        if (!p->set && strcmp((*str) ?: "", snew)) {
           /* make sure that the string is valid all time */
           void *old = *str;
-          *str = strdup(new);
+          *str = strdup(snew);
           free(old);
           save = 1;
         }
@@ -197,11 +214,11 @@ prop_write_values
       case PT_LANGSTR: {
         lang_str_t **lstr1 = cur;
         lang_str_t  *lstr2;
-        new = htsmsg_field_get_map(f);
-        if (!new)
+        snew = htsmsg_field_get_map(f);
+        if (!snew)
           continue;
         if (!p->set) {
-          lstr2 = lang_str_deserialize_map((htsmsg_t *)new);
+          lstr2 = lang_str_deserialize_map((htsmsg_t *)snew);
           if (lang_str_compare(*lstr1, lstr2)) {
             lang_str_destroy(*lstr1);
             *lstr1 = lstr2;
@@ -213,9 +230,9 @@ prop_write_values
         break;
       }
       case PT_PERM: {
-        if (!(new = htsmsg_field_get_str(f)))
+        if (!(snew = htsmsg_field_get_str(f)))
           continue;
-        u32 = (int)strtol(new,NULL,0);
+        u32 = (int)strtol(snew, NULL, 0);
         PROP_UPDATE(u32, uint32_t);
         break;
       }
@@ -225,8 +242,12 @@ prop_write_values
     }
   
     /* Setter */
-    if (p->set && new)
-      save = p->set(obj, new);
+    if (p->set && snew)
+      save = p->set(obj, snew);
+
+    /* Remove dynamic contents */
+    if (dnew)
+      free(dnew);
 
     /* Updated */
     if (save) {
index 392f3e6a9364a4f0a8b05f8ba1c1304679384b47..c1f08372379cb5f4e72a37244b40c5907ea9389d 100644 (file)
@@ -65,6 +65,7 @@ typedef enum {
 #define PO_PERSIST   (1<<17) // Persistent value (return back on save)
 #define PO_DOC       (1<<18) // Use doc callback instead description if exists
 #define PO_DOC_NLIST (1<<19) // Do not show list in doc
+#define PO_TRIM      (1<<20) // Trim whitespaces (left & right) on load
 
 /*
  * min/max/step helpers