2 ===================================================================
3 --- rune.c (revision 59751)
4 +++ rune.c (working copy)
10 #if defined(LIBC_SCCS) && !defined(lint)
11 static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
12 #endif /* LIBC_SCCS and not lint */
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD: src/lib/libc/locale/rune.c,v 1.12 2004/07/29 06:16:19 tjr Exp $");
16 +#include "xlocale_private.h"
18 #include "namespace.h"
19 #include <arpa/inet.h>
23 +#include "runetype.h"
24 +#endif /* !RUNEOFF32 */
29 #include <sys/types.h>
31 #include "un-namespace.h"
32 +#endif /* !RUNEOFF32 */
35 +#if defined(__LP64__) || defined(RUNEOFF32)
37 + * Because the LC_CTYPE files were created with a 32-bit program, we need
38 + * to adjust for the larger pointers in LP64 (the longs have already been
39 + * replaced by 32-bit equivalents). Also, natural alignment will pad
40 + * 64-bit types to 8-byte boundaries, and make structures containing
41 + * 64-bit types sized to 8-byte boundaries.
46 +#define BYTES32BITS 4
47 +#define BYTES64BITS 8
48 +/* whether to skip over a pointer or not (one-to-one with off64) */
62 +#endif /* !RUNEOFF32 */
64 + offsetof(_RuneLocale, __sgetrune),
65 + offsetof(_RuneLocale, __sputrune),
66 + offsetof(_RuneLocale, __runetype_ext),
67 + offsetof(_RuneLocale, __runetype_ext) + offsetof(_RuneRange, __ranges),
68 + offsetof(_RuneLocale, __maplower_ext),
69 + offsetof(_RuneLocale, __maplower_ext) + offsetof(_RuneRange, __ranges),
70 + offsetof(_RuneLocale, __mapupper_ext),
71 + offsetof(_RuneLocale, __mapupper_ext) + offsetof(_RuneRange, __ranges),
72 + offsetof(_RuneLocale, __variable),
73 + offsetof(_RuneLocale, __charclasses),
76 +#define NOFF (sizeof(off64) / sizeof(int))
79 + * This program generates a header file (on stdout) that containes the 32-bit
80 + * offsets, plus some 32-bit sizes
85 + printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry));
86 + printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale));
87 + printf("int off32[] = {\n");
88 + for(i = 0; i < NOFF; i++)
89 + printf("\t%d,\n", off64[i]);
93 +#endif /* RUNEOFF32 */
94 +#else /* !__LP64__ && !RUNEOFF32 */
95 +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
96 +#endif /* __LP64__ || RUNEOFF32 */
99 +struct __xlocale_st_runelocale *
104 + struct __xlocale_st_runelocale *data;
109 if (_fstat(fileno(fp), &sb) < 0)
112 - if (sb.st_size < sizeof(_RuneLocale)) {
113 + if (sb.st_size < SIZEOF32_RUNELOCALE) {
118 - if ((data = malloc(sb.st_size)) == NULL)
120 + /* will adjust later */
121 + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale))) == NULL)
122 +#else /* !__LP64__ */
123 + if ((data = (struct __xlocale_st_runelocale *)malloc(sizeof(struct __xlocale_st_runelocale) - sizeof(_RuneLocale) + sb.st_size)) == NULL)
124 +#endif /* __LP64__ */
126 + data->__refcount = 1;
127 + data->__free_extra = NULL;
130 rewind(fp); /* Someone might have read the magic number once already */
135 - if (fread(data, sb.st_size, 1, fp) != 1) {
136 + rl = &data->_CurrentRuneLocale;
139 + if (fread(rl, SIZEOF32_RUNELOCALE, 1, fp) != 1)
140 +#else /* !__LP64__ */
141 + if (fread(rl, sb.st_size, 1, fp) != 1)
142 +#endif /* __LP64__ */
150 - rl = (_RuneLocale *)data;
151 - lastp = data + sb.st_size;
153 + lastp = (char *)rl + sb.st_size;
155 rl->__variable = rl + 1;
156 +#endif /* __LP64__ */
158 - if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) {
159 + if (memcmp(rl->__magic, _RUNE_MAGIC_A, sizeof(rl->__magic))) {
166 + /* shift things into the right position */
167 + for (x = NOFF - 2; x >= 0; x--)
168 + memmove((char *)rl + off64[x] + (skip[x] ? BYTES64BITS : 0),
169 + (char *)rl + off32[x] + (skip[x] ? BYTES32BITS : 0),
170 + off32[x + 1] - off32[x] - (skip[x] ? BYTES32BITS : 0));
171 +#endif /* __LP64__ */
172 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
173 rl->__invalid_rune = ntohl(rl->__invalid_rune);
174 rl->__variable_len = ntohl(rl->__variable_len);
175 + rl->__ncharclasses = ntohl(rl->__ncharclasses);
176 rl->__runetype_ext.__nranges = ntohl(rl->__runetype_ext.__nranges);
177 rl->__maplower_ext.__nranges = ntohl(rl->__maplower_ext.__nranges);
178 rl->__mapupper_ext.__nranges = ntohl(rl->__mapupper_ext.__nranges);
180 rl->__maplower[x] = ntohl(rl->__maplower[x]);
181 rl->__mapupper[x] = ntohl(rl->__mapupper[x]);
183 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
187 + int count = rl->__runetype_ext.__nranges + rl->__maplower_ext.__nranges
188 + + rl->__mapupper_ext.__nranges;
189 + int extra = sb.st_size - SIZEOF32_RUNELOCALE - count * SIZEOF32_RUNEENTRY - rl->__ncharclasses * sizeof(_RuneCharClass);
198 + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale)
199 + + count * sizeof(_RuneEntry)
200 + + rl->__ncharclasses * sizeof(_RuneCharClass)
203 + rl = &data->_CurrentRuneLocale;
204 + rl->__variable = rl + 1;
205 + rp = (_RuneEntry *)rl->__variable;
206 + for (x = 0; x < count; x++, rp++)
207 + if (fread(rp, SIZEOF32_RUNEENTRY, 1, fp) != 1) {
213 + if (rl->__ncharclasses > 0) {
214 + if (fread(rp, sizeof(_RuneCharClass), rl->__ncharclasses, fp) != rl->__ncharclasses) {
220 + rp = (_RuneEntry *)((char *)rp + rl->__ncharclasses * sizeof(_RuneCharClass));
222 + if (extra > 0 && fread(rp, extra, 1, fp) != 1) {
228 + lastp = (char *)rp + extra;
230 +#endif /* __LP64__ */
231 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
232 rl->__variable = rl->__runetype_ext.__ranges +
233 rl->__runetype_ext.__nranges;
235 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
236 rr = rl->__runetype_ext.__ranges;
238 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
239 rr[x].__min = ntohl(rr[x].__min);
240 rr[x].__max = ntohl(rr[x].__max);
241 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
242 if ((rr[x].__map = ntohl(rr[x].__map)) == 0) {
243 int len = rr[x].__max - rr[x].__min + 1;
244 rr[x].__types = rl->__variable;
245 @@ -153,12 +298,15 @@
249 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
251 rr[x].__types[len] = ntohl(rr[x].__types[len]);
252 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
257 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
258 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
259 rr = rl->__maplower_ext.__ranges;
262 rr[x].__max = ntohl(rr[x].__max);
263 rr[x].__map = ntohl(rr[x].__map);
265 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
267 + if (rl->__ncharclasses > 0) {
268 + rl->__charclasses = (_RuneCharClass *)rl->__variable;
269 + rl->__variable = (void *)(rl->__charclasses + rl->__ncharclasses);
270 + if (rl->__variable > lastp) {
275 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
276 + for (x = 0; x < rl->__ncharclasses; ++x)
277 + rl->__charclasses[x].__mask = ntohl(rl->__charclasses[x].__mask);
278 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
281 if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) {
285 if (!rl->__mapupper_ext.__nranges)
286 rl->__mapupper_ext.__ranges = 0;
289 + data->__datasize = lastp - (void *)data;
292 +#endif /* !RUNEOFF32 */