cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

string_helpers.h (3290B)


      1/* SPDX-License-Identifier: GPL-2.0 */
      2#ifndef _LINUX_STRING_HELPERS_H_
      3#define _LINUX_STRING_HELPERS_H_
      4
      5#include <linux/bits.h>
      6#include <linux/ctype.h>
      7#include <linux/string.h>
      8#include <linux/types.h>
      9
     10struct device;
     11struct file;
     12struct task_struct;
     13
     14/* Descriptions of the types of units to
     15 * print in */
     16enum string_size_units {
     17	STRING_UNITS_10,	/* use powers of 10^3 (standard SI) */
     18	STRING_UNITS_2,		/* use binary powers of 2^10 */
     19};
     20
     21void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
     22		     char *buf, int len);
     23
     24#define UNESCAPE_SPACE		BIT(0)
     25#define UNESCAPE_OCTAL		BIT(1)
     26#define UNESCAPE_HEX		BIT(2)
     27#define UNESCAPE_SPECIAL	BIT(3)
     28#define UNESCAPE_ANY		\
     29	(UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
     30
     31#define UNESCAPE_ALL_MASK	GENMASK(3, 0)
     32
     33int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
     34
     35static inline int string_unescape_inplace(char *buf, unsigned int flags)
     36{
     37	return string_unescape(buf, buf, 0, flags);
     38}
     39
     40static inline int string_unescape_any(char *src, char *dst, size_t size)
     41{
     42	return string_unescape(src, dst, size, UNESCAPE_ANY);
     43}
     44
     45static inline int string_unescape_any_inplace(char *buf)
     46{
     47	return string_unescape_any(buf, buf, 0);
     48}
     49
     50#define ESCAPE_SPACE		BIT(0)
     51#define ESCAPE_SPECIAL		BIT(1)
     52#define ESCAPE_NULL		BIT(2)
     53#define ESCAPE_OCTAL		BIT(3)
     54#define ESCAPE_ANY		\
     55	(ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL)
     56#define ESCAPE_NP		BIT(4)
     57#define ESCAPE_ANY_NP		(ESCAPE_ANY | ESCAPE_NP)
     58#define ESCAPE_HEX		BIT(5)
     59#define ESCAPE_NA		BIT(6)
     60#define ESCAPE_NAP		BIT(7)
     61#define ESCAPE_APPEND		BIT(8)
     62
     63#define ESCAPE_ALL_MASK		GENMASK(8, 0)
     64
     65int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
     66		unsigned int flags, const char *only);
     67
     68static inline int string_escape_mem_any_np(const char *src, size_t isz,
     69		char *dst, size_t osz, const char *only)
     70{
     71	return string_escape_mem(src, isz, dst, osz, ESCAPE_ANY_NP, only);
     72}
     73
     74static inline int string_escape_str(const char *src, char *dst, size_t sz,
     75		unsigned int flags, const char *only)
     76{
     77	return string_escape_mem(src, strlen(src), dst, sz, flags, only);
     78}
     79
     80static inline int string_escape_str_any_np(const char *src, char *dst,
     81		size_t sz, const char *only)
     82{
     83	return string_escape_str(src, dst, sz, ESCAPE_ANY_NP, only);
     84}
     85
     86static inline void string_upper(char *dst, const char *src)
     87{
     88	do {
     89		*dst++ = toupper(*src);
     90	} while (*src++);
     91}
     92
     93static inline void string_lower(char *dst, const char *src)
     94{
     95	do {
     96		*dst++ = tolower(*src);
     97	} while (*src++);
     98}
     99
    100char *kstrdup_quotable(const char *src, gfp_t gfp);
    101char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp);
    102char *kstrdup_quotable_file(struct file *file, gfp_t gfp);
    103
    104char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n);
    105void kfree_strarray(char **array, size_t n);
    106
    107char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n);
    108
    109static inline const char *str_yes_no(bool v)
    110{
    111	return v ? "yes" : "no";
    112}
    113
    114static inline const char *str_on_off(bool v)
    115{
    116	return v ? "on" : "off";
    117}
    118
    119static inline const char *str_enable_disable(bool v)
    120{
    121	return v ? "enable" : "disable";
    122}
    123
    124static inline const char *str_enabled_disabled(bool v)
    125{
    126	return v ? "enabled" : "disabled";
    127}
    128
    129#endif