1 --- rune.c.orig 2004-11-25 11:38:19.000000000 -0800
2 +++ rune.c 2005-04-12 17:18:12.000000000 -0700
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 $");
14 +#include "xlocale_private.h"
16 #include "namespace.h"
17 #include <arpa/inet.h>
19 +#endif /* !RUNEOFF32 */
25 #include <sys/types.h>
27 #include "un-namespace.h"
28 +#endif /* !RUNEOFF32 */
31 +#if defined(__LP64__) || defined(RUNEOFF32)
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.
42 +#define BYTES32BITS 4
43 +#define BYTES64BITS 8
44 +/* whether to skip over a pointer or not (one-to-one with off64) */
58 +#endif /* !RUNEOFF32 */
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),
69 + offsetof(_RuneLocale, __charclasses),
72 +#define NOFF (sizeof(off64) / sizeof(int))
75 + * This program generates a header file (on stdout) that containes the 32-bit
76 + * offsets, plus some 32-bit sizes
81 + printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry));
82 + printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale));
83 + printf("int off32[] = {\n");
84 + for(i = 0; i < NOFF; i++)
85 + printf("\t%d,\n", off64[i]);
89 +#endif /* RUNEOFF32 */
90 +#else /* !__LP64__ && !RUNEOFF32 */
91 +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
92 +#endif /* __LP64__ || RUNEOFF32 */
95 +struct __xlocale_st_runelocale *
100 + struct __xlocale_st_runelocale *data;
105 if (_fstat(fileno(fp), &sb) < 0)
108 - if (sb.st_size < sizeof(_RuneLocale)) {
109 + if (sb.st_size < SIZEOF32_RUNELOCALE) {
114 - if ((data = malloc(sb.st_size)) == NULL)
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__ */
122 + data->__refcount = 1;
123 + data->__free_extra = NULL;
126 rewind(fp); /* Someone might have read the magic number once already */
131 - if (fread(data, sb.st_size, 1, fp) != 1) {
132 + rl = &data->_CurrentRuneLocale;
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__ */
146 - rl = (_RuneLocale *)data;
147 - lastp = data + sb.st_size;
149 + lastp = (char *)rl + sb.st_size;
151 rl->__variable = rl + 1;
152 +#endif /* __LP64__ */
154 - if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) {
155 + if (memcmp(rl->__magic, _RUNE_MAGIC_A, sizeof(rl->__magic))) {
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);
171 + rl->__ncharclasses = ntohl(rl->__ncharclasses);
172 rl->__runetype_ext.__nranges = ntohl(rl->__runetype_ext.__nranges);
173 rl->__maplower_ext.__nranges = ntohl(rl->__maplower_ext.__nranges);
174 rl->__mapupper_ext.__nranges = ntohl(rl->__mapupper_ext.__nranges);
176 rl->__maplower[x] = ntohl(rl->__maplower[x]);
177 rl->__mapupper[x] = ntohl(rl->__mapupper[x]);
179 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
183 + int count = rl->__runetype_ext.__nranges + rl->__maplower_ext.__nranges
184 + + rl->__mapupper_ext.__nranges;
185 + int extra = sb.st_size - SIZEOF32_RUNELOCALE - count * SIZEOF32_RUNEENTRY - rl->__ncharclasses * sizeof(_RuneCharClass);
194 + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale)
195 + + count * sizeof(_RuneEntry)
196 + + rl->__ncharclasses * sizeof(_RuneCharClass)
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) {
209 + if (rl->__ncharclasses > 0) {
210 + if (fread(rp, sizeof(_RuneCharClass), rl->__ncharclasses, fp) != rl->__ncharclasses) {
216 + rp = (_RuneEntry *)((char *)rp + rl->__ncharclasses * sizeof(_RuneCharClass));
218 + if (extra > 0 && fread(rp, extra, 1, fp) != 1) {
224 + lastp = (char *)rp + extra;
226 +#endif /* __LP64__ */
227 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
228 rl->__variable = rl->__runetype_ext.__ranges +
229 rl->__runetype_ext.__nranges;
231 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
232 rr = rl->__runetype_ext.__ranges;
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;
241 @@ -153,12 +296,15 @@
245 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
247 rr[x].__types[len] = ntohl(rr[x].__types[len]);
248 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
253 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
254 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
255 rr = rl->__maplower_ext.__ranges;
258 rr[x].__max = ntohl(rr[x].__max);
259 rr[x].__map = ntohl(rr[x].__map);
261 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
263 + if (rl->__ncharclasses > 0) {
264 + rl->__charclasses = (_RuneCharClass *)rl->__variable;
265 + rl->__variable = (void *)(rl->__charclasses + rl->__ncharclasses);
266 + if (rl->__variable > lastp) {
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 */
277 if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) {
281 if (!rl->__mapupper_ext.__nranges)
282 rl->__mapupper_ext.__ranges = 0;
285 + data->__datasize = lastp - (void *)data;
288 +#endif /* !RUNEOFF32 */