BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file valuemetic.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 * Comparison functions for values.
32 *
33 * Synopsis:
34 * val_lesser(v1, v2)
35 * val_greater(v1, v2)
36 * val_lesser_equal(v1, v2)
37 * val_greater_equal(v1, v2)
38 * val_equal(v1, v2)
39 * val_different(v1, v2)
40 *
41 * Variables:
42 * (empty) - "true" or "false", reflecting the value of the relation in question
43 *
44 * Description:
45 * These statements perform comparisons of values. Order of values is defined by the
46 * following rules:
47 * 1. Values of different types have the following order: strings, lists, maps.
48 * 2. String values are ordered lexicographically, with respect to the numeric values
49 * of their bytes.
50 * 3. List values are ordered lexicographically, where the order of the elements is
51 * defined by recursive application of these rules.
52 * 4. Map values are ordered lexicographically, as if a map was a list of (key, value)
53 * pairs ordered by key, where the order of both keys and values is defined by
54 * recursive application of these rules.
55 */
56  
57 #include <stdlib.h>
58 #include <string.h>
59  
60 #include <ncd/module_common.h>
61  
62 #include <generated/blog_channel_ncd_valuemetic.h>
63  
64 struct instance {
65 NCDModuleInst *i;
66 int result;
67 };
68  
69 typedef int (*compute_func) (NCDValRef v1, NCDValRef v2);
70  
71 static int compute_lesser (NCDValRef v1, NCDValRef v2)
72 {
73 return NCDVal_Compare(v1, v2) < 0;
74 }
75  
76 static int compute_greater (NCDValRef v1, NCDValRef v2)
77 {
78 return NCDVal_Compare(v1, v2) > 0;
79 }
80  
81 static int compute_lesser_equal (NCDValRef v1, NCDValRef v2)
82 {
83 return NCDVal_Compare(v1, v2) <= 0;
84 }
85  
86 static int compute_greater_equal (NCDValRef v1, NCDValRef v2)
87 {
88 return NCDVal_Compare(v1, v2) >= 0;
89 }
90  
91 static int compute_equal (NCDValRef v1, NCDValRef v2)
92 {
93 return NCDVal_Compare(v1, v2) == 0;
94 }
95  
96 static int compute_different (NCDValRef v1, NCDValRef v2)
97 {
98 return NCDVal_Compare(v1, v2) != 0;
99 }
100  
101 static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, compute_func cfunc)
102 {
103 struct instance *o = vo;
104 o->i = i;
105  
106 NCDValRef v1_arg;
107 NCDValRef v2_arg;
108 if (!NCDVal_ListRead(params->args, 2, &v1_arg, &v2_arg)) {
109 ModuleLog(i, BLOG_ERROR, "wrong arity");
110 goto fail0;
111 }
112  
113 o->result = cfunc(v1_arg, v2_arg);
114  
115 NCDModuleInst_Backend_Up(i);
116 return;
117  
118 fail0:
119 NCDModuleInst_Backend_DeadError(i);
120 }
121  
122 static void func_die (void *vo)
123 {
124 struct instance *o = vo;
125  
126 NCDModuleInst_Backend_Dead(o->i);
127 }
128  
129 static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
130 {
131 struct instance *o = vo;
132  
133 if (name == NCD_STRING_EMPTY) {
134 *out = ncd_make_boolean(mem, o->result);
135 return 1;
136 }
137  
138 return 0;
139 }
140  
141 static void func_new_lesser (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
142 {
143 new_templ(vo, i, params, compute_lesser);
144 }
145  
146 static void func_new_greater (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
147 {
148 new_templ(vo, i, params, compute_greater);
149 }
150  
151 static void func_new_lesser_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
152 {
153 new_templ(vo, i, params, compute_lesser_equal);
154 }
155  
156 static void func_new_greater_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
157 {
158 new_templ(vo, i, params, compute_greater_equal);
159 }
160  
161 static void func_new_equal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
162 {
163 new_templ(vo, i, params, compute_equal);
164 }
165  
166 static void func_new_different (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
167 {
168 new_templ(vo, i, params, compute_different);
169 }
170  
171 static struct NCDModule modules[] = {
172 {
173 .type = "val_lesser",
174 .func_new2 = func_new_lesser,
175 .func_die = func_die,
176 .func_getvar2 = func_getvar2,
177 .alloc_size = sizeof(struct instance)
178 }, {
179 .type = "val_greater",
180 .func_new2 = func_new_greater,
181 .func_die = func_die,
182 .func_getvar2 = func_getvar2,
183 .alloc_size = sizeof(struct instance)
184 }, {
185 .type = "val_lesser_equal",
186 .func_new2 = func_new_lesser_equal,
187 .func_die = func_die,
188 .func_getvar2 = func_getvar2,
189 .alloc_size = sizeof(struct instance)
190 }, {
191 .type = "val_greater_equal",
192 .func_new2 = func_new_greater_equal,
193 .func_die = func_die,
194 .func_getvar2 = func_getvar2,
195 .alloc_size = sizeof(struct instance)
196 }, {
197 .type = "val_equal",
198 .func_new2 = func_new_equal,
199 .func_die = func_die,
200 .func_getvar2 = func_getvar2,
201 .alloc_size = sizeof(struct instance)
202 }, {
203 .type = "val_different",
204 .func_new2 = func_new_different,
205 .func_die = func_die,
206 .func_getvar2 = func_getvar2,
207 .alloc_size = sizeof(struct instance)
208 }, {
209 .type = NULL
210 }
211 };
212  
213 const struct NCDModuleGroup ncdmodule_valuemetic = {
214 .modules = modules
215 };