xref: /aosp_15_r20/external/fsverity-utils/programs/utils.h (revision b13c0e4024008a1f948ee8189745cb3371f4ac04)
1*b13c0e40SEric Biggers /* SPDX-License-Identifier: MIT */
2*b13c0e40SEric Biggers /*
3*b13c0e40SEric Biggers  * Utility functions for programs
4*b13c0e40SEric Biggers  *
5*b13c0e40SEric Biggers  * Copyright 2018 Google LLC
6*b13c0e40SEric Biggers  *
7*b13c0e40SEric Biggers  * Use of this source code is governed by an MIT-style
8*b13c0e40SEric Biggers  * license that can be found in the LICENSE file or at
9*b13c0e40SEric Biggers  * https://opensource.org/licenses/MIT.
10*b13c0e40SEric Biggers  */
11*b13c0e40SEric Biggers #ifndef PROGRAMS_UTILS_H
12*b13c0e40SEric Biggers #define PROGRAMS_UTILS_H
13*b13c0e40SEric Biggers 
14*b13c0e40SEric Biggers #include "libfsverity.h"
15*b13c0e40SEric Biggers #include "../common/common_defs.h"
16*b13c0e40SEric Biggers 
17*b13c0e40SEric Biggers #include <stdio.h>
18*b13c0e40SEric Biggers #include <stdlib.h>
19*b13c0e40SEric Biggers #include <string.h>
20*b13c0e40SEric Biggers 
21*b13c0e40SEric Biggers void *xmalloc(size_t size);
22*b13c0e40SEric Biggers void *xzalloc(size_t size);
23*b13c0e40SEric Biggers void *xmemdup(const void *mem, size_t size);
24*b13c0e40SEric Biggers char *xstrdup(const char *s);
25*b13c0e40SEric Biggers 
26*b13c0e40SEric Biggers __printf(1, 2) __cold void error_msg(const char *format, ...);
27*b13c0e40SEric Biggers __printf(1, 2) __cold void error_msg_errno(const char *format, ...);
28*b13c0e40SEric Biggers __printf(1, 2) __cold __noreturn void fatal_error(const char *format, ...);
29*b13c0e40SEric Biggers __cold __noreturn void assertion_failed(const char *expr,
30*b13c0e40SEric Biggers 					const char *file, int line);
31*b13c0e40SEric Biggers 
32*b13c0e40SEric Biggers #define ASSERT(e) ({ if (!(e)) assertion_failed(#e, __FILE__, __LINE__); })
33*b13c0e40SEric Biggers 
34*b13c0e40SEric Biggers void install_libfsverity_error_handler(void);
35*b13c0e40SEric Biggers 
36*b13c0e40SEric Biggers struct filedes {
37*b13c0e40SEric Biggers 	int fd;
38*b13c0e40SEric Biggers 	char *name;		/* filename, for logging or error messages */
39*b13c0e40SEric Biggers };
40*b13c0e40SEric Biggers 
41*b13c0e40SEric Biggers bool open_file(struct filedes *file, const char *filename, int flags, int mode);
42*b13c0e40SEric Biggers bool get_file_size(struct filedes *file, u64 *size_ret);
43*b13c0e40SEric Biggers bool preallocate_file(struct filedes *file, u64 size);
44*b13c0e40SEric Biggers bool full_read(struct filedes *file, void *buf, size_t count);
45*b13c0e40SEric Biggers bool full_write(struct filedes *file, const void *buf, size_t count);
46*b13c0e40SEric Biggers bool full_pwrite(struct filedes *file, const void *buf, size_t count,
47*b13c0e40SEric Biggers 		 u64 offset);
48*b13c0e40SEric Biggers bool filedes_close(struct filedes *file);
49*b13c0e40SEric Biggers int read_callback(void *file, void *buf, size_t count);
50*b13c0e40SEric Biggers 
51*b13c0e40SEric Biggers bool hex2bin(const char *hex, u8 *bin, size_t bin_len);
52*b13c0e40SEric Biggers void bin2hex(const u8 *bin, size_t bin_len, char *hex);
53*b13c0e40SEric Biggers 
54*b13c0e40SEric Biggers #endif /* PROGRAMS_UTILS_H */
55