From ee9f4956ac7437ab35f04d5e71bace000735d654 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 15 Mar 2016 23:21:34 -0400 Subject: Use SerdBuffer for mutable buffers This avoids const violations from abusing SerdChunk as a mutable buffer for string sinks. --- src/node.c | 17 +++++++++-------- src/uri.c | 10 +++++----- src/writer.c | 14 +++++++------- 3 files changed, 21 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/node.c b/src/node.c index a63d350c..22555493 100644 --- a/src/node.c +++ b/src/node.c @@ -175,27 +175,28 @@ serd_node_new_file_uri(const uint8_t* path, evil ? "/" : ""); } - SerdChunk chunk = { uri, uri_len }; + SerdBuffer buffer = { uri, uri_len }; for (size_t i = 0; i < path_len; ++i) { if (evil && path[i] == '\\') { - serd_chunk_sink("/", 1, &chunk); + serd_buffer_sink("/", 1, &buffer); } else if (path[i] == '%') { - serd_chunk_sink("%%", 2, &chunk); + serd_buffer_sink("%%", 2, &buffer); } else if (!escape || is_uri_path_char(path[i])) { - serd_chunk_sink(path + i, 1, &chunk); + serd_buffer_sink(path + i, 1, &buffer); } else { char escape_str[4] = { '%', 0, 0, 0 }; snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", path[i]); - serd_chunk_sink(escape_str, 3, &chunk); + serd_buffer_sink(escape_str, 3, &buffer); } } - serd_chunk_sink_finish(&chunk); + serd_buffer_sink_finish(&buffer); if (out) { - serd_uri_parse(chunk.buf, out); + serd_uri_parse((const uint8_t*)buffer.buf, out); } - return serd_node_from_substring(SERD_URI, chunk.buf, chunk.len); + return serd_node_from_substring( + SERD_URI, (const uint8_t*)buffer.buf, buffer.len); } SerdNode diff --git a/src/uri.c b/src/uri.c index f347f010..1508135c 100644 --- a/src/uri.c +++ b/src/uri.c @@ -68,25 +68,25 @@ serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) ++path; } - SerdChunk chunk = { NULL, 0 }; + SerdBuffer buffer = { NULL, 0 }; for (const uint8_t* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { - serd_chunk_sink("%", 1, &chunk); + serd_buffer_sink("%", 1, &buffer); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { const uint8_t code[3] = {*(s + 1), *(s + 2), 0}; const uint8_t c = (uint8_t)strtoul((const char*)code, NULL, 16); - serd_chunk_sink(&c, 1, &chunk); + serd_buffer_sink(&c, 1, &buffer); s += 2; } else { s += 2; // Junk escape, ignore } } else { - serd_chunk_sink(s, 1, &chunk); + serd_buffer_sink(s, 1, &buffer); } } - return serd_chunk_sink_finish(&chunk); + return serd_buffer_sink_finish(&buffer); } bool diff --git a/src/writer.c b/src/writer.c index 8dddf51c..e9f928b8 100644 --- a/src/writer.c +++ b/src/writer.c @@ -976,18 +976,18 @@ serd_file_sink(const void* buf, size_t len, void* stream) } size_t -serd_chunk_sink(const void* buf, size_t len, void* stream) +serd_buffer_sink(const void* buf, size_t len, void* stream) { - SerdChunk* chunk = (SerdChunk*)stream; - chunk->buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); - memcpy((uint8_t*)chunk->buf + chunk->len, buf, len); - chunk->len += len; + SerdBuffer* buffer = (SerdBuffer*)stream; + buffer->buf = (char*)realloc(buffer->buf, buffer->len + len); + memcpy((uint8_t*)buffer->buf + buffer->len, buf, len); + buffer->len += len; return len; } uint8_t* -serd_chunk_sink_finish(SerdChunk* stream) +serd_buffer_sink_finish(SerdBuffer* stream) { - serd_chunk_sink("", 1, stream); + serd_buffer_sink("", 1, stream); return (uint8_t*)stream->buf; } -- cgit v1.2.1