aboutsummaryrefslogtreecommitdiff
path: root/src/utils.h
blob: f16ea3faf29442c791fbfb6a0c0821d229eb3ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef MAGI_INCLUDED_UTILS
#define MAGI_INCLUDED_UTILS


static void lowercase(char * str)
{
    if (str) {
        while (*str) {
            *str = tolower(*str);
            ++str;
        }
    }
}

static char * create_str(const char * begin, const char * end)
{
    char * res;
    res = malloc(end - begin + 1);
    if (res) {
        memcpy(res, begin, end - begin);
        res[end - begin] = 0;
    } else {
        magi_log("Cannot allocate string.");
    }
    return res;
}

static char * str_alloc(int len)
{
    char * str = malloc(len + 1);
    if (str) {
        str[len] = 0;
    } else {
        magi_log("[request] Cannot allocate string.");
    }
    return str;
}

/* Shouldn't be called with 'c' as not hex digit. */
static char from_hex(char c)
{
    char num;
    if (isdigit(c)) {
        num = c - '0';
    } else {
        num = toupper(c) - 'A' + 10;
    }
    return num;
}

static int is_hex(char c)
{
    return isdigit(c) || strchr("ABCDEF", toupper(c));
}

static int add(char ** dest, int * len, int * size, char c)
{
    int ok = 1;
    if (*len + 1 == *size) {
        *size *= 2;
        *dest = realloc(*dest, *size);
    }
    if (*dest == 0) {
        ok = 0;
        magi_log("Cannot allocate string.");
    } else {
        (*dest)[*len] = c;
        ++*len;
        (*dest)[*len] = 0;
    }
    return ok;
}

static int is_token(char c)
{
    return 32 <= c && c <= 126 && !strchr("()<>@,;:\\\"/[]?={} \t", c);
}

static int is_str_token(char * str)
{
    int is = str && *str; /* Empty string is not valid. */
    while (is && *str) {
        is = is_token(*str);
        ++str;
    }
    return is;
}


#endif