]> wimlib.net Git - wimlib/blob - src/util.h
58a2f2b45279bf43155ac8c839d1b59773b9b031
[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 /* encoding.c */
71 extern void
72 iconv_global_cleanup();
73
74 extern bool wimlib_mbs_is_utf8;
75
76 #define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2,           \
77                                           chartype1, chartype2)         \
78                                                                         \
79 extern int                                                              \
80 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
81                                   size_t *out_nbytes_ret);              \
82                                                                         \
83 extern int                                                              \
84 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes,   \
85                                chartype2 *out);                         \
86                                                                         \
87 extern int                                                              \
88 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes,         \
89                          chartype2 **out_ret,                           \
90                          size_t *out_nbytes_ret);                       \
91
92 /* multi-byte string to UTF16-LE string */
93 DECLARE_CHAR_CONVERSION_FUNCTIONS(mbs, utf16le, mbchar, utf16lechar);
94
95 /* UTF16-LE string to multi-byte string */
96 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, mbs, utf16lechar, mbchar);
97
98 /* UTF-8 string to multi-byte string */
99 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf8, mbs, utf8char, mbchar);
100
101 extern bool
102 utf8_str_contains_nonascii_chars(const utf8char *utf8_str);
103
104 #ifndef min
105 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
106                                         (__a < __b) ? __a : __b; })
107 #endif
108
109 #ifndef max
110 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
111                                         (__a > __b) ? __a : __b; })
112 #endif
113
114 #ifndef swap
115 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
116 #endif
117
118 /**
119  * container_of - cast a member of a structure out to the containing structure
120  * @ptr:        the pointer to the member.
121  * @type:       the type of the container struct this is embedded in.
122  * @member:     the name of the member within the struct.
123  *
124  */
125 #ifndef container_of
126 #define container_of(ptr, type, member) ({                      \
127         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
128         (type *)( (char *)__mptr - offsetof(type,member) );})
129 #endif
130
131 #define DIV_ROUND_UP(numerator, denominator) \
132         (((numerator) + (denominator) - 1) / (denominator))
133
134 #define MODULO_NONZERO(numerator, denominator) \
135         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
136
137 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
138
139 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
140
141 /* Used for buffering FILE IO in a few places */
142 #define BUFFER_SIZE 4096
143
144 static inline void FORMAT(printf, 1, 2)
145 dummy_printf(const char *format, ...)
146 {
147 }
148
149 #ifdef ENABLE_ERROR_MESSAGES
150 extern void
151 wimlib_error(const char *format, ...) FORMAT(printf, 1, 2) COLD;
152
153 extern void
154 wimlib_error_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
155
156 extern void
157 wimlib_warning(const char *format, ...) FORMAT(printf, 1, 2) COLD;
158
159 extern void
160 wimlib_warning_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD;
161 #       define ERROR                    wimlib_error
162 #       define ERROR_WITH_ERRNO         wimlib_error_with_errno
163 #       define WARNING                  wimlib_warning
164 #       define WARNING_WITH_ERRNO       wimlib_warning
165 #else
166 #       define ERROR(format, ...)               dummy_printf(format, ## __VA_ARGS__)
167 #       define ERROR_WITH_ERRNO(format, ...)    dummy_printf(format, ## __VA_ARGS__)
168 #       define WARNING(format, ...)             dummy_printf(format, ## __VA_ARGS__)
169 #       define WARNING_WITH_ERRNO(format, ...)  dummy_printf(format, ## __VA_ARGS__)
170 #endif /* ENABLE_ERROR_MESSAGES */
171
172 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
173 #       include <errno.h>
174 #       define DEBUG(format, ...)                                       \
175         ({                                                              \
176                 int __errno_save = errno;                               \
177                 wimlib_fprintf(stdout, "[%s %d] %s(): " format,         \
178                         __FILE__, __LINE__, __func__, ## __VA_ARGS__);  \
179                 putchar('\n');                                          \
180                 fflush(stdout);                                         \
181                 errno = __errno_save;                                   \
182         })
183
184 #else
185 #       define DEBUG(format, ...) dummy_printf(format, ## __VA_ARGS__)
186 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
187
188 #ifdef ENABLE_MORE_DEBUG
189 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
190 #else
191 #       define DEBUG2(format, ...) dummy_printf(format, ## __VA_ARGS__)
192 #endif /* ENABLE_DEBUG */
193
194 #ifdef ENABLE_ASSERTIONS
195 #include <assert.h>
196 #       define wimlib_assert(expr) assert(expr)
197 #else
198 #       define wimlib_assert(expr)
199 #endif
200
201 #ifdef ENABLE_MORE_ASSERTIONS
202 #define wimlib_assert2(expr) wimlib_assert(expr)
203 #else
204 #define wimlib_assert2(expr)
205 #endif
206
207 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
208
209 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
210 extern void *(*wimlib_malloc_func)(size_t);
211 extern void (*wimlib_free_func)(void *);
212 extern void *(*wimlib_realloc_func)(void *, size_t);
213 extern void *wimlib_calloc(size_t nmemb, size_t size);
214 extern char *wimlib_strdup(const char *str);
215 #       define  MALLOC  wimlib_malloc_func
216 #       define  FREE    wimlib_free_func
217 #       define  REALLOC wimlib_realloc_func
218 #       define  CALLOC  wimlib_calloc
219 #       define  STRDUP  wimlib_strdup
220 #else
221 #       include <stdlib.h>
222 #       include <string.h>
223 #       define  MALLOC  malloc
224 #       define  FREE    free
225 #       define  REALLOC realloc
226 #       define  CALLOC  calloc
227 #       define  STRDUP  strdup
228 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
229
230
231 /* util.c */
232 extern void
233 randomize_byte_array(u8 *p, size_t n);
234
235 extern void
236 randomize_char_array_with_alnum(char p[], size_t n);
237
238 extern const char *
239 path_next_part(const char *path, size_t *first_part_len_ret);
240
241 extern const char *
242 path_basename(const char *path);
243
244 extern const char *
245 path_stream_name(const char *path);
246
247 extern void
248 to_parent_name(char buf[], size_t len);
249
250 extern void
251 print_string(const void *string, size_t len);
252
253 extern int
254 get_num_path_components(const char *path);
255
256 static inline void
257 print_byte_field(const u8 field[], size_t len)
258 {
259         while (len--)
260                 printf("%02hhx", *field++);
261 }
262
263 static inline u32
264 bsr32(u32 n)
265 {
266 #if defined(__x86__) || defined(__x86_64__)
267         asm("bsrl %0, %0;"
268                         : "=r"(n)
269                         : "0" (n));
270         return n;
271 #else
272         u32 pow = 0;
273         while ((n >>= 1) != 0)
274                 pow++;
275         return pow;
276 #endif
277 }
278
279 extern int
280 wimlib_fprintf(FILE *fp, const char *format, ...) FORMAT(printf, 2, 3);
281
282 extern int
283 wimlib_printf(const char *format, ...) FORMAT(printf, 1, 2);
284
285 #endif /* _WIMLIB_UTIL_H */