]> wimlib.net Git - wimlib/blobdiff - src/xml.c
Cleanup
[wimlib] / src / xml.c
index 8d2b9106f3efb9d1ef3f971056ea4c44729a5c33..8179ebce7ec0f9998c2333c3136475b9cfe399a9 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -36,6 +36,7 @@
 #include "wimlib/resource.h"
 #include "wimlib/timestamp.h"
 #include "wimlib/xml.h"
+#include "wimlib/write.h"
 
 #include <libxml/encoding.h>
 #include <libxml/parser.h>
@@ -165,19 +166,35 @@ wim_info_get_num_images(const struct wim_info *info)
                return 0;
 }
 
+/* Architecture constants are from w64 mingw winnt.h  */
+#define PROCESSOR_ARCHITECTURE_INTEL 0
+#define PROCESSOR_ARCHITECTURE_MIPS 1
+#define PROCESSOR_ARCHITECTURE_ALPHA 2
+#define PROCESSOR_ARCHITECTURE_PPC 3
+#define PROCESSOR_ARCHITECTURE_SHX 4
+#define PROCESSOR_ARCHITECTURE_ARM 5
+#define PROCESSOR_ARCHITECTURE_IA64 6
+#define PROCESSOR_ARCHITECTURE_ALPHA64 7
+#define PROCESSOR_ARCHITECTURE_MSIL 8
+#define PROCESSOR_ARCHITECTURE_AMD64 9
+#define PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 10
+
 /* Returns a statically allocated string that is a string representation of the
  * architecture number. */
 static const tchar *
 get_arch(int arch)
 {
        switch (arch) {
-       case 0:
+       case PROCESSOR_ARCHITECTURE_INTEL:
                return T("x86");
-       case 6:
+       case PROCESSOR_ARCHITECTURE_MIPS:
+               return T("MIPS");
+       case PROCESSOR_ARCHITECTURE_ARM:
+               return T("ARM");
+       case PROCESSOR_ARCHITECTURE_IA64:
                return T("ia64");
-       case 9:
+       case PROCESSOR_ARCHITECTURE_AMD64:
                return T("x86_64");
-       /* XXX Are there other arch values? */
        default:
                return T("unknown");
        }
@@ -673,7 +690,7 @@ xml_write_strings_from_specs(xmlTextWriter *writer,
        for (size_t i = 0; i < num_specs; i++) {
                int rc = xml_write_string(writer, specs[i].name,
                                      *(const tchar * const *)
-                                       (struct_with_strings + specs[i].offset));
+                                       (struct_with_strings + specs[i].offset));
                if (rc)
                        return rc;
        }
@@ -736,7 +753,11 @@ xml_write_windows_version(xmlTextWriter *writer,
        if (rc < 0)
                return rc;
 
-       return xmlTextWriterEndElement(writer); /* </VERSION> */
+       rc = xmlTextWriterEndElement(writer); /* </VERSION> */
+       if (rc < 0)
+               return rc;
+
+       return 0;
 }
 
 /* Writes the information contained in a `struct windows_info' to the XML
@@ -786,16 +807,19 @@ xml_write_windows_info(xmlTextWriter *writer,
 
        if (windows_info->windows_version_exists) {
                rc = xml_write_windows_version(writer, &windows_info->windows_version);
-               if (rc < 0)
+               if (rc)
                        return rc;
        }
 
-       rc = xml_write_string(writer, "SYSTEMROOT",
-                             windows_info->system_root);
+       rc = xml_write_string(writer, "SYSTEMROOT", windows_info->system_root);
        if (rc)
                return rc;
 
-       return xmlTextWriterEndElement(writer); /* </WINDOWS> */
+       rc = xmlTextWriterEndElement(writer); /* </WINDOWS> */
+       if (rc < 0)
+               return rc;
+
+       return 0;
 }
 
 /* Writes a time element to the XML document being constructed in memory. */
@@ -881,6 +905,7 @@ xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info)
        rc = xmlTextWriterEndElement(writer); /* </IMAGE> */
        if (rc < 0)
                return rc;
+
        return 0;
 }
 
