1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright 2014 Garrett D'Amore <garrett@damore.org> 5 * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 6 * Copyright (c) 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Paul Borman at Krystal Technologies. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if defined(LIBC_SCCS) && !defined(lint) 38 static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93"; 39 #endif /* LIBC_SCCS and not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 #include "namespace.h" 44 #include <arpa/inet.h> 45 #include <errno.h> 46 #include <runetype.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <stdlib.h> 50 #include <sys/types.h> 51 #include <sys/stat.h> 52 #include <sys/mman.h> 53 #include <fcntl.h> 54 #include <unistd.h> 55 #include "un-namespace.h" 56 57 #include "endian.h" 58 #include "runefile.h" 59 60 _RuneLocale * 61 _Read_RuneMagi(const char *fname) 62 { 63 char *fdata, *data; 64 void *lastp; 65 _FileRuneLocale *frl; 66 _RuneLocale *rl; 67 _FileRuneEntry *frr; 68 _RuneEntry *rr; 69 struct stat sb; 70 int x, saverr; 71 void *variable; 72 _FileRuneEntry *runetype_ext_ranges; 73 _FileRuneEntry *maplower_ext_ranges; 74 _FileRuneEntry *mapupper_ext_ranges; 75 int runetype_ext_len = 0; 76 int fd; 77 78 if ((fd = _open(fname, O_RDONLY)) < 0) { 79 errno = EINVAL; 80 return (NULL); 81 } 82 83 if (_fstat(fd, &sb) < 0) { 84 (void) _close(fd); 85 errno = EINVAL; 86 return (NULL); 87 } 88 89 if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) { 90 (void) _close(fd); 91 errno = EINVAL; 92 return (NULL); 93 } 94 95 96 fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 97 (void) _close(fd); 98 if (fdata == NULL) { 99 errno = EINVAL; 100 return (NULL); 101 } 102 103 frl = (_FileRuneLocale *)(void *)fdata; 104 lastp = fdata + sb.st_size; 105 106 variable = frl + 1; 107 108 if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) { 109 goto invalid; 110 } 111 112 runetype_ext_ranges = (_FileRuneEntry *)variable; 113 variable = runetype_ext_ranges + BSWAP(frl->runetype_ext_nranges); 114 if (variable > lastp) { 115 goto invalid; 116 } 117 118 maplower_ext_ranges = (_FileRuneEntry *)variable; 119 variable = maplower_ext_ranges + BSWAP(frl->maplower_ext_nranges); 120 if (variable > lastp) { 121 goto invalid; 122 } 123 124 mapupper_ext_ranges = (_FileRuneEntry *)variable; 125 variable = mapupper_ext_ranges + BSWAP(frl->mapupper_ext_nranges); 126 if (variable > lastp) { 127 goto invalid; 128 } 129 130 frr = runetype_ext_ranges; 131 for (x = 0; x < BSWAP(frl->runetype_ext_nranges); ++x) { 132 uint32_t *types; 133 134 if (BSWAP(frr[x].map) == 0) { 135 int len = BSWAP(frr[x].max) - BSWAP(frr[x].min) + 1; 136 types = variable; 137 variable = types + len; 138 runetype_ext_len += len; 139 if (variable > lastp) { 140 goto invalid; 141 } 142 } 143 } 144 145 if ((char *)variable + BSWAP(frl->variable_len) > (char *)lastp) { 146 goto invalid; 147 } 148 149 /* 150 * Convert from disk format to host format. 151 */ 152 data = malloc(sizeof(_RuneLocale) + 153 (BSWAP(frl->runetype_ext_nranges) + BSWAP(frl->maplower_ext_nranges) + 154 BSWAP(frl->mapupper_ext_nranges)) * sizeof(_RuneEntry) + 155 runetype_ext_len * sizeof(*rr->__types) + 156 BSWAP(frl->variable_len)); 157 if (data == NULL) { 158 saverr = errno; 159 munmap(fdata, sb.st_size); 160 errno = saverr; 161 return (NULL); 162 } 163 164 rl = (_RuneLocale *)data; 165 rl->__variable = rl + 1; 166 167 memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic)); 168 memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding)); 169 170 rl->__variable_len = BSWAP(frl->variable_len); 171 rl->__runetype_ext.__nranges = BSWAP(frl->runetype_ext_nranges); 172 rl->__maplower_ext.__nranges = BSWAP(frl->maplower_ext_nranges); 173 rl->__mapupper_ext.__nranges = BSWAP(frl->mapupper_ext_nranges); 174 175 for (x = 0; x < _CACHED_RUNES; ++x) { 176 rl->__runetype[x] = BSWAP(frl->runetype[x]); 177 rl->__maplower[x] = BSWAP(frl->maplower[x]); 178 rl->__mapupper[x] = BSWAP(frl->mapupper[x]); 179 } 180 181 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable; 182 rl->__variable = rl->__runetype_ext.__ranges + 183 rl->__runetype_ext.__nranges; 184 185 rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable; 186 rl->__variable = rl->__maplower_ext.__ranges + 187 rl->__maplower_ext.__nranges; 188 189 rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable; 190 rl->__variable = rl->__mapupper_ext.__ranges + 191 rl->__mapupper_ext.__nranges; 192 193 variable = mapupper_ext_ranges + BSWAP(frl->mapupper_ext_nranges); 194 frr = runetype_ext_ranges; 195 rr = rl->__runetype_ext.__ranges; 196 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) { 197 uint32_t *types; 198 199 rr[x].__min = BSWAP(frr[x].min); 200 rr[x].__max = BSWAP(frr[x].max); 201 rr[x].__map = BSWAP(frr[x].map); 202 if (rr[x].__map == 0) { 203 int len = rr[x].__max - rr[x].__min + 1; 204 types = variable; 205 variable = types + len; 206 rr[x].__types = rl->__variable; 207 rl->__variable = rr[x].__types + len; 208 while (len-- > 0) 209 rr[x].__types[len] = types[len]; 210 } else 211 rr[x].__types = NULL; 212 } 213 214 frr = maplower_ext_ranges; 215 rr = rl->__maplower_ext.__ranges; 216 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) { 217 rr[x].__min = BSWAP(frr[x].min); 218 rr[x].__max = BSWAP(frr[x].max); 219 rr[x].__map = BSWAP(frr[x].map); 220 } 221 222 frr = mapupper_ext_ranges; 223 rr = rl->__mapupper_ext.__ranges; 224 for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) { 225 rr[x].__min = BSWAP(frr[x].min); 226 rr[x].__max = BSWAP(frr[x].max); 227 rr[x].__map = BSWAP(frr[x].map); 228 } 229 230 memcpy(rl->__variable, variable, rl->__variable_len); 231 munmap(fdata, sb.st_size); 232 233 /* 234 * Go out and zero pointers that should be zero. 235 */ 236 if (!rl->__variable_len) 237 rl->__variable = NULL; 238 239 if (!rl->__runetype_ext.__nranges) 240 rl->__runetype_ext.__ranges = NULL; 241 242 if (!rl->__maplower_ext.__nranges) 243 rl->__maplower_ext.__ranges = NULL; 244 245 if (!rl->__mapupper_ext.__nranges) 246 rl->__mapupper_ext.__ranges = NULL; 247 248 return (rl); 249 250 invalid: 251 munmap(fdata, sb.st_size); 252 errno = EINVAL; 253 return (NULL); 254 } 255