NCDValue.h 15 KB

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