xref: /qemu/include/qemu/cutils.h (revision a976a99a)
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, unsigned long long *value, char **endptr,
167                int base);
168 int parse_uint_full(const char *s, unsigned long long *value, int base);
169 
170 int qemu_strtosz(const char *nptr, const char **end, uint64_t *result);
171 int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
172 int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
173 
174 char *size_to_str(uint64_t val);
175 
176 /**
177  * freq_to_str:
178  * @freq_hz: frequency to stringify
179  *
180  * Return human readable string for frequency @freq_hz.
181  * Use SI units like KHz, MHz, and so forth.
182  *
183  * The caller is responsible for releasing the value returned
184  * with g_free() after use.
185  */
186 char *freq_to_str(uint64_t freq_hz);
187 
188 /* used to print char* safely */
189 #define STR_OR_NULL(str) ((str) ? (str) : "null")
190 
191 bool buffer_is_zero(const void *buf, size_t len);
192 bool test_buffer_is_zero_next_accel(void);
193 
194 /*
195  * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
196  * Input is limited to 14-bit numbers
197  */
198 
199 int uleb128_encode_small(uint8_t *out, uint32_t n);
200 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
201 
202 /**
203  * qemu_pstrcmp0:
204  * @str1: a non-NULL pointer to a C string (*str1 can be NULL)
205  * @str2: a non-NULL pointer to a C string (*str2 can be NULL)
206  *
207  * Compares *str1 and *str2 with g_strcmp0().
208  *
209  * Returns: an integer less than, equal to, or greater than zero, if
210  * *str1 is <, == or > than *str2.
211  */
212 int qemu_pstrcmp0(const char **str1, const char **str2);
213 
214 /* Find program directory, and save it for later usage with
215  * qemu_get_exec_dir().
216  * Try OS specific API first, if not working, parse from argv0. */
217 void qemu_init_exec_dir(const char *argv0);
218 
219 /* Get the saved exec dir.  */
220 const char *qemu_get_exec_dir(void);
221 
222 /**
223  * get_relocated_path:
224  * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocated.
225  *
226  * Returns a path for @dir that uses the directory of the running executable
227  * as the prefix.
228  *
229  * When a directory named `qemu-bundle` exists in the directory of the running
230  * executable, the path to the directory will be prepended to @dir. For
231  * example, if the directory of the running executable is `/qemu/build` @dir
232  * is `/usr/share/qemu`, the result will be
233  * `/qemu/build/qemu-bundle/usr/share/qemu`. The directory is expected to exist
234  * in the build tree.
235  *
236  * Otherwise, the directory of the running executable will be used as the
237  * prefix and it appends the relative path from `bindir` to @dir. For example,
238  * if the directory of the running executable is `/opt/qemu/bin`, `bindir` is
239  * `/usr/bin` and @dir is `/usr/share/qemu`, the result will be
240  * `/opt/qemu/bin/../share/qemu`.
241  *
242  * The returned string should be freed by the caller.
243  */
244 char *get_relocated_path(const char *dir);
245 
246 static inline const char *yes_no(bool b)
247 {
248      return b ? "yes" : "no";
249 }
250 
251 /*
252  * helper to parse debug environment variables
253  */
254 int parse_debug_env(const char *name, int max, int initial);
255 
256 /*
257  * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer
258  */
259 #define QEMU_HEXDUMP_LINE_BYTES 16 /* Number of bytes to dump */
260 #define QEMU_HEXDUMP_LINE_LEN 75   /* Number of characters in line */
261 void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,
262                        unsigned int len, bool ascii);
263 
264 /*
265  * Hexdump a buffer to a file. An optional string prefix is added to every line
266  */
267 
268 void qemu_hexdump(FILE *fp, const char *prefix,
269                   const void *bufptr, size_t size);
270 
271 #endif
272