6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option) any
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along
20 * with this library; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifndef _WIMLIB_UTIL_H
25 #define _WIMLIB_UTIL_H
31 #include <sys/types.h>
39 typedef unsigned uint;
41 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
42 (__a < __b) ? __a : __b; })
43 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
44 (__a > __b) ? __a : __b; })
45 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
47 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
49 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
51 /* Used for buffering FILE IO in a few places */
52 #define BUFFER_SIZE 4096
54 #ifdef ENABLE_ERROR_MESSAGES
55 extern bool __wimlib_print_errors;
56 extern void wimlib_error(const char *format, ...);
57 extern void wimlib_warning(const char *format, ...);
58 # define ERROR wimlib_error
59 # define WARNING wimlib_warning
61 # define ERROR(format, ...)
62 # define WARNING(format, ...)
63 #endif /* ENABLE_ERROR_MESSAGES */
65 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
67 # define DEBUG(format, ...) \
69 int __errno_save = errno; \
70 fprintf(stdout, "[%s %d] %s(): " format, \
71 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
73 errno = __errno_save; \
77 # define DEBUG(format, ...)
78 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
80 #ifdef ENABLE_MORE_DEBUG
81 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
83 # define DEBUG2(format, ...)
84 #endif /* ENABLE_DEBUG */
86 #ifdef ENABLE_ASSERTIONS
88 # define wimlib_assert(expr) assert(expr)
90 # define wimlib_assert(expr)
94 # define WIMLIBAPI __attribute__((visibility("default")))
95 # define NOINLINE __attribute__((noinline))
96 # define ALWAYS_INLINE inline __attribute__((always_inline))
97 # define COLD __attribute__((cold))
98 # define HOT __attribute__((hot))
102 # define ALWAYS_INLINE inline
105 #endif /* __GNUC__ */
107 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
108 extern void *(*wimlib_malloc_func)(size_t);
109 extern void (*wimlib_free_func)(void *);
110 extern void *(*wimlib_realloc)(void *, size_t);
111 extern void *wimlib_calloc(size_t nmemb, size_t size);
112 extern char *wimlib_strdup(const char *str);
113 # define MALLOC wimlib_malloc_func
114 # define FREE wimlib_free_func
115 # define REALLOC wimlib_realloc_func
116 # define CALLOC wimlib_calloc
117 # define STRDUP wimlib_strdup
121 # define MALLOC malloc
123 # define REALLOC realloc
124 # define CALLOC calloc
125 # define STRDUP strdup
126 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
129 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
130 size_t *utf8_len_ret);
132 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
133 size_t *utf16_len_ret);
135 extern void randomize_byte_array(void *p, size_t n);
137 extern void randomize_char_array_with_alnum(char p[], size_t n);
139 extern int sha1sum(const char *filename, void *buf);
141 extern const char *path_next_part(const char *path,
142 size_t *first_part_len_ret);
144 extern const char *path_basename(const char *path);
146 extern void to_parent_name(char buf[], size_t len);
148 extern void print_string(const void *string, size_t len);
150 extern int get_num_path_components(const char *path);
152 extern ssize_t full_write(int fd, const void *buf, size_t n);
155 static inline void print_byte_field(const u8 field[], size_t len)
158 printf("%02hhx", *field++);
162 #endif /* _WIMLIB_UTIL_H */