1999-11-25 21:30:24 +00:00
|
|
|
/*
|
|
|
|
* Unicode routines for use inside the server
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Alexandre Julliard
|
2002-03-09 23:29:33 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-11-25 21:30:24 +00:00
|
|
|
*/
|
|
|
|
|
2002-04-26 19:05:15 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2000-07-25 21:01:59 +00:00
|
|
|
#include <ctype.h>
|
2021-10-07 12:38:40 +00:00
|
|
|
#include <fcntl.h>
|
1999-11-25 21:30:24 +00:00
|
|
|
#include <stdio.h>
|
2020-11-27 15:41:07 +00:00
|
|
|
#include <stdarg.h>
|
2021-10-07 12:38:40 +00:00
|
|
|
#include <unistd.h>
|
2021-10-23 08:14:38 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
#endif
|
1999-11-25 21:30:24 +00:00
|
|
|
|
2020-11-27 15:41:07 +00:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winternl.h"
|
2020-11-27 15:40:37 +00:00
|
|
|
#include "request.h"
|
1999-11-25 21:30:24 +00:00
|
|
|
#include "unicode.h"
|
2020-11-27 15:41:07 +00:00
|
|
|
#include "file.h"
|
1999-11-25 21:30:24 +00:00
|
|
|
|
2008-01-07 19:17:24 +00:00
|
|
|
/* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
|
|
|
|
static const char utf8_length[128] =
|
|
|
|
{
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
|
|
|
|
0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
|
|
|
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
|
|
|
|
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
|
|
|
|
3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* first byte mask depending on UTF-8 sequence length */
|
|
|
|
static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
|
|
|
|
|
|
|
|
/* minimum Unicode value depending on UTF-8 sequence length */
|
|
|
|
static const unsigned int utf8_minval[4] = { 0x0, 0x80, 0x800, 0x10000 };
|
|
|
|
|
2020-11-27 15:40:37 +00:00
|
|
|
static unsigned short *casemap;
|
|
|
|
|
2008-01-07 19:17:24 +00:00
|
|
|
static inline char to_hex( char ch )
|
|
|
|
{
|
|
|
|
if (isdigit(ch)) return ch - '0';
|
|
|
|
return tolower(ch) - 'a' + 10;
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:48:19 +00:00
|
|
|
static inline WCHAR to_lower( WCHAR ch )
|
|
|
|
{
|
2020-11-27 15:40:37 +00:00
|
|
|
return ch + casemap[casemap[casemap[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0x0f)];
|
2020-03-24 11:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int memicmp_strW( const WCHAR *str1, const WCHAR *str2, data_size_t len )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (len /= sizeof(WCHAR); len; str1++, str2++, len--)
|
|
|
|
if ((ret = to_lower(*str1) - to_lower(*str2))) break;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:48:26 +00:00
|
|
|
unsigned int hash_strW( const WCHAR *str, data_size_t len, unsigned int hash_size )
|
|
|
|
{
|
|
|
|
unsigned int i, hash = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len / sizeof(WCHAR); i++) hash = hash * 65599 + to_lower( str[i] );
|
|
|
|
return hash % hash_size;
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:47:44 +00:00
|
|
|
WCHAR *ascii_to_unicode_str( const char *str, struct unicode_str *ret )
|
|
|
|
{
|
|
|
|
data_size_t i, len = strlen(str);
|
|
|
|
WCHAR *p;
|
|
|
|
|
|
|
|
ret->len = len * sizeof(WCHAR);
|
|
|
|
ret->str = p = mem_alloc( ret->len );
|
|
|
|
if (p) for (i = 0; i < len; i++) p[i] = (unsigned char)str[i];
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-01-07 19:17:24 +00:00
|
|
|
/* parse an escaped string back into Unicode */
|
|
|
|
/* return the number of chars read from the input, or -1 on output overflow */
|
|
|
|
int parse_strW( WCHAR *buffer, data_size_t *len, const char *src, char endchar )
|
|
|
|
{
|
|
|
|
WCHAR *dest = buffer;
|
|
|
|
WCHAR *end = buffer + *len / sizeof(WCHAR);
|
|
|
|
const char *p = src;
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
while (*p && *p != endchar && dest < end)
|
|
|
|
{
|
|
|
|
if (*p == '\\')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
if (!*p) break;
|
|
|
|
switch(*p)
|
|
|
|
{
|
|
|
|
case 'a': *dest++ = '\a'; p++; continue;
|
|
|
|
case 'b': *dest++ = '\b'; p++; continue;
|
|
|
|
case 'e': *dest++ = '\e'; p++; continue;
|
|
|
|
case 'f': *dest++ = '\f'; p++; continue;
|
|
|
|
case 'n': *dest++ = '\n'; p++; continue;
|
|
|
|
case 'r': *dest++ = '\r'; p++; continue;
|
|
|
|
case 't': *dest++ = '\t'; p++; continue;
|
|
|
|
case 'v': *dest++ = '\v'; p++; continue;
|
|
|
|
case 'x': /* hex escape */
|
|
|
|
p++;
|
|
|
|
if (!isxdigit(*p)) *dest = 'x';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest = to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
continue;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7': /* octal escape */
|
|
|
|
*dest = *p++ - '0';
|
|
|
|
if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
|
|
|
|
if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
|
|
|
|
dest++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* unrecognized escape: fall through to normal char handling */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ch = *p++;
|
|
|
|
if (ch < 0x80) *dest++ = ch;
|
|
|
|
else /* parse utf8 char */
|
|
|
|
{
|
|
|
|
int charlen = utf8_length[ch-0x80];
|
|
|
|
unsigned int res = ch & utf8_mask[charlen];
|
|
|
|
|
|
|
|
switch(charlen)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
if ((ch = *p ^ 0x80) >= 0x40) break;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
p++;
|
|
|
|
case 2:
|
|
|
|
if ((ch = *p ^ 0x80) >= 0x40) break;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
p++;
|
|
|
|
case 1:
|
|
|
|
if ((ch = *p ^ 0x80) >= 0x40) break;
|
|
|
|
res = (res << 6) | ch;
|
|
|
|
p++;
|
|
|
|
if (res < utf8_minval[charlen]) break;
|
|
|
|
if (res > 0x10ffff) break;
|
|
|
|
if (res <= 0xffff) *dest++ = res;
|
|
|
|
else /* we need surrogates */
|
|
|
|
{
|
|
|
|
res -= 0x10000;
|
|
|
|
*dest++ = 0xd800 | (res >> 10);
|
|
|
|
if (dest < end) *dest++ = 0xdc00 | (res & 0x3ff);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* ignore invalid char */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dest >= end) return -1; /* overflow */
|
|
|
|
*dest++ = 0;
|
|
|
|
if (!*p) return -1; /* delimiter not found */
|
|
|
|
*len = (dest - buffer) * sizeof(WCHAR);
|
|
|
|
return p + 1 - src;
|
|
|
|
}
|
|
|
|
|
1999-11-25 21:30:24 +00:00
|
|
|
/* dump a Unicode string with proper escaping */
|
2008-01-07 19:17:24 +00:00
|
|
|
int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] )
|
1999-11-25 21:30:24 +00:00
|
|
|
{
|
|
|
|
static const char escapes[32] = ".......abtnvfr.............e....";
|
|
|
|
char buffer[256];
|
|
|
|
char *pos = buffer;
|
|
|
|
int count = 0;
|
|
|
|
|
2020-03-21 10:36:33 +00:00
|
|
|
for (len /= sizeof(WCHAR); len; str++, len--)
|
1999-11-25 21:30:24 +00:00
|
|
|
{
|
|
|
|
if (pos > buffer + sizeof(buffer) - 8)
|
|
|
|
{
|
|
|
|
fwrite( buffer, pos - buffer, 1, f );
|
|
|
|
count += pos - buffer;
|
|
|
|
pos = buffer;
|
|
|
|
}
|
|
|
|
if (*str > 127) /* hex escape */
|
|
|
|
{
|
|
|
|
if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
|
|
|
|
pos += sprintf( pos, "\\x%04x", *str );
|
|
|
|
else
|
|
|
|
pos += sprintf( pos, "\\x%x", *str );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*str < 32) /* octal or C escape */
|
|
|
|
{
|
|
|
|
if (!*str && len == 1) continue; /* do not output terminating NULL */
|
|
|
|
if (escapes[*str] != '.')
|
|
|
|
pos += sprintf( pos, "\\%c", escapes[*str] );
|
|
|
|
else if (len > 1 && str[1] >= '0' && str[1] <= '7')
|
|
|
|
pos += sprintf( pos, "\\%03o", *str );
|
|
|
|
else
|
|
|
|
pos += sprintf( pos, "\\%o", *str );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
|
|
|
|
*pos++ = *str;
|
|
|
|
}
|
|
|
|
fwrite( buffer, pos - buffer, 1, f );
|
|
|
|
count += pos - buffer;
|
|
|
|
return count;
|
|
|
|
}
|
2020-11-27 15:40:37 +00:00
|
|
|
|
|
|
|
static char *get_nls_dir(void)
|
|
|
|
{
|
|
|
|
char *p, *dir, *ret;
|
|
|
|
const char *nlsdir = BIN_TO_NLSDIR;
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
|
|
|
|
dir = realpath( "/proc/self/exe", NULL );
|
|
|
|
#elif defined (__FreeBSD__) || defined(__DragonFly__)
|
2021-10-23 08:14:38 +00:00
|
|
|
static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
|
|
|
|
size_t dir_size = PATH_MAX;
|
|
|
|
dir = malloc( dir_size );
|
|
|
|
if (dir)
|
|
|
|
{
|
2022-10-18 17:01:11 +00:00
|
|
|
if (sysctl( pathname, ARRAY_SIZE( pathname ), dir, &dir_size, NULL, 0 ))
|
2021-10-23 08:14:38 +00:00
|
|
|
{
|
|
|
|
free( dir );
|
|
|
|
dir = NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 15:40:37 +00:00
|
|
|
#else
|
|
|
|
dir = realpath( server_argv0, NULL );
|
|
|
|
#endif
|
|
|
|
if (!dir) return NULL;
|
|
|
|
if (!(p = strrchr( dir, '/' )))
|
|
|
|
{
|
|
|
|
free( dir );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*(++p) = 0;
|
|
|
|
if (p > dir + 8 && !strcmp( p - 8, "/server/" )) nlsdir = "../nls"; /* inside build tree */
|
|
|
|
if ((ret = malloc( strlen(dir) + strlen( nlsdir ) + 1 )))
|
|
|
|
{
|
|
|
|
strcpy( ret, dir );
|
|
|
|
strcat( ret, nlsdir );
|
|
|
|
}
|
|
|
|
free( dir );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load the case mapping table */
|
2020-11-27 15:41:07 +00:00
|
|
|
struct fd *load_intl_file(void)
|
2020-11-27 15:40:37 +00:00
|
|
|
{
|
|
|
|
static const char *nls_dirs[] = { NULL, NLSDIR, "/usr/local/share/wine/nls", "/usr/share/wine/nls" };
|
2021-02-09 13:11:07 +00:00
|
|
|
static const WCHAR nt_pathW[] = {'C',':','\\','w','i','n','d','o','w','s','\\',
|
|
|
|
's','y','s','t','e','m','3','2','\\','l','_','i','n','t','l','.','n','l','s',0};
|
|
|
|
static const struct unicode_str nt_name = { nt_pathW, sizeof(nt_pathW) };
|
2020-11-27 15:40:37 +00:00
|
|
|
unsigned int i, offset, size;
|
|
|
|
unsigned short data;
|
|
|
|
char *path;
|
2020-11-27 15:41:07 +00:00
|
|
|
struct fd *fd = NULL;
|
|
|
|
int unix_fd;
|
|
|
|
mode_t mode = 0600;
|
2020-11-27 15:40:37 +00:00
|
|
|
|
|
|
|
nls_dirs[0] = get_nls_dir();
|
|
|
|
for (i = 0; i < ARRAY_SIZE( nls_dirs ); i++)
|
|
|
|
{
|
|
|
|
if (!nls_dirs[i]) continue;
|
|
|
|
if (!(path = malloc( strlen(nls_dirs[i]) + sizeof("/l_intl.nls" )))) continue;
|
|
|
|
strcpy( path, nls_dirs[i] );
|
|
|
|
strcat( path, "/l_intl.nls" );
|
2021-02-09 13:11:07 +00:00
|
|
|
if ((fd = open_fd( NULL, path, nt_name, O_RDONLY, &mode, FILE_READ_DATA,
|
2020-11-27 15:41:07 +00:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT ))) break;
|
2020-11-27 15:40:37 +00:00
|
|
|
free( path );
|
|
|
|
}
|
2020-11-27 15:41:07 +00:00
|
|
|
if (!fd) fatal_error( "failed to load l_intl.nls\n" );
|
|
|
|
unix_fd = get_unix_fd( fd );
|
2020-11-27 15:40:37 +00:00
|
|
|
/* read initial offset */
|
|
|
|
if (pread( unix_fd, &data, sizeof(data), 0 ) != sizeof(data) || !data) goto failed;
|
|
|
|
offset = data;
|
|
|
|
/* read size of uppercase table */
|
|
|
|
if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
|
|
|
|
offset += data;
|
|
|
|
/* read size of lowercase table */
|
|
|
|
if (pread( unix_fd, &data, sizeof(data), offset * 2 ) != sizeof(data) || !data) goto failed;
|
|
|
|
offset++;
|
|
|
|
size = data - 1;
|
|
|
|
/* read lowercase table */
|
|
|
|
if (!(casemap = malloc( size * 2 ))) goto failed;
|
|
|
|
if (pread( unix_fd, casemap, size * 2, offset * 2 ) != size * 2) goto failed;
|
|
|
|
free( path );
|
2020-11-27 15:41:07 +00:00
|
|
|
return fd;
|
2020-11-27 15:40:37 +00:00
|
|
|
|
|
|
|
failed:
|
|
|
|
fatal_error( "invalid format for casemap table %s\n", path );
|
|
|
|
}
|