4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
9d04e500a2b8dcd13abf3d813c6796aecd7a9689Garrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1993
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The Regents of the University of California. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This code is derived from software contributed to Berkeley by
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Paul Borman at Krystal Technologies.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * are met:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * may be used to endorse or promote products derived from this software
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * without specific prior written permission.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lint.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "file64.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <errno.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <string.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdio.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdlib.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <unistd.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <wchar.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "runetype.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "ldpart.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "mblocal.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "setlocale.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "_ctype.h"
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include "lctype.h"
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include "localeimpl.h"
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amoreextern _RuneLocale *_Read_RuneMagi(const char *);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct lc_ctype lc_ctype_posix = {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_mbrtowc = __mbrtowc_ascii,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_mbsinit = __mbsinit_ascii,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_mbsnrtowcs = __mbsnrtowcs_ascii,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_wcrtomb = __wcrtomb_ascii,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_wcsnrtombs = __wcsnrtombs_ascii,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_is_ascii = 1,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_max_mblen = 1,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_trans_upper = _DefaultRuneLocale.__mapupper,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_trans_lower = _DefaultRuneLocale.__maplower,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .lc_ctype_mask = _DefaultRuneLocale.__runetype,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore};
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct locdata __posix_ctype_locdata = {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .l_lname = "C",
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .l_data = { &lc_ctype_posix, &_DefaultRuneLocale }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore};
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Table of initializers for encodings. When you add a new encoding type,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * this table should be updated.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic struct {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const char *e_name;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore void (*e_init)(struct lc_ctype *);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore} encodings[] = {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "NONE", _none_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "UTF-8", _UTF8_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "EUC-CN", _EUC_CN_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "EUC-JP", _EUC_JP_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "EUC-KR", _EUC_KR_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "EUC-TW", _EUC_TW_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "GB18030", _GB18030_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "GB2312", _GB2312_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "GBK", _GBK_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "BIG5", _BIG5_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { "MSKanji", _MSKanji_init },
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore { NULL, NULL }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore};
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct locdata *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore__lc_ctype_load(const char *name)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore struct locdata *ldata;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore struct lc_ctype *lct;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore _RuneLocale *rl;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int i;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore char path[PATH_MAX];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if ((ldata = __locdata_alloc(name, sizeof (*lct))) == NULL)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct = ldata->l_data[0];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Slurp the locale file into the cache.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) snprintf(path, sizeof (path), "%s/%s/LC_CTYPE/LCL_DATA",
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore _PathLocale, name);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if ((rl = _Read_RuneMagi(path)) == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore __locdata_free(ldata);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore errno = EINVAL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ldata->l_data[1] = rl;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_mbrtowc = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_mbsinit = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_mbsnrtowcs = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_wcrtomb = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_wcsnrtombs = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_ctype_mask = rl->__runetype;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_trans_upper = rl->__mapupper;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lct->lc_trans_lower = rl->__maplower;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /* set up the function pointers */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore for (i = 0; encodings[i].e_name != NULL; i++) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int l = strlen(encodings[i].e_name);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if ((strncmp(rl->__encoding, encodings[i].e_name, l) == 0) &&
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (rl->__encoding[l] == '\0' || rl->__encoding[l] == '@')) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore encodings[i].e_init(lct);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (encodings[i].e_name == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore __locdata_free(ldata);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore errno = EINVAL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (ldata);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}