X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fxml.c;h=b6a010c4035f49189a34eda3f4703c9e3ab111a2;hb=5d52d8c322857646dec623cedc35796dc135e2b9;hp=c4382d430f793462cc4e26120dd560c95decd033;hpb=670a54f5de37bff02c49f37d690b26654cf7fa1d;p=wimlib diff --git a/src/xml.c b/src/xml.c index c4382d43..b6a010c4 100644 --- a/src/xml.c +++ b/src/xml.c @@ -23,18 +23,26 @@ * along with wimlib; if not, see http://www.gnu.org/licenses/. */ -#include "dentry.h" -#include "lookup_table.h" -#include "timestamp.h" -#include "wimlib_internal.h" -#include "xml.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif -#include +#include "wimlib/dentry.h" +#include "wimlib/encoding.h" +#include "wimlib/error.h" +#include "wimlib/file_io.h" +#include "wimlib/lookup_table.h" +#include "wimlib/metadata.h" +#include "wimlib/resource.h" +#include "wimlib/timestamp.h" +#include "wimlib/xml.h" + +#include #include #include #include -#include #include +#include /* Structures used to form an in-memory representation of the XML data (other * than the raw parse tree from libxml). */ @@ -78,11 +86,11 @@ struct image_info { tchar *display_name; tchar *display_description; tchar *flags; - struct wim_lookup_table *lookup_table; /* Temporary field only */ + struct wim_lookup_table *lookup_table; /* temporary field */ }; struct xml_string_spec { - const utf8char *name; + const char *name; size_t offset; }; @@ -126,7 +134,7 @@ get_arch(int arch) return T("x86_64"); /* XXX Are there other arch values? */ default: - return NULL; + return T("unknown"); } } @@ -149,34 +157,36 @@ node_is_text(xmlNode *node) } static inline bool -node_name_is(xmlNode *node, const utf8char *name) +node_name_is(xmlNode *node, const char *name) { /* For now, both upper case and lower case element names are accepted. */ return strcasecmp((const char *)node->name, name) == 0; } -/* Finds the text node that is a child of an element node and returns its - * content converted to a 64-bit unsigned integer. Returns 0 if no text node is - * found. */ static u64 -node_get_u64(const xmlNode *u64_node) +node_get_number(const xmlNode *u64_node, int base) { xmlNode *child; for_node_child(u64_node, child) if (node_is_text(child)) - return strtoull((const char *)child->content, NULL, 10); + return strtoull(child->content, NULL, base); return 0; } +/* Finds the text node that is a child of an element node and returns its + * content converted to a 64-bit unsigned integer. Returns 0 if no text node is + * found. */ +static u64 +node_get_u64(const xmlNode *u64_node) +{ + return node_get_number(u64_node, 10); +} + /* Like node_get_u64(), but expects a number in base 16. */ static u64 node_get_hex_u64(const xmlNode *u64_node) { - xmlNode *child; - for_node_child(u64_node, child) - if (node_is_text(child)) - return strtoull(child->content, NULL, 16); - return 0; + return node_get_number(u64_node, 16); } static int @@ -438,7 +448,7 @@ xml_read_image_info(xmlNode *image_node, struct image_info *image_info) if (!image_info->name) { tchar *empty_name; WARNING("Image with index %d has no name", image_info->index); - empty_name = TMALLOC(1); + empty_name = MALLOC(sizeof(tchar)); if (!empty_name) return WIMLIB_ERR_NOMEM; *empty_name = T('\0'); @@ -529,7 +539,7 @@ print_windows_info(const struct windows_info *windows_info) const struct windows_version *windows_version; tprintf(T("Architecture: %"TS"\n"), - get_arch(windows_info->arch) ?: T("unknown")); + get_arch(windows_info->arch)); if (windows_info->product_name) { tprintf(T("Product Name: %"TS"\n"), @@ -597,7 +607,7 @@ xml_write_string(xmlTextWriter *writer, const char *name, const tchar *tstr) { if (tstr) { - utf8char *utf8_str; + char *utf8_str; int rc = tstr_to_utf8_simple(tstr, &utf8_str); if (rc) return rc; @@ -745,7 +755,7 @@ xml_write_windows_info(xmlTextWriter *writer, /* Writes a time element to the XML document being constructed in memory. */ static int -xml_write_time(xmlTextWriter *writer, const utf8char *element_name, u64 time) +xml_write_time(xmlTextWriter *writer, const char *element_name, u64 time) { int rc; rc = xmlTextWriterStartElement(writer, element_name); @@ -813,7 +823,7 @@ xml_write_image_info(xmlTextWriter *writer, const struct image_info *image_info) if (image_info->windows_info_exists) { rc = xml_write_windows_info(writer, &image_info->windows_info); - if (rc < 0) + if (rc) return rc; } @@ -856,6 +866,8 @@ clone_windows_info(const struct windows_info *old, struct windows_info *new) ret = dup_strings_from_specs(old, new, windows_info_xml_string_specs, ARRAY_LEN(windows_info_xml_string_specs)); + if (ret) + return ret; if (old->languages) { new->languages = CALLOC(old->num_languages, sizeof(new->languages[0])); @@ -982,9 +994,9 @@ xml_delete_image(struct wim_info **wim_info_p, int image) { struct wim_info *wim_info; - DEBUG("Deleting image %d from the XML data.", image); - wim_info = *wim_info_p; + wimlib_assert(image >= 1 && image <= wim_info->num_images); + DEBUG("Deleting image %d from the XML data.", image); destroy_image_info(&wim_info->images[image - 1]); @@ -1029,7 +1041,6 @@ static int calculate_dentry_statistics(struct wim_dentry *dentry, void *arg) { struct image_info *info = arg; - struct wim_lookup_table *lookup_table = info->lookup_table; const struct wim_inode *inode = dentry->d_inode; struct wim_lookup_table_entry *lte; @@ -1086,7 +1097,7 @@ calculate_dentry_statistics(struct wim_dentry *dentry, void *arg) if (inode->i_nlink >= 2 && dentry_is_first_in_inode(dentry)) { for (unsigned i = 0; i < inode->i_num_ads; i++) { if (inode->i_ads_entries[i].stream_name_nbytes) { - lte = inode_stream_lte(inode, i + 1, lookup_table); + lte = inode_stream_lte(inode, i + 1, info->lookup_table); if (lte) { info->hard_link_bytes += inode->i_nlink * wim_resource_size(lte); @@ -1117,9 +1128,9 @@ xml_update_image_info(WIMStruct *w, int image) image_info->dir_count = 0; image_info->total_bytes = 0; image_info->hard_link_bytes = 0; - image_info->lookup_table = w->lookup_table; - for_dentry_in_tree(w->image_metadata[image - 1].root_dentry, + + for_dentry_in_tree(w->image_metadata[image - 1]->root_dentry, calculate_dentry_statistics, image_info); image_info->last_modification_time = get_wim_timestamp(); @@ -1208,7 +1219,7 @@ print_image_info(const struct wim_info *wim_info, int image) wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf)); tprintf(T("Creation Time: %"TS"\n"), buf); - wim_timestamp_to_str(image_info->creation_time, buf, sizeof(buf)); + wim_timestamp_to_str(image_info->last_modification_time, buf, sizeof(buf)); tprintf(T("Last Modification Time: %"TS"\n"), buf); if (image_info->windows_info_exists) print_windows_info(&image_info->windows_info); @@ -1218,14 +1229,14 @@ print_image_info(const struct wim_info *wim_info, int image) } void -libxml_global_init() +libxml_global_init(void) { xmlInitParser(); xmlInitCharEncodingHandlers(); } void -libxml_global_cleanup() +libxml_global_cleanup(void) { xmlCleanupParser(); xmlCleanupCharEncodingHandlers(); @@ -1235,8 +1246,9 @@ libxml_global_cleanup() * Reads the XML data from a WIM file. */ int -read_xml_data(FILE *fp, const struct resource_entry *res_entry, - utf16lechar **xml_data_ret, struct wim_info **info_ret) +read_xml_data(int in_fd, + const struct resource_entry *res_entry, + struct wim_info **info_ret) { utf16lechar *xml_data; xmlDoc *doc; @@ -1264,10 +1276,13 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry, goto out; } - ret = read_uncompressed_resource(fp, res_entry->offset, - res_entry->size, xml_data); - if (ret != 0) + if (full_pread(in_fd, xml_data, + res_entry->size, res_entry->offset) != res_entry->size) + { + ERROR_WITH_ERRNO("Error reading XML data"); + ret = WIMLIB_ERR_READ; goto out_free_xml_data; + } /* Null-terminate just in case */ ((u8*)xml_data)[res_entry->size] = 0; @@ -1295,18 +1310,11 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry, } if (!node_is_element(root) || !node_name_is(root, "WIM")) { - ERROR("Expected for the root XML element (found <%s>)", - root->name); + ERROR("Expected for the root XML element"); 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); @@ -1316,12 +1324,6 @@ out: return ret; } -#define CHECK_RET ({ if (ret < 0) { \ - ERROR("Error writing XML data"); \ - ret = WIMLIB_ERR_WRITE; \ - goto out_free_text_writer; \ - } }) - /* * Writes XML data to a WIM file. * @@ -1330,7 +1332,7 @@ out: * the offset of the XML data. */ int -write_xml_data(const struct wim_info *wim_info, int image, FILE *out, +write_xml_data(const struct wim_info *wim_info, int image, int out_fd, u64 total_bytes, struct resource_entry *out_res_entry) { xmlCharEncodingHandler *encoding_handler; @@ -1344,7 +1346,7 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, (wim_info != NULL && image >= 1 && image <= wim_info->num_images)); - start_offset = ftello(out); + start_offset = filedes_offset(out_fd); if (start_offset == -1) return WIMLIB_ERR_WRITE; @@ -1353,7 +1355,8 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, /* 2 bytes endianness marker for UTF-16LE. This is _required_ for WIM * XML data. */ - if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF)) { + static u8 bom[2] = {0xff, 0xfe}; + if (full_write(out_fd, bom, 2) != 2) { ERROR_WITH_ERRNO("Error writing XML data"); return WIMLIB_ERR_WRITE; } @@ -1379,7 +1382,7 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, goto out; } - out_buffer = xmlOutputBufferCreateFile(out, encoding_handler); + out_buffer = xmlOutputBufferCreateFd(out_fd, encoding_handler); if (!out_buffer) { ERROR("Failed to allocate xmlOutputBuffer"); ret = WIMLIB_ERR_NOMEM; @@ -1396,11 +1399,13 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, DEBUG("Writing element"); ret = xmlTextWriterStartElement(writer, "WIM"); - CHECK_RET; + if (ret < 0) + goto out_write_error; ret = xmlTextWriterWriteFormatElement(writer, "TOTALBYTES", "%"PRIu64, total_bytes); - CHECK_RET; + if (ret < 0) + goto out_write_error; if (wim_info != NULL) { int first, last; @@ -1415,26 +1420,28 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, for (int i = first; i <= last; i++) { ret = xml_write_image_info(writer, &wim_info->images[i - 1]); if (ret) { - CHECK_RET; + if (ret < 0) + goto out_write_error; goto out_free_text_writer; } } } ret = xmlTextWriterEndElement(writer); - CHECK_RET; + if (ret < 0) + goto out_write_error; ret = xmlTextWriterEndDocument(writer); - CHECK_RET; + if (ret < 0) + goto out_write_error; - DEBUG("Ended XML document"); + ret = xmlTextWriterFlush(writer); + if (ret < 0) + goto out_write_error; - /* Call xmlFreeTextWriter() before ftello() because the former will - * flush the file stream. */ - xmlFreeTextWriter(writer); - writer = NULL; + DEBUG("Ended XML document"); - end_offset = ftello(out); + end_offset = filedes_offset(out_fd); if (end_offset == -1) { ret = WIMLIB_ERR_WRITE; } else { @@ -1447,14 +1454,17 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out, out_free_text_writer: /* xmlFreeTextWriter will free the attached xmlOutputBuffer. */ xmlFreeTextWriter(writer); - out_buffer = NULL; + goto out; out_output_buffer_close: - if (out_buffer != NULL) - xmlOutputBufferClose(out_buffer); + xmlOutputBufferClose(out_buffer); out: if (ret == 0) DEBUG("Successfully wrote XML data"); return ret; +out_write_error: + ERROR("Error writing XML data"); + ret = WIMLIB_ERR_WRITE; + goto out_free_text_writer; } /* Returns the name of the specified image. */ @@ -1482,7 +1492,7 @@ wimlib_image_name_in_use(const WIMStruct *w, const tchar *name) if (!name || !*name) return false; for (int i = 1; i <= w->hdr.image_count; i++) - if (tstrcmp(w->wim_info->images[i - 1].name, name) == 0) + if (!tstrcmp(w->wim_info->images[i - 1].name, name)) return true; return false; } @@ -1492,16 +1502,38 @@ wimlib_image_name_in_use(const WIMStruct *w, const tchar *name) 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; + + if (full_pread(w->in_fd, + buf, + w->hdr.xml_res_entry.size, + w->hdr.xml_res_entry.offset) != w->hdr.xml_res_entry.size) + { + ERROR_WITH_ERRNO("Error reading XML data"); + ret = WIMLIB_ERR_READ; + 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. */