BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file value_utils.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  
30 #include <stdint.h>
31 #include <stddef.h>
32 #include <limits.h>
33  
34 #include <misc/debug.h>
35 #include <misc/parse_number.h>
36 #include <misc/strdup.h>
37 #include <misc/balloc.h>
38 #include <system/BTime.h>
39 #include <ncd/NCDVal.h>
40 #include <ncd/NCDStringIndex.h>
41 #include <ncd/NCDModule.h>
42 #include <ncd/static_strings.h>
43  
44 #include "value_utils.h"
45  
46 int ncd_is_none (NCDValRef string)
47 {
48 ASSERT(NCDVal_IsString(string))
49  
50 if (NCDVal_IsIdString(string)) {
51 return NCDVal_IdStringId(string) == NCD_STRING_NONE;
52 } else {
53 return NCDVal_StringEquals(string, "<none>");
54 }
55 }
56  
57 NCDValRef ncd_make_boolean (NCDValMem *mem, int value)
58 {
59 ASSERT(mem)
60  
61 NCD_string_id_t str_id = (value ? NCD_STRING_TRUE : NCD_STRING_FALSE);
62 return NCDVal_NewIdString(mem, str_id);
63 }
64  
65 int ncd_read_boolean (NCDValRef val, int *out)
66 {
67 ASSERT(out)
68  
69 if (!NCDVal_IsString(val)) {
70 return 0;
71 }
72 if (NCDVal_IsIdString(val)) {
73 *out = NCDVal_IdStringId(val) == NCD_STRING_TRUE;
74 } else {
75 *out = NCDVal_StringEquals(val, "true");
76 }
77 return 1;
78 }
79  
80 int ncd_read_uintmax (NCDValRef val, uintmax_t *out)
81 {
82 ASSERT(out)
83  
84 if (!NCDVal_IsString(val)) {
85 return 0;
86 }
87  
88 return parse_unsigned_integer(NCDVal_StringMemRef(val), out);
89 }
90  
91 int ncd_read_time (NCDValRef val, btime_t *out)
92 {
93 ASSERT(out)
94  
95 uintmax_t n;
96 if (!ncd_read_uintmax(val, &n)) {
97 return 0;
98 }
99  
100 if (n > INT64_MAX) {
101 return 0;
102 }
103  
104 *out = n;
105 return 1;
106 }
107  
108 NCD_string_id_t ncd_get_string_id (NCDValRef string)
109 {
110 ASSERT(NCDVal_IsString(string))
111  
112 if (NCDVal_IsIdString(string)) {
113 return NCDVal_IdStringId(string);
114 }
115  
116 return NCDStringIndex_GetBinMr(NCDValMem_StringIndex(string.mem), NCDVal_StringMemRef(string));
117 }
118  
119 NCDValRef ncd_make_uintmax (NCDValMem *mem, uintmax_t value)
120 {
121 ASSERT(mem)
122  
123 int size = compute_decimal_repr_size(value);
124  
125 NCDValRef val = NCDVal_NewStringUninitialized(mem, size);
126  
127 if (!NCDVal_IsInvalid(val)) {
128 char *data = (char *)NCDVal_StringData(val);
129 generate_decimal_repr(value, data, size);
130 }
131  
132 return val;
133 }
134  
135 char * ncd_strdup (NCDValRef stringnonulls)
136 {
137 ASSERT(NCDVal_IsStringNoNulls(stringnonulls))
138  
139 return MemRef_StrDup(NCDVal_StringMemRef(stringnonulls));
140 }
141  
142 int ncd_eval_func_args_ext (NCDCall const *call, size_t start, size_t count, NCDValMem *mem, NCDValRef *out)
143 {
144 ASSERT(start <= NCDCall_ArgCount(call))
145 ASSERT(count <= NCDCall_ArgCount(call) - start)
146  
147 *out = NCDVal_NewList(mem, count);
148 if (NCDVal_IsInvalid(*out)) {
149 goto fail;
150 }
151  
152 for (size_t i = 0; i < count; i++) {
153 NCDValRef elem = NCDCall_EvalArg(call, start + i, mem);
154 if (NCDVal_IsInvalid(elem)) {
155 goto fail;
156 }
157 if (!NCDVal_ListAppend(*out, elem)) {
158 goto fail;
159 }
160 }
161  
162 return 1;
163  
164 fail:
165 return 0;
166 }
167  
168 int ncd_eval_func_args (NCDCall const *call, NCDValMem *mem, NCDValRef *out)
169 {
170 return ncd_eval_func_args_ext(call, 0, NCDCall_ArgCount(call), mem, out);
171 }