X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fxml.c;h=9a0bded26bbfc4f863c9db2ecf820e8a794d5d7f;hb=00614e1689f0314ad221f5b4f864ae0ab4c667a4;hp=89d08e0e4eec1200334772fcd0a3dd888f970feb;hpb=26fdf3f709adf52521e1cf962095987a3e0e2e00;p=wimlib diff --git a/src/xml.c b/src/xml.c index 89d08e0e..9a0bded2 100644 --- a/src/xml.c +++ b/src/xml.c @@ -621,13 +621,6 @@ static int xml_write_windows_info(xmlTextWriter *writer, return rc; } - if (windows_info->system_root) { - rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT", - windows_info->system_root); - if (rc < 0) - return rc; - } - if (windows_info->product_type) { rc = xmlTextWriterWriteElement(writer, "PRODUCTTYPE", windows_info->product_type); @@ -669,6 +662,13 @@ static int xml_write_windows_info(xmlTextWriter *writer, return rc; } + if (windows_info->system_root) { + rc = xmlTextWriterWriteElement(writer, "SYSTEMROOT", + windows_info->system_root); + if (rc < 0) + return rc; + } + return xmlTextWriterEndElement(writer); /* */ } @@ -682,12 +682,12 @@ static int xml_write_time(xmlTextWriter *writer, const char *element_name, return rc; rc = xmlTextWriterWriteFormatElement(writer, "HIGHPART", - "0x%"PRIX32, (u32)(time >> 32)); + "0x%08"PRIX32, (u32)(time >> 32)); if (rc < 0) return rc; rc = xmlTextWriterWriteFormatElement(writer, "LOWPART", - "0x%"PRIX32, (u32)time); + "0x%08"PRIX32, (u32)time); if (rc < 0) return rc; @@ -1065,7 +1065,6 @@ static int calculate_dentry_statistics(struct dentry *dentry, void *arg) void xml_update_image_info(WIMStruct *w, int image) { struct image_info *image_info; - struct dentry *root; char *flags_save; DEBUG("Updating the image info for image %d", image); @@ -1083,7 +1082,7 @@ void xml_update_image_info(WIMStruct *w, int image) for_dentry_in_tree(w->image_metadata[image - 1].root_dentry, calculate_dentry_statistics, image_info); - + image_info->lookup_table = NULL; image_info->flags = flags_save; image_info->last_modification_time = get_wim_timestamp(); @@ -1140,11 +1139,8 @@ out_free_wim_info: * */ void print_image_info(const struct wim_info *wim_info, int image) { - uint i; const struct image_info *image_info; const char *desc; - int start; - int end; time_t time; char *p; @@ -1280,7 +1276,7 @@ out_cleanup_parser: #define CHECK_RET ({ if (ret < 0) { \ ERROR("Error writing XML data"); \ ret = WIMLIB_ERR_WRITE; \ - goto err2; \ + goto out_free_text_writer; \ } }) /* @@ -1291,23 +1287,26 @@ out_cleanup_parser: * the offset of the XML data. */ int write_xml_data(const struct wim_info *wim_info, int image, FILE *out, - u64 total_bytes) + u64 total_bytes, struct resource_entry *out_res_entry) { xmlBuffer *buf; xmlTextWriter *writer; char *utf16_str; int ret; - int num_images; - int i; const xmlChar *content; size_t len; size_t utf16_len; size_t bytes_written; + off_t start_offset, end_offset; - wimlib_assert(image == WIM_ALL_IMAGES || + wimlib_assert(image == WIMLIB_ALL_IMAGES || (wim_info != NULL && image >= 1 && image <= wim_info->num_images)); + start_offset = ftello(out); + if (start_offset == -1) + return WIMLIB_ERR_WRITE; + /* The contents of the element in the XML data, under the * element not the element, is (for non-spit WIMs) the * size of the WIM file excluding the XML data and integrity table, @@ -1324,13 +1323,13 @@ int write_xml_data(const struct wim_info *wim_info, int image, FILE *out, if (!buf) { ERROR("Failed to allocate XML buffer"); ret = WIMLIB_ERR_NOMEM; - goto err0; + goto out; } writer = xmlNewTextWriterMemory(buf, 0); if (!writer) { ERROR("Failed to allocate XML writer"); ret = WIMLIB_ERR_NOMEM; - goto err1; + goto out_buffer_free; } /* XXX */ @@ -1351,18 +1350,15 @@ int write_xml_data(const struct wim_info *wim_info, int image, FILE *out, total_bytes); CHECK_RET; - if (wim_info) - num_images = wim_info->num_images; - else - num_images = 0; - DEBUG("Writing %u elements", num_images); - - for (i = 1; i <= num_images; i++) { - if (image != WIM_ALL_IMAGES && i != image) - continue; - DEBUG("Writing element for image %d", i); - ret = xml_write_image_info(writer, &wim_info->images[i - 1]); - CHECK_RET; + if (wim_info != NULL) { + DEBUG("Writing %d elements", (int)wim_info->num_images); + for (int i = 1; i <= (int)wim_info->num_images; i++) { + if (image != WIMLIB_ALL_IMAGES && i != image) + continue; + DEBUG("Writing element for image %d", i); + ret = xml_write_image_info(writer, &wim_info->images[i - 1]); + CHECK_RET; + } } ret = xmlTextWriterEndElement(writer); @@ -1371,36 +1367,52 @@ int write_xml_data(const struct wim_info *wim_info, int image, FILE *out, ret = xmlTextWriterEndDocument(writer); CHECK_RET; + xmlFreeTextWriter(writer); + writer = NULL; DEBUG("Done composing XML document. Now converting to UTF-16 and " "writing it to the output file."); content = xmlBufferContent(buf); len = xmlBufferLength(buf); + DEBUG("XML UTF-8 length = %zu", len); + utf16_str = utf8_to_utf16(content, len, &utf16_len); if (!utf16_str) { ret = WIMLIB_ERR_NOMEM; - goto err2; + goto out_free_text_writer; } + DEBUG("XML UTF-16 length = %zu", utf16_len); + if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF) || ((bytes_written = fwrite(utf16_str, 1, utf16_len, out)) != utf16_len)) { ERROR_WITH_ERRNO("Error writing XML data"); ret = WIMLIB_ERR_WRITE; - goto err3; + goto out_free_utf16_str; } DEBUG("Cleaning up."); + end_offset = ftello(out); + if (end_offset == -1) { + ret = WIMLIB_ERR_WRITE; + goto out_free_utf16_str; + } + + out_res_entry->offset = start_offset; + out_res_entry->size = end_offset - start_offset; + out_res_entry->original_size = end_offset - start_offset; + out_res_entry->flags = WIM_RESHDR_FLAG_METADATA; ret = 0; -err3: +out_free_utf16_str: FREE(utf16_str); -err2: +out_free_text_writer: xmlFreeTextWriter(writer); -err1: +out_buffer_free: xmlBufferFree(buf); -err0: +out: return ret; } @@ -1431,7 +1443,7 @@ WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name) int i; DEBUG("Checking to see if the image name `%s' is already in use", name); - if (!name || !w->wim_info) + if (!name || !*name || !w->wim_info) return false; for (i = 1; i <= w->wim_info->num_images; i++) if (strcmp(w->wim_info->images[i - 1].name, name) == 0) @@ -1443,6 +1455,9 @@ WIMLIBAPI bool wimlib_image_name_in_use(const WIMStruct *w, const char *name) WIMLIBAPI int wimlib_extract_xml_data(WIMStruct *w, FILE *fp) { DEBUG("Extracting the XML data."); + if (!w->xml_data) + return WIMLIB_ERR_INVALID_PARAM; + if (fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp) != w->hdr.xml_res_entry.size) { ERROR_WITH_ERRNO("Failed to extract XML data");