]> wimlib.net Git - wimlib/blob - src/util.h
71590ffe2dde2d1674e7709c4b24f1765adeff54
[wimlib] / src / util.h
1 #ifndef _WIMLIB_UTIL_H
2 #define _WIMLIB_UTIL_H
3
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <inttypes.h>
8 #include <sys/types.h>
9 #include "config.h"
10
11 #ifdef __GNUC__
12 #       if defined(__CYGWIN__) || defined(__WIN32__)
13 #               define WIMLIBAPI __declspec(dllexport)
14 #       else
15 #               define WIMLIBAPI __attribute__((visibility("default")))
16 #       endif
17 #       define ALWAYS_INLINE inline __attribute__((always_inline))
18 #       define PACKED __attribute__((packed))
19 #       define FORMAT(type, format_str, args_start) \
20                         __attribute__((format(type, format_str, args_start)))
21 #       if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
22 #               define COLD     __attribute__((cold))
23 #       else
24 #               define COLD
25 #       endif
26 #else
27 #       define WIMLIBAPI
28 #       define ALWAYS_INLINE inline
29 #       define FORMAT(type, format_str, args_start)
30 #       define COLD
31 #       define PACKED
32 #endif /* __GNUC__ */
33
34
35 #if 0
36 #ifdef WITH_FUSE
37 #define atomic_inc(ptr) \
38         __sync_fetch_and_add(ptr, 1)
39
40 #define atomic_dec(ptr) \
41         __sync_sub_and_fetch(ptr, 1)
42 #endif
43 #endif
44
45 #ifndef _NTFS_TYPES_H
46 typedef uint8_t  u8;
47 typedef uint16_t u16;
48 typedef uint32_t u32;
49 typedef uint64_t u64;
50 #endif
51
52 /* A pointer to 'mbchar' indicates a string of "multibyte characters" provided
53  * in the default encoding of the user's locale, which may be "UTF-8",
54  * "ISO-8859-1", "C", or any other ASCII-compatible encoding.
55  * "ASCII-compatible" here means any encoding where all ASCII characters have
56  * the same representation, and any non-ASCII character is represented as a
57  * sequence of one or more bytes not already used by any ASCII character. */
58 typedef char mbchar;
59
60 /* A pointer to 'utf8char' indicates a UTF-8 encoded string */
61 typedef char utf8char;
62
63 /* Note: in some places in the code, strings of plain old 'char' are still used.
64  * This means that the string is being operated on in an ASCII-compatible way,
65  * and may be either a multibyte or UTF-8 string.  */
66
67 /* A pointer to 'utf16lechar' indicates a UTF-16LE encoded string */
68 typedef u16 utf16lechar;
69
70 #ifndef min
71 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
72                                         (__a < __b) ? __a : __b; })
73 #endif
74
75 #ifndef max
76 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
77                                         (__a > __b) ? __a : __b; })
78 #endif
79
80 #ifndef swap
81 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
82 #endif
83
84 /**
85  * container_of - cast a member of a structure out to the containing structure
86  * @ptr:        the pointer to the member.
87  * @type:       the type of the container struct this is embedded in.
88  * @member:     the name of the member within the struct.
89  *
90  */
91 #ifndef container_of
92 #define container_of(ptr, type, member) ({                      \
93         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
94         (type *)( (char *)__mptr - offsetof(type,member) );})
95 #endif
96
97 #define DIV_ROUND_UP(numerator, denominator) \
98         (((numerator) + (denominator) - 1) / (denominator))
99
100 #define MODULO_NONZERO(numerator, denominator) \
101         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
102
103 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
104
105 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
106
107 /* Used for buffering FILE IO in a few places */
108 #define BUFFER_SIZE 4096
109
110 static inline void FORMAT(printf, 1, 2)
111 dummy_printf(const char *format, ...)
112 {
113 }
114
115 #ifdef ENABLE_ERROR_MESSAGES
116 extern void
117 wimlib_error(const char *format, ...) FORMAT(printf, 1, 2) COLD;
118
119 extern void
120 wimlib_error_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
121
122 extern void
123 wimlib_warning(const char *format, ...) FORMAT(printf, 1, 2) COLD;
124
125 extern void
126 wimlib_warning_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
127 #       define ERROR                    wimlib_error
128 #       define ERROR_WITH_ERRNO         wimlib_error_with_errno
129 #       define WARNING                  wimlib_warning
130 #       define WARNING_WITH_ERRNO       wimlib_warning
131 #else
132 #       define ERROR(format, ...)               dummy_printf(format, ## __VA_ARGS__)
133 #       define ERROR_WITH_ERRNO(format, ...)    dummy_printf(format, ## __VA_ARGS__)
134 #       define WARNING(format, ...)             dummy_printf(format, ## __VA_ARGS__)
135 #       define WARNING_WITH_ERRNO(format, ...)  dummy_printf(format, ## __VA_ARGS__)
136 #endif /* ENABLE_ERROR_MESSAGES */
137
138 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
139 #       include <errno.h>
140 #       define DEBUG(format, ...)                                       \
141         ({                                                              \
142                 int __errno_save = errno;                               \
143                 wimlib_fprintf(stdout, "[%s %d] %s(): " format,         \
144                         __FILE__, __LINE__, __func__, ## __VA_ARGS__);  \
145                 putchar('\n');                                          \
146                 fflush(stdout);                                         \
147                 errno = __errno_save;                                   \
148         })
149
150 #else
151 #       define DEBUG(format, ...) dummy_printf(format, ## __VA_ARGS__)
152 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
153
154 #ifdef ENABLE_MORE_DEBUG
155 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
156 #else
157 #       define DEBUG2(format, ...) dummy_printf(format, ## __VA_ARGS__)
158 #endif /* ENABLE_DEBUG */
159
160 #ifdef ENABLE_ASSERTIONS
161 #include <assert.h>
162 #       define wimlib_assert(expr) assert(expr)
163 #else
164 #       define wimlib_assert(expr)
165 #endif
166
167 #ifdef ENABLE_MORE_ASSERTIONS
168 #define wimlib_assert2(expr) wimlib_assert(expr)
169 #else
170 #define wimlib_assert2(expr)
171 #endif
172
173 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
174
175 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
176 extern void *(*wimlib_malloc_func)(size_t);
177 extern void (*wimlib_free_func)(void *);
178 extern void *(*wimlib_realloc_func)(void *, size_t);
179 extern void *wimlib_calloc(size_t nmemb, size_t size);
180 extern char *wimlib_strdup(const char *str);
181 #       define  MALLOC  wimlib_malloc_func
182 #       define  FREE    wimlib_free_func
183 #       define  REALLOC wimlib_realloc_func
184 #       define  CALLOC  wimlib_calloc
185 #       define  STRDUP  wimlib_strdup
186 #else
187 #       include <stdlib.h>
188 #       include <string.h>
189 #       define  MALLOC  malloc
190 #       define  FREE    free
191 #       define  REALLOC realloc
192 #       define  CALLOC  calloc
193 #       define  STRDUP  strdup
194 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
195
196
197 /* util.c */
198 extern void
199 randomize_byte_array(u8 *p, size_t n);
200
201 extern void
202 randomize_char_array_with_alnum(char p[], size_t n);
203
204 extern const char *
205 path_next_part(const char *path, size_t *first_part_len_ret);
206
207 extern const char *
208 path_basename(const char *path);
209
210 extern const char *
211 path_stream_name(const char *path);
212
213 extern void
214 to_parent_name(char buf[], size_t len);
215
216 extern void
217 print_string(const void *string, size_t len);
218
219 extern int
220 get_num_path_components(const char *path);
221
222 static inline void
223 print_byte_field(const u8 field[], size_t len)
224 {
225         while (len--)
226                 printf("%02hhx", *field++);
227 }
228
229 static inline u32
230 bsr32(u32 n)
231 {
232 #if defined(__x86__) || defined(__x86_64__)
233         asm("bsrl %0, %0;"
234                         : "=r"(n)
235                         : "0" (n));
236         return n;
237 #else
238         u32 pow = 0;
239         while ((n >>= 1) != 0)
240                 pow++;
241         return pow;
242 #endif
243 }
244
245 extern int
246 wimlib_fprintf(FILE *fp, const char *format, ...) FORMAT(printf, 2, 3);
247
248 extern int
249 wimlib_printf(const char *format, ...) FORMAT(printf, 1, 2);
250
251 #endif /* _WIMLIB_UTIL_H */