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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/* vi:ai:et:ts=8 sw=2
*/
/*
* wzdftpd - a modular and cool ftp server
* Copyright (C) 2002-2004 Pierre Chifflier
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* As a special exemption, Pierre Chifflier
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
#include "wzd_all.h"
#ifndef WZD_USE_PCH
#include <stdio.h>
#include <string.h>
#else /* WZD_USE_PCH */
#endif /*WZD_USE_PCH */
#ifdef HAVE_WCHAR_H
# include <wchar.h>
#endif
#ifdef HAVE_ICONV
# include <iconv.h>
#else
typedef void * iconv_t;
#endif
#if HAVE_LANGINFO_CODESET
# include <langinfo.h>
#endif
#ifdef WIN32
# include <winsock2.h>
#endif
#include "wzd_structs.h"
#include "wzd_log.h"
#include "wzd_utf8.h"
#include "wzd_debug.h"
#ifdef BSD
#define DL_ARG DL_LAZY
#else
#define DL_ARG RTLD_NOW
#endif
#ifdef NEED_UNDERSCORE
#define DL_PREFIX "_"
#else
#define DL_PREFIX
#endif
#define DEFAULT_CODESET "ISO-8859-1"
/*typedef void * iconv_t;*/
typedef size_t (*fn_iconv_t)(iconv_t, const char **, size_t *, char **, size_t *);
typedef iconv_t (*fn_iconv_open_t)(const char *, const char *);
typedef int (*fn_iconv_close_t)(iconv_t);
#ifdef WIN32
static void * _iconv_lib_handle = NULL;
#endif
static fn_iconv_t _iconv_fn_iconv = NULL;
static fn_iconv_open_t _iconv_fn_iconv_open = NULL;
static fn_iconv_close_t _iconv_fn_iconv_close = NULL;
static void _iconv_openlib(void)
{
#ifdef HAVE_UTF8
#ifdef HAVE_ICONV
_iconv_fn_iconv = (fn_iconv_t)&iconv;
_iconv_fn_iconv_open = (fn_iconv_open_t)&iconv_open;
_iconv_fn_iconv_close = (fn_iconv_close_t)&iconv_close;
#else /* HAVE_ICONV */
#ifdef WIN32
if (_iconv_lib_handle == NULL)
{
_iconv_lib_handle = dlopen("libiconv-2.dll", DL_ARG);
if (_iconv_lib_handle == NULL) return;
/** \bug I don't understant why this f*cking windows does not find 'libiconv' using
* the name, I've checked with depends.exe: all API calls are good. Windows does
* just not find it, except if I use the ordinal value, which is _very_ bad.
* This clearly looks like a windows bug in GetProcAddress.
* cd c:\HOMEDIR\wzdftpd\visual
* c:\INSTALL\depends21_x86\depends.exe /pg:1 .\Debug\wzdftpd.exe -f wzd-win32.cfg
*/
_iconv_fn_iconv = (fn_iconv_t)dlsym(_iconv_lib_handle, DL_PREFIX "libiconv");
if (!_iconv_fn_iconv) /* try by ordinal */
_iconv_fn_iconv = (fn_iconv_t)dlsym(_iconv_lib_handle, (char*)0x00000004);
_iconv_fn_iconv_open = (fn_iconv_open_t)dlsym(_iconv_lib_handle, DL_PREFIX "libiconv_open");
_iconv_fn_iconv_close = (fn_iconv_close_t)dlsym(_iconv_lib_handle, DL_PREFIX "libiconv_close");
if ( !_iconv_fn_iconv || !_iconv_fn_iconv || !_iconv_fn_iconv_close )
{
dlclose(_iconv_lib_handle);
_iconv_lib_handle = NULL;
}
}
#endif /* WIN32 */
#endif /* HAVE_ICONV */
#endif /* HAVE_UTF8 */
}
static void _iconv_closelib(void)
{
#ifdef WIN32
if (_iconv_lib_handle)
{
dlclose(_iconv_lib_handle);
_iconv_lib_handle = NULL;
_iconv_fn_iconv = NULL;
_iconv_fn_iconv_open = NULL;
_iconv_fn_iconv_close = NULL;
}
#endif /* HAVE_ICONV */
}
static const char * _local_charset = NULL;
const char * local_charset(void)
{
return _local_charset;
}
const char * charset_detect_local(void)
{
char * codeset = NULL;
#ifdef HAVE_UTF8
#if !(defined WIN32)
# if HAVE_LANGINFO_CODESET
/* should be very common now */
codeset = nl_langinfo (CODESET);
if (strcasecmp(codeset,"ansi_x3.4-1968")==0)
codeset = DEFAULT_CODESET;
out_log(LEVEL_FLOOD,"nl_langinfo: %s\n",codeset);
# else
const char * locale = NULL;
/* on old systems, use getenv */
locale = getenv("LC_ALL");
if (locale == NULL || locale[0] == '\0')
{
locale = getenv("LC_CTYPE");
if (locale == NULL || locale[0] == '\0')
locale = getenv("LANG");
}
codeset = locale; /* something like language_COUNTRY.charset */
out_log(LEVEL_FLOOD,"env: %s\n",codeset);
/* we need to try to translate that into an understandable
* codeset for iconv (see `iconv --list`)
*/
# endif
#else /* !WIN32 */
static char buf[2 + 10 + 1];
/* win32 has a function returning the locale's codepage as a number */
sprintf (buf, "CP%u", GetACP());
codeset = buf;
#endif /* !WIN32 */
#endif /* HAVE_UTF8 */
return codeset;
}
int local_charset_to_utf8(const char *src, char *dst_utf8, size_t max_len, const char *local_charset)
{
#ifdef HAVE_UTF8
size_t nconv, size, avail;
mbstate_t state;
iconv_t cd;
if ( !_iconv_fn_iconv || !_iconv_fn_iconv || !_iconv_fn_iconv_close ) return -1;
cd = (*_iconv_fn_iconv_open)("UTF-8", local_charset);
if (cd == (iconv_t)-1) {
return -1;
}
size = strlen(src);
avail = max_len;
memset(&state, '\0', sizeof(state));
/* conversion to multibyte */
nconv = (*_iconv_fn_iconv)(cd, &src, &size, (char**)&dst_utf8, &avail);
if (nconv == (size_t)-1) {
/* error during conversion, see errno */
(*_iconv_fn_iconv_close)(cd);
return -1;
}
(*_iconv_fn_iconv_close)(cd);
/* terminate output string */
if (avail >= sizeof(wchar_t))
*((wchar_t*)dst_utf8) = L'\0';
return 0;
#else /* HAVE_UTF8 */
return 1;
#endif /* HAVE_UTF8 */
}
int utf8_to_local_charset(const char *src_utf8, char *dst, size_t max_len, const char *local_charset)
{
#ifdef HAVE_UTF8
size_t nconv, size, avail;
mbstate_t state;
iconv_t cd;
if ( !_iconv_fn_iconv || !_iconv_fn_iconv || !_iconv_fn_iconv_close ) return -1;
cd = (*_iconv_fn_iconv_open)(local_charset, "UTF-8");
if (cd == (iconv_t)-1) {
return -1;
}
size = strlen(src_utf8);
avail = max_len;
memset(&state, '\0', sizeof(state));
/* conversion to multibyte */
nconv = (*_iconv_fn_iconv)(cd, &src_utf8, &size, (char**)&dst, &avail);
if (nconv == (size_t)-1) {
/* error during conversion, see errno */
(*_iconv_fn_iconv_close)(cd);
return -1;
}
(*_iconv_fn_iconv_close)(cd);
/* terminate output string */
if (avail >= sizeof(char))
*((char*)dst) = '\0';
return 0;
#else /* HAVE_UTF8 */
return 1;
#endif /* HAVE_UTF8 */
}
/** \brief Valid UTF-8 check
*
* taken from RFC2640, adapted to remove warnings :)
* Checks if a byte sequence is valid UTF-8.
*
* \return 1 if input string is valid UTF-8, else 0
*/
int utf8_valid(const char *buf, size_t len)
{
const unsigned char *endbuf = (unsigned char*)buf + len;
unsigned char byte2mask=0x00, c;
int trailing=0; // trailing (continuation) bytes to follow
while ((unsigned char*)buf != endbuf)
{
c = *buf++;
if (trailing)
if ((c & 0xc0) == 0x80) // does trailing byte follow UTF-8 format ?
{
if (byte2mask) // need to check 2nd byte for proper range
{
if (c & byte2mask) // are appropriate bits set ?
byte2mask = 0x00;
else
return 0;
}
trailing--;
}
else
return 0;
else
if ((c & 0x80) == 0x00) continue; // valid 1-byte UTF-8
else if ((c & 0xe0) == 0xc0) // valid 2-byte UTF-8
if (c & 0x1e) //is UTF-8 byte in proper range ?
trailing = 1;
else
return 0;
else if ((c & 0xf0) == 0xe0) // valid 3-byte UTF-8
{
if (!(c & 0x0f)) // is UTF-8 byte in proper range ?
byte2mask = 0x20; // if not set mask
trailing = 2; // to check next byte
}
else if ((c & 0xf8) == 0xf0) // valid 4-byte UTF-8
{
if (!(c & 0x07)) // is UTF-8 byte in proper range ?
byte2mask = 0x30; // if not set mask
trailing = 3; // to check next byte
}
else if ((c & 0xfc) == 0xf8) // valid 5-byte UTF-8
{
if (!(c & 0x03)) // is UTF-8 byte in proper range ?
byte2mask = 0x38; // if not set mask
trailing = 4; // to check next byte
}
else if ((c & 0xfe) == 0xfc) // valid 6-byte UTF-8
{
if (!(c & 0x01)) // is UTF-8 byte in proper range ?
byte2mask = 0x3c; // if not set mask
trailing = 5; // to check next byte
}
else
return 0;
}
return trailing == 0;
}
void utf8_detect(wzd_config_t * config)
{
_local_charset = charset_detect_local();
_iconv_openlib();
if ( _local_charset && _iconv_fn_iconv && _iconv_fn_iconv && _iconv_fn_iconv_close )
{
out_log(LEVEL_INFO, "UTF-8 detected and enabled\n");
CFG_SET_OPTION(config,CFG_OPT_UTF8_CAPABLE);
} else {
CFG_CLR_OPTION(config,CFG_OPT_UTF8_CAPABLE);
}
}
void utf8_end(wzd_config_t * config)
{
_local_charset = NULL;
_iconv_closelib();
CFG_CLR_OPTION(config,CFG_OPT_UTF8_CAPABLE);
out_log(LEVEL_INFO, "UTF-8 disabled\n");
}
|