NCDValue.h 14 KB

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