14297a3b0SGarrett D'Amore /* 2*2d08521bSGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org> 36b5e5868SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 44297a3b0SGarrett D'Amore * Copyright (c) 1993 54297a3b0SGarrett D'Amore * The Regents of the University of California. All rights reserved. 64297a3b0SGarrett D'Amore * 74297a3b0SGarrett D'Amore * This code is derived from software contributed to Berkeley by 84297a3b0SGarrett D'Amore * Paul Borman at Krystal Technologies. 94297a3b0SGarrett D'Amore * 104297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without 114297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions 124297a3b0SGarrett D'Amore * are met: 134297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright 144297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer. 154297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright 164297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the 174297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution. 184297a3b0SGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors 194297a3b0SGarrett D'Amore * may be used to endorse or promote products derived from this software 204297a3b0SGarrett D'Amore * without specific prior written permission. 214297a3b0SGarrett D'Amore * 224297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 234297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 244297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 254297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 264297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 274297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 284297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 294297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 304297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 314297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 324297a3b0SGarrett D'Amore * SUCH DAMAGE. 334297a3b0SGarrett D'Amore */ 344297a3b0SGarrett D'Amore 354297a3b0SGarrett D'Amore #include "lint.h" 364297a3b0SGarrett D'Amore #include "file64.h" 374297a3b0SGarrett D'Amore #include <errno.h> 384297a3b0SGarrett D'Amore #include <stdio.h> 394297a3b0SGarrett D'Amore #include <string.h> 404297a3b0SGarrett D'Amore #include <stdlib.h> 414297a3b0SGarrett D'Amore #include <sys/types.h> 424297a3b0SGarrett D'Amore #include <sys/stat.h> 43*2d08521bSGarrett D'Amore #include <sys/mman.h> 44*2d08521bSGarrett D'Amore #include <fcntl.h> 45*2d08521bSGarrett D'Amore #include <unistd.h> 464297a3b0SGarrett D'Amore 47*2d08521bSGarrett D'Amore #include "libc.h" 484297a3b0SGarrett D'Amore #include "runetype.h" 494297a3b0SGarrett D'Amore #include "runefile.h" 504297a3b0SGarrett D'Amore 514297a3b0SGarrett D'Amore _RuneLocale * 52*2d08521bSGarrett D'Amore _Read_RuneMagi(const char *fname) 534297a3b0SGarrett D'Amore { 544297a3b0SGarrett D'Amore char *fdata, *data; 554297a3b0SGarrett D'Amore void *lastp; 564297a3b0SGarrett D'Amore _FileRuneLocale *frl; 574297a3b0SGarrett D'Amore _RuneLocale *rl; 584297a3b0SGarrett D'Amore _FileRuneEntry *frr; 594297a3b0SGarrett D'Amore _RuneEntry *rr; 604297a3b0SGarrett D'Amore struct stat sb; 614297a3b0SGarrett D'Amore int x, saverr; 624297a3b0SGarrett D'Amore void *variable; 634297a3b0SGarrett D'Amore _FileRuneEntry *runetype_ext_ranges; 644297a3b0SGarrett D'Amore _FileRuneEntry *maplower_ext_ranges; 654297a3b0SGarrett D'Amore _FileRuneEntry *mapupper_ext_ranges; 664297a3b0SGarrett D'Amore int runetype_ext_len = 0; 67*2d08521bSGarrett D'Amore int fd; 684297a3b0SGarrett D'Amore 69*2d08521bSGarrett D'Amore if ((fd = open(fname, O_RDONLY)) < 0) { 704297a3b0SGarrett D'Amore errno = EINVAL; 714297a3b0SGarrett D'Amore return (NULL); 724297a3b0SGarrett D'Amore } 734297a3b0SGarrett D'Amore 74*2d08521bSGarrett D'Amore if (fstat(fd, &sb) < 0) { 75*2d08521bSGarrett D'Amore (void) close(fd); 76*2d08521bSGarrett D'Amore errno = EINVAL; 774297a3b0SGarrett D'Amore return (NULL); 784297a3b0SGarrett D'Amore } 794297a3b0SGarrett D'Amore 80*2d08521bSGarrett D'Amore if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) { 81*2d08521bSGarrett D'Amore (void) close(fd); 82*2d08521bSGarrett D'Amore errno = EINVAL; 83*2d08521bSGarrett D'Amore return (NULL); 84*2d08521bSGarrett D'Amore } 85*2d08521bSGarrett D'Amore 86*2d08521bSGarrett D'Amore 87*2d08521bSGarrett D'Amore fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 88*2d08521bSGarrett D'Amore (void) close(fd); 89*2d08521bSGarrett D'Amore if (fdata == NULL) { 90*2d08521bSGarrett D'Amore errno = EINVAL; 914297a3b0SGarrett D'Amore return (NULL); 924297a3b0SGarrett D'Amore } 934297a3b0SGarrett D'Amore 944297a3b0SGarrett D'Amore frl = (_FileRuneLocale *)(void *)fdata; 954297a3b0SGarrett D'Amore lastp = fdata + sb.st_size; 964297a3b0SGarrett D'Amore 974297a3b0SGarrett D'Amore variable = frl + 1; 984297a3b0SGarrett D'Amore 994297a3b0SGarrett D'Amore if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) { 100*2d08521bSGarrett D'Amore goto invalid; 1014297a3b0SGarrett D'Amore } 1024297a3b0SGarrett D'Amore 1034297a3b0SGarrett D'Amore runetype_ext_ranges = (_FileRuneEntry *)variable; 1044297a3b0SGarrett D'Amore variable = runetype_ext_ranges + frl->runetype_ext_nranges; 1054297a3b0SGarrett D'Amore if (variable > lastp) { 106*2d08521bSGarrett D'Amore goto invalid; 1074297a3b0SGarrett D'Amore } 1084297a3b0SGarrett D'Amore 1094297a3b0SGarrett D'Amore maplower_ext_ranges = (_FileRuneEntry *)variable; 1104297a3b0SGarrett D'Amore variable = maplower_ext_ranges + frl->maplower_ext_nranges; 1114297a3b0SGarrett D'Amore if (variable > lastp) { 112*2d08521bSGarrett D'Amore goto invalid; 1134297a3b0SGarrett D'Amore } 1144297a3b0SGarrett D'Amore 1154297a3b0SGarrett D'Amore mapupper_ext_ranges = (_FileRuneEntry *)variable; 1164297a3b0SGarrett D'Amore variable = mapupper_ext_ranges + frl->mapupper_ext_nranges; 1174297a3b0SGarrett D'Amore if (variable > lastp) { 118*2d08521bSGarrett D'Amore goto invalid; 1194297a3b0SGarrett D'Amore } 1204297a3b0SGarrett D'Amore 1214297a3b0SGarrett D'Amore frr = runetype_ext_ranges; 1224297a3b0SGarrett D'Amore for (x = 0; x < frl->runetype_ext_nranges; ++x) { 1234297a3b0SGarrett D'Amore uint32_t *types; 1244297a3b0SGarrett D'Amore 1254297a3b0SGarrett D'Amore if (frr[x].map == 0) { 1264297a3b0SGarrett D'Amore int len = frr[x].max - frr[x].min + 1; 1274297a3b0SGarrett D'Amore types = variable; 1284297a3b0SGarrett D'Amore variable = types + len; 1294297a3b0SGarrett D'Amore runetype_ext_len += len; 1304297a3b0SGarrett D'Amore if (variable > lastp) { 131*2d08521bSGarrett D'Amore goto invalid; 1324297a3b0SGarrett D'Amore } 1334297a3b0SGarrett D'Amore } 1344297a3b0SGarrett D'Amore } 1354297a3b0SGarrett D'Amore 1364297a3b0SGarrett D'Amore if ((char *)variable + frl->variable_len > (char *)lastp) { 137*2d08521bSGarrett D'Amore goto invalid; 1384297a3b0SGarrett D'Amore } 1394297a3b0SGarrett D'Amore 1404297a3b0SGarrett D'Amore /* 1414297a3b0SGarrett D'Amore * Convert from disk format to host format. 1424297a3b0SGarrett D'Amore */ 143*2d08521bSGarrett D'Amore data = libc_malloc(sizeof (_RuneLocale) + 1444297a3b0SGarrett D'Amore (frl->runetype_ext_nranges + frl->maplower_ext_nranges + 1454297a3b0SGarrett D'Amore frl->mapupper_ext_nranges) * sizeof (_RuneEntry) + 1464297a3b0SGarrett D'Amore runetype_ext_len * sizeof (*rr->__types) + 1474297a3b0SGarrett D'Amore frl->variable_len); 1484297a3b0SGarrett D'Amore if (data == NULL) { 1494297a3b0SGarrett D'Amore saverr = errno; 150*2d08521bSGarrett D'Amore (void) munmap(fdata, sb.st_size); 1514297a3b0SGarrett D'Amore errno = saverr; 1524297a3b0SGarrett D'Amore return (NULL); 1534297a3b0SGarrett D'Amore } 1544297a3b0SGarrett D'Amore 1554297a3b0SGarrett D'Amore rl = (_RuneLocale *)(void *)data; 1564297a3b0SGarrett D'Amore rl->__variable = rl + 1; 1574297a3b0SGarrett D'Amore 1584297a3b0SGarrett D'Amore (void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic)); 1594297a3b0SGarrett D'Amore (void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding)); 1604297a3b0SGarrett D'Amore 1614297a3b0SGarrett D'Amore rl->__variable_len = frl->variable_len; 1624297a3b0SGarrett D'Amore rl->__runetype_ext.__nranges = frl->runetype_ext_nranges; 1634297a3b0SGarrett D'Amore rl->__maplower_ext.__nranges = frl->maplower_ext_nranges; 1644297a3b0SGarrett D'Amore rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges; 1654297a3b0SGarrett D'Amore 1664297a3b0SGarrett D'Amore for (x = 0; x < _CACHED_RUNES; ++x) { 1674297a3b0SGarrett D'Amore rl->__runetype[x] = frl->runetype[x]; 1684297a3b0SGarrett D'Amore rl->__maplower[x] = frl->maplower[x]; 1694297a3b0SGarrett D'Amore rl->__mapupper[x] = frl->mapupper[x]; 1704297a3b0SGarrett D'Amore } 1714297a3b0SGarrett D'Amore 1724297a3b0SGarrett D'Amore rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable; 1734297a3b0SGarrett D'Amore rl->__variable = rl->__runetype_ext.__ranges + 1744297a3b0SGarrett D'Amore rl->__runetype_ext.__nranges; 1754297a3b0SGarrett D'Amore 1764297a3b0SGarrett D'Amore rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable; 1774297a3b0SGarrett D'Amore rl->__variable = rl->__maplower_ext.__ranges + 1784297a3b0SGarrett D'Amore rl->__maplower_ext.__nranges; 1794297a3b0SGarrett D'Amore 1804297a3b0SGarrett D'Amore rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable; 1814297a3b0SGarrett D'Amore rl->__variable = rl->__mapupper_ext.__ranges + 1824297a3b0SGarrett D'Amore rl->__mapupper_ext.__nranges; 1834297a3b0SGarrett D'Amore 1844297a3b0SGarrett D'Amore variable = mapupper_ext_ranges + frl->mapupper_ext_nranges; 1854297a3b0SGarrett D'Amore frr = runetype_ext_ranges; 1864297a3b0SGarrett D'Amore rr = rl->__runetype_ext.__ranges; 1874297a3b0SGarrett D'Amore for (x = 0; x < rl->__runetype_ext.__nranges; ++x) { 1884297a3b0SGarrett D'Amore uint32_t *types; 1894297a3b0SGarrett D'Amore 1904297a3b0SGarrett D'Amore rr[x].__min = frr[x].min; 1914297a3b0SGarrett D'Amore rr[x].__max = frr[x].max; 1924297a3b0SGarrett D'Amore rr[x].__map = frr[x].map; 1934297a3b0SGarrett D'Amore if (rr[x].__map == 0) { 1944297a3b0SGarrett D'Amore int len = rr[x].__max - rr[x].__min + 1; 1954297a3b0SGarrett D'Amore types = variable; 1964297a3b0SGarrett D'Amore variable = types + len; 1974297a3b0SGarrett D'Amore rr[x].__types = rl->__variable; 1984297a3b0SGarrett D'Amore rl->__variable = rr[x].__types + len; 1994297a3b0SGarrett D'Amore while (len-- > 0) 2004297a3b0SGarrett D'Amore rr[x].__types[len] = types[len]; 2014297a3b0SGarrett D'Amore } else 2024297a3b0SGarrett D'Amore rr[x].__types = NULL; 2034297a3b0SGarrett D'Amore } 2044297a3b0SGarrett D'Amore 2054297a3b0SGarrett D'Amore frr = maplower_ext_ranges; 2064297a3b0SGarrett D'Amore rr = rl->__maplower_ext.__ranges; 2074297a3b0SGarrett D'Amore for (x = 0; x < rl->__maplower_ext.__nranges; ++x) { 2084297a3b0SGarrett D'Amore rr[x].__min = frr[x].min; 2094297a3b0SGarrett D'Amore rr[x].__max = frr[x].max; 2104297a3b0SGarrett D'Amore rr[x].__map = frr[x].map; 2114297a3b0SGarrett D'Amore } 2124297a3b0SGarrett D'Amore 2134297a3b0SGarrett D'Amore frr = mapupper_ext_ranges; 2144297a3b0SGarrett D'Amore rr = rl->__mapupper_ext.__ranges; 2154297a3b0SGarrett D'Amore for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) { 2164297a3b0SGarrett D'Amore rr[x].__min = frr[x].min; 2174297a3b0SGarrett D'Amore rr[x].__max = frr[x].max; 2184297a3b0SGarrett D'Amore rr[x].__map = frr[x].map; 2194297a3b0SGarrett D'Amore } 2204297a3b0SGarrett D'Amore 2214297a3b0SGarrett D'Amore (void) memcpy(rl->__variable, variable, rl->__variable_len); 222*2d08521bSGarrett D'Amore (void) munmap(fdata, sb.st_size); 2234297a3b0SGarrett D'Amore 2244297a3b0SGarrett D'Amore /* 2254297a3b0SGarrett D'Amore * Go out and zero pointers that should be zero. 2264297a3b0SGarrett D'Amore */ 2274297a3b0SGarrett D'Amore if (!rl->__variable_len) 2284297a3b0SGarrett D'Amore rl->__variable = NULL; 2294297a3b0SGarrett D'Amore 2304297a3b0SGarrett D'Amore if (!rl->__runetype_ext.__nranges) 2314297a3b0SGarrett D'Amore rl->__runetype_ext.__ranges = NULL; 2324297a3b0SGarrett D'Amore 2334297a3b0SGarrett D'Amore if (!rl->__maplower_ext.__nranges) 2344297a3b0SGarrett D'Amore rl->__maplower_ext.__ranges = NULL; 2354297a3b0SGarrett D'Amore 2364297a3b0SGarrett D'Amore if (!rl->__mapupper_ext.__nranges) 2374297a3b0SGarrett D'Amore rl->__mapupper_ext.__ranges = NULL; 2384297a3b0SGarrett D'Amore 2394297a3b0SGarrett D'Amore return (rl); 240*2d08521bSGarrett D'Amore 241*2d08521bSGarrett D'Amore invalid: 242*2d08521bSGarrett D'Amore (void) munmap(fdata, sb.st_size); 243*2d08521bSGarrett D'Amore errno = EINVAL; 244*2d08521bSGarrett D'Amore return (NULL); 2454297a3b0SGarrett D'Amore } 246