]> wimlib.net Git - wimlib/blob - src/util.h
extract_wim_resource() refactor
[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 #       define WIMLIBAPI __attribute__((visibility("default")))
13 #       define NOINLINE __attribute__((noinline))
14 #       define ALWAYS_INLINE inline __attribute__((always_inline))
15 #       define FORMAT(type, format_str, args_start) \
16                         __attribute__((format(type, format_str, args_start)))
17 #       if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
18 #               define COLD     __attribute__((cold))
19 #               define HOT      __attribute__((hot))
20 #       else
21 #               define COLD
22 #               define HOT
23 #       endif
24 #else
25 #       define WIMLIBAPI
26 #       define NOINLINE
27 #       define ALWAYS_INLINE inline
28 #       define FORMAT(type, format_str, args_start)
29 #       define COLD
30 #       define HOT
31 #endif /* __GNUC__ */
32
33
34 #if 0
35 #ifdef WITH_FUSE
36 #define atomic_inc(ptr) \
37         __sync_fetch_and_add(ptr, 1)
38
39 #define atomic_dec(ptr) \
40         __sync_sub_and_fetch(ptr, 1)
41 #endif
42 #endif
43
44 #ifndef _NTFS_TYPES_H
45 typedef uint8_t  u8;
46 typedef uint16_t u16;
47 typedef uint32_t u32;
48 typedef uint64_t u64;
49 #endif
50 typedef unsigned uint;
51
52 #ifndef min
53 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
54                                         (__a < __b) ? __a : __b; })
55 #endif
56
57 #ifndef max
58 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
59                                         (__a > __b) ? __a : __b; })
60 #endif
61
62 #ifndef swap
63 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
64 #endif
65
66 /**
67  * container_of - cast a member of a structure out to the containing structure
68  * @ptr:        the pointer to the member.
69  * @type:       the type of the container struct this is embedded in.
70  * @member:     the name of the member within the struct.
71  *
72  */
73 #ifndef container_of
74 #define container_of(ptr, type, member) ({                      \
75         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
76         (type *)( (char *)__mptr - offsetof(type,member) );})
77 #endif
78
79 #define DIV_ROUND_UP(numerator, denominator) \
80         (((numerator) + (denominator) - 1) / (denominator))
81
82 #define MODULO_NONZERO(numerator, denominator) \
83         (((numerator) % (denominator)) ? ((numerator) % (denominator)) : (denominator))
84
85 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
86
87 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
88
89 /* Used for buffering FILE IO in a few places */
90 #define BUFFER_SIZE 4096
91
92 #ifdef ENABLE_ERROR_MESSAGES
93 extern bool __wimlib_print_errors;
94 extern void wimlib_error(const char *format, ...)
95                 FORMAT(printf, 1, 2) COLD;
96 extern void wimlib_error_with_errno(const char *format, ...)
97                 FORMAT(printf, 1, 2) COLD;
98 extern void wimlib_warning(const char *format, ...)
99                 FORMAT(printf, 1, 2) COLD;
100 #       define ERROR            wimlib_error
101 #       define ERROR_WITH_ERRNO wimlib_error_with_errno
102 #       define WARNING          wimlib_warning
103 #else
104 #       define ERROR(format, ...)
105 #       define ERROR_WITH_ERRNO(format, ...)
106 #       define WARNING(format, ...)
107 #endif /* ENABLE_ERROR_MESSAGES */
108
109 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
110 #       include <errno.h>
111 #       define DEBUG(format, ...)                                       \
112         ({                                                              \
113                 int __errno_save = errno;                               \
114                 fprintf(stdout, "[%s %d] %s(): " format,                \
115                         __FILE__, __LINE__, __func__, ## __VA_ARGS__);  \
116                 putchar('\n');                                          \
117                 fflush(stdout);                                         \
118                 errno = __errno_save;                                   \
119         })
120
121 #else
122 #       define DEBUG(format, ...)
123 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
124
125 #ifdef ENABLE_MORE_DEBUG
126 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
127 #else
128 #       define DEBUG2(format, ...)
129 #endif /* ENABLE_DEBUG */
130
131 #ifdef ENABLE_ASSERTIONS
132 #include <assert.h>
133 #       define wimlib_assert(expr) assert(expr)
134 #else
135 #       define wimlib_assert(expr)
136 #endif
137
138
139 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
140 extern void *(*wimlib_malloc_func)(size_t);
141 extern void (*wimlib_free_func)(void *);
142 extern void *(*wimlib_realloc_func)(void *, size_t);
143 extern void *wimlib_calloc(size_t nmemb, size_t size);
144 extern char *wimlib_strdup(const char *str);
145 #       define  MALLOC  wimlib_malloc_func
146 #       define  FREE    wimlib_free_func
147 #       define  REALLOC wimlib_realloc_func
148 #       define  CALLOC  wimlib_calloc
149 #       define  STRDUP  wimlib_strdup
150 #else
151 #       include <stdlib.h>
152 #       include <string.h>
153 #       define  MALLOC  malloc
154 #       define  FREE    free
155 #       define  REALLOC realloc
156 #       define  CALLOC  calloc
157 #       define  STRDUP  strdup
158 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
159
160
161 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
162                            size_t *utf8_len_ret);
163
164 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len,
165                            size_t *utf16_len_ret);
166
167 extern void randomize_byte_array(u8 *p, size_t n);
168
169 extern void randomize_char_array_with_alnum(char p[], size_t n);
170
171 extern const char *path_next_part(const char *path,
172                                   size_t *first_part_len_ret);
173
174 extern const char *path_basename(const char *path);
175
176 extern const char *path_stream_name(const char *path);
177
178 extern void to_parent_name(char buf[], size_t len);
179
180 extern void print_string(const void *string, size_t len);
181
182 extern int get_num_path_components(const char *path);
183
184 static inline void print_byte_field(const u8 field[], size_t len)
185 {
186         while (len--)
187                 printf("%02hhx", *field++);
188 }
189
190 static inline u32 bsr32(u32 n)
191 {
192 #if defined(__x86__) || defined(__x86_64__)
193         asm("bsrl %0, %0;"
194                         : "=r"(n)
195                         : "0" (n));
196         return n;
197 #else
198         u32 pow = 0;
199         while ((n >>= 1) != 0)
200                 pow++;
201         return pow;
202 #endif
203 }
204
205 #endif /* _WIMLIB_UTIL_H */