[libgepub/wip/cosimoc/fixes: 5/8] Don't NULL-terminate document content



commit 943c86d06afad4641d9d0bbb3001be0664d45c4d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Jun 12 16:04:10 2016 -0700

    Don't NULL-terminate document content
    
    We should not try to modify the contents of the archive by inserting
    newlines, as e.g. WebKit will complain that there's extra data at the
    end of the stream.

 libgepub/gepub-archive.c |    9 ++++-----
 libgepub/gepub-doc.c     |   11 ++++++-----
 libgepub/gepub-utils.c   |    2 +-
 3 files changed, 11 insertions(+), 11 deletions(-)
---
diff --git a/libgepub/gepub-archive.c b/libgepub/gepub-archive.c
index d549394..5805b32 100644
--- a/libgepub/gepub-archive.c
+++ b/libgepub/gepub-archive.c
@@ -140,11 +140,10 @@ gepub_archive_read_entry (GepubArchive *archive,
         archive_read_data_skip (archive->archive);
     }
 
-    *bufsize = archive_entry_size (entry) + 1;
-    size = (*bufsize) - 1;
-    *buffer = g_malloc (*bufsize);
+    *bufsize = archive_entry_size (entry);
+    size = *bufsize;
+    *buffer = g_malloc0 (*bufsize);
     archive_read_data (archive->archive, *buffer, size);
-    (*buffer)[size] = '\0';
 
     gepub_archive_close (archive);
     return TRUE;
@@ -164,7 +163,7 @@ gepub_archive_get_root_file (GepubArchive *archive)
     if (!gepub_archive_read_entry (archive, "META-INF/container.xml", &buffer, &bufsize))
         return NULL;
 
-    doc = xmlRecoverDoc (buffer);
+    doc = xmlRecoverMemory (buffer, bufsize);
     root_element = xmlDocGetRootElement (doc);
     root_node = gepub_utils_get_element_by_tag (root_element, "rootfile");
     root_file = xmlGetProp (root_node, "full-path");
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index b38ff5a..1a96fe1 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -36,6 +36,7 @@ struct _GepubDoc {
 
     GepubArchive *archive;
     guchar *content;
+    gsize content_len;
     gchar *content_base;
     gchar *path;
     GHashTable *resources;
@@ -169,7 +170,7 @@ gepub_doc_initable_init (GInitable     *initable,
     file = gepub_archive_get_root_file (doc->archive);
     if (!file)
         return FALSE;
-    if (!gepub_archive_read_entry (doc->archive, file, &(doc->content), &bufsize))
+    if (!gepub_archive_read_entry (doc->archive, file, &doc->content, &doc->content_len))
         return FALSE;
 
     len = strlen (file);
@@ -222,7 +223,7 @@ gepub_doc_fill_resources (GepubDoc *doc)
     gchar *id, *tmpuri, *uri;
     GepubResource *res;
 
-    xdoc = xmlRecoverDoc (doc->content);
+    xdoc = xmlRecoverMemory (doc->content, doc->content_len);
     root_element = xmlDocGetRootElement (xdoc);
     mnode = gepub_utils_get_element_by_tag (root_element, "manifest");
 
@@ -257,7 +258,7 @@ gepub_doc_fill_spine (GepubDoc *doc)
     xmlNode *item = NULL;
     gchar *id;
 
-    xdoc = xmlRecoverDoc (doc->content);
+    xdoc = xmlRecoverMemory (doc->content, doc->content_len);
     root_element = xmlDocGetRootElement (xdoc);
     snode = gepub_utils_get_element_by_tag (root_element, "spine");
 
@@ -306,7 +307,7 @@ gepub_doc_get_metadata (GepubDoc *doc, gchar *mdata)
     gchar *ret;
     xmlChar *text;
 
-    xdoc = xmlRecoverDoc (doc->content);
+    xdoc = xmlRecoverMemory (doc->content, doc->content_len);
     root_element = xmlDocGetRootElement (xdoc);
     mnode = gepub_utils_get_element_by_tag (root_element, "metadata");
     mdata_node = gepub_utils_get_element_by_tag (mnode, mdata);
@@ -604,7 +605,7 @@ gepub_doc_get_cover (GepubDoc *doc)
     gchar *ret;
     xmlChar *text;
 
-    xdoc = xmlRecoverDoc (doc->content);
+    xdoc = xmlRecoverMemory (doc->content, doc->content_len);
     root_element = xmlDocGetRootElement (xdoc);
     mnode = gepub_utils_get_element_by_attr (root_element, "name", "cover");
     text = xmlGetProp(mnode, "content");
diff --git a/libgepub/gepub-utils.c b/libgepub/gepub-utils.c
index 1817f8b..7f3928f 100644
--- a/libgepub/gepub-utils.c
+++ b/libgepub/gepub-utils.c
@@ -208,7 +208,7 @@ gepub_utils_replace_resources (guchar *content, gsize *bufsize, gchar *path)
     xmlNode *root_element = NULL;
     guchar *buffer;
 
-    doc = xmlRecoverDoc (content);
+    doc = xmlRecoverMemory (content, *bufsize);
     root_element = xmlDocGetRootElement (doc);
 
     // replacing css resources


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]