16 typedef unsigned uint;
18 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
19 (__a < __b) ? __a : __b; })
20 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
21 (__a > __b) ? __a : __b; })
22 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
24 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
26 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
28 /* Used for buffering FILE IO in a few places */
29 #define BUFFER_SIZE 4096
31 #ifdef ENABLE_ERROR_MESSAGES
32 extern bool __wimlib_print_errors;
33 extern void wimlib_error(const char *format, ...);
34 extern void wimlib_warning(const char *format, ...);
35 # define ERROR wimlib_error
36 # define WARNING wimlib_warning
38 # define ERROR(format, ...)
39 # define WARNING(format, ...)
40 #endif /* ENABLE_ERROR_MESSAGES */
42 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
44 # define DEBUG(format, ...) \
46 int __errno_save = errno; \
47 fprintf(stdout, "[%s %d] %s(): " format, \
48 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
50 errno = __errno_save; \
54 # define DEBUG(format, ...)
55 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
57 #ifdef ENABLE_MORE_DEBUG
58 # define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
60 # define DEBUG2(format, ...)
61 #endif /* ENABLE_DEBUG */
63 #ifdef ENABLE_ASSERTIONS
65 # define wimlib_assert(expr) assert(expr)
67 # define wimlib_assert(expr)
71 # define WIMLIBAPI __attribute__((visibility("default")))
72 # define NOINLINE __attribute__((noinline))
73 # define ALWAYS_INLINE inline __attribute__((always_inline))
74 # define COLD __attribute__((cold))
75 # define HOT __attribute__((hot))
79 # define ALWAYS_INLINE inline
84 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
85 extern void *(*wimlib_malloc_func)(size_t);
86 extern void (*wimlib_free_func)(void *);
87 extern void *(*wimlib_realloc)(void *, size_t);
88 extern void *wimlib_calloc(size_t nmemb, size_t size);
89 extern char *wimlib_strdup(const char *str);
90 # define MALLOC wimlib_malloc_func
91 # define FREE wimlib_free_func
92 # define REALLOC wimlib_realloc_func
93 # define CALLOC wimlib_calloc
94 # define STRDUP wimlib_strdup
98 # define MALLOC malloc
100 # define REALLOC realloc
101 # define CALLOC calloc
102 # define STRDUP strdup
103 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
106 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
107 size_t *utf8_len_ret);
109 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
110 size_t *utf16_len_ret);
112 extern void randomize_byte_array(void *p, size_t n);
114 extern void randomize_char_array_with_alnum(char p[], size_t n);
116 extern int sha1sum(const char *filename, void *buf);
118 extern const char *path_next_part(const char *path,
119 size_t *first_part_len_ret);
121 extern const char *path_basename(const char *path);
123 extern void to_parent_name(char buf[], size_t len);
125 extern void print_string(const void *string, size_t len);
127 extern int get_num_path_components(const char *path);
129 extern ssize_t full_write(int fd, const void *buf, size_t n);
132 static inline void print_byte_field(const u8 field[], size_t len)
135 printf("%02hhx", *field++);
139 #endif /* _WIMLIB_UTIL_H */