Debian Codegraph @

src/utf8.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server daemon
3 * Copyright (c) 2006-2022 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* UTF8 encoding/decoding */
26
27 #include "conf.h"
28
29 #if defined(PR_USE_NLS)
30
31 #ifdef HAVE_ICONV_H
32 # include <iconv.h>
33 #endif
34
35 #ifdef HAVE_LANGINFO_H
36 # include <langinfo.h>
37 #endif
38
39 #if defined(HAVE_ICONV_H)
40 static iconv_t decode_conv = (iconv_t) -1;
41 static iconv_t encode_conv = (iconv_t) -1;
42
43 static int utf8_convert(iconv_t conv, char *inbuf, size_t *inbuflen,
44 char *outbuf, size_t *outbuflen) {
45 # if defined(HAVE_ICONV)
46 char *start = inbuf;
47
48 while (inbuflen > 0) {
49 size_t nconv;
50
51 pr_signals_handle();
52
53 nconv = iconv(conv, &inbuf, inbuflen, &outbuf, outbuflen);
54 if (nconv == (size_t) -1) {
55 if (errno == EINVAL) {
56 memmove(start, inbuf, *inbuflen);
57 continue;
58 }
59
60 return -1;
61 }
62
63 break;
64 }
65 return 0;
66 # else
67 errno = ENOSYS;
68 return -1;
69 # endif /* HAVE_ICONV */
70 }
71 #endif /* !HAVE_ICONV_H */
72
73 int utf8_free(void) {
74 # if defined(HAVE_ICONV)
75 int res;
76
77 /* Close the iconv handles. */
78 res = iconv_close(encode_conv);
79 if (res < 0) {
80 return -1;
81 }
82
83 res = iconv_close(decode_conv);
84 if (res < 0) {
85 return -1;
86 }
87
88 return 0;
89 # else
90 errno = ENOSYS;
91 return -1;
92 # endif
93 }
94
95 int utf8_init(void) {
96 const char *local_charset;
97
98 #if defined(HAVE_NL_LANGINFO)
99 /* Look up the current charset. If there's a problem, default to
100 * UCS-2.
101 */
102 local_charset = nl_langinfo(CODESET);
103 if (local_charset == NULL) {
104 local_charset = "C";
105 pr_trace_msg("utf8", 1,
106 "unable to determine locale, defaulting to 'C' for UTF8 conversion");
107
108 } else {
109 pr_trace_msg("utf8", 1, "converting UTF8 to local character set '%s'",
110 local_charset);
111 }
112 #else
113 local_charset = "C";
114 pr_trace_msg("utf8", 1,
115 "nl_langinfo(3) not supported, defaulting to using 'C' for UTF8 "
116 "conversion");
117 #endif /* HAVE_NL_LANGINFO */
118
119 # if defined(HAVE_ICONV)
120 /* Get the iconv handles. */
121 encode_conv = iconv_open("UTF-8", local_charset);
122 if (encode_conv == (iconv_t) -1) {
123 return -1;
124 }
125
126 decode_conv = iconv_open(local_charset, "UTF-8");
127 if (decode_conv == (iconv_t) -1) {
128 return -1;
129 }
130
131 return 0;
132 # else
133 errno = ENOSYS;
134 return -1;
135 # endif /* HAVE_ICONV */
136 }
137
138 char *pr_utf8_decode(pool *p, const char *in, size_t inlen, size_t *outlen) {
139 #if defined(HAVE_ICONV_H)
140 size_t inbuflen, outbuflen;
141 char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res = NULL;
142
143 if (p == NULL ||
144 in == NULL ||
145 outlen == NULL) {
146 errno = EINVAL;
147 return NULL;
148 }
149
150 if (decode_conv == (iconv_t) -1) {
151 errno = EPERM;
152 return NULL;
153 }
154
155 inbuf = pcalloc(p, inlen);
156 memcpy(inbuf, in, inlen);
157 inbuflen = inlen;
158
159 outbuflen = sizeof(outbuf);
160
161 if (utf8_convert(decode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) {
162 return NULL;
163 }
164
165 *outlen = sizeof(outbuf) - outbuflen;
166 res = pcalloc(p, *outlen);
167 memcpy(res, outbuf, *outlen);
168
169 return res;
170 #else
171 pr_trace_msg("utf8", 1, "missing iconv support, no UTF8 decoding possible");
172 return pstrdup(p, in);
173 #endif /* !HAVE_ICONV_H */
174 }
175
176 char *pr_utf8_encode(pool *p, const char *in, size_t inlen, size_t *outlen) {
177 #if defined(HAVE_ICONV_H)
178 size_t inbuflen, outbuflen;
179 char *inbuf, outbuf[PR_TUNABLE_PATH_MAX*2], *res;
180
181 if (p == NULL ||
182 in == NULL ||
183 outlen == NULL) {
184 errno = EINVAL;
185 return NULL;
186 }
187
188 if (encode_conv == (iconv_t) -1) {
189 errno = EPERM;
190 return NULL;
191 }
192
193 inbuf = pcalloc(p, inlen);
194 memcpy(inbuf, in, inlen);
195 inbuflen = inlen;
196
197 outbuflen = sizeof(outbuf);
198
199 if (utf8_convert(encode_conv, inbuf, &inbuflen, outbuf, &outbuflen) < 0) {
200 return NULL;
201 }
202
203 *outlen = sizeof(outbuf) - outbuflen;
204 res = pcalloc(p, *outlen);
205 memcpy(res, outbuf, *outlen);
206
207 return res;
208 #else
209 pr_trace_msg("utf8", 1, "missing iconv support, no UTF8 encoding possible");
210 return pstrdup(p, in);
211 #endif /* !HAVE_ICONV_H */
212 }
213 #endif /* PR_USE_NLS */