]> wimlib.net Git - wimlib/blob - src/wim.c
Refactor headers
[wimlib] / src / wim.c
1 /*
2  * wim.c - Stuff that doesn't fit into any other file
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib/error.h"
31 #include "wimlib/dentry.h"
32 #include "wimlib/encoding.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/integrity.h"
35 #include "wimlib/lookup_table.h"
36 #include "wimlib/metadata.h"
37 #ifdef WITH_NTFS_3G
38 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
39 #endif
40 #include "wimlib/security.h"
41 #include "wimlib/wim.h"
42 #include "wimlib/xml.h"
43
44 #ifdef __WIN32__
45 #  include "wimlib/win32.h" /* for realpath() replacement */
46 #endif
47
48 #include <errno.h>
49 #include <fcntl.h>
50 #ifndef __WIN32__
51 #  include <langinfo.h>
52 #endif
53 #include <limits.h>
54 #include <stdarg.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57
58 static int
59 image_print_metadata(WIMStruct *w)
60 {
61         DEBUG("Printing metadata for image %d", w->current_image);
62         print_security_data(wim_security_data(w));
63         return for_dentry_in_tree(wim_root_dentry(w), print_dentry,
64                                   w->lookup_table);
65 }
66
67
68 static int
69 image_print_files(WIMStruct *w)
70 {
71         return for_dentry_in_tree(wim_root_dentry(w), print_dentry_full_path,
72                                   NULL);
73 }
74
75 static WIMStruct *
76 new_wim_struct(void)
77 {
78         WIMStruct *w = CALLOC(1, sizeof(WIMStruct));
79         if (w) {
80                 w->in_fd = -1;
81                 w->out_fd = -1;
82         }
83         return w;
84 }
85
86 /*
87  * Calls a function on images in the WIM.  If @image is WIMLIB_ALL_IMAGES, @visitor
88  * is called on the WIM once for each image, with each image selected as the
89  * current image in turn.  If @image is a certain image, @visitor is called on
90  * the WIM only once, with that image selected.
91  */
92 int
93 for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *))
94 {
95         int ret;
96         int start;
97         int end;
98         int i;
99
100         if (image == WIMLIB_ALL_IMAGES) {
101                 start = 1;
102                 end = w->hdr.image_count;
103         } else if (image >= 1 && image <= w->hdr.image_count) {
104                 start = image;
105                 end = image;
106         } else {
107                 return WIMLIB_ERR_INVALID_IMAGE;
108         }
109         for (i = start; i <= end; i++) {
110                 ret = select_wim_image(w, i);
111                 if (ret != 0)
112                         return ret;
113                 ret = visitor(w);
114                 if (ret != 0)
115                         return ret;
116         }
117         return 0;
118 }
119
120 /* Returns the compression type given in the flags of a WIM header. */
121 static int
122 wim_hdr_flags_compression_type(int wim_hdr_flags)
123 {
124         if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESSION) {
125                 if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_LZX)
126                         return WIMLIB_COMPRESSION_TYPE_LZX;
127                 else if (wim_hdr_flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
128                         return WIMLIB_COMPRESSION_TYPE_XPRESS;
129                 else
130                         return WIMLIB_COMPRESSION_TYPE_INVALID;
131         } else {
132                 return WIMLIB_COMPRESSION_TYPE_NONE;
133         }
134 }
135
136 /*
137  * Creates a WIMStruct for a new WIM file.
138  */
139 WIMLIBAPI int
140 wimlib_create_new_wim(int ctype, WIMStruct **w_ret)
141 {
142         WIMStruct *w;
143         struct wim_lookup_table *table;
144         int ret;
145
146         DEBUG("Creating new WIM with %"TS" compression.",
147               wimlib_get_compression_type_string(ctype));
148
149         /* Allocate the WIMStruct. */
150         w = new_wim_struct();
151         if (!w)
152                 return WIMLIB_ERR_NOMEM;
153
154         ret = init_header(&w->hdr, ctype);
155         if (ret != 0)
156                 goto out_free;
157
158         table = new_lookup_table(9001);
159         if (!table) {
160                 ret = WIMLIB_ERR_NOMEM;
161                 goto out_free;
162         }
163         w->lookup_table = table;
164         *w_ret = w;
165         return 0;
166 out_free:
167         FREE(w);
168         return ret;
169 }
170
171 WIMLIBAPI int
172 wimlib_get_num_images(const WIMStruct *w)
173 {
174         return w->hdr.image_count;
175 }
176
177 int
178 select_wim_image(WIMStruct *w, int image)
179 {
180         struct wim_image_metadata *imd;
181         int ret;
182
183         DEBUG("Selecting image %d", image);
184
185         if (image == WIMLIB_NO_IMAGE) {
186                 ERROR("Invalid image: %d", WIMLIB_NO_IMAGE);
187                 return WIMLIB_ERR_INVALID_IMAGE;
188         }
189
190         if (image == w->current_image)
191                 return 0;
192
193         if (image < 1 || image > w->hdr.image_count) {
194                 ERROR("Cannot select image %d: There are only %u images",
195                       image, w->hdr.image_count);
196                 return WIMLIB_ERR_INVALID_IMAGE;
197         }
198
199         /* If a valid image is currently selected, it can be freed if it is not
200          * modified.  */
201         if (w->current_image != WIMLIB_NO_IMAGE) {
202                 imd = wim_get_current_image_metadata(w);
203                 if (!imd->modified) {
204                         wimlib_assert(list_empty(&imd->unhashed_streams));
205                         DEBUG("Freeing image %u", w->current_image);
206                         destroy_image_metadata(imd, NULL, false);
207                 }
208         }
209         w->current_image = image;
210         imd = wim_get_current_image_metadata(w);
211         if (imd->root_dentry || imd->modified) {
212                 ret = 0;
213         } else {
214                 #ifdef ENABLE_DEBUG
215                 DEBUG("Reading metadata resource specified by the following "
216                       "lookup table entry:");
217                 print_lookup_table_entry(imd->metadata_lte, stderr);
218                 #endif
219                 ret = read_metadata_resource(w, imd);
220                 if (ret)
221                         w->current_image = WIMLIB_NO_IMAGE;
222         }
223         return ret;
224 }
225
226
227 /* Returns the compression type of the WIM file. */
228 WIMLIBAPI int
229 wimlib_get_compression_type(const WIMStruct *w)
230 {
231         return wim_hdr_flags_compression_type(w->hdr.flags);
232 }
233
234 WIMLIBAPI const tchar *
235 wimlib_get_compression_type_string(int ctype)
236 {
237         switch (ctype) {
238                 case WIMLIB_COMPRESSION_TYPE_NONE:
239                         return T("None");
240                 case WIMLIB_COMPRESSION_TYPE_LZX:
241                         return T("LZX");
242                 case WIMLIB_COMPRESSION_TYPE_XPRESS:
243                         return T("XPRESS");
244                 default:
245                         return T("Invalid");
246         }
247 }
248
249 /*
250  * Returns the number of an image in the WIM file, given a string that is either
251  * the number of the image, or the name of the image.  The images are numbered
252  * starting at 1.
253  */
254 WIMLIBAPI int
255 wimlib_resolve_image(WIMStruct *w, const tchar *image_name_or_num)
256 {
257         tchar *p;
258         long image;
259         int i;
260
261         if (!image_name_or_num || !*image_name_or_num)
262                 return WIMLIB_NO_IMAGE;
263
264         if (!tstrcasecmp(image_name_or_num, T("all"))
265             || !tstrcasecmp(image_name_or_num, T("*")))
266                 return WIMLIB_ALL_IMAGES;
267         image = tstrtol(image_name_or_num, &p, 10);
268         if (p != image_name_or_num && *p == T('\0') && image > 0) {
269                 if (image > w->hdr.image_count)
270                         return WIMLIB_NO_IMAGE;
271                 return image;
272         } else {
273                 for (i = 1; i <= w->hdr.image_count; i++) {
274                         if (!tstrcmp(image_name_or_num,
275                                      wimlib_get_image_name(w, i)))
276                                 return i;
277                 }
278                 return WIMLIB_NO_IMAGE;
279         }
280 }
281
282 /* Prints some basic information about a WIM file. */
283 WIMLIBAPI void
284 wimlib_print_wim_information(const WIMStruct *w)
285 {
286         const struct wim_header *hdr;
287
288         hdr = &w->hdr;
289         tputs(T("WIM Information:"));
290         tputs(T("----------------"));
291         tprintf(T("Path:           %"TS"\n"), w->filename);
292         tfputs(T("GUID:           0x"), stdout);
293         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
294         tputchar(T('\n'));
295         tprintf(T("Image Count:    %d\n"), hdr->image_count);
296         tprintf(T("Compression:    %"TS"\n"),
297                 wimlib_get_compression_type_string(wimlib_get_compression_type(w)));
298         tprintf(T("Part Number:    %d/%d\n"), hdr->part_number, hdr->total_parts);
299         tprintf(T("Boot Index:     %d\n"), hdr->boot_idx);
300         tprintf(T("Size:           %"PRIu64" bytes\n"),
301                 wim_info_get_total_bytes(w->wim_info));
302         tprintf(T("Integrity Info: %"TS"\n"),
303                 (w->hdr.integrity.offset != 0) ? T("yes") : T("no"));
304         tprintf(T("Relative path junction: %"TS"\n"),
305                 (hdr->flags & WIM_HDR_FLAG_RP_FIX) ? T("yes") : T("no"));
306         tputchar(T('\n'));
307 }
308
309 WIMLIBAPI bool
310 wimlib_has_integrity_table(const WIMStruct *w)
311 {
312         return w->hdr.integrity.size != 0;
313 }
314
315 WIMLIBAPI void
316 wimlib_print_available_images(const WIMStruct *w, int image)
317 {
318         int first;
319         int last;
320         int i;
321         int n;
322         if (image == WIMLIB_ALL_IMAGES) {
323                 n = tprintf(T("Available Images:\n"));
324                 first = 1;
325                 last = w->hdr.image_count;
326         } else if (image >= 1 && image <= w->hdr.image_count) {
327                 n = tprintf(T("Information for Image %d\n"), image);
328                 first = image;
329                 last = image;
330         } else {
331                 tprintf(T("wimlib_print_available_images(): Invalid image %d"),
332                         image);
333                 return;
334         }
335         for (i = 0; i < n - 1; i++)
336                 tputchar(T('-'));
337         tputchar(T('\n'));
338         for (i = first; i <= last; i++)
339                 print_image_info(w->wim_info, i);
340 }
341
342
343 /* Prints the metadata for the specified image, which may be WIMLIB_ALL_IMAGES, but
344  * not WIMLIB_NO_IMAGE. */
345 WIMLIBAPI int
346 wimlib_print_metadata(WIMStruct *w, int image)
347 {
348         if (w->hdr.part_number != 1) {
349                 ERROR("Cannot show the metadata from part %hu of a %hu-part split WIM!",
350                        w->hdr.part_number, w->hdr.total_parts);
351                 ERROR("Select the first part of the split WIM to see the metadata.");
352                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
353         }
354         return for_image(w, image, image_print_metadata);
355 }
356
357 WIMLIBAPI int
358 wimlib_print_files(WIMStruct *w, int image)
359 {
360         if (w->hdr.part_number != 1) {
361                 ERROR("Cannot list the files from part %hu of a %hu-part split WIM!",
362                        w->hdr.part_number, w->hdr.total_parts);
363                 ERROR("Select the first part of the split WIM if you'd like to list the files.");
364                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
365         }
366         return for_image(w, image, image_print_files);
367 }
368
369 /* Sets the index of the bootable image. */
370 WIMLIBAPI int
371 wimlib_set_boot_idx(WIMStruct *w, int boot_idx)
372 {
373         if (w->hdr.total_parts != 1) {
374                 ERROR("Cannot modify the boot index of a split WIM!");
375                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
376         }
377         if (boot_idx < 0 || boot_idx > w->hdr.image_count)
378                 return WIMLIB_ERR_INVALID_IMAGE;
379         w->hdr.boot_idx = boot_idx;
380         return 0;
381 }
382
383 WIMLIBAPI int
384 wimlib_get_part_number(const WIMStruct *w, int *total_parts_ret)
385 {
386         if (total_parts_ret)
387                 *total_parts_ret = w->hdr.total_parts;
388         return w->hdr.part_number;
389 }
390
391
392 WIMLIBAPI int
393 wimlib_get_boot_idx(const WIMStruct *w)
394 {
395         return w->hdr.boot_idx;
396 }
397
398 static int
399 do_open_wim(const tchar *filename, int *fd_ret)
400 {
401         int fd;
402
403         fd = topen(filename, O_RDONLY | O_BINARY);
404         if (fd == -1) {
405                 ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
406                 return WIMLIB_ERR_OPEN;
407         }
408         *fd_ret = fd;
409         return 0;
410 }
411
412 int
413 reopen_wim(WIMStruct *w)
414 {
415         wimlib_assert(w->in_fd == -1);
416         return do_open_wim(w->filename, &w->in_fd);
417 }
418
419 int
420 close_wim(WIMStruct *w)
421 {
422         if (w->in_fd != -1) {
423                 close(w->in_fd);
424                 w->in_fd = -1;
425         }
426         return 0;
427 }
428
429 /*
430  * Begins the reading of a WIM file; opens the file and reads its header and
431  * lookup table, and optionally checks the integrity.
432  */
433 static int
434 begin_read(WIMStruct *w, const tchar *in_wim_path, int open_flags,
435            wimlib_progress_func_t progress_func)
436 {
437         int ret;
438         int xml_num_images;
439
440         DEBUG("Reading the WIM file `%"TS"'", in_wim_path);
441
442         ret = do_open_wim(in_wim_path, &w->in_fd);
443         if (ret)
444                 return ret;
445
446         /* The absolute path to the WIM is requested so that wimlib_overwrite()
447          * still works even if the process changes its working directory.  This
448          * actually happens if a WIM is mounted read-write, since the FUSE
449          * thread changes directory to "/", and it needs to be able to find the
450          * WIM file again.
451          *
452          * This will break if the full path to the WIM changes in the
453          * intervening time...
454          *
455          * Warning: in Windows native builds, realpath() calls the replacement
456          * function in win32.c.
457          */
458         w->filename = realpath(in_wim_path, NULL);
459         if (!w->filename) {
460                 ERROR_WITH_ERRNO("Failed to resolve WIM filename");
461                 if (errno == ENOMEM)
462                         return WIMLIB_ERR_NOMEM;
463                 else
464                         return WIMLIB_ERR_OPEN;
465         }
466
467         ret = read_header(w->filename, w->in_fd, &w->hdr, open_flags);
468         if (ret)
469                 return ret;
470
471         DEBUG("According to header, WIM contains %u images", w->hdr.image_count);
472
473         /* If the boot index is invalid, print a warning and set it to 0 */
474         if (w->hdr.boot_idx > w->hdr.image_count) {
475                 WARNING("In `%"TS"', image %u is marked as bootable, "
476                         "but there are only %u images in the WIM",
477                         in_wim_path, w->hdr.boot_idx, w->hdr.image_count);
478                 w->hdr.boot_idx = 0;
479         }
480
481         if (wimlib_get_compression_type(w) == WIMLIB_COMPRESSION_TYPE_INVALID) {
482                 ERROR("Invalid compression type (WIM header flags = 0x%x)",
483                       w->hdr.flags);
484                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
485         }
486
487         if (open_flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY) {
488                 ret = check_wim_integrity(w, progress_func);
489                 if (ret == WIM_INTEGRITY_NONEXISTENT) {
490                         WARNING("No integrity information for `%"TS"'; skipping "
491                                 "integrity check.", in_wim_path);
492                 } else if (ret == WIM_INTEGRITY_NOT_OK) {
493                         ERROR("WIM is not intact! (Failed integrity check)");
494                         return WIMLIB_ERR_INTEGRITY;
495                 } else if (ret != WIM_INTEGRITY_OK) {
496                         return ret;
497                 }
498         }
499
500         if (w->hdr.image_count != 0 && w->hdr.part_number == 1) {
501                 w->image_metadata = new_image_metadata_array(w->hdr.image_count);
502                 if (!w->image_metadata)
503                         return WIMLIB_ERR_NOMEM;
504         }
505
506         ret = read_lookup_table(w);
507         if (ret)
508                 return ret;
509
510         ret = read_xml_data(w->in_fd, &w->hdr.xml_res_entry, &w->wim_info);
511         if (ret)
512                 return ret;
513
514         xml_num_images = wim_info_get_num_images(w->wim_info);
515         if (xml_num_images != w->hdr.image_count) {
516                 ERROR("In the file `%"TS"', there are %u <IMAGE> elements "
517                       "in the XML data,", in_wim_path, xml_num_images);
518                 ERROR("but %u images in the WIM!  There must be exactly one "
519                       "<IMAGE> element per image.", w->hdr.image_count);
520                 return WIMLIB_ERR_IMAGE_COUNT;
521         }
522
523         DEBUG("Done beginning read of WIM file `%"TS"'.", in_wim_path);
524         return 0;
525 }
526
527 /*
528  * Opens a WIM file and creates a WIMStruct for it.
529  */
530 WIMLIBAPI int
531 wimlib_open_wim(const tchar *wim_file, int open_flags,
532                 WIMStruct **w_ret,
533                 wimlib_progress_func_t progress_func)
534 {
535         WIMStruct *w;
536         int ret;
537
538         if (!wim_file || !w_ret)
539                 return WIMLIB_ERR_INVALID_PARAM;
540
541         w = new_wim_struct();
542         if (!w)
543                 return WIMLIB_ERR_NOMEM;
544
545         ret = begin_read(w, wim_file, open_flags, progress_func);
546         if (ret == 0)
547                 *w_ret = w;
548         else
549                 wimlib_free(w);
550         return ret;
551 }
552
553 void
554 destroy_image_metadata(struct wim_image_metadata *imd,
555                        struct wim_lookup_table *table,
556                        bool free_metadata_lte)
557 {
558         free_dentry_tree(imd->root_dentry, table);
559         imd->root_dentry = NULL;
560         free_security_data(imd->security_data);
561         imd->security_data = NULL;
562
563         if (free_metadata_lte) {
564                 free_lookup_table_entry(imd->metadata_lte);
565                 imd->metadata_lte = NULL;
566         }
567         if (!table) {
568                 struct wim_lookup_table_entry *lte, *tmp;
569                 list_for_each_entry_safe(lte, tmp, &imd->unhashed_streams, unhashed_list)
570                         free_lookup_table_entry(lte);
571         }
572         INIT_LIST_HEAD(&imd->unhashed_streams);
573         INIT_LIST_HEAD(&imd->inode_list);
574 #ifdef WITH_NTFS_3G
575         if (imd->ntfs_vol) {
576                 do_ntfs_umount(imd->ntfs_vol);
577                 imd->ntfs_vol = NULL;
578         }
579 #endif
580 }
581
582 void
583 put_image_metadata(struct wim_image_metadata *imd,
584                    struct wim_lookup_table *table)
585 {
586         if (imd && --imd->refcnt == 0) {
587                 destroy_image_metadata(imd, table, true);
588                 FREE(imd);
589         }
590 }
591
592 /* Appends the specified image metadata structure to the array of image metadata
593  * for a WIM, and increments the image count. */
594 int
595 append_image_metadata(WIMStruct *w, struct wim_image_metadata *imd)
596 {
597         struct wim_image_metadata **imd_array;
598
599         DEBUG("Reallocating image metadata array for image_count = %u",
600               w->hdr.image_count + 1);
601         imd_array = REALLOC(w->image_metadata,
602                             sizeof(w->image_metadata[0]) * (w->hdr.image_count + 1));
603
604         if (!imd_array)
605                 return WIMLIB_ERR_NOMEM;
606         w->image_metadata = imd_array;
607         imd_array[w->hdr.image_count++] = imd;
608         return 0;
609 }
610
611
612 struct wim_image_metadata *
613 new_image_metadata(void)
614 {
615         struct wim_image_metadata *imd;
616
617         imd = CALLOC(1, sizeof(*imd));
618         if (imd) {
619                 imd->refcnt = 1;
620                 INIT_LIST_HEAD(&imd->inode_list);
621                 INIT_LIST_HEAD(&imd->unhashed_streams);
622                 DEBUG("Created new image metadata (refcnt=1)");
623         } else {
624                 ERROR_WITH_ERRNO("Failed to allocate new image metadata structure");
625         }
626         return imd;
627 }
628
629 struct wim_image_metadata **
630 new_image_metadata_array(unsigned num_images)
631 {
632         struct wim_image_metadata **imd_array;
633
634         DEBUG("Creating new image metadata array for %u images",
635               num_images);
636
637         imd_array = CALLOC(num_images, sizeof(imd_array[0]));
638
639         if (!imd_array) {
640                 ERROR("Failed to allocate memory for %u image metadata structures",
641                       num_images);
642                 return NULL;
643         }
644         for (unsigned i = 0; i < num_images; i++) {
645                 imd_array[i] = new_image_metadata();
646                 if (!imd_array[i]) {
647                         for (unsigned j = 0; j < i; j++)
648                                 put_image_metadata(imd_array[j], NULL);
649                         FREE(imd_array);
650                         return NULL;
651                 }
652         }
653         return imd_array;
654 }
655
656 /* Checksum all streams that are unhashed (other than the metadata streams),
657  * merging them into the lookup table as needed.  This is a no-op unless the
658  * library has previously used to add or mount an image using the same
659  * WIMStruct. */
660 int
661 wim_checksum_unhashed_streams(WIMStruct *w)
662 {
663         int ret;
664         for (int i = 0; i < w->hdr.image_count; i++) {
665                 struct wim_lookup_table_entry *lte, *tmp;
666                 struct wim_image_metadata *imd = w->image_metadata[i];
667                 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
668                         ret = hash_unhashed_stream(lte, w->lookup_table, NULL);
669                         if (ret)
670                                 return ret;
671                 }
672         }
673         return 0;
674 }
675
676 /* Frees the memory for the WIMStruct, including all internal memory; also
677  * closes all files associated with the WIMStruct.  */
678 WIMLIBAPI void
679 wimlib_free(WIMStruct *w)
680 {
681         DEBUG("Freeing WIMStruct");
682
683         if (!w)
684                 return;
685         if (w->in_fd != -1)
686                 close(w->in_fd);
687         if (w->out_fd != -1)
688                 close(w->out_fd);
689
690         free_lookup_table(w->lookup_table);
691
692         FREE(w->filename);
693         free_wim_info(w->wim_info);
694         if (w->image_metadata) {
695                 for (unsigned i = 0; i < w->hdr.image_count; i++)
696                         put_image_metadata(w->image_metadata[i], NULL);
697                 FREE(w->image_metadata);
698         }
699         FREE(w);
700         DEBUG("Freed WIMStruct");
701 }
702
703 static bool
704 test_locale_ctype_utf8(void)
705 {
706 #ifdef __WIN32__
707         return false;
708 #else
709         char *ctype = nl_langinfo(CODESET);
710
711         return (!strstr(ctype, "UTF-8") ||
712                 !strstr(ctype, "UTF8") ||
713                 !strstr(ctype, "utf8") ||
714                 !strstr(ctype, "utf-8"));
715 #endif
716 }
717
718 WIMLIBAPI int
719 wimlib_global_init(int init_flags)
720 {
721         libxml_global_init();
722         if (!(init_flags & WIMLIB_INIT_FLAG_ASSUME_UTF8)) {
723                 wimlib_mbs_is_utf8 = test_locale_ctype_utf8();
724         #ifdef WITH_NTFS_3G
725                 if (!wimlib_mbs_is_utf8)
726                         libntfs3g_global_init();
727         #endif
728         }
729 #ifdef __WIN32__
730         win32_global_init();
731 #endif
732         return 0;
733 }
734
735 /* Free global memory allocations.  Not strictly necessary if the process using
736  * wimlib is just about to exit (as is the case for 'imagex'). */
737 WIMLIBAPI void
738 wimlib_global_cleanup(void)
739 {
740         libxml_global_cleanup();
741         iconv_global_cleanup();
742 #ifdef __WIN32__
743         win32_global_cleanup();
744 #endif
745 }