6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * This file is part of wimlib, a library for working with WIM files.
11 * wimlib is free software; you can redistribute it and/or modify it under the
12 * terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option)
16 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with wimlib; if not, see http://www.gnu.org/licenses/.
25 #include "wimlib_internal.h"
26 #include "endianness.h"
38 /* True if wimlib is to print an informational message when an error occurs.
39 * This can be turned off by calling wimlib_set_print_errors(false). */
40 #ifdef ENABLE_ERROR_MESSAGES
42 bool __wimlib_print_errors = false;
44 void wimlib_error(const char *format, ...)
46 if (__wimlib_print_errors) {
52 fputs("ERROR: ", stderr);
53 vfprintf(stderr, format, va);
60 void wimlib_error_with_errno(const char *format, ...)
62 if (__wimlib_print_errors) {
68 fputs("ERROR: ", stderr);
69 vfprintf(stderr, format, va);
70 fprintf(stderr, ": %s\n", strerror(errno_save));
76 void wimlib_warning(const char *format, ...)
78 if (__wimlib_print_errors) {
84 fputs("WARNING: ", stderr);
85 vfprintf(stderr, format, va);
94 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
96 #ifdef ENABLE_ERROR_MESSAGES
97 __wimlib_print_errors = show_error_messages;
100 if (show_error_messages)
101 return WIMLIB_ERR_UNSUPPORTED;
105 static const char *error_strings[] = {
108 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
109 = "Lookup table is compressed",
110 [WIMLIB_ERR_DECOMPRESSION]
111 = "Failed to decompress compressed data",
112 [WIMLIB_ERR_DELETE_STAGING_DIR]
113 = "Failed to delete staging directory",
115 = "Failed to fork another process",
117 = "An error was returned by fuse_main()",
118 [WIMLIB_ERR_FUSERMOUNT]
119 = "Could not execute the `fusermount' program, or it exited "
120 "with a failure status",
121 [WIMLIB_ERR_IMAGE_COUNT]
122 = "Inconsistent image count among the metadata "
123 "resources, the WIM header, and/or the XML data",
124 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
125 = "Tried to add an image with a name that is already in use",
126 [WIMLIB_ERR_INTEGRITY]
127 = "The WIM failed an integrity check",
128 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
129 = "The WIM is compressed but does not have a chunk "
131 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
132 = "The WIM is compressed, but is not marked as having LZX or "
133 "XPRESS compression",
134 [WIMLIB_ERR_INVALID_DENTRY]
135 = "A directory entry in the WIM was invalid",
136 [WIMLIB_ERR_INVALID_HEADER_SIZE]
137 = "The WIM header was not 208 bytes",
138 [WIMLIB_ERR_INVALID_IMAGE]
139 = "Tried to select an image that does not exist in the WIM",
140 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
141 = "The WIM's integrity table is invalid",
142 [WIMLIB_ERR_INVALID_PARAM]
143 = "An invalid parameter was given",
144 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
145 = "A resource entry in the WIM is invalid",
147 = "Failed to create a hard or symbolic link when extracting "
148 "a file from the WIM",
150 = "Failed to create a directory",
152 = "Failed to create or use a POSIX message queue",
154 = "Ran out of memory",
156 = "Expected a directory",
157 [WIMLIB_ERR_NOT_A_WIM_FILE]
158 = "The file did not begin with the magic characters that "
159 "identify a WIM file",
160 [WIMLIB_ERR_NO_FILENAME]
161 = "The WIM is not identified with a filename",
163 = "NTFS-3g encountered an error (check errno)",
165 = "Failed to open a file",
167 = "Failed to open a directory",
169 = "Could not read data from a file",
171 = "Could not rename a file",
172 [WIMLIB_ERR_SPLIT_INVALID]
173 = "The WIM is part of an invalid split WIM",
174 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
175 = "The WIM is part of a split WIM, which is not supported for this operation",
177 = "Could not read the metadata for a file or directory",
180 [WIMLIB_ERR_UNKNOWN_VERSION]
181 = "The WIM file is marked with an unknown version number",
182 [WIMLIB_ERR_UNSUPPORTED]
183 = "The requested operation is unsupported",
185 = "Failed to write data to a file",
187 = "The XML data of the WIM is invalid",
190 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
192 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
195 return error_strings[code];
200 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
201 void *(*wimlib_malloc_func) (size_t) = malloc;
202 void (*wimlib_free_func) (void *) = free;
203 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
205 void *wimlib_calloc(size_t nmemb, size_t size)
207 size_t total_size = nmemb * size;
208 void *p = MALLOC(total_size);
210 memset(p, 0, total_size);
214 char *wimlib_strdup(const char *str)
220 p = MALLOC(size + 1);
222 memcpy(p, str, size + 1);
226 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
227 void (*free_func)(void *),
228 void *(*realloc_func)(void *, size_t));
231 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
232 void (*free_func)(void *),
233 void *(*realloc_func)(void *, size_t))
235 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
236 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
237 wimlib_free_func = free_func ? free_func : free;
238 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
240 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
241 wimlib_realloc_func);
244 ERROR("Cannot set custom memory allocator functions:");
245 ERROR("wimlib was compiled with the --without-custom-memory-allocator "
247 return WIMLIB_ERR_UNSUPPORTED;
253 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
255 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
257 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
258 size_t *utf8_len_ret)
260 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
261 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
262 if (cd_utf16_to_utf8 == (iconv_t)-1) {
263 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
264 "for converting UTF-16LE to UTF-8");
268 size_t utf16_bytes_left = utf16_len;
269 size_t utf8_bytes_left = utf16_len;
271 char *utf8_str = MALLOC(utf8_bytes_left);
275 char *orig_utf8_str = utf8_str;
277 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
278 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
280 if (num_chars_converted == (size_t)(-1)) {
281 ERROR_WITH_ERRNO("Failed to convert UTF-16LE string to UTF-8 "
287 size_t utf8_len = utf16_len - utf8_bytes_left;
289 *utf8_len_ret = utf8_len;
290 orig_utf8_str[utf8_len] = '\0';
291 return orig_utf8_str;
294 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
296 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
297 * UTF-16 encoding. */
298 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
299 size_t *utf16_len_ret)
301 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
302 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
303 if (cd_utf8_to_utf16 == (iconv_t)-1) {
304 ERROR_WITH_ERRNO("Failed to get conversion descriptor "
305 "for converting UTF-8 to UTF-16LE");
310 size_t utf8_bytes_left = utf8_len;
311 size_t utf16_capacity = utf8_len * 4;
312 size_t utf16_bytes_left = utf16_capacity;
314 char *utf16_str = MALLOC(utf16_capacity + 2);
318 char *orig_utf16_str = utf16_str;
320 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
321 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
323 if (num_chars_converted == (size_t)(-1)) {
324 ERROR_WITH_ERRNO("Failed to convert UTF-8 string to UTF-16LE "
326 FREE(orig_utf16_str);
330 size_t utf16_len = utf16_capacity - utf16_bytes_left;
332 *utf16_len_ret = utf16_len;
333 orig_utf16_str[utf16_len] = '\0';
334 orig_utf16_str[utf16_len + 1] = '\0';
335 return orig_utf16_str;
338 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
341 * Returns short count and set errno on failure. */
342 ssize_t full_write(int fd, const void *buf, size_t n)
349 ret = write(fd, p, n);
363 static bool seeded = false;
365 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
366 void randomize_char_array_with_alnum(char p[], size_t n)
385 /* Fills @n bytes pointer to by @p with random numbers. */
386 void randomize_byte_array(void *__p, size_t n)
398 /* Takes in a path of length @len in @buf, and transforms it into a string for
399 * the path of its parent directory. */
400 void to_parent_name(char buf[], size_t len)
402 ssize_t i = (ssize_t)len - 1;
403 while (i >= 0 && buf[i] == '/')
405 while (i >= 0 && buf[i] != '/')
407 while (i >= 0 && buf[i] == '/')
412 /* Like the basename() function, but does not modify @path; it just returns a
414 const char *path_basename(const char *path)
416 const char *p = path;
421 /* Trailing slashes. */
422 while ((p != path - 1) && *p == '/')
425 while ((p != path - 1) && *p != '/')
432 * Splits a file path into the part before the first '/', or the entire name if
433 * there is no '/', and the part after the first sequence of '/' characters.
435 * @path: The file path to split.
436 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
437 * first part of the path will be returned.
438 * @return: A pointer to the next part of the path, after the first
439 * sequence of '/', or a pointer to the terminating
440 * null byte in the case of a path without any '/'.
442 const char *path_next_part(const char *path, size_t *first_part_len_ret)
445 const char *next_part;
448 while (path[i] != '/' && path[i] != '\0')
450 if (first_part_len_ret)
451 *first_part_len_ret = i;
452 next_part = &path[i];
453 while (*next_part == '/')
458 /* Returns the number of components of @path. */
459 int get_num_path_components(const char *path)
461 int num_components = 0;
467 while (*path && *path != '/')
470 return num_components;
475 * Prints a string. Printable characters are printed as-is, while unprintable
476 * characters are printed as their octal escape codes.
478 void print_string(const void *string, size_t len)
480 const u8 *p = string;
486 printf("\\%03hho", *p);