James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 1 | #ifndef _LINUX_STRING_HELPERS_H_ |
| 2 | #define _LINUX_STRING_HELPERS_H_ |
| 3 | |
| 4 | #include <linux/types.h> |
| 5 | |
| 6 | /* Descriptions of the types of units to |
| 7 | * print in */ |
| 8 | enum string_size_units { |
| 9 | STRING_UNITS_10, /* use powers of 10^3 (standard SI) */ |
| 10 | STRING_UNITS_2, /* use binary powers of 2^10 */ |
| 11 | }; |
| 12 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 13 | void string_get_size(u64 size, u64 blk_size, enum string_size_units units, |
Rasmus Villemoes | d1214c6 | 2015-02-12 15:01:50 -0800 | [diff] [blame] | 14 | char *buf, int len); |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 15 | |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 16 | #define UNESCAPE_SPACE 0x01 |
| 17 | #define UNESCAPE_OCTAL 0x02 |
| 18 | #define UNESCAPE_HEX 0x04 |
| 19 | #define UNESCAPE_SPECIAL 0x08 |
| 20 | #define UNESCAPE_ANY \ |
| 21 | (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) |
| 22 | |
Andy Shevchenko | 16c7fa0 | 2013-04-30 15:27:30 -0700 | [diff] [blame] | 23 | int string_unescape(char *src, char *dst, size_t size, unsigned int flags); |
| 24 | |
| 25 | static inline int string_unescape_inplace(char *buf, unsigned int flags) |
| 26 | { |
| 27 | return string_unescape(buf, buf, 0, flags); |
| 28 | } |
| 29 | |
| 30 | static inline int string_unescape_any(char *src, char *dst, size_t size) |
| 31 | { |
| 32 | return string_unescape(src, dst, size, UNESCAPE_ANY); |
| 33 | } |
| 34 | |
| 35 | static inline int string_unescape_any_inplace(char *buf) |
| 36 | { |
| 37 | return string_unescape_any(buf, buf, 0); |
| 38 | } |
| 39 | |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 40 | #define ESCAPE_SPACE 0x01 |
| 41 | #define ESCAPE_SPECIAL 0x02 |
| 42 | #define ESCAPE_NULL 0x04 |
| 43 | #define ESCAPE_OCTAL 0x08 |
| 44 | #define ESCAPE_ANY \ |
| 45 | (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) |
| 46 | #define ESCAPE_NP 0x10 |
| 47 | #define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) |
| 48 | #define ESCAPE_HEX 0x20 |
| 49 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 50 | int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 51 | unsigned int flags, const char *esc); |
| 52 | |
| 53 | static inline int string_escape_mem_any_np(const char *src, size_t isz, |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 54 | char *dst, size_t osz, const char *esc) |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 55 | { |
| 56 | return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, esc); |
| 57 | } |
| 58 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 59 | static inline int string_escape_str(const char *src, char *dst, size_t sz, |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 60 | unsigned int flags, const char *esc) |
| 61 | { |
| 62 | return string_escape_mem(src, strlen(src), dst, sz, flags, esc); |
| 63 | } |
| 64 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 65 | static inline int string_escape_str_any_np(const char *src, char *dst, |
Andy Shevchenko | c825038 | 2014-10-13 15:55:16 -0700 | [diff] [blame] | 66 | size_t sz, const char *esc) |
| 67 | { |
| 68 | return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, esc); |
| 69 | } |
| 70 | |
James Bottomley | 3c9f368 | 2008-08-31 10:13:54 -0500 | [diff] [blame] | 71 | #endif |