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