Fix spelling mistakes in number of comments
[mech_eap.git] / src / utils / common.c
index 5cf0d57..04a533a 100644 (file)
@@ -86,7 +86,7 @@ int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable)
                return -1;
 
        /* check for optional mask */
-       if (*r == '\0' || isspace(*r)) {
+       if (*r == '\0' || isspace((unsigned char) *r)) {
                /* no mask specified, assume default */
                os_memset(mask, 0xff, ETH_ALEN);
        } else if (maskable && *r == '/') {
@@ -498,7 +498,7 @@ void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
                        *txt++ = 't';
                        break;
                default:
-                       if (data[i] >= 32 && data[i] <= 127) {
+                       if (data[i] >= 32 && data[i] <= 126) {
                                *txt++ = data[i];
                        } else {
                                txt += os_snprintf(txt, end - txt, "\\x%02x",
@@ -697,6 +697,29 @@ int is_hex(const u8 *data, size_t len)
 }
 
 
+int has_ctrl_char(const u8 *data, size_t len)
+{
+       size_t i;
+
+       for (i = 0; i < len; i++) {
+               if (data[i] < 32 || data[i] == 127)
+                       return 1;
+       }
+       return 0;
+}
+
+
+int has_newline(const char *str)
+{
+       while (*str) {
+               if (*str == '\n' || *str == '\r')
+                       return 1;
+               str++;
+       }
+       return 0;
+}
+
+
 size_t merge_byte_arrays(u8 *res, size_t res_len,
                         const u8 *src1, size_t src1_len,
                         const u8 *src2, size_t src2_len)
@@ -973,6 +996,48 @@ int random_mac_addr_keep_oui(u8 *addr)
 
 
 /**
+ * cstr_token - Get next token from const char string
+ * @str: a constant string to tokenize
+ * @delim: a string of delimiters
+ * @last: a pointer to a character following the returned token
+ *      It has to be set to NULL for the first call and passed for any
+ *      further call.
+ * Returns: a pointer to token position in str or NULL
+ *
+ * This function is similar to str_token, but it can be used with both
+ * char and const char strings. Differences:
+ * - The str buffer remains unmodified
+ * - The returned token is not a NULL terminated string, but a token
+ *   position in str buffer. If a return value is not NULL a size
+ *   of the returned token could be calculated as (last - token).
+ */
+const char * cstr_token(const char *str, const char *delim, const char **last)
+{
+       const char *end, *token = str;
+
+       if (!str || !delim || !last)
+               return NULL;
+
+       if (*last)
+               token = *last;
+
+       while (*token && os_strchr(delim, *token))
+               token++;
+
+       if (!*token)
+               return NULL;
+
+       end = token + 1;
+
+       while (*end && !os_strchr(delim, *end))
+               end++;
+
+       *last = end;
+       return token;
+}
+
+
+/**
  * str_token - Get next token from a string
  * @buf: String to tokenize. Note that the string might be modified.
  * @delim: String of delimiters
@@ -982,25 +1047,12 @@ int random_mac_addr_keep_oui(u8 *addr)
  */
 char * str_token(char *str, const char *delim, char **context)
 {
-       char *end, *pos = str;
-
-       if (*context)
-               pos = *context;
-
-       while (*pos && os_strchr(delim, *pos))
-               pos++;
-       if (!*pos)
-               return NULL;
-
-       end = pos + 1;
-       while (*end && !os_strchr(delim, *end))
-               end++;
+       char *token = (char *) cstr_token(str, delim, (const char **) context);
 
-       if (*end)
-               *end++ = '\0';
+       if (token && **context)
+               *(*context)++ = '\0';
 
-       *context = end;
-       return pos;
+       return token;
 }
 
 
@@ -1094,3 +1146,57 @@ int is_ctrl_char(char c)
 {
        return c > 0 && c < 32;
 }
+
+
+/**
+ * ssid_parse - Parse a string that contains SSID in hex or text format
+ * @buf: Input NULL terminated string that contains the SSID
+ * @ssid: Output SSID
+ * Returns: 0 on success, -1 otherwise
+ *
+ * The SSID has to be enclosed in double quotes for the text format or space
+ * or NULL terminated string of hex digits for the hex format. buf can include
+ * additional arguments after the SSID.
+ */
+int ssid_parse(const char *buf, struct wpa_ssid_value *ssid)
+{
+       char *tmp, *res, *end;
+       size_t len;
+
+       ssid->ssid_len = 0;
+
+       tmp = os_strdup(buf);
+       if (!tmp)
+               return -1;
+
+       if (*tmp != '"') {
+               end = os_strchr(tmp, ' ');
+               if (end)
+                       *end = '\0';
+       } else {
+               end = os_strchr(tmp + 1, '"');
+               if (!end) {
+                       os_free(tmp);
+                       return -1;
+               }
+
+               end[1] = '\0';
+       }
+
+       res = wpa_config_parse_string(tmp, &len);
+       if (res && len <= SSID_MAX_LEN) {
+               ssid->ssid_len = len;
+               os_memcpy(ssid->ssid, res, len);
+       }
+
+       os_free(tmp);
+       os_free(res);
+
+       return ssid->ssid_len ? 0 : -1;
+}
+
+
+int str_starts(const char *str, const char *start)
+{
+       return os_strncmp(str, start, os_strlen(start)) == 0;
+}