string_escape_mem(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

okay bestie, welcome to string_escape_mem(9) from the Kernel API Manual ✨ this is literally the page about quoting characters in the given memory buffer, and we are keeping every flag, every byte, and every caveat. no cap πŸ’…

πŸŽ€ NAME πŸŽ€

string_escape_mem β€” quote characters in the given memory buffer. That's the whole vibe πŸ’–

🌸 SYNOPSIS 🌸

int string_escape_mem (const char *src , size_t isz , char *dst , size_t osz , unsigned int flags , const char *only );

πŸ¦‹ ARGUMENTS πŸ¦‹

  • src β€” source buffer (unescaped)
  • isz β€” source buffer size
  • dst β€” destination buffer (escaped)
  • osz β€” destination buffer size
  • flags β€” combination of the flags
  • only β€” NULL-terminated string containing characters used to limit the selected escape class. If characters are included in only that would not normally be escaped by the classes selected in flags, they will be copied to dst unescaped.

✨ DESCRIPTION ✨

The process of escaping byte buffer includes several parts. They are applied in the following sequence.

  1. The character is not matched to the one from only string and thus must go as-is to the output.
  2. The character is matched to the printable and ASCII classes, if asked, and in case of match it passes through to the output.
  3. The character is matched to the printable or ASCII class, if asked, and in case of match it passes through to the output.
  4. The character is checked if it falls into the class given by flags. ESCAPE_OCTAL and ESCAPE_HEX are going last since they cover any character. Note that they actually can't go together, otherwise ESCAPE_HEX will be ignored.
Caller must provide valid source and destination pointers. Be aware that destination buffer will not be NULL-terminated, thus caller have to append it if needs. This part is important, bestie 😭

The supported flags are:

  • ESCAPE_SPACE β€” (special white space, not space itself)
    • '\f' β€” form feed
    • '\r' β€” carriage return
    • '\n' β€” newline
    • '\t' β€” horizontal tab
    • '\v' β€” vertical tab
  • ESCAPE_SPECIAL β€” this one covers the specials:
    • '\'' β€” single quote
    • '\\' β€” backslash
    • '\a' β€” alert (BEL)
    • '\e' β€” escape
  • ESCAPE_NULL β€” '\0' β€” null
  • ESCAPE_OCTAL β€” '\NNN' β€” byte with octal value NNN (3 digits)
  • ESCAPE_ANY β€” all previous together
  • ESCAPE_NP β€” escape only non-printable characters, checked by isprint
  • ESCAPE_ANY_NP β€” all previous together
  • ESCAPE_HEX β€” '\xHH' β€” byte with hexadecimal value HH (2 digits)
  • ESCAPE_NA β€” escape only non-ascii characters, checked by isascii
  • ESCAPE_NAP β€” escape only non-printable or non-ascii characters
  • ESCAPE_APPEND β€” append characters from only to be escaped by the given classes

ESCAPE_APPEND would help to pass additional characters to the escaped, when one of ESCAPE_NP, ESCAPE_NA, or ESCAPE_NAP is provided.

One notable caveat: the ESCAPE_NAP, ESCAPE_NP and ESCAPE_NA have the higher priority than the rest of the flags (ESCAPE_NAP is the highest). It doesn't make much sense to use either of them without ESCAPE_OCTAL or ESCAPE_HEX, because they cover most of the other character classes. ESCAPE_NAP can utilize ESCAPE_SPACE or ESCAPE_SPECIAL in addition to the above.

πŸ’œ RETURN πŸ’œ

The total size of the escaped output that would be generated for the given input and flags. To check whether the output was truncated, compare the return value to osz. There is room left in dst for a '\0' terminator if and only if ret < osz.

πŸ‘‘ SEE ALSO πŸ‘‘

Kernel file ./lib/string_helpers.c, plus the whole squad: string_get_size(9), parse_int_array_user(9), string_unescape(9), kasprintf_strarray(9), kfree_strarray(9), skip_spaces(9), strim(9), sysfs_streq(9), match_string(9), __sysfs_match_string(9), strreplace(9), memcpy_and_pad(9).


made with πŸ’… from source ./lib, dated October 2026, page string_escape_mem(9) β€” mwah 🫢

πŸ“¦ where this page comes from