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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
* Reserved. This file contains Original Code and/or Modifications of
* Original Code as defined in and that are subject to the Apple Public
* Source License Version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. Please obtain a copy of the
* License at http://www.apple.com/publicsource and read it before using
* this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License."
*
* @APPLE_LICENSE_HEADER_END@
*/
/* @(#)charsets.c *
* (c) 2004 Apple Computer, Inc. All Rights Reserved
*
*
* charsets.c -- Routines converting between UTF-8, 16-bit
* little-endian Unicode, and various Windows
* code pages.
*
* MODIFICATION HISTORY:
* 28-Nov-2004 Guy Harris New today
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iconv.h>
#include <langinfo.h>
#include <strings.h>
#ifdef NOTPORTED
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFStringDefaultEncoding.h>
#include <CoreFoundation/CFStringEncodingConverter.h>
#include <sys/mchain.h>
#endif /* NOTPORTED */
#include <netsmb/smb_lib.h>
#include <netsmb/mchain.h>
#include "charsets.h"
#ifdef NOTPORTED
extern uid_t real_uid,eff_uid;
#endif /* NOTPORTED */
/*
* On Solaris, we will need to do some rewriting to use our iconv
* routines for the conversions. For now, we're effectively
* stubbing out code, leaving the details of what happens on
* Darwin in case it's useful as a guide later.
*/
static unsigned
xtoi(char u)
{
if (isdigit(u))
return (u - '0');
else if (islower(u))
return (10 + u - 'a');
else if (isupper(u))
return (10 + u - 'A');
return (16);
}
/* Removes the "%" escape sequences from a URL component.
* See IETF RFC 2396.
*/
char *
unpercent(char * component)
{
char c, *s;
unsigned hi, lo;
if (component)
for (s = component; (c = *s) != 0; s++) {
if (c != '%')
continue;
if ((hi = xtoi(s[1])) > 15 || (lo = xtoi(s[2])) > 15)
continue; /* ignore invalid escapes */
s[0] = hi*16 + lo;
/*
* This was strcpy(s + 1, s + 3);
* But nowadays leftward overlapping copies are
* officially undefined in C. Ours seems to
* work or not depending upon alignment.
*/
memmove(s+1, s+3, strlen(s+3) + 1);
}
return (component);
}
#ifdef NOTPORTED
static CFStringEncoding
get_windows_encoding_equivalent( void )
{
CFStringEncoding encoding;
uint32_t index,region;
/* important! use root ID so you can read the config file! */
seteuid(eff_uid);
__CFStringGetInstallationEncodingAndRegion(&index,®ion);
seteuid(real_uid);
switch ( index )
{
case kCFStringEncodingMacRoman:
if (region) /* anything nonzero is not US */
encoding = kCFStringEncodingDOSLatin1;
else /* US region */
encoding = kCFStringEncodingDOSLatinUS;
break;
case kCFStringEncodingMacJapanese:
encoding = kCFStringEncodingDOSJapanese;
break;
case kCFStringEncodingMacChineseTrad:
encoding = kCFStringEncodingDOSChineseTrad;
break;
case kCFStringEncodingMacKorean:
encoding = kCFStringEncodingDOSKorean;
break;
case kCFStringEncodingMacArabic:
encoding = kCFStringEncodingDOSArabic;
break;
case kCFStringEncodingMacHebrew:
encoding = kCFStringEncodingDOSHebrew;
break;
case kCFStringEncodingMacGreek:
encoding = kCFStringEncodingDOSGreek;
break;
case kCFStringEncodingMacCyrillic:
encoding = kCFStringEncodingDOSCyrillic;
break;
case kCFStringEncodingMacThai:
encoding = kCFStringEncodingDOSThai;
break;
case kCFStringEncodingMacChineseSimp:
encoding = kCFStringEncodingDOSChineseSimplif;
break;
case kCFStringEncodingMacCentralEurRoman:
encoding = kCFStringEncodingDOSLatin2;
break;
case kCFStringEncodingMacTurkish:
encoding = kCFStringEncodingDOSTurkish;
break;
case kCFStringEncodingMacCroatian:
encoding = kCFStringEncodingDOSLatin2;
break;
case kCFStringEncodingMacIcelandic:
encoding = kCFStringEncodingDOSIcelandic;
break;
case kCFStringEncodingMacRomanian:
encoding = kCFStringEncodingDOSLatin2;
break;
case kCFStringEncodingMacFarsi:
encoding = kCFStringEncodingDOSArabic;
break;
case kCFStringEncodingMacUkrainian:
encoding = kCFStringEncodingDOSCyrillic;
break;
default:
encoding = kCFStringEncodingDOSLatin1;
break;
}
return encoding;
}
#endif /* NOTPORTED */
/*
* XXX - NLS, or CF? We should probably use the same routine for all
* conversions.
*/
char *
convert_wincs_to_utf8(const char *windows_string)
{
#ifdef NOTPORTED
CFStringRef s;
CFIndex maxlen;
char *result;
s = CFStringCreateWithCString(NULL, windows_string,
get_windows_encoding_equivalent());
if (s == NULL) {
smb_error("CFStringCreateWithCString for Windows code page failed on \"%s\" ", -1,
windows_string);
/* kCFStringEncodingMacRoman should always succeed */
s = CFStringCreateWithCString(NULL, windows_string,
kCFStringEncodingMacRoman);
if (s == NULL) {
smb_error("CFStringCreateWithCString for Windows code page failed on \"%s\" with kCFStringEncodingMacRoman - skipping",
-1, windows_string);
return NULL;
}
}
maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s),
kCFStringEncodingUTF8) + 1;
result = malloc(maxlen);
if (result == NULL) {
smb_error("Couldn't allocate buffer for UTF-8 string for \"%s\" - skipping", -1,
windows_string);
CFRelease(s);
return NULL;
}
if (!CFStringGetCString(s, result, maxlen, kCFStringEncodingUTF8)) {
smb_error("CFStringGetCString for UTF-8 failed on \"%s\" - skipping",
-1, windows_string);
CFRelease(s);
return NULL;
}
CFRelease(s);
return result;
#else /* NOTPORTED */
return ((char*)windows_string);
#endif /* NOTPORTED */
}
/*
* XXX - NLS, or CF? We should probably use the same routine for all
* conversions.
*/
char *
convert_utf8_to_wincs(const char *utf8_string)
{
#ifdef NOTPORTED
CFStringRef s;
CFIndex maxlen;
char *result;
s = CFStringCreateWithCString(NULL, utf8_string,
kCFStringEncodingUTF8);
if (s == NULL) {
smb_error("CFStringCreateWithCString for UTF-8 failed on \"%s\"", -1,
utf8_string);
return NULL;
}
maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s),
get_windows_encoding_equivalent()) + 1;
result = malloc(maxlen);
if (result == NULL) {
smb_error("Couldn't allocate buffer for Windows code page string for \"%s\" - skipping", -1,
utf8_string);
CFRelease(s);
return NULL;
}
if (!CFStringGetCString(s, result, maxlen,
get_windows_encoding_equivalent())) {
smb_error("CFStringGetCString for Windows code page failed on \"%s\" - skipping",
-1, utf8_string);
CFRelease(s);
return NULL;
}
CFRelease(s);
return result;
#else /* NOTPORTED */
return ((char*)utf8_string);
#endif /* NOTPORTED */
}
/*
* Convert little-endian Unicode string to UTF-8.
* Converts the Unicode string to host byte order in place.
*/
char *
convert_leunicode_to_utf8(unsigned short *unicode_string)
{
unsigned short *unicode_charp, unicode_char;
int len = 0;
for (unicode_charp = unicode_string;
(unicode_char = *unicode_charp) != 0;
unicode_charp++) {
*unicode_charp = letohs(unicode_char);
len = len + 2;
}
return (convert_unicode_to_utf8(unicode_string, len));
}
char *
convert_unicode_to_utf8(unsigned short *unicode_string, int len)
{
iconv_t cd;
char from[BUFSIZ], to[BUFSIZ];
char *tptr = NULL;
const char *fptr;
size_t ileft, oleft, ret;
cd = iconv_open("UTF-8", "UTF-16");
if (cd != (iconv_t)-1) {
ileft = len;
bcopy((char *)unicode_string, from, ileft);
fptr = from;
oleft = BUFSIZ;
tptr = to;
ret = iconv(cd, &fptr, &ileft, &tptr, &oleft);
if (ret != (size_t)-1) {
to[BUFSIZ-oleft] = '\0';
tptr = to;
} else {
tptr = NULL;
}
(void) iconv_close(cd);
}
return (tptr);
}
/*
* Convert UTF-8 string to little-endian Unicode.
*/
unsigned short *
convert_utf8_to_leunicode(const char *utf8_string)
{
#ifdef NOTPORTED
CFStringRef s;
CFIndex maxlen;
unsigned short *result;
CFRange range;
int i;
s = CFStringCreateWithCString(NULL, utf8_string,
kCFStringEncodingUTF8);
if (s == NULL) {
smb_error("CFStringCreateWithCString for UTF-8 failed on \"%s\"", -1,
utf8_string);
return NULL;
}
maxlen = CFStringGetLength(s);
result = malloc(2*(maxlen + 1));
if (result == NULL) {
smb_error("Couldn't allocate buffer for Unicode string for \"%s\" - skipping", -1,
utf8_string);
CFRelease(s);
return NULL;
}
range.location = 0;
range.length = maxlen;
CFStringGetCharacters(s, range, result);
for (i = 0; i < maxlen; i++)
result[i] = CFSwapInt16HostToLittle(result[i]);
result[maxlen] = 0;
CFRelease(s);
return result;
#else /* NOTPORTED */
/* LINTED */ /* XXX Really need to fix this! */
return ((ushort_t *)utf8_string); /* XXX */
#endif /* NOTPORTED */
}
|