unvis(3bsd)

πŸ“¦
version 0.12.2 Β· 2 versions
  • 0.12.2 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.11.5 Β· ubuntu jammy
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– unvis(3bsd) β€” decode a visual representation of characters πŸ’–

okay bestie, gather round, because today we're learning how to take a visual representation of characters and turn it back into the real thing. it's giving reverse-engineering, it's giving glow-up, it's literally the counterpart to vis(3bsd). this page lives in the LOCAL section of the unvis(3bsd) manual and honestly? she's iconic. πŸ¦‹


🌸 LIBRARY 🌸

Utility functions from BSD systems (libbsd, -lbsd) ✨


πŸŽ€ SYNOPSIS πŸŽ€

#include <vis.h>

(See libbsd(7) for include usage.)

int
unvis(char *cp, int c, int *astate, int flag);

int
strunvis(char *dst, const char *src);

int
strnunvis(char *dst, size_t dlen, const char *src);

int
strunvisx(char *dst, const char *src, int flag);

int
strnunvisx(char *dst, size_t dlen, const char *src, int flag);

✨ DESCRIPTION ✨

so here's the tea πŸ“: unvis(), strunvis() and strunvisx() are the functions you call when you want to decode a visual representation of characters β€” the exact kind of thing vis(3bsd) spits out β€” and get the original form back. no cap.

πŸ’« the unvis() vibe

unvis() is the one you feed successive characters in c, one at a time, until a valid sequence gets recognized. the moment that happens, the decoded character is just there, chilling at the character pointed to by cp. main character energy tbh πŸ’…

πŸ’« the strunvis() vibe

strunvis() decodes the characters pointed to by src into the buffer pointed to by dst. basically it simply copies src to dst, decoding any escape sequences along the way, and returns the number of characters placed into dst β€” or -1 if an invalid escape sequence was detected. 😭

⚠️ heads up bestie: the size of dst should be equal to the size of src (that is, no expansion takes place during decoding).

πŸ’« the strunvisx() vibe

strunvisx() does the same thing as strunvis(), but it lets you add a flag that specifies the style src is encoded with. currently the supported flags are: VIS_HTTPSTYLE and VIS_MIMESTYLE. slay. ✨

πŸ’« the state machine (yes, she's real)

unvis() implements a state machine that can decode an arbitrary stream of bytes. here's the important part: all state associated with the bytes being decoded is stored outside the unvis() function β€” that is, a pointer to the state gets passed in. so calls decoding different streams can be freely intermixed. obsessed. 🫢

to start decoding a stream of bytes, first initialize an integer to zero. then call unvis() with each successive byte, along with a pointer to this integer, and a pointer to a destination character.

unvis() has several return codes and you must handle them properly. they are:

  • 0 (zero) β€” another character is necessary; nothing has been recognized yet. πŸ’€
  • UNVIS_VALID β€” a valid character has been recognized and is available at the location pointed to by cp. πŸ’–
  • UNVIS_VALIDPUSH β€” a valid character has been recognized and is available at the location pointed to by cp; however, the character currently passed in should be passed in again. πŸ”
  • UNVIS_NOCHAR β€” a valid sequence was detected, but no character was produced. this return code is necessary to indicate a logical break between characters. 🌸
  • UNVIS_SYNBAD β€” an invalid escape sequence was detected, or the decoder is in an unknown state. the decoder is placed into the starting state. 🚨

when all bytes in the stream have been processed, call unvis() one more time with flag set to UNVIS_END to extract any remaining character (the character passed in is ignored). periodt.

πŸ’« the flag argument

the flag argument is also used to specify the encoding style of the source. here's the breakdown:

  • if set to VIS_HTTPSTYLE or VIS_HTTP1808, unvis() will decode URI strings as specified in RFC 1808.
  • if set to VIS_HTTP1866, unvis() will decode entity references and numeric character references as specified in RFC 1866.
  • if set to VIS_MIMESTYLE, unvis() will decode MIME Quoted-Printable strings as specified in RFC 2045.
  • if set to VIS_NOESCAPE, unvis() will not decode β€˜\’ quoted characters.

πŸ’« the code fragment

the following code fragment illustrates a proper use of unvis() β€” copy it, love it, respect it πŸ™

int state = 0;
char out;

while ((ch = getchar()) != EOF) {
again:
        switch(unvis(&out, ch, &state, 0)) {
        case 0:
        case UNVIS_NOCHAR:
                break;
        case UNVIS_VALID:
                (void)putchar(out);
                break;
        case UNVIS_VALIDPUSH:
                (void)putchar(out);
                goto again;
        case UNVIS_SYNBAD:
                errx(EXIT_FAILURE, "Bad character sequence!");
        }
}
if (unvis(&out, '\0', &state, UNVIS_END) == UNVIS_VALID)
        (void)putchar(out);

πŸ”₯ ERRORS πŸ”₯

the functions strunvis(), strnunvis(), strunvisx(), and strnunvisx() will return -1 on error and set errno to:

  • [EINVAL] β€” an invalid escape sequence was detected, or the decoder is in an unknown state. πŸ’€

in addition, the functions strnunvis() and strnunvisx() will can also set errno on error to:

  • [ENOSPC] β€” not enough space to perform the conversion. 😭

🌈 SEE ALSO 🌈


πŸ’œ HISTORY πŸ’œ

the unvis() function first appeared in 4.4BSD. the strnunvis() and strnunvisx() functions appeared in NetBSD 6.0. glow-up timeline fr fr ✨


πŸ™ˆ BUGS πŸ™ˆ

the names VIS_HTTP1808 and VIS_HTTP1866 are wrong. πŸ’€ like, not me calling out the naming committee but okay. percent-encoding was defined in RFC 1738, the original RFC for URL. and RFC 1866 defines HTML 2.0, an application of SGML, from which it inherits concepts of numeric character references and entity references. so yeah. the names are lies. iykyk. πŸŽ€


and that's the whole page bestie!! this one came from the <strong>Debian</strong> folks, dated <strong>March 12, 2011</strong>, and it's filed under unvis(3bsd). go forth and decode, you absolute legend πŸ‘‘πŸ’…πŸ¦‹βœ¨

πŸ“¦ where this page comes from