Skip to content

Commit

Permalink
fixup! sys: Add clif (CoRE Link Format) module
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrolanzieri committed Oct 2, 2019
1 parent d1ad8e1 commit bcc8819
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
73 changes: 38 additions & 35 deletions sys/clif/clif.c
Expand Up @@ -66,7 +66,8 @@ ssize_t clif_decode_link(clif_t *link, clif_attr_t *attrs, unsigned attrs_len,
const char *buf, size_t maxlen)
{

assert(buf && link);
assert(buf);
assert(link);

char *pos;
const char *end = buf + maxlen;
Expand Down Expand Up @@ -163,7 +164,8 @@ ssize_t clif_add_link_separator(char *buf, size_t maxlen)

ssize_t clif_add_attr(clif_attr_t *attr, char *buf, size_t maxlen)
{
assert(attr && attr->key);
assert(attr);
assert(attr->key);

/* if no length given, calculate it */
if (!attr->key_len) {
Expand Down Expand Up @@ -244,11 +246,12 @@ ssize_t clif_get_target(const char *input, size_t input_len, char **output)

ssize_t clif_get_attr(const char *input, size_t input_len, clif_attr_t *attr)
{
assert(input && attr);
assert(input);
assert(attr);
const char *pos = input;
const char *end = input + input_len;
unsigned quoted = 0;
ssize_t ret_val = CLIF_NOT_FOUND;
bool quoted = false;
bool scan_value = false;

/* initialize attr */
attr->value = NULL;
Expand All @@ -257,7 +260,7 @@ ssize_t clif_get_attr(const char *input, size_t input_len, clif_attr_t *attr)
/* an attribute should start with the separator */
if (*pos != LF_ATTR_SEPARATOR_C) {
DEBUG("Attribute should start with separator, found %c\n", *pos);
goto out;
return CLIF_NOT_FOUND;
}
pos++;
attr->key = pos;
Expand All @@ -267,53 +270,52 @@ ssize_t clif_get_attr(const char *input, size_t input_len, clif_attr_t *attr)
if (*pos == LF_ATTR_SEPARATOR_C || *pos == LF_LINK_SEPARATOR_C) {
/* key ends, no value */
attr->key_len = pos - attr->key;
goto calculate_out;
break;
}
if (*pos == LF_ATTR_VAL_SEPARATOR_C) {
/* key ends, has value */
attr->key_len = pos - attr->key;
/* check if the value is quoted and prepare pointer for value scan */
pos++;
if (*pos == '"') {
quoted = 1;
quoted = true;
pos++;
}
attr->value = (char *)pos;
goto scan_value;
scan_value = true;
break;
}
pos++;
}

/* buffer exhausted and no special character found, calculate length of
* attribute and exit */
attr->key_len = pos - attr->key;
goto calculate_out;

scan_value:
/* iterate over value */
while (pos < end) {
if (quoted) {
if (*pos == '"' && *(pos - 1) != '\\') {
/* found unescaped quote */
attr->value_len = pos - attr->value;
pos++;
break;
if (scan_value) {
/* iterate over value */
while (pos < end) {
if (quoted) {
if (*pos == '"' && *(pos - 1) != '\\') {
/* found unescaped quote */
attr->value_len = pos - attr->value;
pos++;
break;
}
}
}
else {
if (*pos == LF_ATTR_SEPARATOR_C || *pos == LF_LINK_SEPARATOR_C) {
/* value ends */
attr->value_len = pos - attr->value;
break;
else {
if (*pos == LF_ATTR_SEPARATOR_C || *pos == LF_LINK_SEPARATOR_C) {
/* value ends */
attr->value_len = pos - attr->value;
break;
}
}
pos++;
}
pos++;
}
else {
/* buffer exhausted and no special character found, calculate length of
* attribute and exit */
attr->key_len = pos - attr->key;
}

calculate_out:
ret_val = pos - input;
out:
return ret_val;
return pos - input;
}

ssize_t clif_attr_type_to_str(clif_attr_type_t type, const char **str)
Expand All @@ -327,7 +329,8 @@ ssize_t clif_attr_type_to_str(clif_attr_type_t type, const char **str)

clif_attr_type_t clif_get_attr_type(const char *input, size_t input_len)
{
assert(input && input_len > 0);
assert(input);
assert(input_len > 0);
clif_attr_type_t ret = CLIF_ATTR_EXT;
for (unsigned i = 0; i < ATTRS_NUMOF; i++) {
if (input_len == _attr_to_size[i] &&
Expand Down
6 changes: 3 additions & 3 deletions sys/include/clif.h
Expand Up @@ -145,9 +145,9 @@ typedef struct {
* @brief Link format descriptor
*/
typedef struct {
char *target; /**< target string */
unsigned target_len; /**< length of target string */
clif_attr_t *attrs; /**< array of attributes */
char *target; /**< target string */
unsigned target_len; /**< length of target string */
clif_attr_t *attrs; /**< array of attributes */
unsigned attrs_len; /**< size of array of attributes */
} clif_t;

Expand Down

0 comments on commit bcc8819

Please sign in to comment.