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