xref: /netbsd-src/lib/libc/gen/unvis.3 (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1.\"	$NetBSD: unvis.3,v 1.23 2011/03/17 14:06:29 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 (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 cp.
133.It Dv UNVIS_VALIDPUSH
134A valid character has been recognized and is available at the location
135pointed to by cp; however, the character currently passed in should
136be passed in again.
137.It Dv UNVIS_NOCHAR
138A valid sequence was detected, but no character was produced.
139This return code is necessary to indicate a logical break between characters.
140.It Dv UNVIS_SYNBAD
141An invalid escape sequence was detected, or the decoder is in an unknown state.
142The decoder is placed into the starting state.
143.El
144.Pp
145When all bytes in the stream have been processed, call
146.Fn unvis
147one more time with flag set to
148.Dv UNVIS_END
149to extract any remaining character (the character passed in is ignored).
150.Pp
151The
152.Ar flag
153argument is also used to specify the encoding style of the source.
154If set to
155.Dv VIS_HTTPSTYLE
156or
157.Dv VIS_HTTP1808 ,
158.Fn unvis
159will decode URI strings as specified in RFC 1808.
160If set to
161.Dv VIS_HTTP1866 ,
162.Fn unvis
163will decode URI strings as specified in RFC 1866.
164If set to
165.Dv VIS_MIMESTYLE ,
166.Fn unvis
167will decode MIME Quoted-Printable strings as specified in RFC 2045.
168If set to
169.Dv VIS_NOESCAPE ,
170.Fn unvis
171will not decode \e quoted characters.
172.Pp
173The following code fragment illustrates a proper use of
174.Fn unvis .
175.Bd -literal -offset indent
176int state = 0;
177char out;
178
179while ((ch = getchar()) != EOF) {
180again:
181	switch(unvis(\*[Am]out, ch, \*[Am]state, 0)) {
182	case 0:
183	case UNVIS_NOCHAR:
184		break;
185	case UNVIS_VALID:
186		(void)putchar(out);
187		break;
188	case UNVIS_VALIDPUSH:
189		(void)putchar(out);
190		goto again;
191	case UNVIS_SYNBAD:
192		errx(EXIT_FAILURE, "Bad character sequence!");
193	}
194}
195if (unvis(\*[Am]out, '\e0', \*[Am]state, UNVIS_END) == UNVIS_VALID)
196	(void)putchar(out);
197.Ed
198.Sh ERRORS
199The functions
200.Fn strunvis ,
201.Fn strnunvis ,
202.Fn strunvisx ,
203and
204.Fn strnunvisx
205will return \-1 on error and set
206.Va errno
207to:
208.Bl -tag -width Er
209.It Bq Er EINVAL
210An invalid escape sequence was detected, or the decoder is in an unknown state.
211.El
212.Pp
213In addition the functions
214.Fn strnunvis
215and
216.Fn strnunvisx
217will can also set
218.Va errno
219on error to:
220.Bl -tag -width Er
221.It Bq Er ENOSPC
222Not enough space to perform the conversion.
223.El
224.Sh SEE ALSO
225.Xr unvis 1 ,
226.Xr vis 1 ,
227.Xr vis 3
228.Rs
229.%A R. Fielding
230.%T Relative Uniform Resource Locators
231.%O RFC1808
232.Re
233.Sh HISTORY
234The
235.Fn unvis
236function
237first appeared in
238.Bx 4.4 .
239The
240.Fn strnunvis
241and
242.Fn strnunvisx
243functions appeared in
244.Nx 6.0 .
245