codec.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef _LSUP_CODEC_BASE_H
  2. #define _LSUP_CODEC_BASE_H
  3. #include "graph.h"
  4. /**
  5. * Max data size passed to the scanner and parser at each iteration.
  6. */
  7. #ifdef LSUP_RDF_STREAM_CHUNK_SIZE
  8. #define CHUNK_SIZE LSUP_RDF_STREAM_CHUNK_SIZE
  9. #else
  10. #define CHUNK_SIZE 8192
  11. #endif
  12. typedef struct codec_t LSUP_Codec;
  13. /** @brief Codec iterator type.
  14. *
  15. * This structure holds state data including input and output for encoding a
  16. * graph into RDF. Normally it should not be inspected or manipulated directly,
  17. * but rather passed to codec iteration functions for processing RDF.
  18. *
  19. * NOTE: This should be used as an opaque handle, however it is exposed here
  20. * for easier inclusion into each codec.
  21. */
  22. typedef struct codec_iter_t {
  23. const LSUP_Codec * codec; // Codec that generated this iterator.
  24. LSUP_Triple * trp; // RDF fragment being encoded.
  25. LSUP_GraphIterator *gr_it; // Graph iterator.
  26. const LSUP_NSMap * nsm; // Namespace map.
  27. size_t cur; // Internal cursor.
  28. LSUP_rc rc; // Internal return code.
  29. char * rep, // String representation of a RDF fragment.
  30. * str_s, // Temporary string.
  31. * str_p, // Temporary string.
  32. * str_o; // Temporary string.
  33. } LSUP_CodecIterator;
  34. /// Parser state.
  35. typedef struct {
  36. LSUP_GraphIterator * it; ///< Iterator used to build the graph.
  37. LSUP_NSMap * nsm; ///< NS map used in the document.
  38. LSUP_Term * base; ///< Base IRI used in the document.
  39. size_t ct; ///< Statements parsed.
  40. LSUP_rc rc; ///< Internal return code.
  41. } LSUP_TTLParserState;
  42. /** @brief Parse error information.
  43. *
  44. */
  45. /* TODO A plain string will suffice for now.
  46. typedef struct parse_error_t {
  47. unsigned int line; // Line number where the error occurred.
  48. unsigned int linec; // Position in line of the offending token.
  49. char * token; // String representation of the token.
  50. } LSUP_ParseError;
  51. */
  52. /*
  53. * Interface prototypes.
  54. */
  55. /** @brief Term encoder callback type.
  56. *
  57. * @param[in] term Single term handle.
  58. *
  59. * @param[in] nsm Namespace map. May be NULL for no prefix shortening.
  60. *
  61. * @param[out] rep Pointer to a string to be filled with the encoded term. The
  62. * caller is in charge of freeing the string after use. Returns undefined on
  63. * error.
  64. *
  65. * @return LSUP_OK on successful encoding; <0 for other errors.
  66. */
  67. typedef LSUP_rc (*term_enc_fn_t)(
  68. const LSUP_Term *term, const LSUP_NSMap *nsm, char **rep);
  69. /** @brief Initialize a graph encoding loop.
  70. *
  71. * This prototype is to be implemented by graph encoding loops. It should
  72. * create an iterator and perform all initial setup for finding triples.
  73. *
  74. * Implementations MUST set the "codec" member of the iterator to the address
  75. * of the codec that generated it.
  76. *
  77. * @param[in] gr The graph to be encoded. The graph's namespace map is used by
  78. * the codec for namespace prefixing. The graph may only be freed after the
  79. * loop is finalized.
  80. *
  81. * @return A codec iterator handle to be passed to a #gr_codec_iter_fn_t
  82. * function and, eventually, to a #gr_codec_done_fn_t function.
  83. */
  84. typedef LSUP_CodecIterator * (*gr_encode_init_fn_t)(const LSUP_Graph *gr);
  85. /** @brief Perform one encoding iteration.
  86. *
  87. * Implementations of this prototype MUST perform all the steps to encode one
  88. * or more complete triples into an RDF fragment representing those triples.
  89. * The input and output units are up to the implementation and a caller SHOULD
  90. * assume that multiple lines may be yielded at each iteration.
  91. *
  92. * @param[in] it Iterator handle.
  93. *
  94. * @param[out] res Handle to be populated with a string obtained from encoding.
  95. * The output data should be UTF-8 [TODO or UTF-16] encoded. This pointer
  96. * must be initialized (even to NULL) and should be eventually freed manually
  97. * at the end of the loop. It is reallocated at each iteration, so memory from
  98. * a previous iteration may be overwritten with new data.
  99. *
  100. * @return LSUP_OK if a new token was processed; LSUP_END if the end of the
  101. * loop was reached.
  102. */
  103. typedef LSUP_rc (*gr_encode_iter_fn_t)(
  104. LSUP_CodecIterator *it, unsigned char **res);
  105. /** @brief Finalize an encoding operation.
  106. *
  107. * Implementations SHOULD use this function to perform all necessary steps to
  108. * clean up memory and free the iterator handle after a graph has been
  109. * completely encoded.
  110. *
  111. * @param[in] it Iterator handle.
  112. */
  113. typedef void (*gr_encode_done_fn_t)(LSUP_CodecIterator *it);
  114. /** @brief Prototype for decoding a string into a LSUP_Term.
  115. *
  116. * Implementations MAY ignore any other tokens after finding the first one.
  117. *
  118. * @param[in] rep NT representation of the term.
  119. *
  120. * @param[in] nsm Namespace map handle.
  121. *
  122. * @param[out] Pointer to the term handle to be created. Implementaions SHOULD
  123. * return NULL on a parse error.
  124. *
  125. * @return Implementations MUST return LSUP_OK on success and a negative value
  126. * on parsing error.
  127. */
  128. typedef LSUP_rc (*term_decode_fn_t)(
  129. const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
  130. /** @brief Prototype for decoding a complete RDF document into a graph.
  131. *
  132. * Implementations SHOULD consume data from the file handle in chunks.
  133. *
  134. * @param[in] rep Open file handle pointing to the RDF data. Implementations
  135. * MUST NOT close the file handle.
  136. *
  137. * @param[out] gr Pointer to a graph handle to be generated from decoding.
  138. *
  139. * @param[out] ct If not NULL, it may be populated with the number of triples
  140. * parsed (which may be different from the resulting graph size).
  141. * Implementations may choose not not use this, and they must account for the
  142. * value to be NULL.
  143. *
  144. * @param[out] err Pointer to error info string. If no error occurs, it yields
  145. * NULL.
  146. *
  147. * @return Implementations MUST return LSUP_OK on success and a negative value
  148. * on parsing error.
  149. */
  150. typedef LSUP_rc (*gr_decode_fn_t)(
  151. FILE *rep, LSUP_Graph **gr, size_t *ct, char **err);
  152. /** @brief Codec structure.
  153. *
  154. * An instance of this structure is usually defined at compile time (see
  155. * examples in "include/codec_*.h" and "src/codec_*.c") and should have the
  156. * following defined:
  157. *
  158. * - name: A brief (16-char max), human-readable to identify the codec.
  159. * - mimetype: MIME type (32-char max) associated with the codec.
  160. * - extension: File extension associated with the serialized file.
  161. *
  162. * - encode_term: Encode a single term.
  163. *
  164. * - encode_graph_init: Initialize a graph decoding loop.
  165. * - encode_graph_iter: Run one iteration of encoding on one or more triples.
  166. * - encode_graph_done: Finalize the encoding loop and free the support data.
  167. *
  168. * - decode_term: Decode a single term.
  169. * - decode_graph: Decode a RDF document into a graph.
  170. *
  171. * For documentation on the individual encoding and decoding callbacks, see the
  172. * related function prototypes.
  173. */
  174. struct codec_t {
  175. char name[16]; // Name of the codec.
  176. char mimetype[32]; // MIME type associated with the codec.
  177. char extension[8]; // Serialized file extension.
  178. // Encoding.
  179. term_enc_fn_t encode_term; // Term encoder function.
  180. gr_encode_init_fn_t encode_graph_init; // Graph encoder initialization.
  181. gr_encode_iter_fn_t encode_graph_iter; // Graph encoder iteration.
  182. gr_encode_done_fn_t encode_graph_done; // Graph encoder finalization.
  183. // Decoding.
  184. term_decode_fn_t decode_term; // Term decoder function.
  185. gr_decode_fn_t decode_graph; // Graph decoder function.
  186. };
  187. /*
  188. * Common utility functions.
  189. */
  190. /** @brief strdup() for unsigned char.
  191. *
  192. * This is to be used with uint8_t sequences considered to be UTF-8 sequences,
  193. * requird by re2c (it won't work with byte sequences containing `NUL`).
  194. */
  195. inline uint8_t
  196. *uint8_dup (const uint8_t *str)
  197. { return (uint8_t *) strdup ((char *) str); }
  198. /** @brief strndup() for unsigned char.
  199. *
  200. * This is to be used with uint8_t sequences considered to be UTF-8 sequences,
  201. * requird by re2c (it won't work with byte sequences containing `NUL`).
  202. */
  203. inline uint8_t
  204. *uint8_ndup (const uint8_t *str, size_t size)
  205. { return (uint8_t *) strndup ((char *) str, size); }
  206. /** Replace non-printable characters with their literal byte.
  207. *
  208. * Escape backslash is to be added separately.
  209. */
  210. static inline char
  211. escape_char (const char c) {
  212. switch (c) {
  213. case '\t': return 't';
  214. case '\b': return 'b';
  215. case '\n': return 'n';
  216. case '\r': return 'r';
  217. case '\f': return 'f';
  218. default: return c;
  219. }
  220. }
  221. /** @brief Unescape a single character.
  222. *
  223. * Convert escaped special characters such as `\t`, `\n`, etc. into their
  224. * corresponding code points.
  225. *
  226. * Non-special characters are returned unchanged.
  227. *
  228. * @param[in] c Character to unescape. Note that this is the single character
  229. * after `\`.
  230. *
  231. * @return Code point corresponding to the escaped character.
  232. */
  233. inline char
  234. unescape_char (const char c)
  235. {
  236. switch (c) {
  237. case 't': return '\t';
  238. case 'b': return '\b';
  239. case 'n': return '\n';
  240. case 'r': return '\r';
  241. case 'f': return '\f';
  242. default: return c;
  243. }
  244. }
  245. /** @brief Replace \uxxxx and \Uxxxxxxxx with Unicode bytes.
  246. *
  247. * @param[in] esc_str Escaped string.
  248. *
  249. * @param[in] size Maximum number of characters to scan, à la strncpy().
  250. *
  251. * @return String with escape sequences replaced by Unicode bytes.
  252. */
  253. uint8_t *unescape_unicode (const uint8_t *esc_str, size_t size);
  254. #endif