1 --- rune.c.orig 2004-11-25 11:38:19.000000000 -0800
2 +++ rune.c 2005-02-14 19:25:48.000000000 -0800
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 */
30 +#if defined(__LP64__) || defined(RUNEOFF32)
32 + * Because the LC_CTYPE files were created with a 32-bit program, we need
33 + * to adjust for the larger pointers in LP64 (the longs have already been
34 + * replaced by 32-bit equivalents). Also, natural alignment will pad
35 + * 64-bit types to 8-byte boundaries, and make structures containing
36 + * 64-bit types sized to 8-byte boundaries.
41 +#define BYTES32BITS 4
42 +#define BYTES64BITS 8
43 +/* whether to skip over a pointer or not (one-to-one with off64) */
44 +static int skip[] = {
56 +#endif /* !RUNEOFF32 */
57 +static int off64[] = {
58 + offsetof(_RuneLocale, __sgetrune),
59 + offsetof(_RuneLocale, __sputrune),
60 + offsetof(_RuneLocale, __runetype_ext),
61 + offsetof(_RuneLocale, __runetype_ext) + offsetof(_RuneRange, __ranges),
62 + offsetof(_RuneLocale, __maplower_ext),
63 + offsetof(_RuneLocale, __maplower_ext) + offsetof(_RuneRange, __ranges),
64 + offsetof(_RuneLocale, __mapupper_ext),
65 + offsetof(_RuneLocale, __mapupper_ext) + offsetof(_RuneRange, __ranges),
66 + offsetof(_RuneLocale, __variable),
69 +#define NOFF (sizeof(off64) / sizeof(int))
72 + * This program generates a header file (on stdout) that containes the 32-bit
73 + * offsets, plus some 32-bit sizes
78 + printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry));
79 + printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale));
80 + printf("static int off32[] = {\n");
81 + for(i = 0; i < NOFF; i++)
82 + printf("\t%d,\n", off64[i]);
86 +#endif /* RUNEOFF32 */
87 +#else /* !__LP64__ && !RUNEOFF32 */
88 +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
89 +#endif /* __LP64__ || RUNEOFF32 */
93 +struct __xlocale_st_runelocale *
98 + struct __xlocale_st_runelocale *data;
103 if (_fstat(fileno(fp), &sb) < 0)
106 - if (sb.st_size < sizeof(_RuneLocale)) {
107 + if (sb.st_size < SIZEOF32_RUNELOCALE) {
112 - if ((data = malloc(sb.st_size)) == NULL)
114 + /* will adjust later */
115 + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale))) == NULL)
116 +#else /* !__LP64__ */
117 + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale) - sizeof(_RuneLocale) + sb.st_size)) == NULL)
118 +#endif /* __LP64__ */
120 + data->__refcount = 1;
121 + data->__free_extra = NULL;
124 rewind(fp); /* Someone might have read the magic number once already */
129 - if (fread(data, sb.st_size, 1, fp) != 1) {
130 + rl = &data->_CurrentRuneLocale;
133 + if (fread(rl, SIZEOF32_RUNELOCALE, 1, fp) != 1)
134 +#else /* !__LP64__ */
135 + if (fread(rl, sb.st_size, 1, fp) != 1)
136 +#endif /* __LP64__ */
144 - rl = (_RuneLocale *)data;
145 - lastp = data + sb.st_size;
147 + lastp = (char *)rl + sb.st_size;
149 rl->__variable = rl + 1;
150 +#endif /* __LP64__ */
152 if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) {
159 + /* shift things into the right position */
160 + for (x = NOFF - 2; x >= 0; x--)
161 + memmove((char *)rl + off64[x] + (skip[x] ? BYTES64BITS : 0),
162 + (char *)rl + off32[x] + (skip[x] ? BYTES32BITS : 0),
163 + off32[x + 1] - off32[x] - (skip[x] ? BYTES32BITS : 0));
164 +#endif /* __LP64__ */
165 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
166 rl->__invalid_rune = ntohl(rl->__invalid_rune);
167 rl->__variable_len = ntohl(rl->__variable_len);
168 rl->__runetype_ext.__nranges = ntohl(rl->__runetype_ext.__nranges);
170 rl->__maplower[x] = ntohl(rl->__maplower[x]);
171 rl->__mapupper[x] = ntohl(rl->__mapupper[x]);
173 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
177 + int count = rl->__runetype_ext.__nranges + rl->__maplower_ext.__nranges
178 + + rl->__mapupper_ext.__nranges;
179 + int extra = sb.st_size - SIZEOF32_RUNELOCALE - count * SIZEOF32_RUNEENTRY;
188 + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale) +
189 + count * sizeof(_RuneEntry) + extra)) == NULL)
191 + rl = &data->_CurrentRuneLocale;
192 + rl->__variable = rl + 1;
193 + rp = (_RuneEntry *)rl->__variable;
194 + for (x = 0; x < count; x++, rp++)
195 + if (fread(rp, SIZEOF32_RUNEENTRY, 1, fp) != 1) {
201 + if (extra > 0 && fread(rp, extra, 1, fp) != 1) {
207 + lastp = (char *)rp + extra;
209 +#endif /* __LP64__ */
210 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
211 rl->__variable = rl->__runetype_ext.__ranges +
212 rl->__runetype_ext.__nranges;
214 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
215 rr = rl->__runetype_ext.__ranges;
217 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
218 rr[x].__min = ntohl(rr[x].__min);
219 rr[x].__max = ntohl(rr[x].__max);
220 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
221 if ((rr[x].__map = ntohl(rr[x].__map)) == 0) {
222 int len = rr[x].__max - rr[x].__min + 1;
223 rr[x].__types = rl->__variable;
224 @@ -153,12 +282,15 @@
228 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
230 rr[x].__types[len] = ntohl(rr[x].__types[len]);
231 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
236 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
237 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
238 rr = rl->__maplower_ext.__ranges;
241 rr[x].__max = ntohl(rr[x].__max);
242 rr[x].__map = ntohl(rr[x].__map);
244 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
245 if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) {
249 if (!rl->__mapupper_ext.__nranges)
250 rl->__mapupper_ext.__ranges = 0;
253 + data->__datasize = lastp - (void *)data;
256 +#endif /* !RUNEOFF32 */