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