]> wimlib.net Git - wimlib/blob - src/util.h
09a64db328fa14856ed834b9fe8107a0deba3b2c
[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
12 typedef uint8_t  u8;
13 typedef uint16_t u16;
14 typedef uint32_t u32;
15 typedef uint64_t u64;
16 typedef unsigned uint;
17
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;})
23
24 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
25
26 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
27
28 /* Used for buffering FILE IO in a few places */
29 #define BUFFER_SIZE 4096
30
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
37 #else
38 #  define ERROR(format, ...)
39 #  define WARNING(format, ...)
40 #endif /* ENABLE_ERROR_MESSAGES */
41
42 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
43 #include <errno.h>
44 #  define DEBUG(format, ...)  \
45 ({ \
46         int __errno_save = errno; \
47         fprintf(stdout, "[%s %d] %s(): " format, \
48                 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
49         fflush(stdout); \
50         errno = __errno_save; \
51         })
52
53 #else
54 #  define DEBUG(format, ...)
55 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
56
57 #ifdef ENABLE_MORE_DEBUG
58 #  define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
59 #else
60 #  define DEBUG2(format, ...)
61 #endif /* ENABLE_DEBUG */
62
63 #ifdef ENABLE_ASSERTIONS
64 #include <assert.h>
65 #       define wimlib_assert(expr) assert(expr)
66 #else
67 #       define wimlib_assert(expr)
68 #endif
69
70 #ifdef __GNUC__
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))
76 #else
77 #  define WIMLIBAPI
78 #  define NOINLINE
79 #  define ALWAYS_INLINE inline
80 #  define COLD
81 #  define HOT
82 #endif /* __GNUC__ */
83
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
95 #else
96 #include <stdlib.h>
97 #include <string.h>
98 #  define MALLOC malloc
99 #  define FREE free
100 #  define REALLOC realloc
101 #  define CALLOC calloc
102 #  define STRDUP strdup
103 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
104
105
106 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
107                            size_t *utf8_len_ret);
108
109 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len, 
110                            size_t *utf16_len_ret);
111
112 extern void randomize_byte_array(void *p, size_t n);
113
114 extern void randomize_char_array_with_alnum(char p[], size_t n);
115
116 extern int sha1sum(const char *filename, void *buf);
117
118 extern const char *path_next_part(const char *path, 
119                                   size_t *first_part_len_ret);
120
121 extern const char *path_basename(const char *path);
122
123 extern void to_parent_name(char buf[], size_t len);
124
125 extern void print_string(const void *string, size_t len);
126
127 extern int get_num_path_components(const char *path);
128
129 extern ssize_t full_write(int fd, const void *buf, size_t n);
130
131
132 static inline void print_byte_field(const u8 field[], size_t len)
133 {
134         while (len--)
135                 printf("%02hhx", *field++);
136 }
137
138
139 #endif /* _WIMLIB_UTIL_H */