Add a function for parsing combined auth cookies
[blerg.git] / common / app.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include "app.h"
8
9 int parse_url_info(const char *url, struct url_info *info) {
10         const char *c;
11         int len;
12         info->contents = 0;
13
14         c = strchr(url, '/');
15         if (c == NULL) {
16                 len = strlen(url);
17         } else {
18                 len = c - url;
19         }
20         if (len == 0)
21                 return 0;
22         if (len > MAX_TAG_LENGTH)
23                 len = MAX_TAG_LENGTH;
24         memcpy(info->name, url, len);
25         info->name[len] = 0;
26         info->contents |= URL_INFO_NAME;
27
28         if (c == NULL || c[1] == 0)
29                 return info->contents;
30
31         info->record = strtoull(c + 1, NULL, 10);
32         info->contents |= URL_INFO_RECORD;
33
34         c = strchr(c, '-');
35         if (c == NULL || c[1] == 0)
36                 return info->contents;
37
38         info->record_to = strtoull(c + 1, NULL, 10);
39         info->contents |= URL_INFO_RECORD_TO;
40
41         return info->contents;
42 }
43
44 int parse_auth_cookie(const char *str, struct auth_cookie *cookie) {
45         char *token_begin = strchr(str, '/');
46         if (token_begin == NULL) {
47                 return 0;
48         }
49         int len = token_begin - str;
50         if (len > MAX_TAG_LENGTH || len == 0) {
51                 return 0;
52         }
53         memcpy(cookie->name, str, len);
54         cookie->name[len] = 0;
55
56         token_begin++;
57         len = strlen(token_begin);
58         if (len != TOKEN_SIZE * 2) {
59                 return 0;
60         }
61         memcpy(cookie->token, token_begin, TOKEN_SIZE * 2);
62         cookie->token[TOKEN_SIZE * 2] = 0;
63
64         return 1;
65 }
66
67 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
68         uint64_t len = to - from + 1;
69         uint64_t *list = malloc(len * sizeof(uint64_t));
70         uint64_t i;
71
72         for (i = 0; i < len; i++) {
73                 list[i] = from + i;
74         }
75
76         return list;
77 }