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);
224 ERROR("Cannot set custom memory allocator functions:\n");
225 ERROR("wimlib was compiled with the "
226 "--without-custom-memory-allocator flag\n");
227 return WIMLIB_ERR_UNSUPPORTED;
233 static iconv_t cd_utf16_to_utf8 = (iconv_t)(-1);
235 /* Converts a string in the UTF-16 encoding to a newly allocated string in the
237 char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
238 size_t *utf8_len_ret)
240 if (cd_utf16_to_utf8 == (iconv_t)(-1)) {
241 cd_utf16_to_utf8 = iconv_open("UTF-8", "UTF-16LE");
242 if (cd_utf16_to_utf8 == (iconv_t)-1) {
243 ERROR("Failed to get conversion descriptor for "
244 "converting UTF-16LE to UTF-8: %m\n");
248 size_t utf16_bytes_left = utf16_len;
249 size_t utf8_bytes_left = utf16_len;
251 char *utf8_str = MALLOC(utf8_bytes_left);
255 char *orig_utf8_str = utf8_str;
257 size_t num_chars_converted = iconv(cd_utf16_to_utf8, (char**)&utf16_str,
258 &utf16_bytes_left, &utf8_str, &utf8_bytes_left);
260 if (num_chars_converted == (size_t)(-1)) {
261 ERROR("Failed to convert UTF-16LE string to UTF-8 string: "
267 size_t utf8_len = utf16_len - utf8_bytes_left;
269 *utf8_len_ret = utf8_len;
270 orig_utf8_str[utf8_len] = '\0';
271 return orig_utf8_str;
274 static iconv_t cd_utf8_to_utf16 = (iconv_t)(-1);
276 /* Converts a string in the UTF-8 encoding to a newly allocated string in the
277 * UTF-16 encoding. */
278 char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
279 size_t *utf16_len_ret)
281 if (cd_utf8_to_utf16 == (iconv_t)(-1)) {
282 cd_utf8_to_utf16 = iconv_open("UTF-16LE", "UTF-8");
283 if (cd_utf8_to_utf16 == (iconv_t)-1) {
284 ERROR("Failed to get conversion descriptor for "
285 "converting UTF-8 to UTF-16LE: %m\n");
290 size_t utf8_bytes_left = utf8_len;
291 size_t utf16_capacity = utf8_len * 4;
292 size_t utf16_bytes_left = utf16_capacity;
294 char *utf16_str = MALLOC(utf16_capacity + 2);
298 char *orig_utf16_str = utf16_str;
300 size_t num_chars_converted = iconv(cd_utf8_to_utf16, (char**)&utf8_str,
301 &utf8_bytes_left, &utf16_str, &utf16_bytes_left);
303 if (num_chars_converted == (size_t)(-1)) {
304 ERROR("Failed to convert UTF-8 string to UTF-16LE string: "
307 "Not enough room in output buffer" :
309 FREE(orig_utf16_str);
313 size_t utf16_len = utf16_capacity - utf16_bytes_left;
315 *utf16_len_ret = utf16_len;
316 orig_utf16_str[utf16_len] = '\0';
317 orig_utf16_str[utf16_len + 1] = '\0';
318 return orig_utf16_str;
321 /* Write @n bytes from @buf to the file descriptor @fd, retrying on interupt and
324 * Returns short count and set errno on failure. */
325 ssize_t full_write(int fd, const void *buf, size_t n)
332 ret = write(fd, p, n);
346 static bool seeded = false;
348 /* Fills @n bytes pointed to by @p with random alphanumeric characters. */
349 void randomize_char_array_with_alnum(char p[], size_t n)
368 /* Fills @n bytes pointer to by @p with random numbers. */
369 void randomize_byte_array(void *__p, size_t n)
381 /* Takes in a path of length @len in @buf, and transforms it into a string for
382 * the path of its parent directory. */
383 void to_parent_name(char buf[], size_t len)
385 ssize_t i = (ssize_t)len - 1;
386 while (i >= 0 && buf[i] == '/')
388 while (i >= 0 && buf[i] != '/')
390 while (i >= 0 && buf[i] == '/')
395 /* Like the basename() function, but does not modify @path; it just returns a
397 const char *path_basename(const char *path)
399 const char *p = path;
404 /* Trailing slashes. */
405 while ((p != path - 1) && *p == '/')
408 while ((p != path - 1) && *p != '/')
415 * Splits a file path into the part before the first '/', or the entire name if
416 * there is no '/', and the part after the first sequence of '/' characters.
418 * @path: The file path to split.
419 * @first_part_len_ret: A pointer to a `size_t' into which the length of the
420 * first part of the path will be returned.
421 * @return: A pointer to the next part of the path, after the first
422 * sequence of '/', or a pointer to the terminating
423 * null byte in the case of a path without any '/'.
425 const char *path_next_part(const char *path, size_t *first_part_len_ret)
428 const char *next_part;
431 while (path[i] != '/' && path[i] != '\0')
433 if (first_part_len_ret)
434 *first_part_len_ret = i;
435 next_part = &path[i];
436 while (*next_part == '/')
441 /* Returns the number of components of @path. */
442 int get_num_path_components(const char *path)
444 int num_components = 0;
450 while (*path && *path != '/')
453 return num_components;
458 * Prints a string. Printable characters are printed as-is, while unprintable
459 * characters are printed as their octal escape codes.
461 void print_string(const void *string, size_t len)
463 const u8 *p = string;
469 printf("\\%03hho", *p);