From: Eric Biggers Date: Sat, 7 Dec 2013 01:04:19 +0000 (-0600) Subject: Add wimlib_get_xml_data() API X-Git-Tag: v1.5.3~13 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=2b4f92cc80af257566c00e62fd3f0a7e3607416b Add wimlib_get_xml_data() API --- diff --git a/include/wimlib.h b/include/wimlib.h index ca16d940..7bab4a34 100644 --- a/include/wimlib.h +++ b/include/wimlib.h @@ -2374,6 +2374,8 @@ wimlib_extract_image_from_pipe(int pipe_fd, * Extracts the XML data of a WIM file to a file stream. Every WIM file * includes a string of XML that describes the images contained in the WIM. * + * See wimlib_get_xml_data() to read the XML data into memory instead. + * * @param wim * Pointer to the ::WIMStruct for a WIM file, which does not necessarily * have to be standalone (e.g. it could be part of a split WIM). @@ -2498,6 +2500,36 @@ wimlib_get_image_name(const WIMStruct *wim, int image); extern int wimlib_get_wim_info(WIMStruct *wim, struct wimlib_wim_info *info); +/** + * @ingroup G_wim_information + * + * Read the XML data of a WIM file into an in-memory buffer. Every WIM file + * includes a string of XML that describes the images contained in the WIM. + * + * See wimlib_extract_xml_data() to extract the XML data to a file stream + * instead. + * + * @param wim + * Pointer to the ::WIMStruct for a WIM file, which does not necessarily + * have to be standalone (e.g. it could be part of a split WIM). + * @param buf_ret + * On success, a pointer to an allocated buffer containing the raw UTF16-LE + * XML data is written to this location. + * @param bufsize_ret + * The size of the XML data in bytes is written to this location. + * + * @return 0 on success; nonzero on error. + * @retval ::WIMLIB_ERR_INVALID_PARAM + * @p wim is not a ::WIMStruct that was created by wimlib_open_wim(), or + * @p buf_ret or @p bufsize_ret was @c NULL. + * @retval ::WIMLIB_ERR_NOMEM + * @retval ::WIMLIB_ERR_READ + * @retval ::WIMLIB_ERR_UNEXPECTED_END_OF_FILE + * Failed to read the XML data from the WIM. + */ +extern int +wimlib_get_xml_data(WIMStruct *wim, void **buf_ret, size_t *bufsize_ret); + /** * @ingroup G_general * diff --git a/src/xml.c b/src/xml.c index 5258aaf4..deeedb50 100644 --- a/src/xml.c +++ b/src/xml.c @@ -1564,30 +1564,34 @@ 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; + if (wim->filename == NULL) + 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); + *bufsize_ret = wim->hdr.xml_res_entry.original_size; + return res_entry_to_data(&wim->hdr.xml_res_entry, 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; }