]> wimlib.net Git - wimlib/blob - src/util.h
util.h: gcc printf format attribute
[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 typedef uint8_t  u8;
34 typedef uint16_t u16;
35 typedef uint32_t u32;
36 typedef uint64_t u64;
37 typedef unsigned uint;
38
39 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
40                                         (__a < __b) ? __a : __b; })
41 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
42                                         (__a > __b) ? __a : __b; })
43 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
44
45 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
46
47 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
48
49 /* Used for buffering FILE IO in a few places */
50 #define BUFFER_SIZE 4096
51
52 #ifdef ENABLE_ERROR_MESSAGES
53 extern bool __wimlib_print_errors;
54 extern void wimlib_error(const char *format, ...) 
55                 FORMAT(printf, 1, 2) COLD;
56 extern void wimlib_warning(const char *format, ...)
57                 FORMAT(printf, 1, 2) COLD;
58 #       define ERROR wimlib_error
59 #       define WARNING wimlib_warning
60 #else
61 #       define ERROR(format, ...)
62 #       define WARNING(format, ...)
63 #endif /* ENABLE_ERROR_MESSAGES */
64
65 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
66 #       include <errno.h>
67 #       define DEBUG(format, ...)  \
68         ({ \
69                 int __errno_save = errno; \
70                 fprintf(stdout, "[%s %d] %s(): " format, \
71                         __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
72                 fflush(stdout); \
73                 errno = __errno_save; \
74         })
75
76 #else
77 #       define DEBUG(format, ...)
78 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
79
80 #ifdef ENABLE_MORE_DEBUG
81 #       define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
82 #else
83 #       define DEBUG2(format, ...)
84 #endif /* ENABLE_DEBUG */
85
86 #ifdef ENABLE_ASSERTIONS
87 #include <assert.h>
88 #       define wimlib_assert(expr) assert(expr)
89 #else
90 #       define wimlib_assert(expr)
91 #endif
92
93
94 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
95 extern void *(*wimlib_malloc_func)(size_t);
96 extern void (*wimlib_free_func)(void *);
97 extern void *(*wimlib_realloc)(void *, size_t);
98 extern void *wimlib_calloc(size_t nmemb, size_t size);
99 extern char *wimlib_strdup(const char *str);
100 #       define  MALLOC  wimlib_malloc_func
101 #       define  FREE    wimlib_free_func
102 #       define  REALLOC wimlib_realloc_func
103 #       define  CALLOC  wimlib_calloc
104 #       define  STRDUP  wimlib_strdup
105 #else
106 #       include <stdlib.h>
107 #       include <string.h>
108 #       define  MALLOC  malloc
109 #       define  FREE    free
110 #       define  REALLOC realloc
111 #       define  CALLOC  calloc
112 #       define  STRDUP  strdup
113 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
114
115
116 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
117                            size_t *utf8_len_ret);
118
119 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len, 
120                            size_t *utf16_len_ret);
121
122 extern void randomize_byte_array(void *p, size_t n);
123
124 extern void randomize_char_array_with_alnum(char p[], size_t n);
125
126 extern int sha1sum(const char *filename, void *buf);
127
128 extern const char *path_next_part(const char *path, 
129                                   size_t *first_part_len_ret);
130
131 extern const char *path_basename(const char *path);
132
133 extern void to_parent_name(char buf[], size_t len);
134
135 extern void print_string(const void *string, size_t len);
136
137 extern int get_num_path_components(const char *path);
138
139 extern ssize_t full_write(int fd, const void *buf, size_t n);
140
141 static inline void print_byte_field(const u8 field[], size_t len)
142 {
143         while (len--)
144                 printf("%02hhx", *field++);
145 }
146
147
148 #endif /* _WIMLIB_UTIL_H */