NCDVal.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /**
  2. * @file NCDVal.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_NCDVAL_H
  30. #define BADVPN_NCDVAL_H
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <misc/debug.h>
  34. #include <structure/CAvl.h>
  35. #include <ncd/NCDStringIndex.h>
  36. #include <ncd/NCDRefTarget.h>
  37. // these are implementation details. The interface is defined below.
  38. #define NCDVAL_FASTBUF_SIZE 64
  39. #define NCDVAL_FIRST_SIZE 256
  40. #define NCDVAL_MAX_DEPTH 32
  41. #define NCDVAL_MAXIDX INT_MAX
  42. #define NCDVAL_MINIDX INT_MIN
  43. typedef int NCDVal__idx;
  44. struct NCDVal__ref {
  45. NCDVal__idx next;
  46. NCDRefTarget *target;
  47. };
  48. struct NCDVal__string {
  49. int type;
  50. NCDVal__idx length;
  51. char data[];
  52. };
  53. struct NCDVal__list {
  54. int type;
  55. NCDVal__idx maxcount;
  56. NCDVal__idx count;
  57. NCDVal__idx elem_indices[];
  58. };
  59. struct NCDVal__mapelem {
  60. NCDVal__idx key_idx;
  61. NCDVal__idx val_idx;
  62. NCDVal__idx tree_child[2];
  63. NCDVal__idx tree_parent;
  64. int8_t tree_balance;
  65. };
  66. struct NCDVal__idstring {
  67. int type;
  68. NCD_string_id_t string_id;
  69. NCDStringIndex *string_index;
  70. };
  71. struct NCDVal__externalstring {
  72. int type;
  73. const char *data;
  74. size_t length;
  75. struct NCDVal__ref ref;
  76. };
  77. struct NCDVal__composedstring {
  78. int type;
  79. size_t offset;
  80. size_t length;
  81. void (*func_getptr) (void *, size_t, const char **, size_t *);
  82. void *user;
  83. struct NCDVal__ref ref;
  84. };
  85. struct NCDVal__cms_link {
  86. NCDVal__idx link_idx;
  87. NCDVal__idx next_cms_link;
  88. };
  89. typedef struct {
  90. char *buf;
  91. NCDVal__idx size;
  92. NCDVal__idx used;
  93. NCDVal__idx first_ref;
  94. NCDVal__idx first_cms_link;
  95. union {
  96. char fastbuf[NCDVAL_FASTBUF_SIZE];
  97. struct NCDVal__ref align_ref;
  98. struct NCDVal__string align_string;
  99. struct NCDVal__list align_list;
  100. struct NCDVal__mapelem align_mapelem;
  101. struct NCDVal__idstring align_idstring;
  102. struct NCDVal__externalstring align_externalstring;
  103. struct NCDVal__composedstring align_composedstring;
  104. struct NCDVal__cms_link align_cms_link;
  105. };
  106. } NCDValMem;
  107. typedef struct {
  108. NCDValMem *mem;
  109. NCDVal__idx idx;
  110. } NCDValRef;
  111. typedef struct {
  112. NCDVal__idx idx;
  113. } NCDValSafeRef;
  114. typedef struct NCDVal__mapelem NCDVal__maptree_entry;
  115. typedef NCDValMem *NCDVal__maptree_arg;
  116. #include "NCDVal_maptree.h"
  117. #include <structure/CAvl_decl.h>
  118. struct NCDVal__map {
  119. int type;
  120. NCDVal__idx maxcount;
  121. NCDVal__idx count;
  122. NCDVal__MapTree tree;
  123. struct NCDVal__mapelem elems[];
  124. };
  125. typedef struct {
  126. NCDVal__idx elemidx;
  127. } NCDValMapElem;
  128. #define NCDVAL_INSTR_PLACEHOLDER 0
  129. #define NCDVAL_INSTR_REINSERT 1
  130. #define NCDVAL_INSTR_BUMPDEPTH 2
  131. struct NCDVal__instr {
  132. int type;
  133. union {
  134. struct {
  135. NCDVal__idx plid;
  136. NCDVal__idx plidx;
  137. } placeholder;
  138. struct {
  139. NCDVal__idx mapidx;
  140. NCDVal__idx elempos;
  141. } reinsert;
  142. struct {
  143. NCDVal__idx parent_idx;
  144. NCDVal__idx child_idx_idx;
  145. } bumpdepth;
  146. };
  147. };
  148. typedef struct {
  149. struct NCDVal__instr *instrs;
  150. size_t num_instrs;
  151. } NCDValReplaceProg;
  152. typedef struct {
  153. char *data;
  154. int is_allocated;
  155. } NCDValNullTermString;
  156. //
  157. #define NCDVAL_STRING 1
  158. #define NCDVAL_LIST 2
  159. #define NCDVAL_MAP 3
  160. #define NCDVAL_PLACEHOLDER 4
  161. /**
  162. * Initializes a value memory object.
  163. * A value memory object holds memory for value structures. Values within
  164. * the memory are referenced using {@link NCDValRef} objects, which point
  165. * to values within memory objects.
  166. *
  167. * Values may be added to a memory object using functions such as
  168. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  169. * and {@link NCDVal_NewCopy}, which return references to the new values within
  170. * the memory object.
  171. *
  172. * It is not possible to remove values from the memory object, or modify existing
  173. * values other than adding elements to pre-allocated slots in lists and maps.
  174. * Once a value is added, it will consume memory as long as its memory object
  175. * exists. This is by design - this code is intended and optimized for constructing
  176. * and passing around values, not for operating on them in place. In fact, al
  177. * values within a memory object are stored in a single memory buffer, as an
  178. * embedded data structure with relativepointers. For example, map values use an
  179. * embedded AVL tree.
  180. */
  181. void NCDValMem_Init (NCDValMem *o);
  182. /**
  183. * Frees a value memory object.
  184. * All values within the memory object cease to exist, and any {@link NCDValRef}
  185. * object pointing to them must no longer be used.
  186. */
  187. void NCDValMem_Free (NCDValMem *o);
  188. /**
  189. * Initializes the memory object to be a copy of an existing memory object.
  190. * Value references from the original may be used if they are first turned
  191. * to {@link NCDValSafeRef} using {@link NCDVal_ToSafe} and back to
  192. * {@link NCDValRef} using {@link NCDVal_FromSafe} with the new memory object
  193. * specified. Alternatively, {@link NCDVal_Moved} can be used.
  194. * Returns 1 on success and 0 on failure.
  195. */
  196. int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other) WARN_UNUSED;
  197. /**
  198. * For each internal link (e.g. list element) to a ComposedString in the memory
  199. * object, copies the ComposedString to some kind ContinuousString, and updates
  200. * the link to point to the new ContinuousString.
  201. * Additionally, if *\a root_val points to a ComposedString, copies it to a new
  202. * ContinuousString and updates *\a root_val to point to it.
  203. * \a root_val must be non-NULL and *\a root_val must not be an invalid value
  204. * reference.
  205. * Returns 1 on success and 0 on failure. On failure, some strings may have
  206. * been converted, but the memory object is left in a consistent state.
  207. */
  208. int NCDValMem_ConvertNonContinuousStrings (NCDValMem *o, NCDValRef *root_val) WARN_UNUSED;
  209. /**
  210. * Does nothing.
  211. * The value reference object must either point to a valid value within a valid
  212. * memory object, or must be an invalid reference (most functions operating on
  213. * {@link NCDValRef} implicitly require that).
  214. */
  215. void NCDVal_Assert (NCDValRef val);
  216. /**
  217. * Determines if a value reference is invalid.
  218. */
  219. int NCDVal_IsInvalid (NCDValRef val);
  220. /**
  221. * Determines if a value is a placeholder value.
  222. * The value reference must not be an invalid reference.
  223. */
  224. int NCDVal_IsPlaceholder (NCDValRef val);
  225. /**
  226. * Returns the type of the value reference, which must not be an invalid reference.
  227. * Possible values are NCDVAL_STRING, NCDVAL_LIST, NCDVAL_MAP and NCDVAL_PLACEHOLDER.
  228. * The placeholder type is only used internally in the interpreter for argument
  229. * resolution, and is never seen by modules; see {@link NCDVal_NewPlaceholder}.
  230. */
  231. int NCDVal_Type (NCDValRef val);
  232. /**
  233. * Returns an invalid reference.
  234. * An invalid reference must not be passed to any function here, except:
  235. * {@link NCDVal_Assert}, {@link NCDVal_IsInvalid}, {@link NCDVal_ToSafe},
  236. * {@link NCDVal_FromSafe}, {@link NCDVal_Moved}.
  237. */
  238. NCDValRef NCDVal_NewInvalid (void);
  239. /**
  240. * Returns a new placeholder value reference. A placeholder value is a valid value
  241. * containing an integer placeholder identifier.
  242. * This always succeeds; however, the caller must ensure the identifier is
  243. * non-negative and satisfies (NCDVAL_MINIDX + plid < -1).
  244. *
  245. * The placeholder type is only used internally in the interpreter for argument
  246. * resolution, and is never seen by modules. Also see {@link NCDPlaceholderDb}.
  247. */
  248. NCDValRef NCDVal_NewPlaceholder (NCDValMem *mem, int plid);
  249. /**
  250. * Returns the indentifier of a placeholder value.
  251. * The value reference must point to a placeholder value.
  252. */
  253. int NCDVal_PlaceholderId (NCDValRef val);
  254. /**
  255. * Copies a value into the specified memory object. The source
  256. * must not be an invalid reference, however it may reside in any memory
  257. * object (including 'mem').
  258. * Returns a reference to the copied value. On out of memory, returns
  259. * an invalid reference.
  260. */
  261. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  262. /**
  263. * Compares two values, both of which must not be invalid references.
  264. * Returns -1, 0 or 1.
  265. */
  266. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  267. /**
  268. * Converts a value reference to a safe referece format, which remains valid
  269. * if the memory object is moved (safe references do not contain a pointer
  270. * to the memory object, unlike {@link NCDValRef} references).
  271. */
  272. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  273. /**
  274. * Converts a safe value reference to a normal value reference.
  275. * This should be used to recover references from safe references
  276. * after the memory object is moved.
  277. */
  278. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  279. /**
  280. * Fixes a value reference after its memory object was moved.
  281. */
  282. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  283. /**
  284. * Determines if the value implements the String interface.
  285. * The value reference must not be an invalid reference.
  286. */
  287. int NCDVal_IsString (NCDValRef val);
  288. /**
  289. * Determines if the value implements the ContinuousString interface.
  290. * A ContinuousString also implements the String interface.
  291. * The value reference must not be an invalid reference.
  292. */
  293. int NCDVal_IsContinuousString (NCDValRef val);
  294. /**
  295. * Determines if the value is a StoredString.
  296. * A StoredString implements the ContinuousString interface.
  297. * The value reference must not be an invalid reference.
  298. */
  299. int NCDVal_IsStoredString (NCDValRef val);
  300. /**
  301. * Determines if the value is an IdString. See {@link NCDVal_NewIdString}
  302. * for details.
  303. * An IdString implements the ContinuousString interface.
  304. * The value reference must not be an invalid reference.
  305. */
  306. int NCDVal_IsIdString (NCDValRef val);
  307. /**
  308. * Determines if a value is an ExternalString.
  309. * See {@link NCDVal_NewExternalString} for details.
  310. * An ExternalString implements the ContinuousString interface.
  311. * The value reference must not be an invalid reference.
  312. */
  313. int NCDVal_IsExternalString (NCDValRef val);
  314. /**
  315. * Determines if a value is a ComposedString.
  316. * A ComposedString implements the String interface.
  317. */
  318. int NCDVal_IsComposedString (NCDValRef val);
  319. /**
  320. * Determines if a value is a String which contains no null bytes.
  321. * The value reference must not be an invalid reference.
  322. */
  323. int NCDVal_IsStringNoNulls (NCDValRef val);
  324. /**
  325. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  326. */
  327. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  328. /**
  329. * Builds a new StoredString.
  330. * Returns a reference to the new value, or an invalid reference
  331. * on out of memory.
  332. * WARNING: The buffer passed must NOT be part of any value in the
  333. * memory object specified. In particular, you may NOT use this
  334. * function to copy a string that resides in the same memory object.
  335. *
  336. * A StoredString is a kind of ContinuousString which is represented directly in the
  337. * value memory object.
  338. */
  339. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  340. /**
  341. * Builds a new StoredString of the given length with undefined contents.
  342. * You can define the contents of the string later by copying to the address
  343. * returned by {@link NCDVal_StringData}.
  344. */
  345. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  346. /**
  347. * Builds a new IdString.
  348. * Returns a reference to the new value, or an invalid reference
  349. * on out of memory.
  350. *
  351. * An IdString is a kind of ContinuousString which is represented efficiently as a string
  352. * identifier via {@link NCDStringIndex}.
  353. */
  354. NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id,
  355. NCDStringIndex *string_index);
  356. /**
  357. * Builds a new ExternalString, pointing to the given external data. A reference to
  358. * the external data is taken using {@link NCDRefTarget}, unless 'ref_target' is
  359. * NULL. The data must not change while this value exists.
  360. * Returns a reference to the new value, or an invalid reference
  361. * on out of memory.
  362. *
  363. * An ExternalString is a kind of ContinuousString where the actual string contents are
  364. * stored outside of the value memory object.
  365. */
  366. NCDValRef NCDVal_NewExternalString (NCDValMem *mem, const char *data, size_t len,
  367. NCDRefTarget *ref_target);
  368. /**
  369. * Callback function which is called by {@link NCDVal_StringGetPtr} for ComposedString's to
  370. * access the underlying string resource.
  371. * \a user is whatever was passed to 'resource.user' in {@link NCDVal_NewComposedString}.
  372. * \a offset is the offset from the beginning of the string exposed by the resource; it will be
  373. * >= 'offset' and <= 'offset' + 'length' as given to NCDVal_NewComposedString.
  374. * This callback must set *\a out_data and *\a out_length to represent a continuous (sub-)region
  375. * of the string that starts at the byte at index \a offset. The pointed-to data must remain
  376. * valid and unchanged until all references to the string resource are released.
  377. * \a *out_data must be set to non-NULL even if there is no more data in the resource.
  378. */
  379. typedef void (*NCDVal_ComposedString_func_getptr) (void *user, size_t offset, const char **out_data, size_t *out_length);
  380. /**
  381. * Structure representing a string resource used by ComposedString's,
  382. * to simplify {@link NCDVal_NewComposedString} and {@link NCDVal_ComposedStringResource}.
  383. */
  384. struct NCDVal_string_resource {
  385. NCDVal_ComposedString_func_getptr func_getptr;
  386. void *user;
  387. NCDRefTarget *ref_target;
  388. };
  389. /**
  390. * Builds a new ComposedString from a string resource.
  391. * A reference to the underlying string resource via the {@link NCDRefTarget} object
  392. * specified in 'resource.ref_target'.
  393. *
  394. * A ComposedString is a kind of String with an abstract representation exposed via the
  395. * {@link NCDVal_ComposedString_func_getptr} callback.
  396. */
  397. NCDValRef NCDVal_NewComposedString (NCDValMem *mem, struct NCDVal_string_resource resource, size_t offset, size_t length);
  398. /**
  399. * Returns a pointer to the data of a ContinuousString.
  400. * WARNING: the string data may not be null-terminated. To get a null-terminated
  401. * version, use {@link NCDVal_StringNullTerminate}.
  402. * The value reference must point to a ContinuousString.
  403. */
  404. const char * NCDVal_StringData (NCDValRef contstring);
  405. /**
  406. * Returns the length of a String.
  407. * The value reference must point to a String.
  408. */
  409. size_t NCDVal_StringLength (NCDValRef string);
  410. /**
  411. * Returns a pointer into a continuous chunk of data within a String.
  412. * The \a offset must be lesser or equal to the length of the string.
  413. * Both \a out_data and \a out_length must be non-NULL. *\a out_data will be set to point
  414. * into a continuous data chunk starting at \a offset from the beginning of the string, and
  415. * *\a out_length will be set to the number of bytes which are available from that pointer,
  416. * and to no more than \a max_length.
  417. *
  418. * It is only guaranteed that:
  419. * - if offset < length_of_string and max_length > 0, then *out_length > 0,
  420. * - *out_length <= max_length.
  421. *
  422. * This means that:
  423. * - *out_length may be smaller than the remainder of the string,
  424. * - *out_length may be larger than length_of_string - offset, unless limited by max_length.
  425. *
  426. * For clarification, the following code is provided which prints the entire string
  427. * to standard output.
  428. *
  429. * size_t pos = 0;
  430. * size_t length = NCDVal_StringLength(string);
  431. * while (pos < length) {
  432. * const char *chunk_data;
  433. * size_t chunk_len;
  434. * NCDVal_StringGetPtr(string, pos, length - pos, &chunk_data, &chunk_len);
  435. * fwrite(chunk_data, 1, chunk_len, stdout);
  436. * pos += chunk_len;
  437. * }
  438. */
  439. void NCDVal_StringGetPtr (NCDValRef string, size_t offset, size_t max_length, const char **out_data, size_t *out_length);
  440. /**
  441. * Produces a null-terminated continuous version of a String. On success, the result is
  442. * stored into an {@link NCDValNullTermString} structure, and the null-terminated
  443. * string is available via its 'data' member. This function may either simply pass
  444. * through the data pointer (if the string is known to be continuous and null-terminated) or
  445. * produce a null-terminated dynamically allocated copy.
  446. * On success, {@link NCDValNullTermString_Free} should be called to release any allocated
  447. * memory when the null-terminated string is no longer needed. This must be called before
  448. * the memory object is freed, because it may point to data inside the memory object.
  449. * It is guaranteed that *out is not modified on failure.
  450. * Returns 1 on success and 0 on failure.
  451. */
  452. int NCDVal_StringNullTerminate (NCDValRef string, NCDValNullTermString *out) WARN_UNUSED;
  453. /**
  454. * Returns a dummy {@link NCDValNullTermString} which can be freed using
  455. * {@link NCDValNullTermString_Free}, but need not be.
  456. */
  457. NCDValNullTermString NCDValNullTermString_NewDummy (void);
  458. /**
  459. * Releases any memory which was dynamically allocated by {@link NCDVal_StringNullTerminate}
  460. * to null-terminate a string.
  461. */
  462. void NCDValNullTermString_Free (NCDValNullTermString *o);
  463. /**
  464. * Returns the string ID and the string index of an IdString.
  465. * Both the \a out_string_id and \a out_string_index pointers must be non-NULL.
  466. */
  467. void NCDVal_IdStringGet (NCDValRef idstring, NCD_string_id_t *out_string_id,
  468. NCDStringIndex **out_string_index);
  469. /**
  470. * Returns the string ID of an IdString.
  471. */
  472. NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring);
  473. /**
  474. * Returns the string index of an IdString.
  475. */
  476. NCDStringIndex * NCDVal_IdStringStringIndex (NCDValRef idstring);
  477. /**
  478. * Returns the reference target of an ExternalString. This may be NULL
  479. * if the external string is not associated with a reference target.
  480. */
  481. NCDRefTarget * NCDVal_ExternalStringTarget (NCDValRef externalstring);
  482. /**
  483. * Returns the underlying string resource of a ComposedString.
  484. */
  485. struct NCDVal_string_resource NCDVal_ComposedStringResource (NCDValRef composedstring);
  486. /**
  487. * Returns the resource offset of a ComposedString.
  488. */
  489. size_t NCDVal_ComposedStringOffset (NCDValRef composedstring);
  490. /**
  491. * Determines if the String has any null bytes in its contents.
  492. */
  493. int NCDVal_StringHasNulls (NCDValRef string);
  494. /**
  495. * Determines if the String value is equal to the given null-terminated
  496. * string.
  497. * The value reference must point to a String value.
  498. */
  499. int NCDVal_StringEquals (NCDValRef string, const char *data);
  500. /**
  501. * Determines if the String is equal to the given string represented
  502. * by an {@link NCDStringIndex} identifier.
  503. * NOTE: \a string_index must be equal to the string_index of every ID-string
  504. * that exist within this memory object.
  505. */
  506. int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id,
  507. NCDStringIndex *string_index);
  508. /**
  509. * Compares two String's in a manner similar to memcmp().
  510. * The startN and length arguments must refer to a valid region within
  511. * stringN, i.e. startN + length <= length_of_stringN must hold.
  512. */
  513. int NCDVal_StringMemCmp (NCDValRef string1, NCDValRef string2, size_t start1, size_t start2, size_t length);
  514. /**
  515. * Copies a part of a String to a buffer.
  516. * \a start and \a length must refer to a valid region within the string,
  517. * i.e. start + length <= length_of_string must hold.
  518. */
  519. void NCDVal_StringCopyOut (NCDValRef string, size_t start, size_t length, char *dst);
  520. /**
  521. * Determines if a part of a String is equal to the \a length bytes in \a data.
  522. * \a start and \a length must refer to a valid region within the string,
  523. * i.e. start + length <= length_of_string must hold.
  524. */
  525. int NCDVal_StringRegionEquals (NCDValRef string, size_t start, size_t length, const char *data);
  526. /**
  527. * Determines if a value is a list value.
  528. * The value reference must not be an invalid reference.
  529. */
  530. int NCDVal_IsList (NCDValRef val);
  531. /**
  532. * Builds a new list value. The 'maxcount' argument specifies how
  533. * many element slots to preallocate. Not more than that many
  534. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  535. * Returns a reference to the new value, or an invalid reference
  536. * on out of memory.
  537. */
  538. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  539. /**
  540. * Appends a value to to the list value.
  541. * The 'list' reference must point to a list value, and the
  542. * 'elem' reference must be non-invalid and point to a value within
  543. * the same memory object as the list.
  544. * Inserting a value into a list does not in any way change it;
  545. * internally, the list only points to it.
  546. * You must not modify the element after it has been inserted into the
  547. * list.
  548. * Returns 1 on success and 0 on failure (depth limit exceeded).
  549. */
  550. int NCDVal_ListAppend (NCDValRef list, NCDValRef elem) WARN_UNUSED;
  551. /**
  552. * Returns the number of elements in a list value, i.e. the number
  553. * of times {@link NCDVal_ListAppend} was called.
  554. * The 'list' reference must point to a list value.
  555. */
  556. size_t NCDVal_ListCount (NCDValRef list);
  557. /**
  558. * Returns the maximum number of elements a list value may contain,
  559. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  560. * The 'list' reference must point to a list value.
  561. */
  562. size_t NCDVal_ListMaxCount (NCDValRef list);
  563. /**
  564. * Returns a reference to the value at the given position 'pos' in a list,
  565. * starting with zero.
  566. * The 'list' reference must point to a list value.
  567. * The position 'pos' must refer to an existing element, i.e.
  568. * pos < NCDVal_ListCount().
  569. */
  570. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  571. /**
  572. * Returns references to elements within a list by writing them
  573. * via (NCDValRef *) variable arguments.
  574. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  575. * references, as mentioned.
  576. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  577. * references
  578. */
  579. int NCDVal_ListRead (NCDValRef list, int num, ...);
  580. /**
  581. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  582. * elements.
  583. */
  584. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  585. /**
  586. * Determines if a value is a map value.
  587. * The value reference must not be an invalid reference.
  588. */
  589. int NCDVal_IsMap (NCDValRef val);
  590. /**
  591. * Builds a new map value. The 'maxcount' argument specifies how
  592. * many entry slots to preallocate. Not more than that many
  593. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  594. * Returns a reference to the new value, or an invalid reference
  595. * on out of memory.
  596. */
  597. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  598. /**
  599. * Inserts an entry to the map value.
  600. * The 'map' reference must point to a map value, and the
  601. * 'key' and 'val' references must be non-invalid and point to values within
  602. * the same memory object as the map.
  603. * Inserting an entry does not in any way change the 'key'and 'val';
  604. * internally, the map only points to it.
  605. * You must not modify the key after inserting it into a map. This is because
  606. * the map builds an embedded AVL tree of entries indexed by keys.
  607. * If insertion fails due to a maximum depth limit, returns 0.
  608. * Otherwise returns 1, and *out_inserted is set to 1 if the key did not
  609. * yet exist and the entry was inserted, and to 0 if it did exist and the
  610. * entry was not inserted. The 'out_inserted' pointer may be NULL, in which
  611. * case *out_inserted is never set.
  612. */
  613. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val, int *out_inserted) WARN_UNUSED;
  614. /**
  615. * Returns the number of entries in a map value, i.e. the number
  616. * of times {@link NCDVal_MapInsert} was called successfully.
  617. * The 'map' reference must point to a map value.
  618. */
  619. size_t NCDVal_MapCount (NCDValRef map);
  620. /**
  621. * Returns the maximum number of entries a map value may contain,
  622. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  623. * The 'map' reference must point to a map value.
  624. */
  625. size_t NCDVal_MapMaxCount (NCDValRef map);
  626. /**
  627. * Determines if a map entry reference is invalid. This is used in combination
  628. * with the map iteration functions to detect the end of iteration.
  629. */
  630. int NCDVal_MapElemInvalid (NCDValMapElem me);
  631. /**
  632. * Returns a reference to the first entry in a map, with respect to some
  633. * arbitrary order.
  634. * If the map is empty, returns an invalid map entry reference.
  635. */
  636. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  637. /**
  638. * Returns a reference to the entry in a map that follows the entry referenced
  639. * by 'me', with respect to some arbitrary order.
  640. * The 'me' argument must be a non-invalid reference to an entry in the map.
  641. * If 'me' is the last entry, returns an invalid map entry reference.
  642. */
  643. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  644. /**
  645. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  646. * {@link NCDVal_Compare}.
  647. * Ordered iteration is slower and should only be used when needed.
  648. */
  649. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  650. /**
  651. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  652. * {@link NCDVal_Compare}.
  653. * Ordered iteration is slower and should only be used when needed.
  654. */
  655. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  656. /**
  657. * Returns a reference to the key of the map entry referenced by 'me'.
  658. * The 'me' argument must be a non-invalid reference to an entry in the map.
  659. */
  660. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  661. /**
  662. * Returns a reference to the value of the map entry referenced by 'me'.
  663. * The 'me' argument must be a non-invalid reference to an entry in the map.
  664. */
  665. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  666. /**
  667. * Looks for a key in the map. The 'key' reference must be a non-invalid
  668. * value reference, and may point to a value in a different memory object
  669. * than the map.
  670. * If the key exists in the map, returns a reference to the corresponding
  671. * map entry.
  672. * If the key does not exist, returns an invalid map entry reference.
  673. */
  674. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  675. /**
  676. * Retrieves the value reference to the value of the map entry whose key is a
  677. * string value equal to the given null-terminated string. If there is no such
  678. * entry, returns an invalid value reference.
  679. */
  680. NCDValRef NCDVal_MapGetValue (NCDValRef map, const char *key_str);
  681. /**
  682. * Builds a placeholder replacement program, which is a list of instructions for
  683. * efficiently replacing placeholders in identical values in identical memory
  684. * objects.
  685. * To actually perform replacements, make copies of the memory object of this value
  686. * using {@link NCDValMem_InitCopy}, then call {@link NCDValReplaceProg_Execute}
  687. * on the copies.
  688. * The value passed must be a valid value, and not a placeholder.
  689. * Returns 1 on success, 0 on failure.
  690. */
  691. int NCDValReplaceProg_Init (NCDValReplaceProg *o, NCDValRef val);
  692. /**
  693. * Frees the placeholder replacement program.
  694. */
  695. void NCDValReplaceProg_Free (NCDValReplaceProg *o);
  696. /**
  697. * Callback used by {@link NCDValReplaceProg_Execute} to allow the caller to produce
  698. * values of placeholders.
  699. * This function should build a new value within the memory object 'mem' (which is
  700. * the same as of the memory object where placeholders are being replaced).
  701. * On success, it should return 1, writing a valid value reference to *out.
  702. * On failure, it can either return 0, or return 1 but write an invalid value reference.
  703. * This callback must not access the memory object in any other way than building
  704. * new values in it; it must not modify any values that were already present at the
  705. * point it was called.
  706. */
  707. typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  708. /**
  709. * Executes the replacement program, replacing placeholders in a value.
  710. * The memory object must given be identical to the memory object which was used in
  711. * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_InitCopy}.
  712. * This will call the callback 'replace', which should build the values to replace
  713. * the placeholders.
  714. * Returns 1 on success and 0 on failure. On failure, the entire memory object enters
  715. * and inconsistent state and must be freed using {@link NCDValMem_Free} before
  716. * performing any other operation on it.
  717. * The program is passed by value instead of pointer because this appears to be faster.
  718. * Is is not modified in any way.
  719. */
  720. int NCDValReplaceProg_Execute (NCDValReplaceProg prog, NCDValMem *mem, NCDVal_replace_func replace, void *arg);
  721. #endif