NCDValue.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**
  2. * @file NCDValue.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. #ifndef BADVPN_NCD_NCDVALUE_H
  30. #define BADVPN_NCD_NCDVALUE_H
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <misc/debug.h>
  34. #include <structure/LinkedList2.h>
  35. #include <structure/BAVL.h>
  36. #define NCDVALUE_STRING 1
  37. #define NCDVALUE_LIST 2
  38. #define NCDVALUE_MAP 3
  39. /**
  40. * Holds an NCD "value", which is used in the NCD programming when passing arguments to
  41. * statements, among other uses.
  42. *
  43. * Each value is of one of the following three types:
  44. * - String (NCDVALUE_STRING); holds an array of arbitrary bytes, of any size.
  45. * - List (NCDVALUE_LIST); holds an ordered set of any number of values (by recursive
  46. * definition).
  47. * - Map (NCDVALUE_MAP); holds a set of (key, value) pairs, where both 'key' and 'value'
  48. * are values (by recursive definition), and 'key' is unique.
  49. *
  50. * A valid NCDValue structure may be copied freely, which results in multiple valid NCDValue
  51. * structures holding the same value. When one of those is freed (or passed to a function
  52. * which proceeds to take ownership of the value), all the structures become invalid.
  53. * Similarly, if the value is modified via one structure, the others become invalid.
  54. */
  55. typedef struct {
  56. int type;
  57. union {
  58. struct {
  59. uint8_t *string;
  60. size_t string_len;
  61. };
  62. struct {
  63. LinkedList2 list;
  64. size_t list_count;
  65. };
  66. struct {
  67. BAVL map_tree;
  68. size_t map_count;
  69. };
  70. };
  71. } NCDValue;
  72. typedef struct {
  73. LinkedList2Node list_node;
  74. NCDValue v;
  75. } NCDListElement;
  76. typedef struct {
  77. BAVLNode map_tree_node;
  78. NCDValue key;
  79. NCDValue val;
  80. } NCDMapElement;
  81. /**
  82. * Initializes a value by copying an existing value.
  83. *
  84. * @param o value structure to initialize
  85. * @param v an existing value to copy
  86. * @return 1 on success, 0 on failure
  87. */
  88. int NCDValue_InitCopy (NCDValue *o, NCDValue *v) WARN_UNUSED;
  89. /**
  90. * Frees a value.
  91. *
  92. * @param o value to free
  93. */
  94. void NCDValue_Free (NCDValue *o);
  95. /**
  96. * Returns the type of a value.
  97. *
  98. * @param o the value
  99. * @return type of value; one of NCDVALUE_STRING, NCDVALUE_LIST and NCDVALUE_MAP.
  100. */
  101. int NCDValue_Type (NCDValue *o);
  102. /**
  103. * Checks if the value is a string value.
  104. *
  105. * @param o the value
  106. * @return 1 if string, 0 if not
  107. */
  108. int NCDValue_IsString (NCDValue *o);
  109. /**
  110. * Checks if the value is a string value and does not contain
  111. * any null bytes.
  112. *
  113. * @param o the value
  114. * @return 1 if string with no nulls, 0 if not
  115. */
  116. int NCDValue_IsStringNoNulls (NCDValue *o);
  117. /**
  118. * Initializes a string value from a null-terminated string.
  119. * This function can only be used to create string values which do
  120. * not contain any null bytes. To create a string which may contain
  121. * null bytes, use {@link NCDValue_InitStringBin}.
  122. *
  123. * @param o value structure to initialize
  124. * @param str null-terminated string
  125. * @return 1 on success, 0 on failure
  126. */
  127. int NCDValue_InitString (NCDValue *o, const char *str) WARN_UNUSED;
  128. /**
  129. * Initializes a string value from a byte array.
  130. *
  131. * @param o value structure to initialize
  132. * @param str byte array
  133. * @param len number of bytes in byte array
  134. * @return 1 on success, 0 on failure
  135. */
  136. int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len) WARN_UNUSED;
  137. /**
  138. * Returns the pointer to the bytes of a string value. The string is always
  139. * null-terminated (but it itself contain null bytes).
  140. *
  141. * @param o string value
  142. * @return pointer to null-terminated array of bytes
  143. */
  144. char * NCDValue_StringValue (NCDValue *o);
  145. /**
  146. * Returns the length of the string (excuding the internal null termination,
  147. * but including any null bytes in the data).
  148. *
  149. * @param o string value
  150. * @return length of string
  151. */
  152. size_t NCDValue_StringLength (NCDValue *o);
  153. /**
  154. * Checks whether a string contains no null bytes in its data, i.e. strlen(str)==length.
  155. *
  156. * @param o string value
  157. * @return 1 if no null, 0 if nulls
  158. */
  159. int NCDValue_StringHasNoNulls (NCDValue *o);
  160. /**
  161. * Checks whether a string contains any null bytes in its data, i.e. strlen(str) < length.
  162. *
  163. * @param o string value
  164. * @return 1 if nulls, 0 if no nulls
  165. */
  166. int NCDValue_StringHasNulls (NCDValue *o);
  167. /**
  168. * Checks whether the string value is equal to the given null-terminated string.
  169. * Note that this is not equivalent to strcmp()==0, because the string value may
  170. *
  171. * @param o string value
  172. * @param str null-terminated string to compare against
  173. * @return 1 if equal, 0 if not
  174. */
  175. int NCDValue_StringEquals (NCDValue *o, const char *str);
  176. /**
  177. * Checks if the value is a list value.
  178. *
  179. * @param o the value
  180. * @return 1 if list, 0 if not
  181. */
  182. int NCDValue_IsList (NCDValue *o);
  183. /**
  184. * Initializes an empty list value.
  185. *
  186. * @param o value structure to initialize
  187. */
  188. void NCDValue_InitList (NCDValue *o);
  189. /**
  190. * Appends a value to the end of a list.
  191. * On success, the value that was passed for insertion must be assumed freed;
  192. * on failure, it is unaffected.
  193. *
  194. * @param o list value
  195. * @param v value to append
  196. * @return 1 on success, 0 on failure
  197. */
  198. int NCDValue_ListAppend (NCDValue *o, NCDValue v) WARN_UNUSED;
  199. /**
  200. * Appends values from a list to the end of a list.
  201. * On success, the list value that was passed with elements for insertion must be
  202. * assumed freed; on failure, it is unaffected.
  203. *
  204. * @param o list value
  205. * @param l list value whose elements to append
  206. * @return 1 on success, 0 on failure
  207. */
  208. int NCDValue_ListAppendList (NCDValue *o, NCDValue l) WARN_UNUSED;
  209. /**
  210. * Returns the number of elements in a list.
  211. *
  212. * @param o list value
  213. * @return number of elements
  214. */
  215. size_t NCDValue_ListCount (NCDValue *o);
  216. /**
  217. * Returns a pointer to the first elements in a list, or NULL if there are no
  218. * elements.
  219. *
  220. * @param o list value
  221. * @return pointer to first value, or NULL
  222. */
  223. NCDValue * NCDValue_ListFirst (NCDValue *o);
  224. /**
  225. * Given a pointer to an existing element in a list, returns a pointer to the
  226. * element that follows it, or NULL if it is the last.
  227. * Note that the element pointer must point to a value that is really in the list
  228. * right now, and not just equal.
  229. *
  230. * @param o list value
  231. * @param ev pointer to an existing element in the list
  232. * @return pointer to next value, or NULL
  233. */
  234. NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev);
  235. /**
  236. * Attempts to retrieve pointers to elements from a list.
  237. * Pass exactly 'num' extra NCDValue ** arguments. If the list has exactly
  238. * 'num' elements, this function succeeds, and returns pointers to them via the
  239. * passed variable arguments; if not, it fails.
  240. *
  241. * @param o list value
  242. * @param num number of values to read. Must be >=0, and exactly that many
  243. * variable arguments of type NCDValue ** must follow, all non-NULL.
  244. * @return 1 on succees, 0 on failure
  245. */
  246. int NCDValue_ListRead (NCDValue *o, int num, ...) WARN_UNUSED;
  247. /**
  248. * Like {@link NCDValue_ListRead}, but the list only needs to have >= 'num' values,
  249. * instead of exactly 'num'.
  250. */
  251. int NCDValue_ListReadHead (NCDValue *o, int num, ...) WARN_UNUSED;
  252. /**
  253. * Returns a pointer to the element of the list at the given position.
  254. * This performs a linear search from the beginning.
  255. *
  256. * @param o list value
  257. * @param pos index of element to retrieve; must be < length.
  258. */
  259. NCDValue * NCDValue_ListGet (NCDValue *o, size_t pos);
  260. /**
  261. * Removes the first element from a list and returns it.
  262. * The caller takes ownership of the removed value and is responsible for freeing
  263. * it.
  264. * The list must have at least one element.
  265. *
  266. * @param o list value
  267. * @return value that was the first on the list
  268. */
  269. NCDValue NCDValue_ListShift (NCDValue *o);
  270. /**
  271. * Removes an element from a list and returns it.
  272. * The caller takes ownership of the removed value and is responsible for freeing
  273. * it; the passed element pointer becomes invalid.
  274. * Note that the element pointer must point to a value that is really in the list
  275. * right now, and not just equal.
  276. *
  277. * @param o list value
  278. * @param ev pointer to element of list to remove
  279. * @return value that was just removed
  280. */
  281. NCDValue NCDValue_ListRemove (NCDValue *o, NCDValue *ev);
  282. /**
  283. * Checks if the value is a map value.
  284. *
  285. * @param o the value
  286. * @return 1 if map, 0 if not
  287. */
  288. int NCDValue_IsMap (NCDValue *o);
  289. /**
  290. * Initializes an empty map value.
  291. *
  292. * @param o value structure to initialize
  293. */
  294. void NCDValue_InitMap (NCDValue *o);
  295. /**
  296. * Returns the number of entries in a map.
  297. *
  298. * @param o map value
  299. * @return number of entries
  300. */
  301. size_t NCDValue_MapCount (NCDValue *o);
  302. /**
  303. * Returns the pointer to the first key in the map, or NULL if
  304. * the map is empty.
  305. * The keys are ordered according to {@link NCDValue_Compare}.
  306. *
  307. * @param o map value
  308. * @return pointer to first key, or NULL
  309. */
  310. NCDValue * NCDValue_MapFirstKey (NCDValue *o);
  311. /**
  312. * Given a pointer to an existing key in a map, returns a pointer to the
  313. * key that follows it, or NULL if this is the last key.
  314. * Note that the key pointer must point to a value that is really a key in the map
  315. * right now, and not just equal to some key.
  316. *
  317. * @param o map value
  318. * @param ekey pointer to an existing key in the map
  319. * @return pointer to next key, or NULL
  320. */
  321. NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
  322. /**
  323. * Given a pointer to an existing key in a map, returns a pointer to the
  324. * value associated with it.
  325. * Note that the key pointer must point to a value that is really a key in the
  326. * map right now, and not just equal.
  327. *
  328. * @param o map value
  329. * @param ekey pointer to an existing key in the map
  330. * @return pointer to the associated value
  331. */
  332. NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
  333. /**
  334. * Looks for a key in a map that is equal to the given key.
  335. *
  336. * @param o map value
  337. * @param key key to look for
  338. * @return pointer to the key in the map, or NULL if not found
  339. */
  340. NCDValue * NCDValue_MapFindKey (NCDValue *o, NCDValue *key);
  341. /**
  342. * Inserts a (key, value) entry into the map.
  343. * The map must not already contain a key equal to the provided key.
  344. * On success, the key and value that were passed for insertion must be assumed freed;
  345. * on failure, they are unaffected.
  346. *
  347. * @param o map value
  348. * @param key key to insert
  349. * @param val value to insert
  350. * @return pointer to the newly inserted key in the map, or NULL if insertion failed.
  351. */
  352. NCDValue * NCDValue_MapInsert (NCDValue *o, NCDValue key, NCDValue val) WARN_UNUSED;
  353. /**
  354. * Removes an entry from the map and returns the key and value that were just removed.
  355. * The entry to remove is specified by a pointer to an existing key in the map.
  356. * The caller takes ownership of the removed key and value value and is responsible for
  357. * freeing them; the passed key pointer becomes invalid.
  358. * Note that the key pointer must point to a value that is really a key in the map
  359. * right now, and not just equal to some key.
  360. *
  361. * @param o map value
  362. * @param ekey pointer to an existing key in the map whose entry to remove
  363. * @param out_key the key of the removed entry will be returned here; must not be NULL.
  364. * @param out_val the value of the removed entry will be returned here; must not be NULL.
  365. */
  366. void NCDValue_MapRemove (NCDValue *o, NCDValue *ekey, NCDValue *out_key, NCDValue *out_val);
  367. /**
  368. * Looks for an entry in the map with a string key equal to the given null-terminated
  369. * string.
  370. * If such key exists, it returns a pointer to its associated value; if not, it returns
  371. * NULL.
  372. * NOTE: this returns a pointer to the value, not the key, unlike
  373. * {@link NCDValue_MapFindKey}.
  374. *
  375. * @param o map value
  376. * @param key_str null-terminated string specifying the key to look for
  377. * @return pointer to value, or NULL if there is no such key
  378. */
  379. NCDValue * NCDValue_MapFindValueByString (NCDValue *o, const char *key_str);
  380. /**
  381. * Compares a value with another value.
  382. * This function defines a total order on the set of all possible values.
  383. *
  384. * @param o first value
  385. * @param v second value
  386. * @return -1 if 'o' is lesser than 'v', 0 if equal, 1 if greater
  387. */
  388. int NCDValue_Compare (NCDValue *o, NCDValue *v);
  389. #endif