@@ -1129,9 +1154,9 @@ calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
         */
        lte = inode_unnamed_lte(inode, info->lookup_table);
        if (lte) {
-               info->total_bytes += wim_resource_size(lte);
+               info->total_bytes += lte->size;
                if (!dentry_is_first_in_inode(dentry))
-                       info->hard_link_bytes += wim_resource_size(lte);
+                       info->hard_link_bytes += lte->size;
        }
 
        if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) {
@@ -1140,7 +1165,7 @@ calculate_dentry_statistics(struct wim_dentry *dentry, void *arg)
                                lte = inode_stream_lte(inode, i + 1, info->lookup_table);
                                if (lte) {
                                        info->hard_link_bytes += inode->i_nlink *
-                                                                wim_resource_size(lte);
+                                                                lte->size;
                                }
                        }
                }
@@ -1287,23 +1312,18 @@ int
 read_wim_xml_data(WIMStruct *wim)
 {
        void *buf;
+       size_t bufsize;
        u8 *xml_data;
        xmlDoc *doc;
        xmlNode *root;
        int ret;
-       const struct resource_entry *res_entry;
 
-       res_entry = &wim->hdr.xml_res_entry;
-
-       DEBUG("Reading XML data: %"PRIu64" bytes at offset %"PRIu64"",
-             (u64)res_entry->size, res_entry->offset);
-
-       ret = res_entry_to_data(res_entry, wim, &buf);
+       ret = wimlib_get_xml_data(wim, &buf, &bufsize);
        if (ret)
                goto out;
        xml_data = buf;
 
-       doc = xmlReadMemory((const char *)xml_data, res_entry->original_size,
+       doc = xmlReadMemory((const char *)xml_data, bufsize,
                            NULL, "UTF-16LE", 0);
        if (!doc) {
                ERROR("Failed to parse XML data");
@@ -1464,6 +1484,7 @@ out_output_buffer_close:
 out_buffer_free:
        xmlBufferFree(buf);
 out:
+       DEBUG("ret=%d", ret);
        return ret;
 
 out_write_error:
@@ -1475,7 +1496,7 @@ out_write_error:
 /* Writes the XML data to a WIM file.  */
 int
 write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
-                  struct resource_entry *out_res_entry,
+                  struct wim_reshdr *out_reshdr,
                   int write_resource_flags)
 {
        int ret;
@@ -1497,10 +1518,13 @@ write_wim_xml_data(WIMStruct *wim, int image, u64 total_bytes,
                                             WIM_RESHDR_FLAG_METADATA,
                                             &wim->out_fd,
                                             WIMLIB_COMPRESSION_TYPE_NONE,
-                                            out_res_entry,
+                                            0,
+                                            out_reshdr,
                                             NULL,
-                                            write_resource_flags);
+                                            write_resource_flags,
+                                            &wim->lzx_context);
        FREE(xml_data);
+       DEBUG("ret=%d", ret);
        return ret;
 }
 
@@ -1537,30 +1561,39 @@ wimlib_image_name_in_use(const WIMStruct *wim, const tchar *name)
 
 /* API function documented in wimlib.h  */
 WIMLIBAPI int
-wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
+wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret)
 {
-       size_t size;
-       void *buf;
-       int ret;
+       const struct wim_reshdr *xml_reshdr;
+
+       if (wim->filename == NULL && filedes_is_seekable(&wim->in_fd))
+               return WIMLIB_ERR_INVALID_PARAM;
 
-       if (!wim->filename)
+       if (buf_ret == NULL || bufsize_ret == NULL)
                return WIMLIB_ERR_INVALID_PARAM;
 
-       ret = res_entry_to_data(&wim->hdr.xml_res_entry, wim, &buf);
+       xml_reshdr = &wim->hdr.xml_data_reshdr;
+
+       DEBUG("Reading XML data.");
+       *bufsize_ret = xml_reshdr->uncompressed_size;
+       return wim_reshdr_to_data(xml_reshdr, wim, buf_ret);
+}
+
+WIMLIBAPI int
+wimlib_extract_xml_data(WIMStruct *wim, FILE *fp)
+{
+       int ret;
+       void *buf;
+       size_t bufsize;
+
+       ret = wimlib_get_xml_data(wim, &buf, &bufsize);
        if (ret)
-               goto out;
+               return ret;
 
-       size = wim->hdr.xml_res_entry.original_size;
-       if (fwrite(buf, 1, size, fp) != size) {
+       if (fwrite(buf, 1, bufsize, fp) != bufsize) {
                ERROR_WITH_ERRNO("Failed to extract XML data");
                ret = WIMLIB_ERR_WRITE;
-               goto out_free_buf;
        }
-
-       ret = 0;
-out_free_buf:
        FREE(buf);
-out:
        return ret;
 }