buffer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #ifndef _LSUP_BUFFER_H
  2. #define _LSUP_BUFFER_H
  3. #include "core.h"
  4. /** @brief "NULL" key, a value that is never user-provided.
  5. *
  6. * Used to mark special values (e.g. deleted records).
  7. */
  8. #define NULL_KEY 0
  9. /// Triple position of s, p, o.
  10. typedef enum {
  11. TRP_POS_S = 0,
  12. TRP_POS_P = 1,
  13. TRP_POS_O = 2,
  14. } LSUP_TriplePos;
  15. /// Buffer flags, stored in buffer structure.
  16. typedef enum {
  17. LSUP_BUF_BORROWED = 1<<0, /**< Borrowed buffer. This indicates that
  18. * the memory block pointed to by the
  19. * buffer is owned by another function,
  20. * and instructs #LSUP_buffer_free() to
  21. * only free the buffer handle, but not
  22. * the underlying data.
  23. */
  24. } LSUP_BufferFlag;
  25. /** @brief General-purpose data buffer.
  26. *
  27. * The structure is transparently exposed so that the related API only defines
  28. * few basic helper methods. Other operations, such as appending, may be
  29. * performed by simply using the addr and size attributes.
  30. *
  31. * A buffer can be initialized once and reused multiple times, e.g. in a loop,
  32. * without being freed between iterations, by using #LSUP_buffer_init.
  33. */
  34. typedef struct LSUP_Buffer {
  35. /*@null@*/ unsigned char *addr;
  36. size_t size;
  37. LSUP_BufferFlag flags;
  38. } LSUP_Buffer;
  39. /** @brief Triple of byte buffers.
  40. *
  41. * This is a generic data triple. Store implementations should handle this
  42. * data type rather than RDF terms and triples. Conversion to/from RDF terms
  43. * and triples is done in the term and triple modules.
  44. */
  45. typedef struct buffer_triple_t {
  46. LSUP_Buffer *s;
  47. LSUP_Buffer *p;
  48. LSUP_Buffer *o;
  49. } LSUP_BufferTriple;
  50. /*
  51. * Function prototypes.
  52. */
  53. /** @brief Initialize or reuse a buffer handle.
  54. *
  55. * The handle must have been created with #LSUP_buffer_new*().
  56. *
  57. * The data block is resized without being freed first. The handle must be
  58. * eventually freed with #LSUP_buffer_done() after use.
  59. *
  60. * @param[in] buf A buffer handle obtained with #LSUP_buffer_new or by manual
  61. * allocation.
  62. *
  63. * @param[in] size New size.
  64. *
  65. * @param[in] data If not NULL, data to replace the existing ones. The size
  66. * of the data to be copied is determined by the size parameter. If NULL, the
  67. * existing data are preserved as with a normal realloc().
  68. */
  69. LSUP_rc
  70. LSUP_buffer_init (
  71. LSUP_Buffer *buf, const size_t size, const unsigned char *data);
  72. /** @brief Create a new buffer and optionally populate it with data.
  73. *
  74. * To change the buffer size and/or data later call #LSUP_buffer_init.
  75. *
  76. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->addr, buf1->size);
  77. *
  78. * @param[in] size Length of the data.
  79. *
  80. * @param[in] data Optional data to initially populate the object with. If
  81. * NULL, the buffer data are garbage.
  82. *
  83. * @return LSUP_Buffer handle. It must be freed with #LSUP_buffer_free. NULL
  84. * on error.
  85. */
  86. inline LSUP_Buffer *
  87. LSUP_buffer_new (const unsigned char *data, const size_t size)
  88. {
  89. LSUP_Buffer *buf;
  90. CALLOC_GUARD (buf, NULL);
  91. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  92. free (buf->addr);
  93. free (buf);
  94. return NULL;
  95. }
  96. return buf;
  97. }
  98. /** @brief Create a borrowed buffer (memory view).
  99. *
  100. * A borrowed buffer does not own the memory block pointed to and should not
  101. * be freed. It can be identified by the LSUP_BUF_BORROWED flag.
  102. *
  103. * @param[in] data Address of data handled by the buffer.
  104. *
  105. * @param[in] size Length of the data.
  106. *
  107. * @return LSUP_Buffer handle. It must be freed with #LSUP_buffer_free, which
  108. * will correctly leave the underlying data alone. NULL on error.
  109. */
  110. inline LSUP_Buffer *
  111. LSUP_buffer_new_borrowed (unsigned char *data, const size_t size)
  112. {
  113. LSUP_Buffer *buf;
  114. MALLOC_GUARD (buf, NULL);
  115. buf->addr = data;
  116. buf->size = size;
  117. buf->flags = LSUP_BUF_BORROWED;
  118. return buf;
  119. }
  120. /** @brief Dummy buffer to be used with #LSUP_buffer_init.
  121. */
  122. #define BUF_DUMMY LSUP_buffer_new (NULL, 0)
  123. /** @brief Free the content of a buffer.
  124. */
  125. void LSUP_buffer_done (LSUP_Buffer *buf);
  126. /** @brief Free a buffer.
  127. */
  128. void LSUP_buffer_free (LSUP_Buffer *buf);
  129. /** @brief Hash a buffer.
  130. */
  131. inline LSUP_Key
  132. LSUP_buffer_hash (const LSUP_Buffer *buf)
  133. {
  134. return (buf == NULL) ? NULL_KEY :
  135. LSUP_HASH (buf->addr, buf->size, LSUP_HASH_SEED);
  136. }
  137. /** @brief Print a byte string of a given length in a human-readable format.
  138. *
  139. * The string is printed in Python style: printable characters are output
  140. * literally, and non-printable ones as hex sequences.
  141. */
  142. void LSUP_buffer_print (const LSUP_Buffer *buf);
  143. /** @brief Format a buffer into anb ASCII string.
  144. *
  145. * The string has non-printable characters escaped as "\xNN".
  146. *
  147. * @param[in] buf Buffer to convert.
  148. *
  149. * @return Formatted string. It must be freed with free().
  150. */
  151. char *
  152. LSUP_buffer_as_str (const LSUP_Buffer *buf);
  153. /** @brief Compare two buffers.
  154. *
  155. * The return value is the same as memcmp.
  156. */
  157. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  158. {
  159. return memcmp (
  160. buf1->addr, buf2->addr,
  161. (buf1->size > buf2->size ? buf1->size : buf2->size));
  162. }
  163. /** @brief Return whether two buffers are equal.
  164. *
  165. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  166. * first.
  167. */
  168. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  169. {
  170. if (buf1->size != buf2->size) return false;
  171. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  172. }
  173. /*
  174. * Buffer triples.
  175. */
  176. /** @brief Create a new buffer triple.
  177. *
  178. * @important The triple must be freed with #LSUP_btriple_free().
  179. *
  180. * @param[in] sspo Serialized triple pointer to initialize.
  181. *
  182. * @param[in] s Subject as a serialized buffer.
  183. *
  184. * @param[in] p Predicate as a serialized buffer.
  185. *
  186. * @param[in] o Object as a serialized buffer.
  187. *
  188. * @return New triple.
  189. */
  190. LSUP_BufferTriple *
  191. LSUP_btriple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  192. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  193. *
  194. * @important The triple must be freed with #LSUP_btriple_free().
  195. *
  196. * @param[in] sspo Serialized triple pointer to initialize.
  197. *
  198. * @param[in] s Subject as a serialized buffer.
  199. *
  200. * @param[in] p Predicate as a serialized buffer.
  201. *
  202. * @param[in] o Object as a serialized buffer.
  203. *
  204. * @return LSUP_OK on success.
  205. */
  206. LSUP_rc
  207. LSUP_btriple_init (
  208. LSUP_BufferTriple *sspo,
  209. LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  210. /** @brief Free the internal pointers of a buffer triple.
  211. *
  212. * @param[in] sspo Buffer triple to be freed.
  213. */
  214. void
  215. LSUP_btriple_done (LSUP_BufferTriple *sspo);
  216. /** @brief Free a buffer triple and all its internal pointers.
  217. *
  218. * @important If the buffer pointers are not to be freed (e.g. they are owned
  219. * by a back end), use a simple free(sspo) instead of this.
  220. *
  221. * @param[in] sspo Buffer triple to be freed.
  222. */
  223. void
  224. LSUP_btriple_free (LSUP_BufferTriple *sspo);
  225. /** @brief Get serialized triple by term position.
  226. *
  227. * Useful for looping over all terms.
  228. *
  229. * @param[in] btrp Serialized triple pointer.
  230. *
  231. * @param[in] n A number between 0÷2.
  232. *
  233. * @return Corresponding serialized term or NULL if n is out of range.
  234. */
  235. inline LSUP_Buffer *
  236. LSUP_btriple_pos (const LSUP_BufferTriple *btrp, LSUP_TriplePos n)
  237. {
  238. if (n == TRP_POS_S) return btrp->s;
  239. if (n == TRP_POS_P) return btrp->p;
  240. if (n == TRP_POS_O) return btrp->o;
  241. return NULL;
  242. }
  243. /** @brief Hash a buffer triple.
  244. *
  245. * @todo This doesn't handle blank nodes correctly. RDF_Canon should be ported
  246. * to this library.
  247. *
  248. * @param[in] strp Serialized triple to hash.
  249. *
  250. * @return Hash value.
  251. */
  252. inline LSUP_Key
  253. LSUP_btriple_hash (const LSUP_BufferTriple *strp)
  254. {
  255. return LSUP_HASH (
  256. strp->s->addr, strp->s->size,
  257. LSUP_HASH (
  258. strp->p->addr, strp->p->size,
  259. LSUP_HASH (strp->o->addr, strp->o->size, LSUP_HASH_SEED)
  260. )
  261. );
  262. }
  263. /** @brief Dummy buffer triple.
  264. *
  265. * Triple of dummy buffer, with #LSUP_Buffer size space allocated, but no
  266. * contents.
  267. *
  268. * Free with #LSUP_btriple_free().
  269. */
  270. #define BTRP_DUMMY LSUP_btriple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY)
  271. #endif