1994-05-27 05:00:24 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2015-08-08 18:22:14 +00:00
|
|
|
* Copyright 2014 Garrett D'Amore <garrett@damore.org>
|
|
|
|
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
|
1994-05-27 05:00:24 +00:00
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Paul Borman at Krystal Technologies.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 05:00:24 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
2002-03-22 21:53:29 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "namespace.h"
|
2002-02-18 20:35:27 +00:00
|
|
|
#include <arpa/inet.h>
|
2002-08-08 05:51:54 +00:00
|
|
|
#include <errno.h>
|
2004-07-29 06:16:19 +00:00
|
|
|
#include <runetype.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <stdio.h>
|
1995-10-23 01:34:17 +00:00
|
|
|
#include <string.h>
|
1994-05-27 05:00:24 +00:00
|
|
|
#include <stdlib.h>
|
1995-10-23 01:34:17 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-08-08 18:22:14 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2001-01-24 13:01:12 +00:00
|
|
|
#include "un-namespace.h"
|
1994-09-24 15:59:33 +00:00
|
|
|
|
2005-05-16 09:32:41 +00:00
|
|
|
#include "runefile.h"
|
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
_RuneLocale *
|
2015-08-08 18:22:14 +00:00
|
|
|
_Read_RuneMagi(const char *fname)
|
1995-10-23 01:34:17 +00:00
|
|
|
{
|
2005-02-26 21:47:54 +00:00
|
|
|
char *fdata, *data;
|
1995-10-23 01:34:17 +00:00
|
|
|
void *lastp;
|
2005-02-26 21:47:54 +00:00
|
|
|
_FileRuneLocale *frl;
|
1994-05-27 05:00:24 +00:00
|
|
|
_RuneLocale *rl;
|
2005-02-26 21:47:54 +00:00
|
|
|
_FileRuneEntry *frr;
|
1995-10-23 01:34:17 +00:00
|
|
|
_RuneEntry *rr;
|
|
|
|
struct stat sb;
|
2002-08-08 05:51:54 +00:00
|
|
|
int x, saverr;
|
2005-02-26 21:47:54 +00:00
|
|
|
void *variable;
|
|
|
|
_FileRuneEntry *runetype_ext_ranges;
|
|
|
|
_FileRuneEntry *maplower_ext_ranges;
|
|
|
|
_FileRuneEntry *mapupper_ext_ranges;
|
|
|
|
int runetype_ext_len = 0;
|
2015-08-08 18:22:14 +00:00
|
|
|
int fd;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
if ((fd = _open(fname, O_RDONLY)) < 0) {
|
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (NULL);
|
2015-08-08 18:22:14 +00:00
|
|
|
}
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
if (_fstat(fd, &sb) < 0) {
|
|
|
|
(void) _close(fd);
|
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
|
|
|
|
(void) _close(fd);
|
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
|
|
|
|
fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
(void) _close(fd);
|
2020-01-09 20:49:26 +00:00
|
|
|
if (fdata == MAP_FAILED) {
|
2015-08-08 18:22:14 +00:00
|
|
|
errno = EINVAL;
|
2002-08-08 05:51:54 +00:00
|
|
|
return (NULL);
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
frl = (_FileRuneLocale *)(void *)fdata;
|
2005-02-26 21:47:54 +00:00
|
|
|
lastp = fdata + sb.st_size;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
variable = frl + 1;
|
1994-05-27 05:00:24 +00:00
|
|
|
|
2015-08-08 18:22:14 +00:00
|
|
|
if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
|
|
|
|
goto invalid;
|
1994-05-27 05:00:24 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
runetype_ext_ranges = (_FileRuneEntry *)variable;
|
2018-10-20 20:51:05 +00:00
|
|
|
variable = runetype_ext_ranges + frl->runetype_ext_nranges;
|
2005-02-26 21:47:54 +00:00
|
|
|
if (variable > lastp) {
|
2015-08-08 18:22:14 +00:00
|
|
|
goto invalid;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
maplower_ext_ranges = (_FileRuneEntry *)variable;
|
2018-10-20 20:51:05 +00:00
|
|
|
variable = maplower_ext_ranges + frl->maplower_ext_nranges;
|
2005-02-26 21:47:54 +00:00
|
|
|
if (variable > lastp) {
|
2015-08-08 18:22:14 +00:00
|
|
|
goto invalid;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
mapupper_ext_ranges = (_FileRuneEntry *)variable;
|
2018-10-20 20:51:05 +00:00
|
|
|
variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
|
2005-02-26 21:47:54 +00:00
|
|
|
if (variable > lastp) {
|
2015-08-08 18:22:14 +00:00
|
|
|
goto invalid;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
frr = runetype_ext_ranges;
|
2018-10-20 20:51:05 +00:00
|
|
|
for (x = 0; x < frl->runetype_ext_nranges; ++x) {
|
2005-02-26 21:47:54 +00:00
|
|
|
uint32_t *types;
|
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
if (frr[x].map == 0) {
|
|
|
|
int len = frr[x].max - frr[x].min + 1;
|
2005-02-26 21:47:54 +00:00
|
|
|
types = variable;
|
|
|
|
variable = types + len;
|
|
|
|
runetype_ext_len += len;
|
|
|
|
if (variable > lastp) {
|
2015-08-08 18:22:14 +00:00
|
|
|
goto invalid;
|
2005-02-26 21:47:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
if ((char *)variable + frl->variable_len > (char *)lastp) {
|
2015-08-08 18:22:14 +00:00
|
|
|
goto invalid;
|
2005-02-26 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from disk format to host format.
|
|
|
|
*/
|
|
|
|
data = malloc(sizeof(_RuneLocale) +
|
2018-10-20 20:51:05 +00:00
|
|
|
(frl->runetype_ext_nranges + frl->maplower_ext_nranges +
|
|
|
|
frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
|
|
|
|
runetype_ext_len * sizeof(*rr->__types) + frl->variable_len);
|
2005-02-26 21:47:54 +00:00
|
|
|
if (data == NULL) {
|
|
|
|
saverr = errno;
|
2015-08-08 18:22:14 +00:00
|
|
|
munmap(fdata, sb.st_size);
|
2005-02-26 21:47:54 +00:00
|
|
|
errno = saverr;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
rl = (_RuneLocale *)data;
|
|
|
|
rl->__variable = rl + 1;
|
|
|
|
|
|
|
|
memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
|
|
|
|
memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
|
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
rl->__variable_len = frl->variable_len;
|
|
|
|
rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
|
|
|
|
rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
|
|
|
|
rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
|
2005-02-26 21:47:54 +00:00
|
|
|
|
|
|
|
for (x = 0; x < _CACHED_RUNES; ++x) {
|
2018-10-20 20:51:05 +00:00
|
|
|
rl->__runetype[x] = frl->runetype[x];
|
|
|
|
rl->__maplower[x] = frl->maplower[x];
|
|
|
|
rl->__mapupper[x] = frl->mapupper[x];
|
2005-02-26 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
|
|
|
|
rl->__variable = rl->__runetype_ext.__ranges +
|
|
|
|
rl->__runetype_ext.__nranges;
|
|
|
|
|
|
|
|
rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
|
|
|
|
rl->__variable = rl->__maplower_ext.__ranges +
|
|
|
|
rl->__maplower_ext.__nranges;
|
|
|
|
|
|
|
|
rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
|
|
|
|
rl->__variable = rl->__mapupper_ext.__ranges +
|
|
|
|
rl->__mapupper_ext.__nranges;
|
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
|
2005-02-26 21:47:54 +00:00
|
|
|
frr = runetype_ext_ranges;
|
|
|
|
rr = rl->__runetype_ext.__ranges;
|
2004-06-23 07:01:44 +00:00
|
|
|
for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
|
2005-02-26 21:47:54 +00:00
|
|
|
uint32_t *types;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2018-10-20 20:51:05 +00:00
|
|
|
rr[x].__min = frr[x].min;
|
|
|
|
rr[x].__max = frr[x].max;
|
|
|
|
rr[x].__map = frr[x].map;
|
2005-02-26 21:47:54 +00:00
|
|
|
if (rr[x].__map == 0) {
|
2004-06-23 07:01:44 +00:00
|
|
|
int len = rr[x].__max - rr[x].__min + 1;
|
2005-02-26 21:47:54 +00:00
|
|
|
types = variable;
|
|
|
|
variable = types + len;
|
2004-06-23 07:01:44 +00:00
|
|
|
rr[x].__types = rl->__variable;
|
|
|
|
rl->__variable = rr[x].__types + len;
|
1995-10-23 01:34:17 +00:00
|
|
|
while (len-- > 0)
|
2005-02-26 21:47:54 +00:00
|
|
|
rr[x].__types[len] = types[len];
|
1995-10-23 01:34:17 +00:00
|
|
|
} else
|
2005-02-26 21:47:54 +00:00
|
|
|
rr[x].__types = NULL;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
frr = maplower_ext_ranges;
|
|
|
|
rr = rl->__maplower_ext.__ranges;
|
2004-06-23 07:01:44 +00:00
|
|
|
for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
|
2018-10-20 20:51:05 +00:00
|
|
|
rr[x].__min = frr[x].min;
|
|
|
|
rr[x].__max = frr[x].max;
|
|
|
|
rr[x].__map = frr[x].map;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
frr = mapupper_ext_ranges;
|
|
|
|
rr = rl->__mapupper_ext.__ranges;
|
2004-06-23 07:01:44 +00:00
|
|
|
for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
|
2018-10-20 20:51:05 +00:00
|
|
|
rr[x].__min = frr[x].min;
|
|
|
|
rr[x].__max = frr[x].max;
|
|
|
|
rr[x].__map = frr[x].map;
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:47:54 +00:00
|
|
|
memcpy(rl->__variable, variable, rl->__variable_len);
|
2015-08-08 18:22:14 +00:00
|
|
|
munmap(fdata, sb.st_size);
|
2005-02-26 21:47:54 +00:00
|
|
|
|
1995-10-23 01:34:17 +00:00
|
|
|
/*
|
|
|
|
* Go out and zero pointers that should be zero.
|
|
|
|
*/
|
2004-06-23 07:01:44 +00:00
|
|
|
if (!rl->__variable_len)
|
2005-02-26 21:47:54 +00:00
|
|
|
rl->__variable = NULL;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2004-06-23 07:01:44 +00:00
|
|
|
if (!rl->__runetype_ext.__nranges)
|
2005-02-26 21:47:54 +00:00
|
|
|
rl->__runetype_ext.__ranges = NULL;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2004-06-23 07:01:44 +00:00
|
|
|
if (!rl->__maplower_ext.__nranges)
|
2005-02-26 21:47:54 +00:00
|
|
|
rl->__maplower_ext.__ranges = NULL;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2004-06-23 07:01:44 +00:00
|
|
|
if (!rl->__mapupper_ext.__nranges)
|
2005-02-26 21:47:54 +00:00
|
|
|
rl->__mapupper_ext.__ranges = NULL;
|
1995-10-23 01:34:17 +00:00
|
|
|
|
2002-08-08 05:51:54 +00:00
|
|
|
return (rl);
|
2015-08-08 18:22:14 +00:00
|
|
|
|
|
|
|
invalid:
|
|
|
|
munmap(fdata, sb.st_size);
|
|
|
|
errno = EINVAL;
|
|
|
|
return (NULL);
|
1995-10-23 01:34:17 +00:00
|
|
|
}
|