Fix UB: Make read_int() follow SEI CERT INT13-C and INT34-C.
[metastore.git] / src / utils.h
blob586a7f32a045c93fb95aa5d1f3f7f5ba6e3e3e60
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Main functions of the program.
5 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * Copyright (C) 2018 Przemyslaw Pawelczyk <przemoc@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef UTILS_H
22 #define UTILS_H
24 /* For uint64_t */
25 #include <stdint.h>
26 /* For ssize_t */
27 #include <unistd.h>
28 /* For FILE */
29 #include <stdio.h>
30 /* For struct passwd */
31 #include <pwd.h>
32 /* For struct group */
33 #include <grp.h>
35 /* Adjusts the verbosity level for msg() */
36 void adjust_verbosity(int adj);
38 /* Verbosity levels using stdout */
39 #define MSG_NORMAL 0
40 #define MSG_DEBUG 1
41 #define MSG_QUIET -1
42 /* Verbosity levels using stderr */
43 #define MSG_WARNING -2
44 #define MSG_ERROR -3
45 #define MSG_CRITICAL -4
47 /* Prints messages to console according to the current verbosity */
48 int msg(int level, const char *fmt, ...);
50 /* Malloc which either succeeds or exits */
51 void *xmalloc(size_t size);
53 /* Ditto for strdup */
54 char *xstrdup(const char *s);
56 /* Human-readable printout of binary data */
57 void binary_print(const char *s, ssize_t len);
59 /* Writes data to a file or exits on failure */
60 void xfwrite(const void *ptr, size_t size, FILE *stream);
62 /* Writes an int to a file, using len bytes, in little-endian order */
63 void write_int(uint64_t value, size_t len, FILE *to);
65 /* Writes a binary string to a file */
66 void write_binary_string(const char *string, size_t len, FILE *to);
68 /* Writes a normal C string to a file */
69 void write_string(const char *string, FILE *to);
71 /* Reads an int from a file, using len bytes, in little-endian order */
72 uint64_t read_int(char **from, size_t len, const char *max);
74 /* Reads a binary string from a file */
75 char *read_binary_string(char **from, size_t len, const char *max);
77 /* Reads a normal C string from a file */
78 char *read_string(char **from, const char *max);
80 /* Caching version of getgrnam */
81 struct group *xgetgrnam(const char *name);
83 /* Caching version of getgrgid */
84 struct group *xgetgrgid(gid_t gid);
86 /* Caching version of getpwnam */
87 struct passwd *xgetpwnam(const char *name);
89 /* Caching version of getpwuid */
90 struct passwd *xgetpwuid(uid_t uid);
92 #endif /* UTILS_H */