Skip to content

Commit

Permalink
context.c: Replace sanitize code with function
Browse files Browse the repository at this point in the history
This function can be used in other similar scenarios where
sanitization is needed

Signed-off-by: Mihail Chindris <[email protected]>
  • Loading branch information
Mihail Chindris authored and pcercuei committed Jun 18, 2021
1 parent 38aa87f commit 3a5caab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
39 changes: 31 additions & 8 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 70,32 @@ static ssize_t sanitize_xml(char *ptr, ssize_t len, const char *str)
return count;
}

ssize_t iio_xml_print_and_sanitized_param(char *ptr, ssize_t len,
const char *before, char *param,
const char *after)
{
ssize_t ret, alen = 0;

/* Print before */
ret = iio_snprintf(ptr, len, "%s", before);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);

/* Print param */
ret = sanitize_xml(ptr, len, param);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);

/* Print after */
ret = iio_snprintf(ptr, len, "%s", after);
if (ret < 0)
return ret;

return alen ret;
}

static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len,
const struct iio_context *ctx)
{
Expand All @@ -91,18 117,15 @@ static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len,

for (i = 0; i < ctx->nb_attrs; i ) {
ret = iio_snprintf(ptr, len,
"<context-attribute name=\"%s\" value=\"",
"<context-attribute name=\"%s\" ",
ctx->attrs[i]);
if (ret < 0)
return ret;

iio_update_xml_indexes(ret, &ptr, &len, &alen);
ret = sanitize_xml(ptr, len, ctx->values[i]);
if (ret < 0)
return ret;

iio_update_xml_indexes(ret, &ptr, &len, &alen);
ret = iio_snprintf(ptr, len, "\" />");
ret = iio_xml_print_and_sanitized_param(ptr, len,
"value=\"",
ctx->values[i],
"\" />");
if (ret < 0)
return ret;

Expand Down
4 changes: 4 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 271,10 @@ int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr,

ssize_t __iio_printf iio_snprintf(char *buf, size_t len, const char *fmt, ...);

ssize_t iio_xml_print_and_sanitized_param(char *ptr, ssize_t len,
const char *before, char *param,
const char *after);

static inline void iio_update_xml_indexes(ssize_t ret, char **ptr, ssize_t *len,
ssize_t *alen)
{
Expand Down

0 comments on commit 3a5caab

Please sign in to comment.