BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file index.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Synopsis: |
||
32 | * index index(string value) |
||
33 | * index index::next() |
||
34 | * |
||
35 | * Description: |
||
36 | * Non-negative integer with range of a size_t. |
||
37 | * The first form creates an index from the given decimal string. |
||
38 | * The second form cretes an index with value one more than an existing |
||
39 | * index. |
||
40 | * |
||
41 | * Variables: |
||
42 | * string (empty) - the index value. Note this may be different from |
||
43 | * than the value given to index() if it was not in normal form. |
||
44 | */ |
||
45 | |||
46 | #include <stdlib.h> |
||
47 | #include <string.h> |
||
48 | |||
49 | #include <ncd/module_common.h> |
||
50 | |||
51 | #include <generated/blog_channel_ncd_index.h> |
||
52 | |||
53 | struct instance { |
||
54 | NCDModuleInst *i; |
||
55 | size_t value; |
||
56 | }; |
||
57 | |||
58 | static void func_new_templ (void *vo, NCDModuleInst *i, size_t value) |
||
59 | { |
||
60 | struct instance *o = vo; |
||
61 | o->i = i; |
||
62 | |||
63 | // set value |
||
64 | o->value = value; |
||
65 | |||
66 | // signal up |
||
67 | NCDModuleInst_Backend_Up(o->i); |
||
68 | } |
||
69 | |||
70 | static void func_new_from_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
71 | { |
||
72 | // read arguments |
||
73 | NCDValRef arg_value; |
||
74 | if (!NCDVal_ListRead(params->args, 1, &arg_value)) { |
||
75 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
76 | goto fail0; |
||
77 | } |
||
78 | |||
79 | // parse value |
||
80 | uintmax_t value; |
||
81 | if (!ncd_read_uintmax(arg_value, &value)) { |
||
82 | ModuleLog(i, BLOG_ERROR, "wrong value"); |
||
83 | goto fail0; |
||
84 | } |
||
85 | |||
86 | // check overflow |
||
87 | if (value > SIZE_MAX) { |
||
88 | ModuleLog(i, BLOG_ERROR, "value too large"); |
||
89 | goto fail0; |
||
90 | } |
||
91 | |||
92 | func_new_templ(vo, i, value); |
||
93 | return; |
||
94 | |||
95 | fail0: |
||
96 | NCDModuleInst_Backend_DeadError(i); |
||
97 | } |
||
98 | |||
99 | static void func_new_from_index (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
100 | { |
||
101 | struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user); |
||
102 | |||
103 | // check overflow |
||
104 | if (index->value == SIZE_MAX) { |
||
105 | ModuleLog(i, BLOG_ERROR, "overflow"); |
||
106 | goto fail0; |
||
107 | } |
||
108 | |||
109 | func_new_templ(vo, i, index->value + 1); |
||
110 | return; |
||
111 | |||
112 | fail0: |
||
113 | NCDModuleInst_Backend_DeadError(i); |
||
114 | } |
||
115 | |||
116 | static void func_die (void *vo) |
||
117 | { |
||
118 | struct instance *o = vo; |
||
119 | |||
120 | NCDModuleInst_Backend_Dead(o->i); |
||
121 | } |
||
122 | |||
123 | static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) |
||
124 | { |
||
125 | struct instance *o = vo; |
||
126 | |||
127 | if (!strcmp(name, "")) { |
||
128 | *out = ncd_make_uintmax(mem, o->value); |
||
129 | return 1; |
||
130 | } |
||
131 | |||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | static struct NCDModule modules[] = { |
||
136 | { |
||
137 | .type = "index", |
||
138 | .func_new2 = func_new_from_value, |
||
139 | .func_die = func_die, |
||
140 | .func_getvar = func_getvar, |
||
141 | .alloc_size = sizeof(struct instance) |
||
142 | }, { |
||
143 | .type = "index::next", |
||
144 | .base_type = "index", |
||
145 | .func_new2 = func_new_from_index, |
||
146 | .func_die = func_die, |
||
147 | .func_getvar = func_getvar, |
||
148 | .alloc_size = sizeof(struct instance) |
||
149 | }, { |
||
150 | .type = NULL |
||
151 | } |
||
152 | }; |
||
153 | |||
154 | const struct NCDModuleGroup ncdmodule_index = { |
||
155 | .modules = modules |
||
156 | }; |