xref: /minix3/lib/libc/gen/unvis.3 (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1.\"	$NetBSD: unvis.3,v 1.27 2012/12/15 07:34:36 wiz Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)unvis.3	8.2 (Berkeley) 12/11/93
31.\"
32.Dd March 12, 2011
33.Dt UNVIS 3
34.Os
35.Sh NAME
36.Nm unvis ,
37.Nm strunvis
38.Nd decode a visual representation of characters
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In vis.h
43.Ft int
44.Fn unvis "char *cp" "int c" "int *astate" "int flag"
45.Ft int
46.Fn strunvis "char *dst" "const char *src"
47.Ft int
48.Fn strnunvis "char *dst" "size_t dlen" "const char *src"
49.Ft int
50.Fn strunvisx "char *dst" "const char *src" "int flag"
51.Ft int
52.Fn strnunvisx "char *dst" "size_t dlen" "const char *src" "int flag"
53.Sh DESCRIPTION
54The
55.Fn unvis ,
56.Fn strunvis
57and
58.Fn strunvisx
59functions
60are used to decode a visual representation of characters, as produced
61by the
62.Xr vis 3
63function, back into
64the original form.
65.Pp
66The
67.Fn unvis
68function is called with successive characters in
69.Ar c
70until a valid sequence is recognized, at which time the decoded
71character is available at the character pointed to by
72.Ar cp .
73.Pp
74The
75.Fn strunvis
76function decodes the characters pointed to by
77.Ar src
78into the buffer pointed to by
79.Ar dst .
80The
81.Fn strunvis
82function simply copies
83.Ar src
84to
85.Ar dst ,
86decoding any escape sequences along the way,
87and returns the number of characters placed into
88.Ar dst ,
89or \-1 if an
90invalid escape sequence was detected.
91The size of
92.Ar dst
93should be equal to the size of
94.Ar src
95(that is, no expansion takes place during decoding).
96.Pp
97The
98.Fn strunvisx
99function does the same as the
100.Fn strunvis
101function,
102but it allows you to add a flag that specifies the style the string
103.Ar src
104is encoded with.
105Currently, the supported flags are:
106.Dv VIS_HTTPSTYLE
107and
108.Dv VIS_MIMESTYLE .
109.Pp
110The
111.Fn unvis
112function implements a state machine that can be used to decode an
113arbitrary stream of bytes.
114All state associated with the bytes being decoded is stored outside the
115.Fn unvis
116function (that is, a pointer to the state is passed in), so
117calls decoding different streams can be freely intermixed.
118To start decoding a stream of bytes, first initialize an integer to zero.
119Call
120.Fn unvis
121with each successive byte, along with a pointer
122to this integer, and a pointer to a destination character.
123The
124.Fn unvis
125function has several return codes that must be handled properly.
126They are:
127.Bl -tag -width UNVIS_VALIDPUSH
128.It Li \&0 No (zero)
129Another character is necessary; nothing has been recognized yet.
130.It Dv UNVIS_VALID
131A valid character has been recognized and is available at the location
132pointed to by
133.Fa cp .
134.It Dv UNVIS_VALIDPUSH
135A valid character has been recognized and is available at the location
136pointed to by
137.Fa cp ;
138however, the character currently passed in should be passed in again.
139.It Dv UNVIS_NOCHAR
140A valid sequence was detected, but no character was produced.
141This return code is necessary to indicate a logical break between characters.
142.It Dv UNVIS_SYNBAD
143An invalid escape sequence was detected, or the decoder is in an unknown state.
144The decoder is placed into the starting state.
145.El
146.Pp
147When all bytes in the stream have been processed, call
148.Fn unvis
149one more time with flag set to
150.Dv UNVIS_END
151to extract any remaining character (the character passed in is ignored).
152.Pp
153The
154.Fa flag
155argument is also used to specify the encoding style of the source.
156If set to
157.Dv VIS_HTTPSTYLE
158or
159.Dv VIS_HTTP1808 ,
160.Fn unvis
161will decode URI strings as specified in RFC 1808.
162If set to
163.Dv VIS_HTTP1866 ,
164.Fn unvis
165will decode entity references and numeric character references
166as specified in RFC 1866.
167If set to
168.Dv VIS_MIMESTYLE ,
169.Fn unvis
170will decode MIME Quoted-Printable strings as specified in RFC 2045.
171If set to
172.Dv VIS_NOESCAPE ,
173.Fn unvis
174will not decode
175.Ql \e
176quoted characters.
177.Pp
178The following code fragment illustrates a proper use of
179.Fn unvis .
180.Bd -literal -offset indent
181int state = 0;
182char out;
183
184while ((ch = getchar()) != EOF) {
185again:
186	switch(unvis(\*[Am]out, ch, \*[Am]state, 0)) {
187	case 0:
188	case UNVIS_NOCHAR:
189		break;
190	case UNVIS_VALID:
191		(void)putchar(out);
192		break;
193	case UNVIS_VALIDPUSH:
194		(void)putchar(out);
195		goto again;
196	case UNVIS_SYNBAD:
197		errx(EXIT_FAILURE, "Bad character sequence!");
198	}
199}
200if (unvis(\*[Am]out, '\e0', \*[Am]state, UNVIS_END) == UNVIS_VALID)
201	(void)putchar(out);
202.Ed
203.Sh ERRORS
204The functions
205.Fn strunvis ,
206.Fn strnunvis ,
207.Fn strunvisx ,
208and
209.Fn strnunvisx
210will return \-1 on error and set
211.Va errno
212to:
213.Bl -tag -width Er
214.It Bq Er EINVAL
215An invalid escape sequence was detected, or the decoder is in an unknown state.
216.El
217.Pp
218In addition the functions
219.Fn strnunvis
220and
221.Fn strnunvisx
222will can also set
223.Va errno
224on error to:
225.Bl -tag -width Er
226.It Bq Er ENOSPC
227Not enough space to perform the conversion.
228.El
229.Sh SEE ALSO
230.Xr unvis 1 ,
231.Xr vis 1 ,
232.Xr vis 3
233.Rs
234.%A R. Fielding
235.%T Relative Uniform Resource Locators
236.%O RFC1808
237.Re
238.Sh HISTORY
239The
240.Fn unvis
241function
242first appeared in
243.Bx 4.4 .
244The
245.Fn strnunvis
246and
247.Fn strnunvisx
248functions appeared in
249.Nx 6.0 .
250.Sh BUGS
251The names
252.Dv VIS_HTTP1808
253and
254.Dv VIS_HTTP1866
255are wrong.
256Percent-encoding was defined in RFC 1738, the original RFC for URL.
257RFC 1866 defines HTML 2.0, an application of SGML, from which it
258inherits concepts of numeric character references and entity
259references.
260