summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/u8_validate.3c
blob: f8700785010fe0b7ae90b23f10c4ba5d69f18476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'\" te
.\" Copyright (c) 2007, Sun Microsystems Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH U8_VALIDATE 3C "Sep 18, 2007"
.SH NAME
u8_validate \- validate UTF-8 characters and calculate the byte length
.SH SYNOPSIS
.LP
.nf
#include <sys/u8_textprep.h>

\fBint\fR u8_validate(\fBchar *\fR\fIu8str\fR, \fBsize_t\fR \fIn\fR, \fBchar **\fR\fIlist\fR, \fBint\fR \fIflag\fR,
     \fBint *\fR\fIerrnum\fR);
.fi

.SH PARAMETERS
.sp
.ne 2
.na
\fB\fIu8str\fR\fR
.ad
.RS 10n
The UTF-8 string to be validated.
.RE

.sp
.ne 2
.na
\fB\fIn\fR\fR
.ad
.RS 10n
The maximum number of bytes in \fIu8str\fR that can be examined and validated.
.RE

.sp
.ne 2
.na
\fB\fIlist\fR\fR
.ad
.RS 10n
A list of null-terminated character strings in UTF-8 that must be additionally
checked against as invalid characters. The last string in \fIlist\fR must be
null to indicate there is no further string.
.RE

.sp
.ne 2
.na
\fB\fIflag\fR\fR
.ad
.RS 10n
Possible validation options constructed by a bitwise-inclusive-OR of the
following values:
.sp
.ne 2
.na
\fB\fBU8_VALIDATE_ENTIRE\fR\fR
.ad
.sp .6
.RS 4n
By default, \fBu8_validate()\fR looks at the first character or up to \fIn\fR
bytes, whichever is smaller in terms of the number of bytes to be consumed, and
returns with the result.
.sp
When this option is used, \fBu8_validate()\fR will check up to \fIn\fR bytes
from \fIu8str\fR and possibly more than a character before returning the
result.
.RE

.sp
.ne 2
.na
\fB\fBU8_VALIDATE_CHECK_ADDITIONAL\fR\fR
.ad
.sp .6
.RS 4n
By default, \fBu8_validate()\fR does not use list supplied.
.sp
When this option is supplied with a list of character strings,
\fBu8_validate()\fR additionally validates \fIu8str\fR against the character
strings supplied with \fIlist\fR and returns EBADF in \fIerrnum\fR if
\fIu8str\fR has any one of the character strings in \fIlist\fR.
.RE

.sp
.ne 2
.na
\fB\fBU8_VALIDATE_UCS2_RANGE\fR\fR
.ad
.sp .6
.RS 4n
By default, \fBu8_validate()\fR uses the entire Unicode coding space of U+0000
to U+10FFFF.
.sp
When this option is specified, the valid Unicode coding space is smaller to
U+0000 to U+FFFF.
.RE

.RE

.sp
.ne 2
.na
\fB\fIerrnum\fR\fR
.ad
.RS 10n
An error occurred during validation.  The following values are supported:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 10n
Validation failed because list-specified characters were found in the string
pointed to by \fIu8str\fR.
.RE

.sp
.ne 2
.na
\fB\fBEILSEQ\fR\fR
.ad
.RS 10n
Validation failed because an illegal byte was found in the string pointed to by
\fIu8str\fR.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
Validation failed because an incomplete byte was found in the string pointed to
by  \fIu8str\fR.
.RE

.sp
.ne 2
.na
\fB\fBERANGE\fR\fR
.ad
.RS 10n
Validation failed because character bytes were encountered that are outside the
range of the Unicode coding space.
.RE

.RE

.SH DESCRIPTION
.sp
.LP
The \fBu8_validate()\fR function validates \fIu8str\fR in UTF-8 and determines
the number of bytes constituting the character(s) pointed to by \fIu8str\fR.
.SH RETURN VALUES
.sp
.LP
If \fIu8str\fR is a null pointer, \fBu8_validate()\fR returns 0. Otherwise,
\fBu8_validate()\fR returns either the number of bytes that constitute the
characters if the next \fIn\fR or fewer bytes form valid characters, or -1 if
there is an validation failure, in which case it may set \fIerrnum\fR to
indicate the error.
.SH EXAMPLES
.LP
\fBExample 1 \fRDetermine the length of the first UTF-8 character.
.sp
.in +2
.nf
#include <sys/u8_textprep.h>

char u8[MAXPATHLEN];
int errnum;
\&.
\&.
\&.
len = u8_validate(u8, 4, (char **)NULL, 0, &errnum);
if (len == -1) {
    switch (errnum) {
        case EILSEQ:
        case EINVAL:
            return (MYFS4_ERR_INVAL);
        case EBADF:
            return (MYFS4_ERR_BADNAME);
        case ERANGE:
            return (MYFS4_ERR_BADCHAR);
        default:
            return (-10);
    }
}
.fi
.in -2

.LP
\fBExample 2 \fRCheck if there are any invalid characters in the entire string.
.sp
.in +2
.nf
#include <sys/u8_textprep.h>

char u8[MAXPATHLEN];
int n;
int errnum;
\&.
\&.
\&.
n = strlen(u8);
len = u8_validate(u8, n, (char **)NULL, U8_VALIDATE_ENTIRE, &errnum);
if (len == -1) {
    switch (errnum) {
        case EILSEQ:
        case EINVAL:
            return (MYFS4_ERR_INVAL);
        case EBADF:
            return (MYFS4_ERR_BADNAME);
        case ERANGE:
            return (MYFS4_ERR_BADCHAR);
        default:
            return (-10);
    }
}
.fi
.in -2

.LP
\fBExample 3 \fRCheck if there is any invalid character, including prohibited
characters, in the entire string.
.sp
.in +2
.nf
#include <sys/u8_textprep.h>

char u8[MAXPATHLEN];
int n;
int errnum;
char *prohibited[4] = {
    ".", "..", "\e\e", NULL
};
\&.
\&.
\&.
n = strlen(u8);
len = u8_validate(u8, n, prohibited,
    (U8_VALIDATE_ENTIRE|U8_VALIDATE_CHECK_ADDITIONAL), &errnum);
if (len == -1) {
    switch (errnum) {
        case EILSEQ:
        case EINVAL:
            return (MYFS4_ERR_INVAL);
        case EBADF:
            return (MYFS4_ERR_BADNAME);
        case ERANGE:
            return (MYFS4_ERR_BADCHAR);
        default:
            return (-10);
    }
}
.fi
.in -2

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.sp
.LP
\fBu8_strcmp\fR(3C), \fBu8_textprep_str\fR(3C), \fBattributes\fR(5),
\fBu8_strcmp\fR(9F), \fBu8_textprep_str\fR(9F), \fBu8_validate\fR(9F)
.sp
.LP
The Unicode Standard (http://www.unicode.org)