NCDValue.h 15 KB

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