blob: 049d770156fb657c8dda835c8a0a83850e898f93 [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
20#ifdef __CHECKER__
21# define __force __attribute__((force))
22#else
23# define __force
24#endif
25
26#define __printf(fmt_idx, vargs_idx) \
27 __attribute__((format(printf, fmt_idx, vargs_idx)))
28
29#define __noreturn __attribute__((noreturn))
30#define __cold __attribute__((cold))
31
32#define min(a, b) ({ \
33 __typeof__(a) _a = (a); \
34 __typeof__(b) _b = (b); \
35 _a < _b ? _a : _b; \
36})
37#define max(a, b) ({ \
38 __typeof__(a) _a = (a); \
39 __typeof__(b) _b = (b); \
40 _a > _b ? _a : _b; \
41})
42
43#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
44
45#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
46
47/*
48 * Round 'v' up to the next 'alignment'-byte aligned boundary.
49 * 'alignment' must be a power of 2.
50 */
51#define ALIGN(v, alignment) (((v) + ((alignment) - 1)) & ~((alignment) - 1))
52
53static inline bool is_power_of_2(unsigned long n)
54{
55 return n != 0 && ((n & (n - 1)) == 0);
56}
57
58static inline int ilog2(unsigned long n)
59{
60 return (8 * sizeof(n) - 1) - __builtin_clzl(n);
61}
62
63/* ========== Endianness conversion ========== */
64
65#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
66# define cpu_to_le16(v) ((__force __le16)(u16)(v))
67# define le16_to_cpu(v) ((__force u16)(__le16)(v))
68# define cpu_to_le32(v) ((__force __le32)(u32)(v))
69# define le32_to_cpu(v) ((__force u32)(__le32)(v))
70# define cpu_to_le64(v) ((__force __le64)(u64)(v))
71# define le64_to_cpu(v) ((__force u64)(__le64)(v))
72# define cpu_to_be16(v) ((__force __be16)__builtin_bswap16(v))
73# define be16_to_cpu(v) (__builtin_bswap16((__force u16)(v)))
74# define cpu_to_be32(v) ((__force __be32)__builtin_bswap32(v))
75# define be32_to_cpu(v) (__builtin_bswap32((__force u32)(v)))
76# define cpu_to_be64(v) ((__force __be64)__builtin_bswap64(v))
77# define be64_to_cpu(v) (__builtin_bswap64((__force u64)(v)))
78#else
79# define cpu_to_le16(v) ((__force __le16)__builtin_bswap16(v))
80# define le16_to_cpu(v) (__builtin_bswap16((__force u16)(v)))
81# define cpu_to_le32(v) ((__force __le32)__builtin_bswap32(v))
82# define le32_to_cpu(v) (__builtin_bswap32((__force u32)(v)))
83# define cpu_to_le64(v) ((__force __le64)__builtin_bswap64(v))
84# define le64_to_cpu(v) (__builtin_bswap64((__force u64)(v)))
85# define cpu_to_be16(v) ((__force __be16)(u16)(v))
86# define be16_to_cpu(v) ((__force u16)(__be16)(v))
87# define cpu_to_be32(v) ((__force __be32)(u32)(v))
88# define be32_to_cpu(v) ((__force u32)(__be32)(v))
89# define cpu_to_be64(v) ((__force __be64)(u64)(v))
90# define be64_to_cpu(v) ((__force u64)(__be64)(v))
91#endif
92
93/* ========== Memory allocation ========== */
94
95void *xmalloc(size_t size);
96void *xzalloc(size_t size);
97void *xmemdup(const void *mem, size_t size);
98char *xstrdup(const char *s);
99__printf(1, 2) char *xasprintf(const char *format, ...);
100
101/* ========== Error messages and assertions ========== */
102
Adam Langley8957d752018-08-06 16:11:22 -0700103__cold void do_error_msg(const char *format, va_list va, int err);
Eric Biggers431c67b2018-06-27 15:01:06 -0700104__printf(1, 2) __cold void error_msg(const char *format, ...);
105__printf(1, 2) __cold void error_msg_errno(const char *format, ...);
106__printf(1, 2) __cold __noreturn void fatal_error(const char *format, ...);
107__cold __noreturn void assertion_failed(const char *expr,
108 const char *file, int line);
109
110#define ASSERT(e) ({ if (!(e)) assertion_failed(#e, __FILE__, __LINE__); })
111
112/* ========== File utilities ========== */
113
114struct filedes {
115 int fd;
116 bool autodelete; /* unlink when closed? */
117 char *name; /* filename, for logging or error messages */
118 u64 pos; /* lseek() position */
119};
120
121bool open_file(struct filedes *file, const char *filename, int flags, int mode);
122bool open_tempfile(struct filedes *file);
123bool get_file_size(struct filedes *file, u64 *size_ret);
124bool filedes_seek(struct filedes *file, u64 pos, int whence);
125bool full_read(struct filedes *file, void *buf, size_t count);
126bool full_pread(struct filedes *file, void *buf, size_t count, u64 offset);
127bool full_write(struct filedes *file, const void *buf, size_t count);
128bool full_pwrite(struct filedes *file, const void *buf, size_t count,
129 u64 offset);
130bool copy_file_data(struct filedes *src, struct filedes *dst, u64 length);
131bool write_zeroes(struct filedes *file, u64 length);
132bool filedes_close(struct filedes *file);
133
134/* ========== String utilities ========== */
135
136bool hex2bin(const char *hex, u8 *bin, size_t bin_len);
137void bin2hex(const u8 *bin, size_t bin_len, char *hex);
138
139struct string_list {
140 char **strings;
141 size_t length;
142 size_t capacity;
143};
144
145#define STRING_LIST_INITIALIZER { .strings = NULL, .length = 0, .capacity = 0 }
146#define STRING_LIST(_list) struct string_list _list = STRING_LIST_INITIALIZER
147
148void string_list_append(struct string_list *list, char *string);
149void string_list_destroy(struct string_list *list);
150
151#endif /* UTIL_H */