]>
git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/rune.c
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: src/lib/libc/locale/rune.c,v 1.12 2004/07/29 06:16:19 tjr Exp $");
44 #include "xlocale_private.h"
46 #include "namespace.h"
47 #include <arpa/inet.h>
52 #endif /* !RUNEOFF32 */
57 #include <sys/types.h>
59 #include "un-namespace.h"
60 #endif /* !RUNEOFF32 */
62 #if defined(__LP64__) || defined(RUNEOFF32)
64 * Because the LC_CTYPE files were created with a 32-bit program, we need
65 * to adjust for the larger pointers in LP64 (the longs have already been
66 * replaced by 32-bit equivalents). Also, natural alignment will pad
67 * 64-bit types to 8-byte boundaries, and make structures containing
68 * 64-bit types sized to 8-byte boundaries.
75 /* whether to skip over a pointer or not (one-to-one with off64) */
89 #endif /* !RUNEOFF32 */
91 offsetof(_RuneLocale
, __sgetrune
),
92 offsetof(_RuneLocale
, __sputrune
),
93 offsetof(_RuneLocale
, __runetype_ext
),
94 offsetof(_RuneLocale
, __runetype_ext
) + offsetof(_RuneRange
, __ranges
),
95 offsetof(_RuneLocale
, __maplower_ext
),
96 offsetof(_RuneLocale
, __maplower_ext
) + offsetof(_RuneRange
, __ranges
),
97 offsetof(_RuneLocale
, __mapupper_ext
),
98 offsetof(_RuneLocale
, __mapupper_ext
) + offsetof(_RuneRange
, __ranges
),
99 offsetof(_RuneLocale
, __variable
),
100 offsetof(_RuneLocale
, __charclasses
),
103 #define NOFF (sizeof(off64) / sizeof(int))
106 * This program generates a header file (on stdout) that containes the 32-bit
107 * offsets, plus some 32-bit sizes
112 printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry
));
113 printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale
));
114 printf("int off32[] = {\n");
115 for(i
= 0; i
< NOFF
; i
++)
116 printf("\t%d,\n", off64
[i
]);
120 #endif /* RUNEOFF32 */
121 #else /* !__LP64__ && !RUNEOFF32 */
122 #define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
123 #endif /* __LP64__ || RUNEOFF32 */
126 struct __xlocale_st_runelocale
*
130 struct __xlocale_st_runelocale
*data
;
137 if (_fstat(fileno(fp
), &sb
) < 0)
140 if (sb
.st_size
< SIZEOF32_RUNELOCALE
) {
146 /* will adjust later */
147 if ((data
= (struct __xlocale_st_runelocale
*)malloc(sizeof(struct __xlocale_st_runelocale
))) == NULL
)
148 #else /* !__LP64__ */
149 if ((data
= (struct __xlocale_st_runelocale
*)malloc(sizeof(struct __xlocale_st_runelocale
) - sizeof(_RuneLocale
) + sb
.st_size
)) == NULL
)
150 #endif /* __LP64__ */
152 data
->__refcount
= 1;
153 data
->__free_extra
= NULL
;
156 rewind(fp
); /* Someone might have read the magic number once already */
164 rl
= &data
->_CurrentRuneLocale
;
167 if (fread(rl
, SIZEOF32_RUNELOCALE
, 1, fp
) != 1)
168 #else /* !__LP64__ */
169 if (fread(rl
, sb
.st_size
, 1, fp
) != 1)
170 #endif /* __LP64__ */
179 lastp
= (char *)rl
+ sb
.st_size
;
181 rl
->__variable
= rl
+ 1;
182 #endif /* __LP64__ */
184 if (memcmp(rl
->__magic
, _RUNE_MAGIC_A
, sizeof(rl
->__magic
))) {
191 /* shift things into the right position */
192 for (x
= NOFF
- 2; x
>= 0; x
--)
193 memmove((char *)rl
+ off64
[x
] + (skip
[x
] ? BYTES64BITS
: 0),
194 (char *)rl
+ off32
[x
] + (skip
[x
] ? BYTES32BITS
: 0),
195 off32
[x
+ 1] - off32
[x
] - (skip
[x
] ? BYTES32BITS
: 0));
196 #endif /* __LP64__ */
197 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
198 rl
->__invalid_rune
= ntohl(rl
->__invalid_rune
);
199 rl
->__variable_len
= ntohl(rl
->__variable_len
);
200 rl
->__ncharclasses
= ntohl(rl
->__ncharclasses
);
201 rl
->__runetype_ext
.__nranges
= ntohl(rl
->__runetype_ext
.__nranges
);
202 rl
->__maplower_ext
.__nranges
= ntohl(rl
->__maplower_ext
.__nranges
);
203 rl
->__mapupper_ext
.__nranges
= ntohl(rl
->__mapupper_ext
.__nranges
);
205 for (x
= 0; x
< _CACHED_RUNES
; ++x
) {
206 rl
->__runetype
[x
] = ntohl(rl
->__runetype
[x
]);
207 rl
->__maplower
[x
] = ntohl(rl
->__maplower
[x
]);
208 rl
->__mapupper
[x
] = ntohl(rl
->__mapupper
[x
]);
210 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
214 int count
= rl
->__runetype_ext
.__nranges
+ rl
->__maplower_ext
.__nranges
215 + rl
->__mapupper_ext
.__nranges
;
216 int extra
= sb
.st_size
- SIZEOF32_RUNELOCALE
- count
* SIZEOF32_RUNEENTRY
- rl
->__ncharclasses
* sizeof(_RuneCharClass
);
225 if ((data
= (struct __xlocale_st_runelocale
*)reallocf(data
, sizeof(struct __xlocale_st_runelocale
)
226 + count
* sizeof(_RuneEntry
)
227 + rl
->__ncharclasses
* sizeof(_RuneCharClass
)
230 rl
= &data
->_CurrentRuneLocale
;
231 rl
->__variable
= rl
+ 1;
232 rp
= (_RuneEntry
*)rl
->__variable
;
233 for (x
= 0; x
< count
; x
++, rp
++)
234 if (fread(rp
, SIZEOF32_RUNEENTRY
, 1, fp
) != 1) {
240 if (rl
->__ncharclasses
> 0) {
241 if (fread(rp
, sizeof(_RuneCharClass
), rl
->__ncharclasses
, fp
) != rl
->__ncharclasses
) {
247 rp
= (_RuneEntry
*)((char *)rp
+ rl
->__ncharclasses
* sizeof(_RuneCharClass
));
249 if (extra
> 0 && fread(rp
, extra
, 1, fp
) != 1) {
255 lastp
= (char *)rp
+ extra
;
257 #endif /* __LP64__ */
258 rl
->__runetype_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
259 rl
->__variable
= rl
->__runetype_ext
.__ranges
+
260 rl
->__runetype_ext
.__nranges
;
261 if (rl
->__variable
> lastp
) {
267 rl
->__maplower_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
268 rl
->__variable
= rl
->__maplower_ext
.__ranges
+
269 rl
->__maplower_ext
.__nranges
;
270 if (rl
->__variable
> lastp
) {
276 rl
->__mapupper_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
277 rl
->__variable
= rl
->__mapupper_ext
.__ranges
+
278 rl
->__mapupper_ext
.__nranges
;
279 if (rl
->__variable
> lastp
) {
285 for (x
= 0; x
< rl
->__runetype_ext
.__nranges
; ++x
) {
286 rr
= rl
->__runetype_ext
.__ranges
;
288 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
289 rr
[x
].__min
= ntohl(rr
[x
].__min
);
290 rr
[x
].__max
= ntohl(rr
[x
].__max
);
291 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
292 if ((rr
[x
].__map
= ntohl(rr
[x
].__map
)) == 0) {
293 int len
= rr
[x
].__max
- rr
[x
].__min
+ 1;
294 rr
[x
].__types
= rl
->__variable
;
295 rl
->__variable
= rr
[x
].__types
+ len
;
296 if (rl
->__variable
> lastp
) {
301 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
303 rr
[x
].__types
[len
] = ntohl(rr
[x
].__types
[len
]);
304 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
309 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
310 for (x
= 0; x
< rl
->__maplower_ext
.__nranges
; ++x
) {
311 rr
= rl
->__maplower_ext
.__ranges
;
313 rr
[x
].__min
= ntohl(rr
[x
].__min
);
314 rr
[x
].__max
= ntohl(rr
[x
].__max
);
315 rr
[x
].__map
= ntohl(rr
[x
].__map
);
318 for (x
= 0; x
< rl
->__mapupper_ext
.__nranges
; ++x
) {
319 rr
= rl
->__mapupper_ext
.__ranges
;
321 rr
[x
].__min
= ntohl(rr
[x
].__min
);
322 rr
[x
].__max
= ntohl(rr
[x
].__max
);
323 rr
[x
].__map
= ntohl(rr
[x
].__map
);
325 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
327 if (rl
->__ncharclasses
> 0) {
328 rl
->__charclasses
= (_RuneCharClass
*)rl
->__variable
;
329 rl
->__variable
= (void *)(rl
->__charclasses
+ rl
->__ncharclasses
);
330 if (rl
->__variable
> lastp
) {
335 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
336 for (x
= 0; x
< rl
->__ncharclasses
; ++x
)
337 rl
->__charclasses
[x
].__mask
= ntohl(rl
->__charclasses
[x
].__mask
);
338 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
341 if (((char *)rl
->__variable
) + rl
->__variable_len
> (char *)lastp
) {
348 * Go out and zero pointers that should be zero.
350 if (!rl
->__variable_len
)
353 if (!rl
->__runetype_ext
.__nranges
)
354 rl
->__runetype_ext
.__ranges
= 0;
356 if (!rl
->__maplower_ext
.__nranges
)
357 rl
->__maplower_ext
.__ranges
= 0;
359 if (!rl
->__mapupper_ext
.__nranges
)
360 rl
->__mapupper_ext
.__ranges
= 0;
362 data
->__datasize
= lastp
- (void *)data
;
365 #endif /* !RUNEOFF32 */