NCDVal.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. typedef struct {
  157. char *data;
  158. int is_allocated;
  159. } NCDValContString;
  160. //
  161. #define NCDVAL_STRING 1
  162. #define NCDVAL_LIST 2
  163. #define NCDVAL_MAP 3
  164. #define NCDVAL_PLACEHOLDER 4
  165. /**
  166. * Initializes a value memory object.
  167. * A value memory object holds memory for value structures. Values within
  168. * the memory are referenced using {@link NCDValRef} objects, which point
  169. * to values within memory objects.
  170. *
  171. * Values may be added to a memory object using functions such as
  172. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  173. * and {@link NCDVal_NewCopy}, which return references to the new values within
  174. * the memory object.
  175. *
  176. * It is not possible to remove values from the memory object, or modify existing
  177. * values other than adding elements to pre-allocated slots in lists and maps.
  178. * Once a value is added, it will consume memory as long as its memory object
  179. * exists. This is by design - this code is intended and optimized for constructing
  180. * and passing around values, not for operating on them in place. In fact, al
  181. * values within a memory object are stored in a single memory buffer, as an
  182. * embedded data structure with relativepointers. For example, map values use an
  183. * embedded AVL tree.
  184. */
  185. void NCDValMem_Init (NCDValMem *o);
  186. /**
  187. * Frees a value memory object.
  188. * All values within the memory object cease to exist, and any {@link NCDValRef}
  189. * object pointing to them must no longer be used.
  190. */
  191. void NCDValMem_Free (NCDValMem *o);
  192. /**
  193. * Initializes the memory object to be a copy of an existing memory object.
  194. * Value references from the original may be used if they are first turned
  195. * to {@link NCDValSafeRef} using {@link NCDVal_ToSafe} and back to
  196. * {@link NCDValRef} using {@link NCDVal_FromSafe} with the new memory object
  197. * specified. Alternatively, {@link NCDVal_Moved} can be used.
  198. * Returns 1 on success and 0 on failure.
  199. */
  200. int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other) WARN_UNUSED;
  201. /**
  202. * For each internal link (e.g. list element) to a ComposedString in the memory
  203. * object, copies the ComposedString to some kind ContinuousString, and updates
  204. * the link to point to the new ContinuousString.
  205. * Additionally, if *\a root_val points to a ComposedString, copies it to a new
  206. * ContinuousString and updates *\a root_val to point to it.
  207. * \a root_val must be non-NULL and *\a root_val must not be an invalid value
  208. * reference.
  209. * Returns 1 on success and 0 on failure. On failure, some strings may have
  210. * been converted, but the memory object is left in a consistent state.
  211. */
  212. int NCDValMem_ConvertNonContinuousStrings (NCDValMem *o, NCDValRef *root_val) WARN_UNUSED;
  213. /**
  214. * Does nothing.
  215. * The value reference object must either point to a valid value within a valid
  216. * memory object, or must be an invalid reference (most functions operating on
  217. * {@link NCDValRef} implicitly require that).
  218. */
  219. void NCDVal_Assert (NCDValRef val);
  220. /**
  221. * Determines if a value reference is invalid.
  222. */
  223. int NCDVal_IsInvalid (NCDValRef val);
  224. /**
  225. * Determines if a value is a placeholder value.
  226. * The value reference must not be an invalid reference.
  227. */
  228. int NCDVal_IsPlaceholder (NCDValRef val);
  229. /**
  230. * Returns the type of the value reference, which must not be an invalid reference.
  231. * Possible values are NCDVAL_STRING, NCDVAL_LIST, NCDVAL_MAP and NCDVAL_PLACEHOLDER.
  232. * The placeholder type is only used internally in the interpreter for argument
  233. * resolution, and is never seen by modules; see {@link NCDVal_NewPlaceholder}.
  234. */
  235. int NCDVal_Type (NCDValRef val);
  236. /**
  237. * Returns an invalid reference.
  238. * An invalid reference must not be passed to any function here, except:
  239. * {@link NCDVal_Assert}, {@link NCDVal_IsInvalid}, {@link NCDVal_ToSafe},
  240. * {@link NCDVal_FromSafe}, {@link NCDVal_Moved}.
  241. */
  242. NCDValRef NCDVal_NewInvalid (void);
  243. /**
  244. * Returns a new placeholder value reference. A placeholder value is a valid value
  245. * containing an integer placeholder identifier.
  246. * This always succeeds; however, the caller must ensure the identifier is
  247. * non-negative and satisfies (NCDVAL_MINIDX + plid < -1).
  248. *
  249. * The placeholder type is only used internally in the interpreter for argument
  250. * resolution, and is never seen by modules. Also see {@link NCDPlaceholderDb}.
  251. */
  252. NCDValRef NCDVal_NewPlaceholder (NCDValMem *mem, int plid);
  253. /**
  254. * Returns the indentifier of a placeholder value.
  255. * The value reference must point to a placeholder value.
  256. */
  257. int NCDVal_PlaceholderId (NCDValRef val);
  258. /**
  259. * Copies a value into the specified memory object. The source
  260. * must not be an invalid reference, however it may reside in any memory
  261. * object (including 'mem').
  262. * Returns a reference to the copied value. On out of memory, returns
  263. * an invalid reference.
  264. */
  265. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  266. /**
  267. * Compares two values, both of which must not be invalid references.
  268. * Returns -1, 0 or 1.
  269. */
  270. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  271. /**
  272. * Converts a value reference to a safe referece format, which remains valid
  273. * if the memory object is moved (safe references do not contain a pointer
  274. * to the memory object, unlike {@link NCDValRef} references).
  275. */
  276. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  277. /**
  278. * Converts a safe value reference to a normal value reference.
  279. * This should be used to recover references from safe references
  280. * after the memory object is moved.
  281. */
  282. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  283. /**
  284. * Fixes a value reference after its memory object was moved.
  285. */
  286. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  287. /**
  288. * Determines if all strings within this value are ContinuousString's,
  289. * by recusively walking the entire value.
  290. * If all strings are ContinuousString's, returns 1; if there is at least
  291. * one string which is not a ContinuousString, returns 0.
  292. * The value reference must not be an invalid reference.
  293. */
  294. int NCDVal_HasOnlyContinuousStrings (NCDValRef val);
  295. /**
  296. * Determines if the value implements the String interface.
  297. * The value reference must not be an invalid reference.
  298. */
  299. int NCDVal_IsString (NCDValRef val);
  300. /**
  301. * Determines if the value implements the ContinuousString interface.
  302. * A ContinuousString also implements the String interface.
  303. * The value reference must not be an invalid reference.
  304. */
  305. int NCDVal_IsContinuousString (NCDValRef val);
  306. /**
  307. * Determines if the value is a StoredString.
  308. * A StoredString implements the ContinuousString interface.
  309. * The value reference must not be an invalid reference.
  310. */
  311. int NCDVal_IsStoredString (NCDValRef val);
  312. /**
  313. * Determines if the value is an IdString. See {@link NCDVal_NewIdString}
  314. * for details.
  315. * An IdString implements the ContinuousString interface.
  316. * The value reference must not be an invalid reference.
  317. */
  318. int NCDVal_IsIdString (NCDValRef val);
  319. /**
  320. * Determines if a value is an ExternalString.
  321. * See {@link NCDVal_NewExternalString} for details.
  322. * An ExternalString implements the ContinuousString interface.
  323. * The value reference must not be an invalid reference.
  324. */
  325. int NCDVal_IsExternalString (NCDValRef val);
  326. /**
  327. * Determines if a value is a ComposedString.
  328. * A ComposedString implements the String interface.
  329. */
  330. int NCDVal_IsComposedString (NCDValRef val);
  331. /**
  332. * Determines if a value is a String which contains no null bytes.
  333. * The value reference must not be an invalid reference.
  334. */
  335. int NCDVal_IsStringNoNulls (NCDValRef val);
  336. /**
  337. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  338. */
  339. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  340. /**
  341. * Builds a new StoredString.
  342. * Returns a reference to the new value, or an invalid reference
  343. * on out of memory.
  344. * WARNING: The buffer passed must NOT be part of any value in the
  345. * memory object specified. In particular, you may NOT use this
  346. * function to copy a string that resides in the same memory object.
  347. *
  348. * A StoredString is a kind of ContinuousString which is represented directly in the
  349. * value memory object.
  350. */
  351. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  352. /**
  353. * Builds a new StoredString of the given length with undefined contents.
  354. * You can define the contents of the string later by copying to the address
  355. * returned by {@link NCDVal_StringData}.
  356. */
  357. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  358. /**
  359. * Builds a new IdString.
  360. * Returns a reference to the new value, or an invalid reference
  361. * on out of memory.
  362. *
  363. * An IdString is a kind of ContinuousString which is represented efficiently as a string
  364. * identifier via {@link NCDStringIndex}.
  365. */
  366. NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id,
  367. NCDStringIndex *string_index);
  368. /**
  369. * Builds a new ExternalString, pointing to the given external data. A reference to
  370. * the external data is taken using {@link NCDRefTarget}, unless 'ref_target' is
  371. * NULL. The data must not change while this value exists.
  372. * Returns a reference to the new value, or an invalid reference
  373. * on out of memory.
  374. *
  375. * An ExternalString is a kind of ContinuousString where the actual string contents are
  376. * stored outside of the value memory object.
  377. */
  378. NCDValRef NCDVal_NewExternalString (NCDValMem *mem, const char *data, size_t len,
  379. NCDRefTarget *ref_target);
  380. /**
  381. * Callback function which is called by {@link NCDVal_StringGetPtr} for ComposedString's to
  382. * 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. NCDRefTarget *ref_target;
  401. } NCDValStringResource;
  402. /**
  403. * Like {@link NCDVal_StringGetPtr}, but directly acceses a string resource backing a
  404. * ComposedString. The \a offset here is from the beginning of the resource; assuming you have
  405. * obtained the resource from a ComposedString using {@link NCDVal_ComposedStringResource},
  406. * you should not access data outside of the range outside of the one defined by
  407. * {@link NCDVal_ComposedStringOffset} and {@link NCDVal_StringLength}.
  408. */
  409. void NCDValStringResource_GetPtr (NCDValStringResource resource, size_t offset, size_t max_length, const char **out_data, size_t *out_length);
  410. /**
  411. * Builds a new ComposedString from a string resource.
  412. * A reference to the underlying string resource via the {@link NCDRefTarget} object
  413. * specified in 'resource.ref_target'.
  414. *
  415. * A ComposedString is a kind of String with an abstract representation exposed via the
  416. * {@link NCDVal_ComposedString_func_getptr} callback.
  417. */
  418. NCDValRef NCDVal_NewComposedString (NCDValMem *mem, NCDValStringResource resource, size_t offset, size_t length);
  419. /**
  420. * Returns a pointer to the data of a ContinuousString.
  421. * WARNING: the string data may not be null-terminated. To get a null-terminated
  422. * version, use {@link NCDVal_StringNullTerminate}.
  423. * The value reference must point to a ContinuousString.
  424. */
  425. const char * NCDVal_StringData (NCDValRef contstring);
  426. /**
  427. * Returns the length of a String.
  428. * The value reference must point to a String.
  429. */
  430. size_t NCDVal_StringLength (NCDValRef string);
  431. /**
  432. * Returns a pointer into a continuous chunk of data within a String.
  433. * The \a offset must be lesser than the length of the string, and \a max_length
  434. * must be greater than zero.
  435. * Both \a out_data and \a out_length must be non-NULL. *\a out_data will be set to point
  436. * into a continuous data chunk starting at \a offset from the beginning of the string, and
  437. * *\a out_length will be set to the number of bytes which are available from that pointer,
  438. * and to no more than \a max_length.
  439. *
  440. * It is only guaranteed that:
  441. * - *out_length > 0,
  442. * - *out_length <= max_length.
  443. *
  444. * This means that:
  445. * - *out_length may be smaller than the remainder of the string,
  446. * - *out_length may be larger than length_of_string - offset, i.e. you may be provided
  447. * bytes that are outside of this string, unless limited by max_length.
  448. *
  449. * For clarification, the following code is provided which prints any String to standard
  450. * output.
  451. *
  452. * size_t pos = 0;
  453. * size_t length = NCDVal_StringLength(string);
  454. * while (pos < length) {
  455. * const char *chunk_data;
  456. * size_t chunk_len;
  457. * NCDVal_StringGetPtr(string, pos, length - pos, &chunk_data, &chunk_len);
  458. * fwrite(chunk_data, 1, chunk_len, stdout);
  459. * pos += chunk_len;
  460. * }
  461. */
  462. void NCDVal_StringGetPtr (NCDValRef string, size_t offset, size_t max_length, const char **out_data, size_t *out_length);
  463. /**
  464. * Produces a null-terminated continuous version of a String. On success, the result is
  465. * stored into an {@link NCDValNullTermString} structure, and the null-terminated
  466. * string is available via its 'data' member. This function may either simply pass
  467. * through the data pointer (if the string is known to be continuous and null-terminated) or
  468. * produce a null-terminated dynamically allocated copy.
  469. * On success, {@link NCDValNullTermString_Free} should be called to release any allocated
  470. * memory when the null-terminated string is no longer needed. This must be called before
  471. * the memory object is freed, because it may point to data inside the memory object.
  472. * It is guaranteed that *out is not modified on failure.
  473. * Returns 1 on success and 0 on failure.
  474. */
  475. int NCDVal_StringNullTerminate (NCDValRef string, NCDValNullTermString *out) WARN_UNUSED;
  476. /**
  477. * Returns a dummy {@link NCDValNullTermString} which can be freed using
  478. * {@link NCDValNullTermString_Free}, but need not be.
  479. */
  480. NCDValNullTermString NCDValNullTermString_NewDummy (void);
  481. /**
  482. * Releases any memory which was dynamically allocated by {@link NCDVal_StringNullTerminate}
  483. * to null-terminate a string.
  484. */
  485. void NCDValNullTermString_Free (NCDValNullTermString *o);
  486. /**
  487. * Produces a continuous version of a String. On success, the result is stored into an
  488. * {@link NCDValContString} structure, and the continuous string is available via its
  489. * 'data' member. This function may either simply pass through the data pointer (if the
  490. * string is known to be continuous) or produce a continuous dynamically allocated copy.
  491. * On success, {@link NCDValContString_Free} should be called to release any allocated
  492. * memory when the continuous string is no longer needed. This must be called before
  493. * the memory object is freed, because it may point to data inside the memory object.
  494. * It is guaranteed that *out is not modified on failure.
  495. * Returns 1 on success and 0 on failure.
  496. */
  497. int NCDVal_StringContinuize (NCDValRef string, NCDValContString *out) WARN_UNUSED;
  498. /**
  499. * Returns a dummy {@link NCDValContString} which can be freed using
  500. * {@link NCDValContString_Free}, but need not be.
  501. */
  502. NCDValContString NCDValContString_NewDummy (void);
  503. /**
  504. * Releases any memory which was dynamically allocated by {@link NCDVal_StringContinuize}
  505. * to continuize a string.
  506. */
  507. void NCDValContString_Free (NCDValContString *o);
  508. /**
  509. * Returns the string ID and the string index of an IdString.
  510. * Both the \a out_string_id and \a out_string_index pointers must be non-NULL.
  511. */
  512. void NCDVal_IdStringGet (NCDValRef idstring, NCD_string_id_t *out_string_id,
  513. NCDStringIndex **out_string_index);
  514. /**
  515. * Returns the string ID of an IdString.
  516. */
  517. NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring);
  518. /**
  519. * Returns the string index of an IdString.
  520. */
  521. NCDStringIndex * NCDVal_IdStringStringIndex (NCDValRef idstring);
  522. /**
  523. * Returns the reference target of an ExternalString. This may be NULL
  524. * if the external string is not associated with a reference target.
  525. */
  526. NCDRefTarget * NCDVal_ExternalStringTarget (NCDValRef externalstring);
  527. /**
  528. * Returns the underlying string resource of a ComposedString.
  529. */
  530. NCDValStringResource NCDVal_ComposedStringResource (NCDValRef composedstring);
  531. /**
  532. * Returns the resource offset of a ComposedString.
  533. */
  534. size_t NCDVal_ComposedStringOffset (NCDValRef composedstring);
  535. /**
  536. * Determines if the String has any null bytes in its contents.
  537. */
  538. int NCDVal_StringHasNulls (NCDValRef string);
  539. /**
  540. * Determines if the String value is equal to the given null-terminated
  541. * string.
  542. * The value reference must point to a String value.
  543. */
  544. int NCDVal_StringEquals (NCDValRef string, const char *data);
  545. /**
  546. * Determines if the String is equal to the given string represented
  547. * by an {@link NCDStringIndex} identifier.
  548. * NOTE: \a string_index must be equal to the string_index of every ID-string
  549. * that exist within this memory object.
  550. */
  551. int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id,
  552. NCDStringIndex *string_index);
  553. /**
  554. * Compares two String's in a manner similar to memcmp().
  555. * The startN and length arguments must refer to a valid region within
  556. * stringN, i.e. startN + length <= length_of_stringN must hold.
  557. */
  558. int NCDVal_StringMemCmp (NCDValRef string1, NCDValRef string2, size_t start1, size_t start2, size_t length);
  559. /**
  560. * Copies a part of a String to a buffer.
  561. * \a start and \a length must refer to a valid region within the string,
  562. * i.e. start + length <= length_of_string must hold.
  563. */
  564. void NCDVal_StringCopyOut (NCDValRef string, size_t start, size_t length, char *dst);
  565. /**
  566. * Determines if a part of a String is equal to the \a length bytes in \a data.
  567. * \a start and \a length must refer to a valid region within the string,
  568. * i.e. start + length <= length_of_string must hold.
  569. */
  570. int NCDVal_StringRegionEquals (NCDValRef string, size_t start, size_t length, const char *data);
  571. /**
  572. * Determines if a value is a list value.
  573. * The value reference must not be an invalid reference.
  574. */
  575. int NCDVal_IsList (NCDValRef val);
  576. /**
  577. * Builds a new list value. The 'maxcount' argument specifies how
  578. * many element slots to preallocate. Not more than that many
  579. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  580. * Returns a reference to the new value, or an invalid reference
  581. * on out of memory.
  582. */
  583. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  584. /**
  585. * Appends a value to to the list value.
  586. * The 'list' reference must point to a list value, and the
  587. * 'elem' reference must be non-invalid and point to a value within
  588. * the same memory object as the list.
  589. * Inserting a value into a list does not in any way change it;
  590. * internally, the list only points to it.
  591. * You must not modify the element after it has been inserted into the
  592. * list.
  593. * Returns 1 on success and 0 on failure (depth limit exceeded).
  594. */
  595. int NCDVal_ListAppend (NCDValRef list, NCDValRef elem) WARN_UNUSED;
  596. /**
  597. * Returns the number of elements in a list value, i.e. the number
  598. * of times {@link NCDVal_ListAppend} was called.
  599. * The 'list' reference must point to a list value.
  600. */
  601. size_t NCDVal_ListCount (NCDValRef list);
  602. /**
  603. * Returns the maximum number of elements a list value may contain,
  604. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  605. * The 'list' reference must point to a list value.
  606. */
  607. size_t NCDVal_ListMaxCount (NCDValRef list);
  608. /**
  609. * Returns a reference to the value at the given position 'pos' in a list,
  610. * starting with zero.
  611. * The 'list' reference must point to a list value.
  612. * The position 'pos' must refer to an existing element, i.e.
  613. * pos < NCDVal_ListCount().
  614. */
  615. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  616. /**
  617. * Returns references to elements within a list by writing them
  618. * via (NCDValRef *) variable arguments.
  619. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  620. * references, as mentioned.
  621. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  622. * references
  623. */
  624. int NCDVal_ListRead (NCDValRef list, int num, ...);
  625. /**
  626. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  627. * elements.
  628. */
  629. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  630. /**
  631. * Determines if a value is a map value.
  632. * The value reference must not be an invalid reference.
  633. */
  634. int NCDVal_IsMap (NCDValRef val);
  635. /**
  636. * Builds a new map value. The 'maxcount' argument specifies how
  637. * many entry slots to preallocate. Not more than that many
  638. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  639. * Returns a reference to the new value, or an invalid reference
  640. * on out of memory.
  641. */
  642. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  643. /**
  644. * Inserts an entry to the map value.
  645. * The 'map' reference must point to a map value, and the
  646. * 'key' and 'val' references must be non-invalid and point to values within
  647. * the same memory object as the map.
  648. * Inserting an entry does not in any way change the 'key'and 'val';
  649. * internally, the map only points to it.
  650. * You must not modify the key after inserting it into a map. This is because
  651. * the map builds an embedded AVL tree of entries indexed by keys.
  652. * If insertion fails due to a maximum depth limit, returns 0.
  653. * Otherwise returns 1, and *out_inserted is set to 1 if the key did not
  654. * yet exist and the entry was inserted, and to 0 if it did exist and the
  655. * entry was not inserted. The 'out_inserted' pointer may be NULL, in which
  656. * case *out_inserted is never set.
  657. */
  658. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val, int *out_inserted) WARN_UNUSED;
  659. /**
  660. * Returns the number of entries in a map value, i.e. the number
  661. * of times {@link NCDVal_MapInsert} was called successfully.
  662. * The 'map' reference must point to a map value.
  663. */
  664. size_t NCDVal_MapCount (NCDValRef map);
  665. /**
  666. * Returns the maximum number of entries a map value may contain,
  667. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  668. * The 'map' reference must point to a map value.
  669. */
  670. size_t NCDVal_MapMaxCount (NCDValRef map);
  671. /**
  672. * Determines if a map entry reference is invalid. This is used in combination
  673. * with the map iteration functions to detect the end of iteration.
  674. */
  675. int NCDVal_MapElemInvalid (NCDValMapElem me);
  676. /**
  677. * Returns a reference to the first entry in a map, with respect to some
  678. * arbitrary order.
  679. * If the map is empty, returns an invalid map entry reference.
  680. */
  681. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  682. /**
  683. * Returns a reference to the entry in a map that follows the entry referenced
  684. * by 'me', with respect to some arbitrary order.
  685. * The 'me' argument must be a non-invalid reference to an entry in the map.
  686. * If 'me' is the last entry, returns an invalid map entry reference.
  687. */
  688. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  689. /**
  690. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  691. * {@link NCDVal_Compare}.
  692. * Ordered iteration is slower and should only be used when needed.
  693. */
  694. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  695. /**
  696. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  697. * {@link NCDVal_Compare}.
  698. * Ordered iteration is slower and should only be used when needed.
  699. */
  700. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  701. /**
  702. * Returns a reference to the key of the map entry referenced by 'me'.
  703. * The 'me' argument must be a non-invalid reference to an entry in the map.
  704. */
  705. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  706. /**
  707. * Returns a reference to the value of the map entry referenced by 'me'.
  708. * The 'me' argument must be a non-invalid reference to an entry in the map.
  709. */
  710. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  711. /**
  712. * Looks for a key in the map. The 'key' reference must be a non-invalid
  713. * value reference, and may point to a value in a different memory object
  714. * than the map.
  715. * If the key exists in the map, returns a reference to the corresponding
  716. * map entry.
  717. * If the key does not exist, returns an invalid map entry reference.
  718. */
  719. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  720. /**
  721. * Retrieves the value reference to the value of the map entry whose key is a
  722. * string value equal to the given null-terminated string. If there is no such
  723. * entry, returns an invalid value reference.
  724. */
  725. NCDValRef NCDVal_MapGetValue (NCDValRef map, const char *key_str);
  726. /**
  727. * Builds a placeholder replacement program, which is a list of instructions for
  728. * efficiently replacing placeholders in identical values in identical memory
  729. * objects.
  730. * To actually perform replacements, make copies of the memory object of this value
  731. * using {@link NCDValMem_InitCopy}, then call {@link NCDValReplaceProg_Execute}
  732. * on the copies.
  733. * The value passed must be a valid value, and not a placeholder.
  734. * Returns 1 on success, 0 on failure.
  735. */
  736. int NCDValReplaceProg_Init (NCDValReplaceProg *o, NCDValRef val);
  737. /**
  738. * Frees the placeholder replacement program.
  739. */
  740. void NCDValReplaceProg_Free (NCDValReplaceProg *o);
  741. /**
  742. * Callback used by {@link NCDValReplaceProg_Execute} to allow the caller to produce
  743. * values of placeholders.
  744. * This function should build a new value within the memory object 'mem' (which is
  745. * the same as of the memory object where placeholders are being replaced).
  746. * On success, it should return 1, writing a valid value reference to *out.
  747. * On failure, it can either return 0, or return 1 but write an invalid value reference.
  748. * This callback must not access the memory object in any other way than building
  749. * new values in it; it must not modify any values that were already present at the
  750. * point it was called.
  751. */
  752. typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  753. /**
  754. * Executes the replacement program, replacing placeholders in a value.
  755. * The memory object must given be identical to the memory object which was used in
  756. * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_InitCopy}.
  757. * This will call the callback 'replace', which should build the values to replace
  758. * the placeholders.
  759. * Returns 1 on success and 0 on failure. On failure, the entire memory object enters
  760. * and inconsistent state and must be freed using {@link NCDValMem_Free} before
  761. * performing any other operation on it.
  762. * The program is passed by value instead of pointer because this appears to be faster.
  763. * Is is not modified in any way.
  764. */
  765. int NCDValReplaceProg_Execute (NCDValReplaceProg prog, NCDValMem *mem, NCDVal_replace_func replace, void *arg);
  766. #endif