Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_STRING_HELPERS_H_
0003 #define _LINUX_STRING_HELPERS_H_
0004 
0005 #include <linux/bits.h>
0006 #include <linux/ctype.h>
0007 #include <linux/string.h>
0008 #include <linux/types.h>
0009 
0010 struct device;
0011 struct file;
0012 struct task_struct;
0013 
0014 /* Descriptions of the types of units to
0015  * print in */
0016 enum string_size_units {
0017     STRING_UNITS_10,    /* use powers of 10^3 (standard SI) */
0018     STRING_UNITS_2,     /* use binary powers of 2^10 */
0019 };
0020 
0021 void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
0022              char *buf, int len);
0023 
0024 #define UNESCAPE_SPACE      BIT(0)
0025 #define UNESCAPE_OCTAL      BIT(1)
0026 #define UNESCAPE_HEX        BIT(2)
0027 #define UNESCAPE_SPECIAL    BIT(3)
0028 #define UNESCAPE_ANY        \
0029     (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
0030 
0031 #define UNESCAPE_ALL_MASK   GENMASK(3, 0)
0032 
0033 int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
0034 
0035 static inline int string_unescape_inplace(char *buf, unsigned int flags)
0036 {
0037     return string_unescape(buf, buf, 0, flags);
0038 }
0039 
0040 static inline int string_unescape_any(char *src, char *dst, size_t size)
0041 {
0042     return string_unescape(src, dst, size, UNESCAPE_ANY);
0043 }
0044 
0045 static inline int string_unescape_any_inplace(char *buf)
0046 {
0047     return string_unescape_any(buf, buf, 0);
0048 }
0049 
0050 #define ESCAPE_SPACE        BIT(0)
0051 #define ESCAPE_SPECIAL      BIT(1)
0052 #define ESCAPE_NULL     BIT(2)
0053 #define ESCAPE_OCTAL        BIT(3)
0054 #define ESCAPE_ANY      \
0055     (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
0056 #define ESCAPE_NP       BIT(4)
0057 #define ESCAPE_ANY_NP       (ESCAPE_ANY | ESCAPE_NP)
0058 #define ESCAPE_HEX      BIT(5)
0059 #define ESCAPE_NA       BIT(6)
0060 #define ESCAPE_NAP      BIT(7)
0061 #define ESCAPE_APPEND       BIT(8)
0062 
0063 #define ESCAPE_ALL_MASK     GENMASK(8, 0)
0064 
0065 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
0066         unsigned int flags, const char *only);
0067 
0068 static inline int string_escape_mem_any_np(const char *src, size_t isz,
0069         char *dst, size_t osz, const char *only)
0070 {
0071     return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
0072 }
0073 
0074 static inline int string_escape_str(const char *src, char *dst, size_t sz,
0075         unsigned int flags, const char *only)
0076 {
0077     return string_escape_mem(src, strlen(src), dst, sz, flags, only);
0078 }
0079 
0080 static inline int string_escape_str_any_np(const char *src, char *dst,
0081         size_t sz, const char *only)
0082 {
0083     return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
0084 }
0085 
0086 static inline void string_upper(char *dst, const char *src)
0087 {
0088     do {
0089         *dst++ = toupper(*src);
0090     } while (*src++);
0091 }
0092 
0093 static inline void string_lower(char *dst, const char *src)
0094 {
0095     do {
0096         *dst++ = tolower(*src);
0097     } while (*src++);
0098 }
0099 
0100 char *kstrdup_quotable(const char *src, gfp_t gfp);
0101 char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
0102 char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
0103 
0104 char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
0105 void kfree_strarray(char **array, size_t n);
0106 
0107 char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);
0108 
0109 static inline const char *str_yes_no(bool v)
0110 {
0111     return v ? "yes" : "no";
0112 }
0113 
0114 static inline const char *str_on_off(bool v)
0115 {
0116     return v ? "on" : "off";
0117 }
0118 
0119 static inline const char *str_enable_disable(bool v)
0120 {
0121     return v ? "enable" : "disable";
0122 }
0123 
0124 static inline const char *str_enabled_disabled(bool v)
0125 {
0126     return v ? "enabled" : "disabled";
0127 }
0128 
0129 #endif