• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Utility functions and macros for the 'fsverity' program
4  *
5  * Copyright (C) 2018 Google LLC
6  */
7 #ifndef UTIL_H
8 #define UTIL_H
9 
10 #include <inttypes.h>
11 #include <stdarg.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 
15 typedef uint8_t u8;
16 typedef uint16_t u16;
17 typedef uint32_t u32;
18 typedef uint64_t u64;
19 
20 #ifndef __force
21 #  ifdef __CHECKER__
22 #    define __force	__attribute__((force))
23 #  else
24 #    define __force
25 #  endif
26 #endif
27 
28 #ifndef __printf
29 #  define __printf(fmt_idx, vargs_idx) \
30 	__attribute__((format(printf, fmt_idx, vargs_idx)))
31 #endif
32 
33 #ifndef __noreturn
34 #  define __noreturn	__attribute__((noreturn))
35 #endif
36 
37 #ifndef __cold
38 #  define __cold	__attribute__((cold))
39 #endif
40 
41 #define min(a, b) ({			\
42 	__typeof__(a) _a = (a);		\
43 	__typeof__(b) _b = (b);		\
44 	_a < _b ? _a : _b;		\
45 })
46 #define max(a, b) ({			\
47 	__typeof__(a) _a = (a);		\
48 	__typeof__(b) _b = (b);		\
49 	_a > _b ? _a : _b;		\
50 })
51 
52 #define roundup(x, y) ({		\
53 	__typeof__(y) _y = (y);		\
54 	(((x) + _y - 1) / _y) * _y;	\
55 })
56 
57 #define ARRAY_SIZE(A)		(sizeof(A) / sizeof((A)[0]))
58 
59 #define DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
60 
is_power_of_2(unsigned long n)61 static inline bool is_power_of_2(unsigned long n)
62 {
63 	return n != 0 && ((n & (n - 1)) == 0);
64 }
65 
ilog2(unsigned long n)66 static inline int ilog2(unsigned long n)
67 {
68 	return (8 * sizeof(n) - 1) - __builtin_clzl(n);
69 }
70 
71 /* ========== Endianness conversion ========== */
72 
73 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
74 #  define cpu_to_le16(v)	((__force __le16)(u16)(v))
75 #  define le16_to_cpu(v)	((__force u16)(__le16)(v))
76 #  define cpu_to_le32(v)	((__force __le32)(u32)(v))
77 #  define le32_to_cpu(v)	((__force u32)(__le32)(v))
78 #  define cpu_to_le64(v)	((__force __le64)(u64)(v))
79 #  define le64_to_cpu(v)	((__force u64)(__le64)(v))
80 #else
81 #  define cpu_to_le16(v)	((__force __le16)__builtin_bswap16(v))
82 #  define le16_to_cpu(v)	(__builtin_bswap16((__force u16)(v)))
83 #  define cpu_to_le32(v)	((__force __le32)__builtin_bswap32(v))
84 #  define le32_to_cpu(v)	(__builtin_bswap32((__force u32)(v)))
85 #  define cpu_to_le64(v)	((__force __le64)__builtin_bswap64(v))
86 #  define le64_to_cpu(v)	(__builtin_bswap64((__force u64)(v)))
87 #endif
88 
89 /* ========== Memory allocation ========== */
90 
91 void *xmalloc(size_t size);
92 void *xzalloc(size_t size);
93 void *xmemdup(const void *mem, size_t size);
94 char *xstrdup(const char *s);
95 
96 /* ========== Error messages and assertions ========== */
97 
98 __cold void do_error_msg(const char *format, va_list va, int err);
99 __printf(1, 2) __cold void error_msg(const char *format, ...);
100 __printf(1, 2) __cold void error_msg_errno(const char *format, ...);
101 __printf(1, 2) __cold __noreturn void fatal_error(const char *format, ...);
102 __cold __noreturn void assertion_failed(const char *expr,
103 					const char *file, int line);
104 
105 #define ASSERT(e) ({ if (!(e)) assertion_failed(#e, __FILE__, __LINE__); })
106 
107 /* ========== File utilities ========== */
108 
109 struct filedes {
110 	int fd;
111 	char *name;		/* filename, for logging or error messages */
112 };
113 
114 bool open_file(struct filedes *file, const char *filename, int flags, int mode);
115 bool get_file_size(struct filedes *file, u64 *size_ret);
116 bool full_read(struct filedes *file, void *buf, size_t count);
117 bool full_write(struct filedes *file, const void *buf, size_t count);
118 bool filedes_close(struct filedes *file);
119 
120 /* ========== String utilities ========== */
121 
122 bool hex2bin(const char *hex, u8 *bin, size_t bin_len);
123 void bin2hex(const u8 *bin, size_t bin_len, char *hex);
124 
125 #endif /* UTIL_H */
126