string_helpers.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef _LINUX_STRING_HELPERS_H_
  2. #define _LINUX_STRING_HELPERS_H_
  3. #include <linux/types.h>
  4. /* Descriptions of the types of units to
  5. * print in */
  6. enum string_size_units {
  7. STRING_UNITS_10, /* use powers of 10^3 (standard SI) */
  8. STRING_UNITS_2, /* use binary powers of 2^10 */
  9. };
  10. int string_get_size(u64 size, enum string_size_units units,
  11. char *buf, int len);
  12. #define UNESCAPE_SPACE 0x01
  13. #define UNESCAPE_OCTAL 0x02
  14. #define UNESCAPE_HEX 0x04
  15. #define UNESCAPE_SPECIAL 0x08
  16. #define UNESCAPE_ANY \
  17. (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
  18. /**
  19. * string_unescape - unquote characters in the given string
  20. * @src: source buffer (escaped)
  21. * @dst: destination buffer (unescaped)
  22. * @size: size of the destination buffer (0 to unlimit)
  23. * @flags: combination of the flags (bitwise OR):
  24. * %UNESCAPE_SPACE:
  25. * '\f' - form feed
  26. * '\n' - new line
  27. * '\r' - carriage return
  28. * '\t' - horizontal tab
  29. * '\v' - vertical tab
  30. * %UNESCAPE_OCTAL:
  31. * '\NNN' - byte with octal value NNN (1 to 3 digits)
  32. * %UNESCAPE_HEX:
  33. * '\xHH' - byte with hexadecimal value HH (1 to 2 digits)
  34. * %UNESCAPE_SPECIAL:
  35. * '\"' - double quote
  36. * '\\' - backslash
  37. * '\a' - alert (BEL)
  38. * '\e' - escape
  39. * %UNESCAPE_ANY:
  40. * all previous together
  41. *
  42. * Returns amount of characters processed to the destination buffer excluding
  43. * trailing '\0'.
  44. *
  45. * Because the size of the output will be the same as or less than the size of
  46. * the input, the transformation may be performed in place.
  47. *
  48. * Caller must provide valid source and destination pointers. Be aware that
  49. * destination buffer will always be NULL-terminated. Source string must be
  50. * NULL-terminated as well.
  51. */
  52. int string_unescape(char *src, char *dst, size_t size, unsigned int flags);
  53. static inline int string_unescape_inplace(char *buf, unsigned int flags)
  54. {
  55. return string_unescape(buf, buf, 0, flags);
  56. }
  57. static inline int string_unescape_any(char *src, char *dst, size_t size)
  58. {
  59. return string_unescape(src, dst, size, UNESCAPE_ANY);
  60. }
  61. static inline int string_unescape_any_inplace(char *buf)
  62. {
  63. return string_unescape_any(buf, buf, 0);
  64. }
  65. #endif