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