buffer.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. void LSUP_buffer_done (LSUP_Buffer *buf);
  130. /** @brief Free a buffer.
  131. */
  132. void LSUP_buffer_free (LSUP_Buffer *buf);
  133. /** @brief Hash a buffer.
  134. */
  135. inline LSUP_Key
  136. LSUP_buffer_hash (const LSUP_Buffer *buf)
  137. {
  138. return (buf == NULL) ? NULL_KEY :
  139. LSUP_HASH (buf->addr, buf->size, LSUP_HASH_SEED);
  140. }
  141. /** @brief Print a byte string of a given length in a human-readable format.
  142. *
  143. * The string is printed in Python style: printable characters are output
  144. * literally, and non-printable ones as hex sequences.
  145. */
  146. void LSUP_buffer_print (const LSUP_Buffer *buf);
  147. /** @brief Format a buffer into anb ASCII string.
  148. *
  149. * The string has non-printable characters escaped as "\xNN".
  150. *
  151. * @param[in] buf Buffer to convert.
  152. *
  153. * @return Formatted string. It must be freed with free().
  154. */
  155. char *
  156. LSUP_buffer_as_str (const LSUP_Buffer *buf);
  157. /** @brief Compare two buffers.
  158. *
  159. * The return value is the same as memcmp.
  160. */
  161. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  162. {
  163. return memcmp (
  164. buf1->addr, buf2->addr,
  165. (buf1->size > buf2->size ? buf1->size : buf2->size));
  166. }
  167. /** @brief Return whether two buffers are equal.
  168. *
  169. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  170. * first.
  171. */
  172. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  173. {
  174. if (buf1->size != buf2->size) return false;
  175. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  176. }
  177. /*
  178. * Buffer triples.
  179. */
  180. /** @brief Create a new buffer triple.
  181. *
  182. * @important The triple must be freed with #LSUP_btriple_free().
  183. *
  184. * @param[in] s Subject as a serialized buffer.
  185. *
  186. * @param[in] p Predicate as a serialized buffer.
  187. *
  188. * @param[in] o Object as a serialized buffer.
  189. *
  190. * @return New triple.
  191. */
  192. LSUP_BufferTriple *
  193. LSUP_btriple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  194. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  195. *
  196. * @important The triple must be freed with #LSUP_btriple_free().
  197. *
  198. * @param[in] sspo Serialized triple pointer to initialize.
  199. *
  200. * @param[in] s Subject as a serialized buffer.
  201. *
  202. * @param[in] p Predicate as a serialized buffer.
  203. *
  204. * @param[in] o Object as a serialized buffer.
  205. *
  206. * @return LSUP_OK on success.
  207. */
  208. LSUP_rc
  209. LSUP_btriple_init (
  210. LSUP_BufferTriple *sspo,
  211. LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  212. /** @brief Free the internal pointers of a buffer triple.
  213. *
  214. * @param[in] sspo Buffer triple to be freed.
  215. */
  216. void
  217. LSUP_btriple_done (LSUP_BufferTriple *sspo);
  218. /** @brief Free a buffer triple and all its internal pointers.
  219. *
  220. * @important If the buffer pointers are not to be freed (e.g. they are owned
  221. * by a back end), use a simple free(sspo) instead of this.
  222. *
  223. * @param[in] sspo Buffer triple to be freed.
  224. */
  225. void
  226. LSUP_btriple_free (LSUP_BufferTriple *sspo);
  227. /** @brief Get serialized triple by term position.
  228. *
  229. * Useful for looping over all terms.
  230. *
  231. * @param[in] btrp Serialized triple pointer.
  232. *
  233. * @param[in] n A number between 0÷2.
  234. *
  235. * @return Corresponding serialized term or NULL if n is out of range.
  236. */
  237. inline LSUP_Buffer *
  238. LSUP_btriple_pos (const LSUP_BufferTriple *btrp, LSUP_TriplePos n)
  239. {
  240. if (n == TRP_POS_S) return btrp->s;
  241. if (n == TRP_POS_P) return btrp->p;
  242. if (n == TRP_POS_O) return btrp->o;
  243. return NULL;
  244. }
  245. /** @brief Hash a buffer triple.
  246. *
  247. * @todo This doesn't handle blank nodes correctly. RDF_Canon should be ported
  248. * to this library.
  249. *
  250. * @param[in] strp Serialized triple to hash.
  251. *
  252. * @return Hash value.
  253. */
  254. inline LSUP_Key
  255. LSUP_btriple_hash (const LSUP_BufferTriple *strp)
  256. {
  257. return LSUP_HASH (
  258. strp->s->addr, strp->s->size,
  259. LSUP_HASH (
  260. strp->p->addr, strp->p->size,
  261. LSUP_HASH (strp->o->addr, strp->o->size, LSUP_HASH_SEED)
  262. )
  263. );
  264. }
  265. /** @brief Dummy buffer triple.
  266. *
  267. * Triple of dummy buffer, with #LSUP_Buffer size space allocated, but no
  268. * contents.
  269. *
  270. * Free with #LSUP_btriple_free().
  271. */
  272. #define BTRP_DUMMY LSUP_btriple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY)
  273. /// @} END defgroup buffer
  274. #endif