BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDValCons.h
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 #ifndef BADVPN_NCDVALCONS_H
31 #define BADVPN_NCDVALCONS_H
32  
33 #include <limits.h>
34 #include <stdint.h>
35  
36 #include <misc/debug.h>
37 #include <ncd/NCDVal.h>
38  
39 struct NCDValCons__temp_elem {
40 NCDValSafeRef ref;
41 int next;
42 };
43  
44 /**
45 * Value constructor; implements a mechanism for efficiently constructing
46 * NCD values into {@link NCDVal} compact representation, but without
47 * having to know the number of list or map elements in advance.
48 * For the purpose of value construction, values are representing using
49 * {@link NCDValConsVal} objects.
50 */
51 typedef struct {
52 NCDValMem *mem;
53 struct NCDValCons__temp_elem *elems;
54 int elems_size;
55 int elems_capacity;
56 } NCDValCons;
57  
58 #define NCDVALCONS_TYPE_COMPLETE 1
59 #define NCDVALCONS_TYPE_INCOMPLETE_LIST 2
60 #define NCDVALCONS_TYPE_INCOMPLETE_MAP 3
61  
62 /**
63 * Abstract handle which represents a value during constuction via
64 * {@link NCDValCons}.
65 */
66 typedef struct {
67 int cons_type;
68 union {
69 NCDValSafeRef complete_ref;
70 struct {
71 int elems_idx;
72 int count;
73 } incomplete;
74 } u;
75 } NCDValConsVal;
76  
77 #define NCDVALCONS_ERROR_MEMORY 1
78 #define NCDVALCONS_ERROR_DUPLICATE_KEY 2
79 #define NCDVALCONS_ERROR_DEPTH 3
80  
81 /**
82 * Initializes a value constructor.
83 *
84 * @param o value constructor to initialize
85 * @param mem memory object where values will be stored into
86 * @return 1 on success, 0 on failure
87 */
88 int NCDValCons_Init (NCDValCons *o, NCDValMem *mem) WARN_UNUSED;
89  
90 /**
91 * Frees the value constructor. This only means the constuctor does
92 * not exist any more; any values constructed and completed using
93 * {@link NCDValCons_Complete} remain in the memory object.
94 *
95 * @param o value constructor to free
96 */
97 void NCDValCons_Free (NCDValCons *o);
98  
99 /**
100 * Creates a new string value with the given data.
101 *
102 * @param o value constructor
103 * @param data pointer to string data. This must not point into the
104 * memory object the value constructor is using. The data
105 * is copied.
106 * @param len length of the string
107 * @param out on success, *out will be set to a handle representing
108 * the new string
109 * @param out_error on failure, *out_error will be set to an error code
110 * @return 1 on success, 0 on failure
111 */
112 int NCDValCons_NewString (NCDValCons *o, const uint8_t *data, size_t len, NCDValConsVal *out, int *out_error) WARN_UNUSED;
113  
114 /**
115 * Creates an empty list value.
116 *
117 * @param o value constructor
118 * @param out *out will be set to a handle representing the new list
119 */
120 void NCDValCons_NewList (NCDValCons *o, NCDValConsVal *out);
121  
122 /**
123 * Creates an empty map value.
124 *
125 * @param o value constructor
126 * @param out *out will be set to a handle representing the new map
127 */
128 void NCDValCons_NewMap (NCDValCons *o, NCDValConsVal *out);
129  
130 /**
131 * Prepends an element to a list value.
132 *
133 * @param o value constructor
134 * @param list pointer to the handle representing the list. On success,
135 * the handle will be modified, and the old handle must not
136 * be used any more.
137 * @param elem handle representing the value to be prepended. This handle
138 * must not be used any more after being prepended to the list.
139 * @param out_error on failure, *out_error will be set to an error code
140 * @return 1 on success, 0 on failure
141 */
142 int NCDValCons_ListPrepend (NCDValCons *o, NCDValConsVal *list, NCDValConsVal elem, int *out_error) WARN_UNUSED;
143  
144 /**
145 * Inserts an entry into a map value.
146 *
147 * @param o value constructor
148 * @param map pointer to the handle representing the map. On success,
149 * the handle will be modified, and the old handle must not
150 * be used any more.
151 * @param key handle representing the key of the entry. This handle
152 * must not be used any more after being inserted into the map.
153 * @param value handle representing the value of the entry. This handle
154 * must not be used any more after being inserted into the
155 * map.
156 * @param out_error on failure, *out_error will be set to an error code
157 * @return 1 on success, 0 on failure
158 */
159 int NCDValCons_MapInsert (NCDValCons *o, NCDValConsVal *map, NCDValConsVal key, NCDValConsVal value, int *out_error) WARN_UNUSED;
160  
161 /**
162 * Completes a value represented by a {@link NCDValConsVal} handle,
163 * producing a {@link NCDValRef} object which refers to this value within
164 * the memory object.
165 *
166 * @param o value constructor
167 * @param val handle representing the value to be completed. After a value
168 * is completed, the handle must not be used any more.
169 * @param out on success, *out will be set to a {@link NCDValRef} object
170 * referencing the completed value
171 * @param out_error on failure, *out_error will be set to an error code
172 * @return 1 on success, 0 on failure
173 */
174 int NCDValCons_Complete (NCDValCons *o, NCDValConsVal val, NCDValRef *out, int *out_error) WARN_UNUSED;
175  
176 #endif