unvis(3bsd)
π 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 ofdstshould be equal to the size ofsrc(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 bycp. πUNVIS_VALIDPUSHβ a valid character has been recognized and is available at the location pointed to bycp; 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_HTTPSTYLEorVIS_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
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian bookworm: libbsd-dev 0.11.7-2
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms