/* Copyright 2011-2020 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "reader.h" #include "byte_source.h" #include "node.h" #include "serd/serd.h" #include "serd_internal.h" #include "stack.h" #include "system.h" #include "world.h" #include #include #include #include #include #include static SerdStatus serd_reader_prepare(SerdReader* reader); SerdStatus r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...) { va_list args; va_start(args, fmt); const Cursor* const cur = &reader->source.cur; const SerdError e = { st, cur->filename, cur->line, cur->col, fmt, &args }; serd_world_error(reader->world, &e); va_end(args); return st; } void set_blank_id(SerdReader* reader, SerdNode* node, size_t buf_size) { char* buf = (char*)(node + 1); const char* prefix = reader->bprefix ? (const char*)reader->bprefix : ""; node->n_bytes = (size_t)snprintf(buf, buf_size, "%sb%u", prefix, reader->next_id++); } size_t genid_size(SerdReader* reader) { return reader->bprefix_len + 1 + 10 + 1; // + "b" + UINT32_MAX + \0 } SerdNode* blank_id(SerdReader* reader) { SerdNode* ref = push_node_padded( reader, genid_size(reader), SERD_BLANK, "", 0); if (ref) { set_blank_id(reader, ref, genid_size(reader)); } return ref; } SerdNode* push_node_padded(SerdReader* reader, size_t maxlen, SerdType type, const char* str, size_t n_bytes) { // Push a null byte to ensure the previous node was null terminated char* terminator = (char*)serd_stack_push(&reader->stack, 1); if (!terminator) { return NULL; } *terminator = 0; void* mem = serd_stack_push_aligned( &reader->stack, sizeof(SerdNode) + maxlen + 1, sizeof(SerdNode)); if (!mem) { reader->status = SERD_ERR_OVERFLOW; return NULL; } SerdNode* const node = (SerdNode*)mem; node->n_bytes = n_bytes; node->flags = 0; node->type = type; char* buf = (char*)(node + 1); memcpy(buf, str, n_bytes + 1); #ifdef SERD_STACK_CHECK reader->allocs = realloc( reader->allocs, sizeof(reader->allocs) * (++reader->n_allocs)); reader->allocs[reader->n_allocs - 1] = node; #endif return node; } SerdNode* push_node(SerdReader* reader, SerdType type, const char* str, size_t n_bytes) { return push_node_padded(reader, n_bytes, type, str, n_bytes); } SerdStatus emit_statement(SerdReader* reader, ReadContext ctx, SerdNode* o) { SerdNode* graph = ctx.graph; if (!reader->sink->statement) { return SERD_SUCCESS; } else if (!graph && reader->default_graph) { graph = reader->default_graph; } /* Zero the pad of the object node on the top of the stack. Lower nodes (subject and predicate) were already zeroed by subsequent pushes. */ serd_node_zero_pad(o); const SerdStatus st = reader->sink->statement( reader->sink->handle, *ctx.flags, graph, ctx.subject, ctx.predicate, o); *ctx.flags &= SERD_ANON_CONT|SERD_LIST_CONT; // Preserve only cont flags return st; } static SerdStatus read_statement(SerdReader* reader) { switch (reader->syntax) { default: return read_n3_statement(reader); } } SerdStatus serd_reader_read_document(SerdReader* reader) { if (!reader->source.prepared) { SerdStatus st = serd_reader_prepare(reader); if (st) { return st; } } return ((reader->syntax == SERD_NQUADS) ? read_nquadsDoc(reader) : read_turtleTrigDoc(reader)); } SerdReader* serd_reader_new(SerdWorld* world, SerdSyntax syntax, const SerdSink* sink, size_t stack_size) { SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader)); me->world = world; me->sink = sink; me->default_graph = NULL; me->stack = serd_stack_new(stack_size); me->syntax = syntax; me->next_id = 1; me->strict = true; me->rdf_first = push_node(me, SERD_URI, NS_RDF "first", 48); me->rdf_rest = push_node(me, SERD_URI, NS_RDF "rest", 47); me->rdf_nil = push_node(me, SERD_URI, NS_RDF "nil", 46); return me; } void serd_reader_set_strict(SerdReader* reader, bool strict) { reader->strict = strict; } void serd_reader_free(SerdReader* reader) { serd_node_free(reader->default_graph); #ifdef SERD_STACK_CHECK free(reader->allocs); #endif free(reader->stack.buf); free(reader->bprefix); free(reader); } void serd_reader_add_blank_prefix(SerdReader* reader, const char* prefix) { free(reader->bprefix); reader->bprefix_len = 0; reader->bprefix = NULL; if (prefix) { reader->bprefix_len = strlen(prefix); reader->bprefix = (char*)malloc(reader->bprefix_len + 1); memcpy(reader->bprefix, prefix, reader->bprefix_len + 1); } } void serd_reader_set_default_graph(SerdReader* reader, const SerdNode* graph) { serd_node_free(reader->default_graph); reader->default_graph = serd_node_copy(graph); } static SerdStatus skip_bom(SerdReader* me) { if (serd_byte_source_peek(&me->source) == 0xEF) { serd_byte_source_advance(&me->source); if (serd_byte_source_peek(&me->source) != 0xBB || serd_byte_source_advance(&me->source) || serd_byte_source_peek(&me->source) != 0xBF || serd_byte_source_advance(&me->source)) { r_err(me, SERD_ERR_BAD_SYNTAX, "corrupt byte order mark\n"); return SERD_ERR_BAD_SYNTAX; } } return SERD_SUCCESS; } SerdStatus serd_reader_start_stream(SerdReader* reader, SerdReadFunc read_func, SerdStreamErrorFunc error_func, void* stream, const char* name, size_t page_size) { return serd_byte_source_open_source( &reader->source, read_func, error_func, NULL, stream, name, page_size); } SerdStatus serd_reader_start_file(SerdReader* reader, const char* uri, bool bulk) { char* const path = serd_file_uri_parse(uri, NULL); if (!path) { return SERD_ERR_BAD_ARG; } FILE* fd = serd_world_fopen(reader->world, path, "rb"); free(path); if (!fd) { return SERD_ERR_UNKNOWN; } return serd_byte_source_open_source( &reader->source, bulk ? (SerdReadFunc)fread : serd_file_read_byte, (SerdStreamErrorFunc)ferror, (SerdStreamCloseFunc)fclose, fd, uri, bulk ? SERD_PAGE_SIZE : 1); } SerdStatus serd_reader_start_string(SerdReader* reader, const char* utf8) { return serd_byte_source_open_string(&reader->source, utf8); } static SerdStatus serd_reader_prepare(SerdReader* reader) { reader->status = serd_byte_source_prepare(&reader->source); if (reader->status == SERD_SUCCESS) { reader->status = skip_bom(reader); } else if (reader->status == SERD_FAILURE) { reader->source.eof = true; } else { r_err(reader, reader->status, "read error: %s\n", strerror(errno)); } return reader->status; } SerdStatus serd_reader_read_chunk(SerdReader* reader) { SerdStatus st = SERD_SUCCESS; if (!reader->source.prepared) { st = serd_reader_prepare(reader); } else if (reader->source.eof) { st = serd_byte_source_advance(&reader->source); } if (peek_byte(reader) == 0) { // Skip leading null byte, for reading from a null-delimited socket eat_byte_safe(reader, 0); } return st ? st : read_statement(reader); } SerdStatus serd_reader_finish(SerdReader* reader) { return serd_byte_source_close(&reader->source); }