]>
Commit | Line | Data |
---|---|---|
3d9156a7 | 1 | --- rune.c.orig 2004-11-25 11:38:19.000000000 -0800 |
224c7076 A |
2 | +++ rune.c 2005-04-12 17:18:12.000000000 -0700 |
3 | @@ -34,28 +34,98 @@ | |
3d9156a7 A |
4 | * SUCH DAMAGE. |
5 | */ | |
9385eb3d | 6 | |
3d9156a7 A |
7 | +#ifndef RUNEOFF32 |
8 | #if defined(LIBC_SCCS) && !defined(lint) | |
9 | static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93"; | |
10 | #endif /* LIBC_SCCS and not lint */ | |
11 | #include <sys/cdefs.h> | |
12 | __FBSDID("$FreeBSD: src/lib/libc/locale/rune.c,v 1.12 2004/07/29 06:16:19 tjr Exp $"); | |
13 | ||
14 | +#include "xlocale_private.h" | |
15 | + | |
9385eb3d | 16 | #include "namespace.h" |
3d9156a7 | 17 | #include <arpa/inet.h> |
9385eb3d | 18 | #include <errno.h> |
3d9156a7 A |
19 | +#endif /* !RUNEOFF32 */ |
20 | #include <runetype.h> | |
9385eb3d | 21 | #include <stdio.h> |
3d9156a7 A |
22 | +#ifndef RUNEOFF32 |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include "un-namespace.h" | |
28 | +#endif /* !RUNEOFF32 */ | |
224c7076 A |
29 | |
30 | -_RuneLocale * | |
3d9156a7 A |
31 | +#if defined(__LP64__) || defined(RUNEOFF32) |
32 | +/* | |
33 | + * Because the LC_CTYPE files were created with a 32-bit program, we need | |
34 | + * to adjust for the larger pointers in LP64 (the longs have already been | |
35 | + * replaced by 32-bit equivalents). Also, natural alignment will pad | |
36 | + * 64-bit types to 8-byte boundaries, and make structures containing | |
37 | + * 64-bit types sized to 8-byte boundaries. | |
38 | + */ | |
39 | +#include <stddef.h> | |
40 | +#ifndef RUNEOFF32 | |
41 | +#include "rune32.h" | |
42 | +#define BYTES32BITS 4 | |
43 | +#define BYTES64BITS 8 | |
44 | +/* whether to skip over a pointer or not (one-to-one with off64) */ | |
224c7076 | 45 | +int skip[] = { |
3d9156a7 A |
46 | + 1, |
47 | + 1, | |
48 | + 0, | |
49 | + 1, | |
50 | + 0, | |
51 | + 1, | |
52 | + 0, | |
53 | + 1, | |
54 | + 1, | |
224c7076 | 55 | + 1, |
3d9156a7 A |
56 | + 0 |
57 | +}; | |
58 | +#endif /* !RUNEOFF32 */ | |
224c7076 | 59 | +int off64[] = { |
3d9156a7 A |
60 | + offsetof(_RuneLocale, __sgetrune), |
61 | + offsetof(_RuneLocale, __sputrune), | |
62 | + offsetof(_RuneLocale, __runetype_ext), | |
63 | + offsetof(_RuneLocale, __runetype_ext) + offsetof(_RuneRange, __ranges), | |
64 | + offsetof(_RuneLocale, __maplower_ext), | |
65 | + offsetof(_RuneLocale, __maplower_ext) + offsetof(_RuneRange, __ranges), | |
66 | + offsetof(_RuneLocale, __mapupper_ext), | |
67 | + offsetof(_RuneLocale, __mapupper_ext) + offsetof(_RuneRange, __ranges), | |
68 | + offsetof(_RuneLocale, __variable), | |
224c7076 | 69 | + offsetof(_RuneLocale, __charclasses), |
3d9156a7 A |
70 | + sizeof(_RuneLocale) |
71 | +}; | |
72 | +#define NOFF (sizeof(off64) / sizeof(int)) | |
73 | +#ifdef RUNEOFF32 | |
74 | +/* | |
75 | + * This program generates a header file (on stdout) that containes the 32-bit | |
76 | + * offsets, plus some 32-bit sizes | |
77 | + */ | |
78 | +main() | |
79 | +{ | |
80 | + int i; | |
81 | + printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry)); | |
82 | + printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale)); | |
224c7076 | 83 | + printf("int off32[] = {\n"); |
3d9156a7 A |
84 | + for(i = 0; i < NOFF; i++) |
85 | + printf("\t%d,\n", off64[i]); | |
86 | + printf("};\n"); | |
87 | + return 0; | |
88 | +} | |
89 | +#endif /* RUNEOFF32 */ | |
90 | +#else /* !__LP64__ && !RUNEOFF32 */ | |
91 | +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale) | |
92 | +#endif /* __LP64__ || RUNEOFF32 */ | |
224c7076 | 93 | + |
3d9156a7 A |
94 | +#ifndef RUNEOFF32 |
95 | +struct __xlocale_st_runelocale * | |
96 | _Read_RuneMagi(fp) | |
97 | FILE *fp; | |
98 | { | |
99 | - char *data; | |
100 | + struct __xlocale_st_runelocale *data; | |
101 | void *lastp; | |
102 | _RuneLocale *rl; | |
103 | _RuneEntry *rr; | |
224c7076 | 104 | @@ -65,13 +135,20 @@ |
3d9156a7 A |
105 | if (_fstat(fileno(fp), &sb) < 0) |
106 | return (NULL); | |
107 | ||
108 | - if (sb.st_size < sizeof(_RuneLocale)) { | |
109 | + if (sb.st_size < SIZEOF32_RUNELOCALE) { | |
110 | errno = EFTYPE; | |
111 | return (NULL); | |
112 | } | |
113 | ||
114 | - if ((data = malloc(sb.st_size)) == NULL) | |
115 | +#ifdef __LP64__ | |
116 | + /* will adjust later */ | |
117 | + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale))) == NULL) | |
118 | +#else /* !__LP64__ */ | |
119 | + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale) - sizeof(_RuneLocale) + sb.st_size)) == NULL) | |
120 | +#endif /* __LP64__ */ | |
121 | return (NULL); | |
122 | + data->__refcount = 1; | |
123 | + data->__free_extra = NULL; | |
124 | ||
125 | errno = 0; | |
126 | rewind(fp); /* Someone might have read the magic number once already */ | |
224c7076 | 127 | @@ -82,26 +159,43 @@ |
3d9156a7 A |
128 | return (NULL); |
129 | } | |
130 | ||
131 | - if (fread(data, sb.st_size, 1, fp) != 1) { | |
132 | + rl = &data->_CurrentRuneLocale; | |
133 | + | |
134 | +#ifdef __LP64__ | |
135 | + if (fread(rl, SIZEOF32_RUNELOCALE, 1, fp) != 1) | |
136 | +#else /* !__LP64__ */ | |
137 | + if (fread(rl, sb.st_size, 1, fp) != 1) | |
138 | +#endif /* __LP64__ */ | |
139 | + { | |
140 | saverr = errno; | |
141 | free(data); | |
142 | errno = saverr; | |
143 | return (NULL); | |
144 | } | |
145 | ||
146 | - rl = (_RuneLocale *)data; | |
147 | - lastp = data + sb.st_size; | |
148 | +#ifndef __LP64__ | |
149 | + lastp = (char *)rl + sb.st_size; | |
150 | ||
151 | rl->__variable = rl + 1; | |
152 | +#endif /* __LP64__ */ | |
153 | ||
224c7076 A |
154 | - if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) { |
155 | + if (memcmp(rl->__magic, _RUNE_MAGIC_A, sizeof(rl->__magic))) { | |
3d9156a7 | 156 | free(data); |
224c7076 | 157 | errno = EFTYPE; |
3d9156a7 A |
158 | return (NULL); |
159 | } | |
160 | ||
161 | +#ifdef __LP64__ | |
162 | + /* shift things into the right position */ | |
163 | + for (x = NOFF - 2; x >= 0; x--) | |
164 | + memmove((char *)rl + off64[x] + (skip[x] ? BYTES64BITS : 0), | |
165 | + (char *)rl + off32[x] + (skip[x] ? BYTES32BITS : 0), | |
166 | + off32[x + 1] - off32[x] - (skip[x] ? BYTES32BITS : 0)); | |
167 | +#endif /* __LP64__ */ | |
168 | +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN | |
169 | rl->__invalid_rune = ntohl(rl->__invalid_rune); | |
170 | rl->__variable_len = ntohl(rl->__variable_len); | |
224c7076 | 171 | + rl->__ncharclasses = ntohl(rl->__ncharclasses); |
3d9156a7 | 172 | rl->__runetype_ext.__nranges = ntohl(rl->__runetype_ext.__nranges); |
224c7076 A |
173 | rl->__maplower_ext.__nranges = ntohl(rl->__maplower_ext.__nranges); |
174 | rl->__mapupper_ext.__nranges = ntohl(rl->__mapupper_ext.__nranges); | |
175 | @@ -111,7 +205,54 @@ | |
3d9156a7 A |
176 | rl->__maplower[x] = ntohl(rl->__maplower[x]); |
177 | rl->__mapupper[x] = ntohl(rl->__mapupper[x]); | |
178 | } | |
179 | +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ | |
224c7076 | 180 | |
3d9156a7 A |
181 | +#ifdef __LP64__ |
182 | + { | |
183 | + int count = rl->__runetype_ext.__nranges + rl->__maplower_ext.__nranges | |
184 | + + rl->__mapupper_ext.__nranges; | |
224c7076 | 185 | + int extra = sb.st_size - SIZEOF32_RUNELOCALE - count * SIZEOF32_RUNEENTRY - rl->__ncharclasses * sizeof(_RuneCharClass); |
3d9156a7 | 186 | + _RuneEntry *rp; |
224c7076 | 187 | + |
3d9156a7 A |
188 | + if (extra < 0) { |
189 | + saverr = errno; | |
190 | + free(data); | |
191 | + errno = saverr; | |
192 | + return (NULL); | |
193 | + } | |
224c7076 A |
194 | + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale) |
195 | + + count * sizeof(_RuneEntry) | |
196 | + + rl->__ncharclasses * sizeof(_RuneCharClass) | |
197 | + + extra)) == NULL) | |
3d9156a7 A |
198 | + return (NULL); |
199 | + rl = &data->_CurrentRuneLocale; | |
200 | + rl->__variable = rl + 1; | |
201 | + rp = (_RuneEntry *)rl->__variable; | |
202 | + for (x = 0; x < count; x++, rp++) | |
203 | + if (fread(rp, SIZEOF32_RUNEENTRY, 1, fp) != 1) { | |
204 | + saverr = errno; | |
205 | + free(data); | |
206 | + errno = saverr; | |
207 | + return (NULL); | |
208 | + } | |
224c7076 A |
209 | + if (rl->__ncharclasses > 0) { |
210 | + if (fread(rp, sizeof(_RuneCharClass), rl->__ncharclasses, fp) != rl->__ncharclasses) { | |
211 | + saverr = errno; | |
212 | + free(data); | |
213 | + errno = saverr; | |
214 | + return (NULL); | |
215 | + } | |
216 | + rp = (_RuneEntry *)((char *)rp + rl->__ncharclasses * sizeof(_RuneCharClass)); | |
217 | + } | |
3d9156a7 A |
218 | + if (extra > 0 && fread(rp, extra, 1, fp) != 1) { |
219 | + saverr = errno; | |
220 | + free(data); | |
221 | + errno = saverr; | |
222 | + return (NULL); | |
223 | + } | |
224 | + lastp = (char *)rp + extra; | |
225 | + } | |
226 | +#endif /* __LP64__ */ | |
227 | rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable; | |
228 | rl->__variable = rl->__runetype_ext.__ranges + | |
229 | rl->__runetype_ext.__nranges; | |
224c7076 | 230 | @@ -142,8 +283,10 @@ |
3d9156a7 A |
231 | for (x = 0; x < rl->__runetype_ext.__nranges; ++x) { |
232 | rr = rl->__runetype_ext.__ranges; | |
233 | ||
234 | +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN | |
235 | rr[x].__min = ntohl(rr[x].__min); | |
236 | rr[x].__max = ntohl(rr[x].__max); | |
237 | +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ | |
238 | if ((rr[x].__map = ntohl(rr[x].__map)) == 0) { | |
239 | int len = rr[x].__max - rr[x].__min + 1; | |
240 | rr[x].__types = rl->__variable; | |
224c7076 | 241 | @@ -153,12 +296,15 @@ |
3d9156a7 A |
242 | errno = EFTYPE; |
243 | return (NULL); | |
244 | } | |
245 | +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN | |
246 | while (len-- > 0) | |
247 | rr[x].__types[len] = ntohl(rr[x].__types[len]); | |
248 | +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ | |
249 | } else | |
250 | rr[x].__types = 0; | |
251 | } | |
252 | ||
253 | +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN | |
254 | for (x = 0; x < rl->__maplower_ext.__nranges; ++x) { | |
255 | rr = rl->__maplower_ext.__ranges; | |
256 | ||
224c7076 | 257 | @@ -174,6 +320,22 @@ |
3d9156a7 A |
258 | rr[x].__max = ntohl(rr[x].__max); |
259 | rr[x].__map = ntohl(rr[x].__map); | |
260 | } | |
261 | +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ | |
224c7076 A |
262 | + |
263 | + if (rl->__ncharclasses > 0) { | |
264 | + rl->__charclasses = (_RuneCharClass *)rl->__variable; | |
265 | + rl->__variable = (void *)(rl->__charclasses + rl->__ncharclasses); | |
266 | + if (rl->__variable > lastp) { | |
267 | + free(data); | |
268 | + errno = EFTYPE; | |
269 | + return (NULL); | |
270 | + } | |
271 | +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN | |
272 | + for (x = 0; x < rl->__ncharclasses; ++x) | |
273 | + rl->__charclasses[x].__mask = ntohl(rl->__charclasses[x].__mask); | |
274 | +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ | |
275 | + } | |
276 | + | |
3d9156a7 A |
277 | if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) { |
278 | free(data); | |
279 | errno = EFTYPE; | |
224c7076 | 280 | @@ -195,5 +357,7 @@ |
3d9156a7 A |
281 | if (!rl->__mapupper_ext.__nranges) |
282 | rl->__mapupper_ext.__ranges = 0; | |
283 | ||
284 | - return (rl); | |
285 | + data->__datasize = lastp - (void *)data; | |
286 | + return (data); | |
287 | } | |
288 | +#endif /* !RUNEOFF32 */ |