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 */ 11const 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 */ 20const 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 */ 40void 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 */ 53void 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 */ 70char *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 */ 87int 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 */ 103int 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 */ 119int 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 */ 140char *qemu_strsep(char **input, const char *delim); 141#ifdef HAVE_STRCHRNUL 142static inline const char *qemu_strchrnul(const char *s, int c) 143{ 144 return strchrnul(s, c); 145} 146#else 147const char *qemu_strchrnul(const char *s, int c); 148#endif 149time_t mktimegm(struct tm *tm); 150int qemu_parse_fd(const char *param); 151int qemu_strtoi(const char *nptr, const char **endptr, int base, 152 int *result); 153int qemu_strtoui(const char *nptr, const char **endptr, int base, 154 unsigned int *result); 155int qemu_strtol(const char *nptr, const char **endptr, int base, 156 long *result); 157int qemu_strtoul(const char *nptr, const char **endptr, int base, 158 unsigned long *result); 159int qemu_strtoi64(const char *nptr, const char **endptr, int base, 160 int64_t *result); 161int qemu_strtou64(const char *nptr, const char **endptr, int base, 162 uint64_t *result); 163int qemu_strtod(const char *nptr, const char **endptr, double *result); 164int qemu_strtod_finite(const char *nptr, const char **endptr, double *result); 165 166int parse_uint(const char *s, const char **endptr, int base, uint64_t *value); 167int parse_uint_full(const char *s, int base, uint64_t *value); 168 169int qemu_strtosz(const char *nptr, const char **end, uint64_t *result); 170int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result); 171int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result); 172 173char *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 */ 185char *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/* 191 * Check if a buffer is all zeroes. 192 */ 193 194bool buffer_is_zero_ool(const void *vbuf, size_t len); 195bool buffer_is_zero_ge256(const void *vbuf, size_t len); 196bool test_buffer_is_zero_next_accel(void); 197 198static inline bool buffer_is_zero_sample3(const char *buf, size_t len) 199{ 200 /* 201 * For any reasonably sized buffer, these three samples come from 202 * three different cachelines. In qemu-img usage, we find that 203 * each byte eliminates more than half of all buffer testing. 204 * It is therefore critical to performance that the byte tests 205 * short-circuit, so that we do not pull in additional cache lines. 206 * Do not "optimize" this to !(a | b | c). 207 */ 208 return !buf[0] && !buf[len - 1] && !buf[len / 2]; 209} 210 211#ifdef __OPTIMIZE__ 212static inline bool buffer_is_zero(const void *buf, size_t len) 213{ 214 return (__builtin_constant_p(len) && len >= 256 215 ? buffer_is_zero_sample3(buf, len) && 216 buffer_is_zero_ge256(buf, len) 217 : buffer_is_zero_ool(buf, len)); 218} 219#else 220#define buffer_is_zero buffer_is_zero_ool 221#endif 222 223/* 224 * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) 225 * Input is limited to 14-bit numbers 226 */ 227 228int uleb128_encode_small(uint8_t *out, uint32_t n); 229int uleb128_decode_small(const uint8_t *in, uint32_t *n); 230 231/** 232 * qemu_pstrcmp0: 233 * @str1: a non-NULL pointer to a C string (*str1 can be NULL) 234 * @str2: a non-NULL pointer to a C string (*str2 can be NULL) 235 * 236 * Compares *str1 and *str2 with g_strcmp0(). 237 * 238 * Returns: an integer less than, equal to, or greater than zero, if 239 * *str1 is <, == or > than *str2. 240 */ 241int qemu_pstrcmp0(const char **str1, const char **str2); 242 243/* Find program directory, and save it for later usage with 244 * get_relocated_path(). 245 * Try OS specific API first, if not working, parse from argv0. */ 246void qemu_init_exec_dir(const char *argv0); 247 248/** 249 * get_relocated_path: 250 * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocated. 251 * 252 * Returns a path for @dir that uses the directory of the running executable 253 * as the prefix. 254 * 255 * When a directory named `qemu-bundle` exists in the directory of the running 256 * executable, the path to the directory will be prepended to @dir. For 257 * example, if the directory of the running executable is `/qemu/build` @dir 258 * is `/usr/share/qemu`, the result will be 259 * `/qemu/build/qemu-bundle/usr/share/qemu`. The directory is expected to exist 260 * in the build tree. 261 * 262 * Otherwise, the directory of the running executable will be used as the 263 * prefix and it appends the relative path from `bindir` to @dir. For example, 264 * if the directory of the running executable is `/opt/qemu/bin`, `bindir` is 265 * `/usr/bin` and @dir is `/usr/share/qemu`, the result will be 266 * `/opt/qemu/bin/../share/qemu`. 267 * 268 * The returned string should be freed by the caller. 269 */ 270char *get_relocated_path(const char *dir); 271 272static inline const char *yes_no(bool b) 273{ 274 return b ? "yes" : "no"; 275} 276 277/* 278 * helper to parse debug environment variables 279 */ 280int parse_debug_env(const char *name, int max, int initial); 281 282/** 283 * qemu_hexdump_line: 284 * @str: GString into which to append 285 * @buf: buffer to dump 286 * @len: number of bytes to dump 287 * @unit_len: add a space between every @unit_len bytes 288 * @block_len: add an extra space between every @block_len bytes 289 * 290 * Append @len bytes of @buf as hexadecimal into @str. 291 * Add spaces between every @unit_len and @block_len bytes. 292 * If @str is NULL, allocate a new string and return it; 293 * otherwise return @str. 294 */ 295GString *qemu_hexdump_line(GString *str, const void *buf, size_t len, 296 size_t unit_len, size_t block_len); 297 298/* 299 * Hexdump a buffer to a file. An optional string prefix is added to every line 300 */ 301 302void qemu_hexdump(FILE *fp, const char *prefix, 303 const void *bufptr, size_t size); 304 305/** 306 * qemu_hexdump_to_buffer: 307 * @buffer: output string buffer 308 * @buffer_size: amount of available space in buffer. Must be at least 309 * data_size*2+1. 310 * @data: input bytes 311 * @data_size: number of bytes in data 312 * 313 * Converts the @data_size bytes in @data into hex digit pairs, writing them to 314 * @buffer. Finally, a nul terminating character is written; @buffer therefore 315 * needs space for (data_size*2+1) chars. 316 */ 317void qemu_hexdump_to_buffer(char *restrict buffer, size_t buffer_size, 318 const uint8_t *restrict data, size_t data_size); 319 320#endif 321

