Fix ordering in perl subscription_list
[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         if (str == NULL)
46                 return 0;
47         if (cookie == NULL)
48                 return 0;
49
50         char *token_begin = strchr(str, '/');
51         if (token_begin == NULL) {
52                 return 0;
53         }
54         int len = token_begin - str;
55         if (len > MAX_TAG_LENGTH || len == 0) {
56                 return 0;
57         }
58         memcpy(cookie->name, str, len);
59         cookie->name[len] = 0;
60
61         token_begin++;
62         len = strlen(token_begin);
63         if (len != TOKEN_SIZE * 2) {
64                 return 0;
65         }
66         memcpy(cookie->token, token_begin, TOKEN_SIZE * 2);
67         cookie->token[TOKEN_SIZE * 2] = 0;
68
69         return 1;
70 }
71
72 uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
73         uint64_t len = to - from + 1;
74         uint64_t *list = malloc(len * sizeof(uint64_t));
75         uint64_t i;
76
77         for (i = 0; i < len; i++) {
78                 list[i] = from + i;
79         }
80
81         return list;
82 }