NCDVal.h 30 KB

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