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_error_messages(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);
59 void wimlib_warning(const char *format, ...)
61 if (__wimlib_print_errors) {
67 fputs("WARNING: ", stderr);
68 vfprintf(stderr, format, va);
76 WIMLIBAPI int wimlib_set_print_errors(bool show_error_messages)
78 #ifdef ENABLE_ERROR_MESSAGES
79 __wimlib_print_errors = show_error_messages;
82 if (show_error_messages)
83 return WIMLIB_ERR_UNSUPPORTED;
87 static const char *error_strings[] = {
90 [WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE]
91 = "Lookup table is compressed",
92 [WIMLIB_ERR_DECOMPRESSION]
93 = "Failed to decompress compressed data",
94 [WIMLIB_ERR_DELETE_STAGING_DIR]
95 = "Failed to delete staging directory",
97 = "Failed to fork another process",
99 = "An error was returned by fuse_main()",
100 [WIMLIB_ERR_FUSERMOUNT]
101 = "Could not execute the `fusermount' program, or it exited "
102 "with a failure status",
103 [WIMLIB_ERR_IMAGE_COUNT]
104 = "Inconsistent image count among the metadata "
105 "resources, the WIM header, and/or the XML data",
106 [WIMLIB_ERR_IMAGE_NAME_COLLISION]
107 = "Tried to add an image with a name that is already in use",
108 [WIMLIB_ERR_INTEGRITY]
109 = "The WIM failed an integrity check",
110 [WIMLIB_ERR_INVALID_CHUNK_SIZE]
111 = "The WIM is compressed but does not have a chunk "
113 [WIMLIB_ERR_INVALID_COMPRESSION_TYPE]
114 = "The WIM is compressed, but is not marked as having LZX or "
115 "XPRESS compression",
116 [WIMLIB_ERR_INVALID_DENTRY]
117 = "A directory entry in the WIM was invalid",
118 [WIMLIB_ERR_INVALID_HEADER_SIZE]
119 = "The WIM header was not 208 bytes",
120 [WIMLIB_ERR_INVALID_IMAGE]
121 = "Tried to select an image that does not exist in the WIM",
122 [WIMLIB_ERR_INVALID_INTEGRITY_TABLE]
123 = "The WIM's integrity table is invalid",
124 [WIMLIB_ERR_INVALID_PARAM]
125 = "An invalid parameter was given",
126 [WIMLIB_ERR_INVALID_RESOURCE_SIZE]
127 = "A resource entry in the WIM is invalid",
129 = "Failed to create a hard or symbolic link when extracting "
130 "a file from the WIM",
132 = "Failed to create a directory",
134 = "Failed to create or use a POSIX message queue",
136 = "Ran out of memory",
138 = "Expected a directory",
139 [WIMLIB_ERR_NOT_A_WIM_FILE]
140 = "The file did not begin with the magic characters that "
141 "identify a WIM file",
142 [WIMLIB_ERR_NO_FILENAME]
143 = "The WIM is not identified with a filename",
145 = "Failed to open a file",
147 = "Failed to open a directory",
149 = "Could not read data from a file",
151 = "Could not rename a file",
152 [WIMLIB_ERR_SPLIT_INVALID]
153 = "The WIM is part of an invalid split WIM",
154 [WIMLIB_ERR_SPLIT_UNSUPPORTED]
155 = "The WIM is part of a split WIM, which is not supported for this operation",
157 = "Could not read the metadata for a file or directory",
160 [WIMLIB_ERR_UNKNOWN_VERSION]
161 = "The WIM file is marked with an unknown version number",
162 [WIMLIB_ERR_UNSUPPORTED]
163 = "The requested operation is unsupported",
165 = "Failed to write data to a file",
167 = "The XML data of the WIM is invalid",
170 WIMLIBAPI const char *wimlib_get_error_string(enum wimlib_error_code code)
172 if (code < WIMLIB_ERR_SUCCESS || code > WIMLIB_ERR_XML)
175 return error_strings[code];
180 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
181 void *(*wimlib_malloc_func)(size_t) = malloc;
182 void (*wimlib_free_func)(void *) = free;
183 void *(*wimlib_realloc_func)(void *, size_t) = realloc;
185 void *wimlib_calloc(size_t nmemb, size_t size)
187 size_t total_size = nmemb * size;
188 void *p = MALLOC(total_size);
190 memset(p, 0, total_size);
194 char *wimlib_strdup(const char *str)
200 p = MALLOC(size + 1);
202 memcpy(p, str, size + 1);
206 extern void xml_set_memory_allocator(void *(*malloc_func)(size_t),
207 void (*free_func)(void *),
208 void *(*realloc_func)(void *, size_t));
211 WIMLIBAPI int wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
212 void (*free_func)(void *),
213 void *(*realloc_func)(void *, size_t))
215 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
216 wimlib_malloc_func = malloc_func ? malloc_func : malloc;
217 wimlib_free_func = free_func ? free_func : free;
218 wimlib_realloc_func = realloc_func ? realloc_func : realloc;
220 xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
221 wimlib_realloc_func);
223 ERROR("Cannot set custom memory allocator functions:\n");
224 ERROR("wimlib was compiled with the "
225 "--without-custom-memory-allocator flag\n");
226 return WIMLIB_ERR_UNSUPPORTED;
232 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
234 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
236 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
237 size_t *utf8_len_ret)
239 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
240 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
241 if (cd_utf16_to_utf8 == (iconv_t)-1) {
242 ERROR("Failed to get conversion descriptor for "
243 "converting UTF-16LE to UTF-8: %m\n");
247 size_t utf16_bytes_left = utf16_len;
248 size_t utf8_bytes_left = utf16_len;
250 char *utf8_str = MALLOC(utf8_bytes_left);
254 char *orig_utf8_str = utf8_str;
256 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
257 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
259 if (num_chars_converted == (size_t)(-1)) {
260 ERROR("Failed to convert UTF-16LE string to UTF-8 string: "
266 size_t utf8_len = utf16_len - utf8_bytes_left;
268 *utf8_len_ret = utf8_len;
269 orig_utf8_str[utf8_len] = '\0';
270 return orig_utf8_str;
273 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
275 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
276 * UTF-16 encoding. */
277 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
278 size_t *utf16_len_ret)
280 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
281 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
282 if (cd_utf8_to_utf16 == (iconv_t)-1) {
283 ERROR("Failed to get conversion descriptor for "
284 "converting UTF-8 to UTF-16LE: %m\n");
289 size_t utf8_bytes_left = utf8_len;
290 size_t utf16_capacity = utf8_len * 4;
291 size_t utf16_bytes_left = utf16_capacity;
293 char *utf16_str = MALLOC(utf16_capacity + 2);
297 char *orig_utf16_str = utf16_str;
299 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
300 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
302 if (num_chars_converted == (size_t)(-1)) {
303 ERROR("Failed to convert UTF-8 string to UTF-16LE string: "
306 "Not enough room in output buffer" :
308 FREE(orig_utf16_str);
312 size_t utf16_len = utf16_capacity - utf16_bytes_left;
314 *utf16_len_ret = utf16_len;
315 orig_utf16_str[utf16_len] = '\0';
316 orig_utf16_str[utf16_len + 1] = '\0';
317 return orig_utf16_str;
320 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
323 * Returns short count and set errno on failure. */
324 ssize_t full_write(int fd, const void *buf, size_t n)
331 ret = write(fd, p, n);
345 static bool seeded = false;
347 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
348 void randomize_char_array_with_alnum(char p[], size_t n)
367 /* Fills @n bytes pointer to by @p with random numbers. */
368 void randomize_byte_array(void *__p, size_t n)
380 /* Takes in a path of length @len in @buf, and transforms it into a string for
381 * the path of its parent directory. */
382 void to_parent_name(char buf[], size_t len)
384 ssize_t i = (ssize_t)len - 1;
385 while (i >= 0 && buf[i] == '/')
387 while (i >= 0 && buf[i] != '/')
389 while (i >= 0 && buf[i] == '/')
394 /* Like the basename() function, but does not modify @path; it just returns a
396 const char *path_basename(const char *path)
398 const char *p = path;
403 /* Trailing slashes. */
404 while ((p != path - 1) && *p == '/')
407 while ((p != path - 1) && *p != '/')
414 * Splits a file path into the part before the first '/', or the entire name if
415 * there is no '/', and the part after the first sequence of '/' characters.
417 * @path: The file path to split.
418 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
419 * first part of the path will be returned.
420 * @return: A pointer to the next part of the path, after the first
421 * sequence of '/', or a pointer to the terminating
422 * null byte in the case of a path without any '/'.
424 const char *path_next_part(const char *path, size_t *first_part_len_ret)
427 const char *next_part;
430 while (path[i] != '/' && path[i] != '\0')
432 if (first_part_len_ret)
433 *first_part_len_ret = i;
434 next_part = &path[i];
435 while (*next_part == '/')
440 /* Returns the number of components of @path. */
441 int get_num_path_components(const char *path)
443 int num_components = 0;
449 while (*path && *path != '/')
452 return num_components;
457 * Prints a string. Printable characters are printed as-is, while unprintable
458 * characters are printed as their octal escape codes.
460 void print_string(const void *string, size_t len)
462 const u8 *p = string;
468 printf("\\%03hho", *p);