buffer.h 6.9 KB

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