resourcematch.c 4.19 KB
Newer Older
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
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <errno.h>

#include "resourcematch.h"

static size_t count_groups(const char *pattern)
{
    size_t ret = 0;

    while (*pattern) {
        for (; *pattern && *pattern != '%'; pattern++);

        if (!*pattern) {
            return ret;
        }

        pattern++;

        if (!*pattern)
            return ret;

        if (*pattern != '%')
            ret++;

        pattern++;
    }

    return ret;
}

static resourcematch_extract_t * allocate_extract(const char *pattern)
{
    size_t groups = count_groups(pattern);
    resourcematch_extract_t *ret;

    ret = calloc(1, sizeof(*ret));
    if (!ret)
        return NULL;

    ret->groups = groups;
    ret->group = calloc(groups, sizeof(*ret->group));

    return ret;
}

static void strip_common_prefix(const char **pattern, const char **string)
{
    const char *p = *pattern;
    const char *s = *string;

    for (; *p && *p != '%' && *p == *s; p++, s++);

    *pattern = p;
    *string = s;
}

static inline void setup_group(resourcematch_extract_t *extract, size_t idx, char type)
{
    if (!extract)
        return;

    extract->group[idx].type = type;
    extract->group[idx].raw = NULL;
}

static inline resourcematch_result_t match_lli(const char **string, resourcematch_extract_t *extract, size_t idx, int base)
{
    long long int ret;
    char *endptr;

    errno = 0;
    ret = strtoll(*string, &endptr, base);
85
    if (endptr == *string)
86
        return RESOURCEMATCH_NOMATCH;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    if (errno != 0)
        return RESOURCEMATCH_ERROR;

    if (extract) {
        extract->group[idx].result.lli = ret;
    }

    *string = endptr;

    return RESOURCEMATCH_MATCH;
}

resourcematch_result_t resourcematch_match(const char *pattern, const char *string, resourcematch_extract_t **extract)
{
    resourcematch_result_t ret;
    resourcematch_extract_t *matches = NULL;
    size_t idx = 0;

    if (!pattern || !string)
        return RESOURCEMATCH_ERROR;

    if (extract) {
        matches = allocate_extract(pattern);
        if (!matches)
            return RESOURCEMATCH_NOMATCH;
    }

    while (1) {
        strip_common_prefix(&pattern, &string);

        if (!*pattern && !*string) {
            if (extract)
                *extract = matches;

            return RESOURCEMATCH_MATCH;
        } else if (!*pattern || !*string) {
            if (extract)
                resourcematch_extract_free(matches);

            return RESOURCEMATCH_NOMATCH;
        }

        if (*pattern != '%') {
            if (extract)
                resourcematch_extract_free(matches);

            return RESOURCEMATCH_NOMATCH;
        }

        pattern++;

        switch (*pattern) {
            case '%':
                if (*string == '%') {
                    string++;
                } else {
                    if (extract)
                        resourcematch_extract_free(matches);

                    return RESOURCEMATCH_NOMATCH;
                }
            break;
#define _test_int(type,base) \
            case (type): \
                setup_group(matches, idx, *pattern); \
\
                ret = match_lli(&string, matches, idx, (base)); \
                if (ret != RESOURCEMATCH_MATCH) { \
                    if (extract) \
                        resourcematch_extract_free(matches); \
\
                    return ret; \
                } \
                idx++; \
            break;

            _test_int('i', 0);
            _test_int('d', 10);
            _test_int('x', 16);
            _test_int('o', 8);

            default:
                if (extract)
                    resourcematch_extract_free(matches);

                return RESOURCEMATCH_ERROR;
            break;
        }

        pattern++;
    }
}

void resourcematch_extract_free(resourcematch_extract_t *extract)
{
    size_t i;

    if (!extract)
        return;

    for (i = 0; i < extract->groups; i++) {
        free(extract->group[i].raw);
    }

    free(extract->group);
    free(extract);
}