summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-12-28 13:19:47 +0100
committerTor Andersson <tor.andersson@artifex.com>2017-01-09 13:21:40 +0100
commitbbcc85a9f746c161b2e23c6057e69ec7b967252b (patch)
tree8ead60bd762cc0cbf945c002c769e74699594c0b /source
parente9667e7f8ab7c154d8932916a22c33cf2bad0445 (diff)
downloadmupdf-bbcc85a9f746c161b2e23c6057e69ec7b967252b.tar.xz
Add fz_terminate_buffer function.
Non-destructively zero terminate a fz_buffer for use as a C string.
Diffstat (limited to 'source')
-rw-r--r--source/fitz/buffer.c14
-rw-r--r--source/fitz/stext-search.c5
-rw-r--r--source/html/epub-doc.c7
-rw-r--r--source/html/html-doc.c8
-rw-r--r--source/html/html-layout.c7
-rw-r--r--source/svg/svg-doc.c4
-rw-r--r--source/xps/xps-zip.c7
7 files changed, 21 insertions, 31 deletions
diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c
index 13aae983..a2880659 100644
--- a/source/fitz/buffer.c
+++ b/source/fitz/buffer.c
@@ -152,6 +152,15 @@ fz_trim_buffer(fz_context *ctx, fz_buffer *buf)
fz_resize_buffer(ctx, buf, buf->len);
}
+void
+fz_terminate_buffer(fz_context *ctx, fz_buffer *buf)
+{
+ /* ensure that there is a zero-byte after the end of the data */
+ if (buf->len + 1 > buf->cap)
+ fz_grow_buffer(ctx, buf);
+ buf->data[buf->len] = 0;
+}
+
size_t
fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap)
{
@@ -165,10 +174,7 @@ fz_string_from_buffer(fz_context *ctx, fz_buffer *buf)
{
if (!buf)
return "";
-
- if (buf->data[buf->len-1] != 0)
- fz_write_buffer_byte(ctx, buf, 0);
-
+ fz_terminate_buffer(ctx, buf);
return (const char *)buf->data;
}
diff --git a/source/fitz/stext-search.c b/source/fitz/stext-search.c
index 3904b01d..99584581 100644
--- a/source/fitz/stext-search.c
+++ b/source/fitz/stext-search.c
@@ -278,9 +278,8 @@ fz_copy_selection(fz_context *ctx, fz_stext_page *page, fz_rect rect)
}
}
- fz_write_buffer_byte(ctx, buffer, 0);
-
- fz_buffer_extract(ctx, buffer, &s);
+ fz_terminate_buffer(ctx, buffer);
+ fz_buffer_extract(ctx, buffer, &s); /* take over the data */
fz_drop_buffer(ctx, buffer);
return (char*)s;
}
diff --git a/source/html/epub-doc.c b/source/html/epub-doc.c
index 590004f6..cf7f1ecf 100644
--- a/source/html/epub-doc.c
+++ b/source/html/epub-doc.c
@@ -256,7 +256,6 @@ epub_parse_chapter(fz_context *ctx, epub_document *doc, const char *path)
fz_dirname(base_uri, path, sizeof base_uri);
buf = fz_read_archive_entry(ctx, zip, path);
- fz_write_buffer_byte(ctx, buf, 0);
ch = fz_malloc_struct(ctx, epub_chapter);
ch->path = fz_strdup(ctx, path);
@@ -316,7 +315,7 @@ epub_parse_ncx(fz_context *ctx, epub_document *doc, const char *path)
fz_dirname(base_uri, path, sizeof base_uri);
buf = fz_read_archive_entry(ctx, zip, path);
- fz_write_buffer_byte(ctx, buf, 0);
+ fz_terminate_buffer(ctx, buf);
len = fz_buffer_storage(ctx, buf, &data);
ncx = fz_parse_xml(ctx, data, len, 0);
fz_drop_buffer(ctx, buf);
@@ -359,7 +358,7 @@ epub_parse_header(fz_context *ctx, epub_document *doc)
/* parse META-INF/container.xml to find OPF */
buf = fz_read_archive_entry(ctx, zip, "META-INF/container.xml");
- fz_write_buffer_byte(ctx, buf, 0);
+ fz_terminate_buffer(ctx, buf);
len = fz_buffer_storage(ctx, buf, &data);
container_xml = fz_parse_xml(ctx, data, len, 0);
fz_drop_buffer(ctx, buf);
@@ -376,7 +375,7 @@ epub_parse_header(fz_context *ctx, epub_document *doc)
/* parse OPF to find NCX and spine */
buf = fz_read_archive_entry(ctx, zip, full_path);
- fz_write_buffer_byte(ctx, buf, 0);
+ fz_terminate_buffer(ctx, buf);
len = fz_buffer_storage(ctx, buf, &data);
content_opf = fz_parse_xml(ctx, data, len, 0);
fz_drop_buffer(ctx, buf);
diff --git a/source/html/html-doc.c b/source/html/html-doc.c
index 05a86d83..c19d5049 100644
--- a/source/html/html-doc.c
+++ b/source/html/html-doc.c
@@ -139,12 +139,8 @@ htdoc_open_document_with_stream(fz_context *ctx, fz_stream *file)
doc->set = fz_new_html_font_set(ctx);
buf = fz_read_all(ctx, file, 0);
-
fz_try(ctx)
- {
- fz_write_buffer_byte(ctx, buf, 0);
doc->html = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, fz_user_css(ctx));
- }
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
@@ -175,12 +171,8 @@ htdoc_open_document(fz_context *ctx, const char *filename)
doc->set = fz_new_html_font_set(ctx);
buf = fz_read_file(ctx, filename);
-
fz_try(ctx)
- {
- fz_write_buffer_byte(ctx, buf, 0);
doc->html = fz_parse_html(ctx, doc->set, doc->zip, ".", buf, fz_user_css(ctx));
- }
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
diff --git a/source/html/html-layout.c b/source/html/html-layout.c
index 91297f10..ff2102d1 100644
--- a/source/html/html-layout.c
+++ b/source/html/html-layout.c
@@ -406,10 +406,7 @@ static fz_image *load_html_image(fz_context *ctx, fz_archive *zip, const char *b
buf = fz_read_archive_entry(ctx, zip, path);
#if FZ_ENABLE_SVG
if (strstr(path, ".svg"))
- {
- fz_write_buffer_byte(ctx, buf, 0);
img = fz_new_image_from_svg(ctx, buf);
- }
else
#endif
img = fz_new_image_from_buffer(ctx, buf);
@@ -2552,7 +2549,7 @@ fz_parse_html(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const cha
fz_xml *xml;
fz_html *html;
unsigned char *data;
- size_t len = fz_buffer_storage(ctx, buf, &data);
+ size_t len;
fz_css_match match;
struct genstate g;
@@ -2565,6 +2562,8 @@ fz_parse_html(fz_context *ctx, fz_html_font_set *set, fz_archive *zip, const cha
g.emit_white = 0;
g.last_brk_cls = UCDN_LINEBREAK_CLASS_OP;
+ fz_terminate_buffer(ctx, buf);
+ len = fz_buffer_storage(ctx, buf, &data);
xml = fz_parse_xml(ctx, data, len, 1);
g.css = fz_new_css(ctx);
diff --git a/source/svg/svg-doc.c b/source/svg/svg-doc.c
index d0ff1e4a..06de56f5 100644
--- a/source/svg/svg-doc.c
+++ b/source/svg/svg-doc.c
@@ -91,6 +91,7 @@ svg_open_document_with_buffer(fz_context *ctx, fz_buffer *buf)
size_t len;
unsigned char *data;
+ fz_terminate_buffer(ctx, buf);
len = fz_buffer_storage(ctx, buf, &data);
root = fz_parse_xml(ctx, data, len, 0);
@@ -115,10 +116,7 @@ svg_open_document_with_stream(fz_context *ctx, fz_stream *file)
buf = fz_read_all(ctx, file, 0);
fz_try(ctx)
- {
- fz_write_buffer_byte(ctx, buf, 0);
doc = svg_open_document_with_buffer(ctx, buf);
- }
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
diff --git a/source/xps/xps-zip.c b/source/xps/xps-zip.c
index 53ea15f9..fe1dcce8 100644
--- a/source/xps/xps-zip.c
+++ b/source/xps/xps-zip.c
@@ -92,11 +92,8 @@ xps_read_part(fz_context *ctx, xps_document *doc, char *partname)
}
}
- fz_write_buffer_byte(ctx, buf, 0); /* zero-terminate */
-
- /* take over the data */
- /* size doesn't include the added zero-terminator */
- size = fz_buffer_extract(ctx, buf, &data) - 1;
+ fz_terminate_buffer(ctx, buf); /* zero-terminate */
+ size = fz_buffer_extract(ctx, buf, &data); /* take over the data ('size' excludes the zero terminator) */
fz_drop_buffer(ctx, buf);
return xps_new_part(ctx, doc, partname, data, size);