Save original xml_data only when needed
authorEric Biggers <ebiggers3@gmail.com>
Tue, 9 Apr 2013 16:14:46 +0000 (11:14 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Tue, 9 Apr 2013 16:18:31 +0000 (11:18 -0500)
src/wim.c
src/wimlib_internal.h
src/xml.c
src/xml.h

index a5044c6..9bc0255 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -470,8 +470,7 @@ begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
        if (ret)
                return ret;
 
-       ret = read_xml_data(w->fp, &w->hdr.xml_res_entry,
-                           &w->xml_data, &w->wim_info);
+       ret = read_xml_data(w->fp, &w->hdr.xml_res_entry, &w->wim_info);
        if (ret)
                return ret;
 
@@ -664,7 +663,6 @@ wimlib_free(WIMStruct *w)
        free_lookup_table(w->lookup_table);
 
        FREE(w->filename);
-       FREE(w->xml_data);
        free_wim_info(w->wim_info);
        if (w->image_metadata) {
                for (unsigned i = 0; i < w->hdr.image_count; i++)
index 6396c00..8f4d295 100644 (file)
@@ -295,9 +295,6 @@ struct WIMStruct {
        /* The lookup table for the WIM file. */
        struct wim_lookup_table *lookup_table;
 
-       /* Pointer to the XML data read from the WIM file (UTF16LE-encoded). */
-       utf16lechar *xml_data;
-
        /* Information retrieved from the XML data, arranged in an orderly
         * manner. */
        struct wim_info *wim_info;
index ff37757..f189183 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -1237,7 +1237,7 @@ libxml_global_cleanup()
  */
 int
 read_xml_data(FILE *fp, const struct resource_entry *res_entry,
-             utf16lechar **xml_data_ret, struct wim_info **info_ret)
+             struct wim_info **info_ret)
 {
        utf16lechar *xml_data;
        xmlDoc *doc;
@@ -1267,7 +1267,7 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry,
 
        ret = read_uncompressed_resource(fp, res_entry->offset,
                                         res_entry->size, xml_data);
-       if (ret != 0)
+       if (ret)
                goto out_free_xml_data;
 
        /* Null-terminate just in case */
@@ -1300,13 +1300,7 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry,
                ret = WIMLIB_ERR_XML;
                goto out_free_doc;
        }
-
        ret = xml_read_wim_info(root, info_ret);
-       if (ret != 0)
-               goto out_free_doc;
-
-       *xml_data_ret = xml_data;
-       xml_data = NULL;
 out_free_doc:
        DEBUG("Freeing XML tree.");
        xmlFreeDoc(doc);
@@ -1493,15 +1487,33 @@ WIMLIBAPI int
 wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
 {
        size_t bytes_written;
+       size_t size;
+       void *buf;
+       int ret;
 
-       if (!w->xml_data)
-               return WIMLIB_ERR_INVALID_PARAM;
-       bytes_written = fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp);
-       if (bytes_written != w->hdr.xml_res_entry.size) {
+       size = w->hdr.xml_res_entry.size;
+       if (sizeof(size_t) < sizeof(u64))
+               if (size != w->hdr.xml_res_entry.size)
+                       return WIMLIB_ERR_INVALID_PARAM;
+
+       buf = MALLOC(size);
+       if (!buf)
+               return WIMLIB_ERR_NOMEM;
+
+       ret = read_uncompressed_resource(w->fp, w->hdr.xml_res_entry.offset,
+                                        size, buf);
+       if (ret)
+               goto out_free_buf;
+
+       if (fwrite(buf, 1, size, fp) != size) {
                ERROR_WITH_ERRNO("Failed to extract XML data");
-               return WIMLIB_ERR_WRITE;
+               ret = WIMLIB_ERR_WRITE;
+       } else {
+               ret = 0;
        }
-       return 0;
+out_free_buf:
+       FREE(buf);
+       return ret;
 }
 
 /* Sets the name of an image in the WIM. */
index 070dada..80965fa 100644 (file)
--- a/src/xml.h
+++ b/src/xml.h
@@ -39,7 +39,6 @@ print_image_info(const struct wim_info *wim_info, int image);
 
 extern int
 read_xml_data(FILE *fp, const struct resource_entry *res,
-             utf16lechar **xml_data_ret,
              struct wim_info **info_ret);
 
 extern int