]> wimlib.net Git - wimlib/blob - src/util.h
Initial commit (current version is wimlib 0.6.2)
[wimlib] / src / util.h
1 /*
2  * util.h
3  *
4  * Header for util.c.
5  *
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24 #ifndef _WIMLIB_UTIL_H
25 #define _WIMLIB_UTIL_H
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <inttypes.h>
31 #include "config.h"
32
33
34 typedef uint8_t  u8;
35 typedef uint16_t u16;
36 typedef uint32_t u32;
37 typedef uint64_t u64;
38 typedef unsigned uint;
39
40 #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
41                                         (__a < __b) ? __a : __b; })
42 #define max(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
43                                         (__a > __b) ? __a : __b; })
44 #define swap(a, b) ({typeof(a) _a = a; (a) = (b); (b) = _a;})
45
46 #define ARRAY_LEN(array) (sizeof(array) / sizeof((array)[0]))
47
48 #define ZERO_ARRAY(array) memset(array, 0, sizeof(array))
49
50 #ifdef ENABLE_ERROR_MESSAGES
51 extern bool __wimlib_print_errors;
52 extern void wimlib_error(const char *format, ...);
53 #  define ERROR wimlib_error
54 #else
55 #  define ERROR(format, ...)
56 #endif /* ENABLE_ERROR_MESSAGES */
57
58 #if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG)
59 #include <errno.h>
60 #  define DEBUG(format, ...)  \
61 ({ \
62         int __errno_save = errno; \
63         fprintf(stdout, "[%s %d] %s(): " format, \
64                 __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
65         fflush(stdout); \
66         errno = __errno_save; \
67         })
68
69 #else
70 #  define DEBUG(format, ...)
71 #endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */
72
73 #ifdef ENABLE_MORE_DEBUG
74 #  define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__)
75 #else
76 #  define DEBUG2(format, ...)
77 #endif /* ENABLE_DEBUG */
78
79 #ifdef ENABLE_ASSERTIONS
80 #include <assert.h>
81 #       define wimlib_assert(expr) assert(expr)
82 #else
83 #       define wimlib_assert(expr)
84 #endif
85
86 #ifdef __GNUC__
87 #  define WIMLIBAPI __attribute__((visibility("default")))
88 #  define NOINLINE __attribute__((noinline))
89 #  define ALWAYS_INLINE inline __attribute__((always_inline))
90 #  define COLD     __attribute__((cold))
91 #  define HOT      __attribute__((hot))
92 #else
93 #  define WIMLIBAPI
94 #  define NOINLINE
95 #  define ALWAYS_INLINE inline
96 #  define COLD
97 #  define HOT
98 #endif /* __GNUC__ */
99
100 #ifdef ENABLE_CUSTOM_MEMORY_ALLOCATOR
101 extern void *(*wimlib_malloc_func)(size_t);
102 extern void (*wimlib_free_func)(void *);
103 extern void *(*wimlib_realloc)(void *, size_t);
104 extern void *wimlib_calloc(size_t nmemb, size_t size);
105 extern char *wimlib_strdup(const char *str);
106 #  define MALLOC wimlib_malloc_func
107 #  define FREE wimlib_free_func
108 #  define REALLOC wimlib_realloc_func
109 #  define CALLOC wimlib_calloc
110 #  define STRDUP wimlib_strdup
111 #else
112 #include <stdlib.h>
113 #include <string.h>
114 #  define MALLOC malloc
115 #  define FREE free
116 #  define REALLOC realloc
117 #  define CALLOC calloc
118 #  define STRDUP strdup
119 #endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */
120
121
122 extern char *utf16_to_utf8(const char *utf16_str, size_t utf16_len,
123                            size_t *utf8_len_ret);
124
125 extern char *utf8_to_utf16(const char *utf8_str, size_t utf8_len, 
126                            size_t *utf16_len_ret);
127
128 extern void randomize_byte_array(void *p, size_t n);
129
130 extern void randomize_char_array_with_alnum(char p[], size_t n);
131
132 extern int sha1sum(const char *filename, void *buf);
133
134 extern const char *path_next_part(const char *path, 
135                                   size_t *first_part_len_ret);
136
137 extern const char *path_basename(const char *path);
138
139 extern void to_parent_name(char buf[], size_t len);
140
141 extern void print_string(const void *string, size_t len);
142
143 extern int get_num_path_components(const char *path);
144
145 extern ssize_t full_write(int fd, const void *buf, size_t n);
146
147
148 static inline void print_byte_field(const u8 field[], size_t len)
149 {
150         while (len--)
151                 printf("%02hhx", *field++);
152 }
153
154
155 #endif /* _WIMLIB_UTIL_H */