xref: /openbmc/qemu/include/qemu/cutils.h (revision bd1386cc)
1f348b6d1SVeronia Bahaa #ifndef QEMU_CUTILS_H
2175de524SMarkus Armbruster #define QEMU_CUTILS_H
3f348b6d1SVeronia Bahaa 
4cfb34489SPaolo Bonzini /*
5cfb34489SPaolo Bonzini  * si_prefix:
6cfb34489SPaolo Bonzini  * @exp10: exponent of 10, a multiple of 3 between -18 and 18 inclusive.
7cfb34489SPaolo Bonzini  *
8cfb34489SPaolo Bonzini  * Return a SI prefix (n, u, m, K, M, etc.) corresponding
9cfb34489SPaolo Bonzini  * to the given exponent of 10.
10cfb34489SPaolo Bonzini  */
11cfb34489SPaolo Bonzini const char *si_prefix(unsigned int exp10);
12cfb34489SPaolo Bonzini 
13cfb34489SPaolo Bonzini /*
14cfb34489SPaolo Bonzini  * iec_binary_prefix:
15cfb34489SPaolo Bonzini  * @exp2: exponent of 2, a multiple of 10 between 0 and 60 inclusive.
16cfb34489SPaolo Bonzini  *
17cfb34489SPaolo Bonzini  * Return an IEC binary prefix (Ki, Mi, etc.) corresponding
18cfb34489SPaolo Bonzini  * to the given exponent of 2.
19cfb34489SPaolo Bonzini  */
20cfb34489SPaolo Bonzini const char *iec_binary_prefix(unsigned int exp2);
21cfb34489SPaolo Bonzini 
22f348b6d1SVeronia Bahaa /**
23f348b6d1SVeronia Bahaa  * pstrcpy:
24f348b6d1SVeronia Bahaa  * @buf: buffer to copy string into
25f348b6d1SVeronia Bahaa  * @buf_size: size of @buf in bytes
26f348b6d1SVeronia Bahaa  * @str: string to copy
27f348b6d1SVeronia Bahaa  *
28f348b6d1SVeronia Bahaa  * Copy @str into @buf, including the trailing NUL, but do not
29f348b6d1SVeronia Bahaa  * write more than @buf_size bytes. The resulting buffer is
30f348b6d1SVeronia Bahaa  * always NUL terminated (even if the source string was too long).
31f348b6d1SVeronia Bahaa  * If @buf_size is zero or negative then no bytes are copied.
32f348b6d1SVeronia Bahaa  *
33f348b6d1SVeronia Bahaa  * This function is similar to strncpy(), but avoids two of that
34f348b6d1SVeronia Bahaa  * function's problems:
35f348b6d1SVeronia Bahaa  *  * if @str fits in the buffer, pstrcpy() does not zero-fill the
36f348b6d1SVeronia Bahaa  *    remaining space at the end of @buf
37f348b6d1SVeronia Bahaa  *  * if @str is too long, pstrcpy() will copy the first @buf_size-1
38f348b6d1SVeronia Bahaa  *    bytes and then add a NUL
39f348b6d1SVeronia Bahaa  */
40f348b6d1SVeronia Bahaa void pstrcpy(char *buf, int buf_size, const char *str);
41f348b6d1SVeronia Bahaa /**
42f348b6d1SVeronia Bahaa  * strpadcpy:
43f348b6d1SVeronia Bahaa  * @buf: buffer to copy string into
44f348b6d1SVeronia Bahaa  * @buf_size: size of @buf in bytes
45f348b6d1SVeronia Bahaa  * @str: string to copy
46f348b6d1SVeronia Bahaa  * @pad: character to pad the remainder of @buf with
47f348b6d1SVeronia Bahaa  *
48f348b6d1SVeronia Bahaa  * Copy @str into @buf (but *not* its trailing NUL!), and then pad the
49f348b6d1SVeronia Bahaa  * rest of the buffer with the @pad character. If @str is too large
50f348b6d1SVeronia Bahaa  * for the buffer then it is truncated, so that @buf contains the
51f348b6d1SVeronia Bahaa  * first @buf_size characters of @str, with no terminator.
52f348b6d1SVeronia Bahaa  */
53f348b6d1SVeronia Bahaa void strpadcpy(char *buf, int buf_size, const char *str, char pad);
54f348b6d1SVeronia Bahaa /**
55f348b6d1SVeronia Bahaa  * pstrcat:
56f348b6d1SVeronia Bahaa  * @buf: buffer containing existing string
57f348b6d1SVeronia Bahaa  * @buf_size: size of @buf in bytes
58f348b6d1SVeronia Bahaa  * @s: string to concatenate to @buf
59f348b6d1SVeronia Bahaa  *
60f348b6d1SVeronia Bahaa  * Append a copy of @s to the string already in @buf, but do not
61f348b6d1SVeronia Bahaa  * allow the buffer to overflow. If the existing contents of @buf
62f348b6d1SVeronia Bahaa  * plus @str would total more than @buf_size bytes, then write
63f348b6d1SVeronia Bahaa  * as much of @str as will fit followed by a NUL terminator.
64f348b6d1SVeronia Bahaa  *
65f348b6d1SVeronia Bahaa  * @buf must already contain a NUL-terminated string, or the
66f348b6d1SVeronia Bahaa  * behaviour is undefined.
67f348b6d1SVeronia Bahaa  *
68f348b6d1SVeronia Bahaa  * Returns: @buf.
69f348b6d1SVeronia Bahaa  */
70f348b6d1SVeronia Bahaa char *pstrcat(char *buf, int buf_size, const char *s);
71f348b6d1SVeronia Bahaa /**
72f348b6d1SVeronia Bahaa  * strstart:
73f348b6d1SVeronia Bahaa  * @str: string to test
74f348b6d1SVeronia Bahaa  * @val: prefix string to look for
75f348b6d1SVeronia Bahaa  * @ptr: NULL, or pointer to be written to indicate start of
76f348b6d1SVeronia Bahaa  *       the remainder of the string
77f348b6d1SVeronia Bahaa  *
78f348b6d1SVeronia Bahaa  * Test whether @str starts with the prefix @val.
79f348b6d1SVeronia Bahaa  * If it does (including the degenerate case where @str and @val
80f348b6d1SVeronia Bahaa  * are equal) then return true. If @ptr is not NULL then a
81f348b6d1SVeronia Bahaa  * pointer to the first character following the prefix is written
82f348b6d1SVeronia Bahaa  * to it. If @val is not a prefix of @str then return false (and
83f348b6d1SVeronia Bahaa  * @ptr is not written to).
84f348b6d1SVeronia Bahaa  *
85f348b6d1SVeronia Bahaa  * Returns: true if @str starts with prefix @val, false otherwise.
86f348b6d1SVeronia Bahaa  */
87f348b6d1SVeronia Bahaa int strstart(const char *str, const char *val, const char **ptr);
88f348b6d1SVeronia Bahaa /**
89f348b6d1SVeronia Bahaa  * stristart:
90f348b6d1SVeronia Bahaa  * @str: string to test
91f348b6d1SVeronia Bahaa  * @val: prefix string to look for
92f348b6d1SVeronia Bahaa  * @ptr: NULL, or pointer to be written to indicate start of
93f348b6d1SVeronia Bahaa  *       the remainder of the string
94f348b6d1SVeronia Bahaa  *
95f348b6d1SVeronia Bahaa  * Test whether @str starts with the case-insensitive prefix @val.
96f348b6d1SVeronia Bahaa  * This function behaves identically to strstart(), except that the
97f348b6d1SVeronia Bahaa  * comparison is made after calling qemu_toupper() on each pair of
98f348b6d1SVeronia Bahaa  * characters.
99f348b6d1SVeronia Bahaa  *
100f348b6d1SVeronia Bahaa  * Returns: true if @str starts with case-insensitive prefix @val,
101f348b6d1SVeronia Bahaa  *          false otherwise.
102f348b6d1SVeronia Bahaa  */
103f348b6d1SVeronia Bahaa int stristart(const char *str, const char *val, const char **ptr);
104f348b6d1SVeronia Bahaa /**
105f348b6d1SVeronia Bahaa  * qemu_strnlen:
106f348b6d1SVeronia Bahaa  * @s: string
107f348b6d1SVeronia Bahaa  * @max_len: maximum number of bytes in @s to scan
108f348b6d1SVeronia Bahaa  *
109f348b6d1SVeronia Bahaa  * Return the length of the string @s, like strlen(), but do not
110f348b6d1SVeronia Bahaa  * examine more than @max_len bytes of the memory pointed to by @s.
111f348b6d1SVeronia Bahaa  * If no NUL terminator is found within @max_len bytes, then return
112f348b6d1SVeronia Bahaa  * @max_len instead.
113f348b6d1SVeronia Bahaa  *
114f348b6d1SVeronia Bahaa  * This function has the same behaviour as the POSIX strnlen()
115f348b6d1SVeronia Bahaa  * function.
116f348b6d1SVeronia Bahaa  *
117f348b6d1SVeronia Bahaa  * Returns: length of @s in bytes, or @max_len, whichever is smaller.
118f348b6d1SVeronia Bahaa  */
119f348b6d1SVeronia Bahaa int qemu_strnlen(const char *s, int max_len);
120f348b6d1SVeronia Bahaa /**
121f348b6d1SVeronia Bahaa  * qemu_strsep:
122f348b6d1SVeronia Bahaa  * @input: pointer to string to parse
123f348b6d1SVeronia Bahaa  * @delim: string containing delimiter characters to search for
124f348b6d1SVeronia Bahaa  *
125f348b6d1SVeronia Bahaa  * Locate the first occurrence of any character in @delim within
126f348b6d1SVeronia Bahaa  * the string referenced by @input, and replace it with a NUL.
127f348b6d1SVeronia Bahaa  * The location of the next character after the delimiter character
128f348b6d1SVeronia Bahaa  * is stored into @input.
129f348b6d1SVeronia Bahaa  * If the end of the string was reached without finding a delimiter
130f348b6d1SVeronia Bahaa  * character, then NULL is stored into @input.
131f348b6d1SVeronia Bahaa  * If @input points to a NULL pointer on entry, return NULL.
132f348b6d1SVeronia Bahaa  * The return value is always the original value of *@input (and
133f348b6d1SVeronia Bahaa  * so now points to a NUL-terminated string corresponding to the
134f348b6d1SVeronia Bahaa  * part of the input up to the first delimiter).
135f348b6d1SVeronia Bahaa  *
136f348b6d1SVeronia Bahaa  * This function has the same behaviour as the BSD strsep() function.
137f348b6d1SVeronia Bahaa  *
138f348b6d1SVeronia Bahaa  * Returns: the pointer originally in @input.
139f348b6d1SVeronia Bahaa  */
140f348b6d1SVeronia Bahaa char *qemu_strsep(char **input, const char *delim);
1415c99fa37SKeno Fischer #ifdef HAVE_STRCHRNUL
qemu_strchrnul(const char * s,int c)1425c99fa37SKeno Fischer static inline const char *qemu_strchrnul(const char *s, int c)
1435c99fa37SKeno Fischer {
1445c99fa37SKeno Fischer     return strchrnul(s, c);
1455c99fa37SKeno Fischer }
1465c99fa37SKeno Fischer #else
1475c99fa37SKeno Fischer const char *qemu_strchrnul(const char *s, int c);
1485c99fa37SKeno Fischer #endif
149f348b6d1SVeronia Bahaa time_t mktimegm(struct tm *tm);
150f348b6d1SVeronia Bahaa int qemu_parse_fd(const char *param);
151473a2a33SDaniel P. Berrange int qemu_strtoi(const char *nptr, const char **endptr, int base,
152473a2a33SDaniel P. Berrange                 int *result);
153473a2a33SDaniel P. Berrange int qemu_strtoui(const char *nptr, const char **endptr, int base,
154473a2a33SDaniel P. Berrange                  unsigned int *result);
155f348b6d1SVeronia Bahaa int qemu_strtol(const char *nptr, const char **endptr, int base,
156f348b6d1SVeronia Bahaa                 long *result);
157f348b6d1SVeronia Bahaa int qemu_strtoul(const char *nptr, const char **endptr, int base,
158f348b6d1SVeronia Bahaa                  unsigned long *result);
159b30d1886SMarkus Armbruster int qemu_strtoi64(const char *nptr, const char **endptr, int base,
160f348b6d1SVeronia Bahaa                   int64_t *result);
161b30d1886SMarkus Armbruster int qemu_strtou64(const char *nptr, const char **endptr, int base,
162f348b6d1SVeronia Bahaa                   uint64_t *result);
163ca28f548SDavid Hildenbrand int qemu_strtod(const char *nptr, const char **endptr, double *result);
164ca28f548SDavid Hildenbrand int qemu_strtod_finite(const char *nptr, const char **endptr, double *result);
165f348b6d1SVeronia Bahaa 
166*bd1386ccSEric Blake int parse_uint(const char *s, const char **endptr, int base, uint64_t *value);
167*bd1386ccSEric Blake int parse_uint_full(const char *s, int base, uint64_t *value);
168f348b6d1SVeronia Bahaa 
169af02f4c5SDavid Hildenbrand int qemu_strtosz(const char *nptr, const char **end, uint64_t *result);
170af02f4c5SDavid Hildenbrand int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
171af02f4c5SDavid Hildenbrand int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
172d2734d26SMarkus Armbruster 
17331e40415SPhilippe Mathieu-Daudé char *size_to_str(uint64_t val);
17431e40415SPhilippe Mathieu-Daudé 
175709616c7SPhilippe Mathieu-Daudé /**
176709616c7SPhilippe Mathieu-Daudé  * freq_to_str:
177709616c7SPhilippe Mathieu-Daudé  * @freq_hz: frequency to stringify
178709616c7SPhilippe Mathieu-Daudé  *
179709616c7SPhilippe Mathieu-Daudé  * Return human readable string for frequency @freq_hz.
180709616c7SPhilippe Mathieu-Daudé  * Use SI units like KHz, MHz, and so forth.
181709616c7SPhilippe Mathieu-Daudé  *
182709616c7SPhilippe Mathieu-Daudé  * The caller is responsible for releasing the value returned
183709616c7SPhilippe Mathieu-Daudé  * with g_free() after use.
184709616c7SPhilippe Mathieu-Daudé  */
185709616c7SPhilippe Mathieu-Daudé char *freq_to_str(uint64_t freq_hz);
186709616c7SPhilippe Mathieu-Daudé 
187f348b6d1SVeronia Bahaa /* used to print char* safely */
188f348b6d1SVeronia Bahaa #define STR_OR_NULL(str) ((str) ? (str) : "null")
189f348b6d1SVeronia Bahaa 
190f348b6d1SVeronia Bahaa bool buffer_is_zero(const void *buf, size_t len);
191efad6682SRichard Henderson bool test_buffer_is_zero_next_accel(void);
192f348b6d1SVeronia Bahaa 
193f348b6d1SVeronia Bahaa /*
194f348b6d1SVeronia Bahaa  * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128)
195f348b6d1SVeronia Bahaa  * Input is limited to 14-bit numbers
196f348b6d1SVeronia Bahaa  */
197f348b6d1SVeronia Bahaa 
198f348b6d1SVeronia Bahaa int uleb128_encode_small(uint8_t *out, uint32_t n);
199f348b6d1SVeronia Bahaa int uleb128_decode_small(const uint8_t *in, uint32_t *n);
200f348b6d1SVeronia Bahaa 
20185e33a28SMarc-André Lureau /**
20285e33a28SMarc-André Lureau  * qemu_pstrcmp0:
20385e33a28SMarc-André Lureau  * @str1: a non-NULL pointer to a C string (*str1 can be NULL)
20485e33a28SMarc-André Lureau  * @str2: a non-NULL pointer to a C string (*str2 can be NULL)
20585e33a28SMarc-André Lureau  *
20685e33a28SMarc-André Lureau  * Compares *str1 and *str2 with g_strcmp0().
20785e33a28SMarc-André Lureau  *
20885e33a28SMarc-André Lureau  * Returns: an integer less than, equal to, or greater than zero, if
20985e33a28SMarc-André Lureau  * *str1 is <, == or > than *str2.
21085e33a28SMarc-André Lureau  */
21185e33a28SMarc-André Lureau int qemu_pstrcmp0(const char **str1, const char **str2);
21285e33a28SMarc-André Lureau 
21306680b15SMarc-André Lureau /* Find program directory, and save it for later usage with
21406680b15SMarc-André Lureau  * qemu_get_exec_dir().
21506680b15SMarc-André Lureau  * Try OS specific API first, if not working, parse from argv0. */
21606680b15SMarc-André Lureau void qemu_init_exec_dir(const char *argv0);
21706680b15SMarc-André Lureau 
21806680b15SMarc-André Lureau /* Get the saved exec dir.  */
21906680b15SMarc-André Lureau const char *qemu_get_exec_dir(void);
220f4f5ed2cSPaolo Bonzini 
221f4f5ed2cSPaolo Bonzini /**
222f4f5ed2cSPaolo Bonzini  * get_relocated_path:
223f4f5ed2cSPaolo Bonzini  * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocated.
224f4f5ed2cSPaolo Bonzini  *
225f4f5ed2cSPaolo Bonzini  * Returns a path for @dir that uses the directory of the running executable
226cf60ccc3SAkihiko Odaki  * as the prefix.
227cf60ccc3SAkihiko Odaki  *
228cf60ccc3SAkihiko Odaki  * When a directory named `qemu-bundle` exists in the directory of the running
229cf60ccc3SAkihiko Odaki  * executable, the path to the directory will be prepended to @dir. For
230cf60ccc3SAkihiko Odaki  * example, if the directory of the running executable is `/qemu/build` @dir
231cf60ccc3SAkihiko Odaki  * is `/usr/share/qemu`, the result will be
232cf60ccc3SAkihiko Odaki  * `/qemu/build/qemu-bundle/usr/share/qemu`. The directory is expected to exist
233cf60ccc3SAkihiko Odaki  * in the build tree.
234cf60ccc3SAkihiko Odaki  *
235cf60ccc3SAkihiko Odaki  * Otherwise, the directory of the running executable will be used as the
236cf60ccc3SAkihiko Odaki  * prefix and it appends the relative path from `bindir` to @dir. For example,
237cf60ccc3SAkihiko Odaki  * if the directory of the running executable is `/opt/qemu/bin`, `bindir` is
238cf60ccc3SAkihiko Odaki  * `/usr/bin` and @dir is `/usr/share/qemu`, the result will be
239cf60ccc3SAkihiko Odaki  * `/opt/qemu/bin/../share/qemu`.
240cf60ccc3SAkihiko Odaki  *
241090afdc5SPaolo Bonzini  * The returned string should be freed by the caller.
242f4f5ed2cSPaolo Bonzini  */
243f4f5ed2cSPaolo Bonzini char *get_relocated_path(const char *dir);
244f4f5ed2cSPaolo Bonzini 
yes_no(bool b)24599997823SMarc-André Lureau static inline const char *yes_no(bool b)
24699997823SMarc-André Lureau {
24799997823SMarc-André Lureau      return b ? "yes" : "no";
24899997823SMarc-André Lureau }
24999997823SMarc-André Lureau 
250415b7327SMarc-André Lureau /*
251415b7327SMarc-André Lureau  * helper to parse debug environment variables
252415b7327SMarc-André Lureau  */
253415b7327SMarc-André Lureau int parse_debug_env(const char *name, int max, int initial);
254415b7327SMarc-André Lureau 
255415b7327SMarc-André Lureau /*
256415b7327SMarc-André Lureau  * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer
257415b7327SMarc-André Lureau  */
258415b7327SMarc-André Lureau #define QEMU_HEXDUMP_LINE_BYTES 16 /* Number of bytes to dump */
259415b7327SMarc-André Lureau #define QEMU_HEXDUMP_LINE_LEN 75   /* Number of characters in line */
260415b7327SMarc-André Lureau void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,
261415b7327SMarc-André Lureau                        unsigned int len, bool ascii);
262415b7327SMarc-André Lureau 
263415b7327SMarc-André Lureau /*
264415b7327SMarc-André Lureau  * Hexdump a buffer to a file. An optional string prefix is added to every line
265415b7327SMarc-André Lureau  */
266415b7327SMarc-André Lureau 
267415b7327SMarc-André Lureau void qemu_hexdump(FILE *fp, const char *prefix,
268415b7327SMarc-André Lureau                   const void *bufptr, size_t size);
269415b7327SMarc-André Lureau 
270f348b6d1SVeronia Bahaa #endif
271