]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu/cutils.h
Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[mirror_qemu.git] / include / qemu / cutils.h
CommitLineData
f348b6d1 1#ifndef QEMU_CUTILS_H
175de524 2#define QEMU_CUTILS_H
f348b6d1 3
f348b6d1
VB
4/**
5 * pstrcpy:
6 * @buf: buffer to copy string into
7 * @buf_size: size of @buf in bytes
8 * @str: string to copy
9 *
10 * Copy @str into @buf, including the trailing NUL, but do not
11 * write more than @buf_size bytes. The resulting buffer is
12 * always NUL terminated (even if the source string was too long).
13 * If @buf_size is zero or negative then no bytes are copied.
14 *
15 * This function is similar to strncpy(), but avoids two of that
16 * function's problems:
17 * * if @str fits in the buffer, pstrcpy() does not zero-fill the
18 * remaining space at the end of @buf
19 * * if @str is too long, pstrcpy() will copy the first @buf_size-1
20 * bytes and then add a NUL
21 */
22void pstrcpy(char *buf, int buf_size, const char *str);
23/**
24 * strpadcpy:
25 * @buf: buffer to copy string into
26 * @buf_size: size of @buf in bytes
27 * @str: string to copy
28 * @pad: character to pad the remainder of @buf with
29 *
30 * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
31 * rest of the buffer with the @pad character. If @str is too large
32 * for the buffer then it is truncated, so that @buf contains the
33 * first @buf_size characters of @str, with no terminator.
34 */
35void strpadcpy(char *buf, int buf_size, const char *str, char pad);
36/**
37 * pstrcat:
38 * @buf: buffer containing existing string
39 * @buf_size: size of @buf in bytes
40 * @s: string to concatenate to @buf
41 *
42 * Append a copy of @s to the string already in @buf, but do not
43 * allow the buffer to overflow. If the existing contents of @buf
44 * plus @str would total more than @buf_size bytes, then write
45 * as much of @str as will fit followed by a NUL terminator.
46 *
47 * @buf must already contain a NUL-terminated string, or the
48 * behaviour is undefined.
49 *
50 * Returns: @buf.
51 */
52char *pstrcat(char *buf, int buf_size, const char *s);
53/**
54 * strstart:
55 * @str: string to test
56 * @val: prefix string to look for
57 * @ptr: NULL, or pointer to be written to indicate start of
58 * the remainder of the string
59 *
60 * Test whether @str starts with the prefix @val.
61 * If it does (including the degenerate case where @str and @val
62 * are equal) then return true. If @ptr is not NULL then a
63 * pointer to the first character following the prefix is written
64 * to it. If @val is not a prefix of @str then return false (and
65 * @ptr is not written to).
66 *
67 * Returns: true if @str starts with prefix @val, false otherwise.
68 */
69int strstart(const char *str, const char *val, const char **ptr);
70/**
71 * stristart:
72 * @str: string to test
73 * @val: prefix string to look for
74 * @ptr: NULL, or pointer to be written to indicate start of
75 * the remainder of the string
76 *
77 * Test whether @str starts with the case-insensitive prefix @val.
78 * This function behaves identically to strstart(), except that the
79 * comparison is made after calling qemu_toupper() on each pair of
80 * characters.
81 *
82 * Returns: true if @str starts with case-insensitive prefix @val,
83 * false otherwise.
84 */
85int stristart(const char *str, const char *val, const char **ptr);
86/**
87 * qemu_strnlen:
88 * @s: string
89 * @max_len: maximum number of bytes in @s to scan
90 *
91 * Return the length of the string @s, like strlen(), but do not
92 * examine more than @max_len bytes of the memory pointed to by @s.
93 * If no NUL terminator is found within @max_len bytes, then return
94 * @max_len instead.
95 *
96 * This function has the same behaviour as the POSIX strnlen()
97 * function.
98 *
99 * Returns: length of @s in bytes, or @max_len, whichever is smaller.
100 */
101int qemu_strnlen(const char *s, int max_len);
102/**
103 * qemu_strsep:
104 * @input: pointer to string to parse
105 * @delim: string containing delimiter characters to search for
106 *
107 * Locate the first occurrence of any character in @delim within
108 * the string referenced by @input, and replace it with a NUL.
109 * The location of the next character after the delimiter character
110 * is stored into @input.
111 * If the end of the string was reached without finding a delimiter
112 * character, then NULL is stored into @input.
113 * If @input points to a NULL pointer on entry, return NULL.
114 * The return value is always the original value of *@input (and
115 * so now points to a NUL-terminated string corresponding to the
116 * part of the input up to the first delimiter).
117 *
118 * This function has the same behaviour as the BSD strsep() function.
119 *
120 * Returns: the pointer originally in @input.
121 */
122char *qemu_strsep(char **input, const char *delim);
5c99fa37
KF
123#ifdef HAVE_STRCHRNUL
124static inline const char *qemu_strchrnul(const char *s, int c)
125{
126 return strchrnul(s, c);
127}
128#else
129const char *qemu_strchrnul(const char *s, int c);
130#endif
f348b6d1
VB
131time_t mktimegm(struct tm *tm);
132int qemu_fdatasync(int fd);
61c490e2 133int qemu_msync(void *addr, size_t length, int fd);
f348b6d1 134int qemu_parse_fd(const char *param);
473a2a33
DB
135int qemu_strtoi(const char *nptr, const char **endptr, int base,
136 int *result);
137int qemu_strtoui(const char *nptr, const char **endptr, int base,
138 unsigned int *result);
f348b6d1
VB
139int qemu_strtol(const char *nptr, const char **endptr, int base,
140 long *result);
141int qemu_strtoul(const char *nptr, const char **endptr, int base,
142 unsigned long *result);
b30d1886
MA
143int qemu_strtoi64(const char *nptr, const char **endptr, int base,
144 int64_t *result);
145int qemu_strtou64(const char *nptr, const char **endptr, int base,
f348b6d1 146 uint64_t *result);
ca28f548
DH
147int qemu_strtod(const char *nptr, const char **endptr, double *result);
148int qemu_strtod_finite(const char *nptr, const char **endptr, double *result);
f348b6d1
VB
149
150int parse_uint(const char *s, unsigned long long *value, char **endptr,
151 int base);
152int parse_uint_full(const char *s, unsigned long long *value, int base);
153
af02f4c5
DH
154int qemu_strtosz(const char *nptr, const char **end, uint64_t *result);
155int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
156int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
d2734d26 157
31e40415
PMD
158char *size_to_str(uint64_t val);
159
709616c7
PMD
160/**
161 * freq_to_str:
162 * @freq_hz: frequency to stringify
163 *
164 * Return human readable string for frequency @freq_hz.
165 * Use SI units like KHz, MHz, and so forth.
166 *
167 * The caller is responsible for releasing the value returned
168 * with g_free() after use.
169 */
170char *freq_to_str(uint64_t freq_hz);
171
f348b6d1
VB
172/* used to print char* safely */
173#define STR_OR_NULL(str) ((str) ? (str) : "null")
174
f348b6d1 175bool buffer_is_zero(const void *buf, size_t len);
efad6682 176bool test_buffer_is_zero_next_accel(void);
f348b6d1
VB
177
178/*
179 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
180 * Input is limited to 14-bit numbers
181 */
182
183int uleb128_encode_small(uint8_t *out, uint32_t n);
184int uleb128_decode_small(const uint8_t *in, uint32_t *n);
185
85e33a28
MAL
186/**
187 * qemu_pstrcmp0:
188 * @str1: a non-NULL pointer to a C string (*str1 can be NULL)
189 * @str2: a non-NULL pointer to a C string (*str2 can be NULL)
190 *
191 * Compares *str1 and *str2 with g_strcmp0().
192 *
193 * Returns: an integer less than, equal to, or greater than zero, if
194 * *str1 is <, == or > than *str2.
195 */
196int qemu_pstrcmp0(const char **str1, const char **str2);
197
f4f5ed2c
PB
198
199/**
200 * get_relocated_path:
201 * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocated.
202 *
203 * Returns a path for @dir that uses the directory of the running executable
204 * as the prefix. For example, if `bindir` is `/usr/bin` and @dir is
205 * `/usr/share/qemu`, the function will append `../share/qemu` to the
206 * directory that contains the running executable and return the result.
090afdc5 207 * The returned string should be freed by the caller.
f4f5ed2c
PB
208 */
209char *get_relocated_path(const char *dir);
210
99997823
MAL
211static inline const char *yes_no(bool b)
212{
213 return b ? "yes" : "no";
214}
215
415b7327
MAL
216/*
217 * helper to parse debug environment variables
218 */
219int parse_debug_env(const char *name, int max, int initial);
220
221/*
222 * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer
223 */
224#define QEMU_HEXDUMP_LINE_BYTES 16 /* Number of bytes to dump */
225#define QEMU_HEXDUMP_LINE_LEN 75 /* Number of characters in line */
226void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,
227 unsigned int len, bool ascii);
228
229/*
230 * Hexdump a buffer to a file. An optional string prefix is added to every line
231 */
232
233void qemu_hexdump(FILE *fp, const char *prefix,
234 const void *bufptr, size_t size);
235
f348b6d1 236#endif