buffer.h 8.4 KB

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