charsets.c revision 4bff34e37def8a90f9194d81bc345c52ba20086a
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * @APPLE_LICENSE_HEADER_START@
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Reserved. This file contains Original Code and/or Modifications of
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Original Code as defined in and that are subject to the Apple Public
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Source License Version 1.0 (the 'License'). You may not use this file
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * except in compliance with the License. Please obtain a copy of the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * License at http://www.apple.com/publicsource and read it before using
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * this file.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * The Original Code and all software distributed under the License are
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * License for the specific language governing rights and limitations
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * under the License."
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * @APPLE_LICENSE_HEADER_END@
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * (c) 2004 Apple Computer, Inc. All Rights Reserved
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * charsets.c -- Routines converting between UTF-8, 16-bit
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * little-endian Unicode, and various Windows
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * code pages.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * MODIFICATION HISTORY:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 28-Nov-2004 Guy Harris New today
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#pragma ident "%Z%%M% %I% %E% SMI"
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <CoreFoundation/CFStringEncodingConverter.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * On Solaris, we will need to do some rewriting to use our iconv
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * routines for the conversions. For now, we're effectively
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * stubbing out code, leaving the details of what happens on
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Darwin in case it's useful as a guide later.
4bff34e37def8a90f9194d81bc345c52ba20086athurlowstatic unsigned
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (u - '0');
4bff34e37def8a90f9194d81bc345c52ba20086athurlow else if (islower(u))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow else if (isupper(u))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (16);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Removes the "%" escape sequences from a URL component.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * See IETF RFC 2396.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char c, *s;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow for (s = component; (c = *s) != 0; s++) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (c != '%')
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if ((hi = xtoi(s[1])) > 15 || (lo = xtoi(s[2])) > 15)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow continue; /* ignore invalid escapes */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * This was strcpy(s + 1, s + 3);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * But nowadays leftward overlapping copies are
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * officially undefined in C. Ours seems to
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * work or not depending upon alignment.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /* important! use root ID so you can read the config file! */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow __CFStringGetInstallationEncodingAndRegion(&index,®ion);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow else /* US region */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * XXX - NLS, or CF? We should probably use the same routine for all
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * conversions.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (s == NULL) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringCreateWithCString for Windows code page failed on \"%s\" ", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /* kCFStringEncodingMacRoman should always succeed */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (s == NULL) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringCreateWithCString for Windows code page failed on \"%s\" with kCFStringEncodingMacRoman - skipping",
4bff34e37def8a90f9194d81bc345c52ba20086athurlow maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s),
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("Couldn't allocate buffer for UTF-8 string for \"%s\" - skipping", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (!CFStringGetCString(s, result, maxlen, kCFStringEncodingUTF8)) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringGetCString for UTF-8 failed on \"%s\" - skipping",
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#else /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return ((char*)windows_string);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * XXX - NLS, or CF? We should probably use the same routine for all
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * conversions.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (s == NULL) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringCreateWithCString for UTF-8 failed on \"%s\"", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s),
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("Couldn't allocate buffer for Windows code page string for \"%s\" - skipping", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringGetCString for Windows code page failed on \"%s\" - skipping",
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#else /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return ((char*)utf8_string);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Convert little-endian Unicode string to UTF-8.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Converts the Unicode string to host byte order in place.
4bff34e37def8a90f9194d81bc345c52ba20086athurlowconvert_leunicode_to_utf8(unsigned short *unicode_string)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (convert_unicode_to_utf8(unicode_string, len));
4bff34e37def8a90f9194d81bc345c52ba20086athurlowconvert_unicode_to_utf8(unsigned short *unicode_string, int len)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *fptr;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Convert UTF-8 string to little-endian Unicode.
4bff34e37def8a90f9194d81bc345c52ba20086athurlowunsigned short *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow unsigned short *result;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (s == NULL) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("CFStringCreateWithCString for UTF-8 failed on \"%s\"", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_error("Couldn't allocate buffer for Unicode string for \"%s\" - skipping", -1,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow for (i = 0; i < maxlen; i++)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#else /* NOTPORTED */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#endif /* NOTPORTED */