Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
cwc: trim username and hostname string on load (config/webui), fixes …
…#4135
  • Loading branch information
perexg committed Dec 14, 2016
1 parent e9cb021 commit f6f9b8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/descrambler/cwc.c
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
71 changes: 46 additions & 25 deletions src/prop.c
Expand Up @@ -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;
Expand All @@ -99,20 +100,20 @@ 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);

/* 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);

Expand Down Expand Up @@ -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))
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/prop.h
Expand Up @@ -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
Expand Down

0 comments on commit f6f9b8a

Please sign in to comment.