blob: c4cbe48f5ee60cc5ab47ad11b3e95308f40fd59b [file] [log] [blame]
Eric Biggers431c67b2018-06-27 15:01:06 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Utility functions and macros for the 'fsverity' program
4 *
Eric Biggers8387ad32018-08-21 12:37:56 -07005 * Copyright (C) 2018 Google LLC
Eric Biggers431c67b2018-06-27 15:01:06 -07006 */
7#ifndef UTIL_H
8#define UTIL_H
9
10#include <inttypes.h>
Adam Langley8957d752018-08-06 16:11:22 -070011#include <stdarg.h>
Eric Biggers431c67b2018-06-27 15:01:06 -070012#include <stdbool.h>
13#include <stddef.h>
14
15typedef uint8_t u8;
16typedef uint16_t u16;
17typedef uint32_t u32;
18typedef uint64_t u64;
19
Eric Biggersb3a3a232018-09-05 13:56:24 -070020#ifndef __force
21# ifdef __CHECKER__
22# define __force __attribute__((force))
23# else
24# define __force
25# endif
Eric Biggers431c67b2018-06-27 15:01:06 -070026#endif
27
Eric Biggersb3a3a232018-09-05 13:56:24 -070028#ifndef __printf
29# define __printf(fmt_idx, vargs_idx) \
Eric Biggers431c67b2018-06-27 15:01:06 -070030 __attribute__((format(printf, fmt_idx, vargs_idx)))
Eric Biggersb3a3a232018-09-05 13:56:24 -070031#endif
Eric Biggers431c67b2018-06-27 15:01:06 -070032
Eric Biggersb3a3a232018-09-05 13:56:24 -070033#ifndef __noreturn
34# define __noreturn __attribute__((noreturn))
35#endif
36
37#ifndef __cold
38# define __cold __attribute__((cold))
39#endif
Eric Biggers431c67b2018-06-27 15:01:06 -070040
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 ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
53
54#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
55
56/*
57 * Round 'v' up to the next 'alignment'-byte aligned boundary.
58 * 'alignment' must be a power of 2.
59 */
60#define ALIGN(v, alignment) (((v) + ((alignment) - 1)) & ~((alignment) - 1))
61
62static inline bool is_power_of_2(unsigned long n)
63{
64 return n != 0 && ((n & (n - 1)) == 0);
65}
66
67static inline int ilog2(unsigned long n)
68{
69 return (8 * sizeof(n) - 1) - __builtin_clzl(n);
70}
71
72/* ========== Endianness conversion ========== */
73
74#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
75# define cpu_to_le16(v) ((__force __le16)(u16)(v))
76# define le16_to_cpu(v) ((__force u16)(__le16)(v))
77# define cpu_to_le32(v) ((__force __le32)(u32)(v))
78# define le32_to_cpu(v) ((__force u32)(__le32)(v))
79# define cpu_to_le64(v) ((__force __le64)(u64)(v))
80# define le64_to_cpu(v) ((__force u64)(__le64)(v))
81# define cpu_to_be16(v) ((__force __be16)__builtin_bswap16(v))
82# define be16_to_cpu(v) (__builtin_bswap16((__force u16)(v)))
83# define cpu_to_be32(v) ((__force __be32)__builtin_bswap32(v))
84# define be32_to_cpu(v) (__builtin_bswap32((__force u32)(v)))
85# define cpu_to_be64(v) ((__force __be64)__builtin_bswap64(v))
86# define be64_to_cpu(v) (__builtin_bswap64((__force u64)(v)))
87#else
88# define cpu_to_le16(v) ((__force __le16)__builtin_bswap16(v))
89# define le16_to_cpu(v) (__builtin_bswap16((__force u16)(v)))
90# define cpu_to_le32(v) ((__force __le32)__builtin_bswap32(v))
91# define le32_to_cpu(v) (__builtin_bswap32((__force u32)(v)))
92# define cpu_to_le64(v) ((__force __le64)__builtin_bswap64(v))
93# define le64_to_cpu(v) (__builtin_bswap64((__force u64)(v)))
94# define cpu_to_be16(v) ((__force __be16)(u16)(v))
95# define be16_to_cpu(v) ((__force u16)(__be16)(v))
96# define cpu_to_be32(v) ((__force __be32)(u32)(v))
97# define be32_to_cpu(v) ((__force u32)(__be32)(v))
98# define cpu_to_be64(v) ((__force __be64)(u64)(v))
99# define be64_to_cpu(v) ((__force u64)(__be64)(v))
100#endif
101
102/* ========== Memory allocation ========== */
103
104void *xmalloc(size_t size);
105void *xzalloc(size_t size);
106void *xmemdup(const void *mem, size_t size);
107char *xstrdup(const char *s);
108__printf(1, 2) char *xasprintf(const char *format, ...);
109
110/* ========== Error messages and assertions ========== */
111
Adam Langley8957d752018-08-06 16:11:22 -0700112__cold void do_error_msg(const char *format, va_list va, int err);
Eric Biggers431c67b2018-06-27 15:01:06 -0700113__printf(1, 2) __cold void error_msg(const char *format, ...);
114__printf(1, 2) __cold void error_msg_errno(const char *format, ...);
115__printf(1, 2) __cold __noreturn void fatal_error(const char *format, ...);
116__cold __noreturn void assertion_failed(const char *expr,
117 const char *file, int line);
118
119#define ASSERT(e) ({ if (!(e)) assertion_failed(#e, __FILE__, __LINE__); })
120
121/* ========== File utilities ========== */
122
123struct filedes {
124 int fd;
125 bool autodelete; /* unlink when closed? */
126 char *name; /* filename, for logging or error messages */
127 u64 pos; /* lseek() position */
128};
129
130bool open_file(struct filedes *file, const char *filename, int flags, int mode);
131bool open_tempfile(struct filedes *file);
132bool get_file_size(struct filedes *file, u64 *size_ret);
133bool filedes_seek(struct filedes *file, u64 pos, int whence);
134bool full_read(struct filedes *file, void *buf, size_t count);
135bool full_pread(struct filedes *file, void *buf, size_t count, u64 offset);
136bool full_write(struct filedes *file, const void *buf, size_t count);
137bool full_pwrite(struct filedes *file, const void *buf, size_t count,
138 u64 offset);
139bool copy_file_data(struct filedes *src, struct filedes *dst, u64 length);
140bool write_zeroes(struct filedes *file, u64 length);
141bool filedes_close(struct filedes *file);
142
143/* ========== String utilities ========== */
144
145bool hex2bin(const char *hex, u8 *bin, size_t bin_len);
146void bin2hex(const u8 *bin, size_t bin_len, char *hex);
147
148struct string_list {
149 char **strings;
150 size_t length;
151 size_t capacity;
152};
153
154#define STRING_LIST_INITIALIZER { .strings = NULL, .length = 0, .capacity = 0 }
155#define STRING_LIST(_list) struct string_list _list = STRING_LIST_INITIALIZER
156
157void string_list_append(struct string_list *list, char *string);
158void string_list_destroy(struct string_list *list);
159
160#endif /* UTIL_H */