]>
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
37 #pragma clang diagnostic push
38 #pragma clang diagnostic ignored "-Wstrict-prototypes"
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD: src/lib/libc/locale/rune.c,v 1.12 2004/07/29 06:16:19 tjr Exp $");
47 #include "xlocale_private.h"
49 #include "namespace.h"
50 #include <arpa/inet.h>
55 #endif /* !RUNEOFF32 */
60 #include <sys/types.h>
62 #include "un-namespace.h"
63 #endif /* !RUNEOFF32 */
65 #if defined(__LP64__) || defined(RUNEOFF32)
67 * Because the LC_CTYPE files were created with a 32-bit program, we need
68 * to adjust for the larger pointers in LP64 (the longs have already been
69 * replaced by 32-bit equivalents). Also, natural alignment will pad
70 * 64-bit types to 8-byte boundaries, and make structures containing
71 * 64-bit types sized to 8-byte boundaries.
78 /* whether to skip over a pointer or not (one-to-one with off64) */
92 #endif /* !RUNEOFF32 */
94 offsetof(_RuneLocale
, __sgetrune
),
95 offsetof(_RuneLocale
, __sputrune
),
96 offsetof(_RuneLocale
, __runetype_ext
),
97 offsetof(_RuneLocale
, __runetype_ext
) + offsetof(_RuneRange
, __ranges
),
98 offsetof(_RuneLocale
, __maplower_ext
),
99 offsetof(_RuneLocale
, __maplower_ext
) + offsetof(_RuneRange
, __ranges
),
100 offsetof(_RuneLocale
, __mapupper_ext
),
101 offsetof(_RuneLocale
, __mapupper_ext
) + offsetof(_RuneRange
, __ranges
),
102 offsetof(_RuneLocale
, __variable
),
103 offsetof(_RuneLocale
, __charclasses
),
106 #define NOFF (sizeof(off64) / sizeof(int))
109 * This program generates a header file (on stdout) that containes the 32-bit
110 * offsets, plus some 32-bit sizes
115 printf("#define SIZEOF32_RUNEENTRY %d\n", sizeof(_RuneEntry
));
116 printf("#define SIZEOF32_RUNELOCALE %d\n", sizeof(_RuneLocale
));
117 printf("int off32[] = {\n");
118 for(i
= 0; i
< NOFF
; i
++)
119 printf("\t%d,\n", off64
[i
]);
123 #endif /* RUNEOFF32 */
124 #else /* !__LP64__ && !RUNEOFF32 */
125 #define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
126 #endif /* __LP64__ || RUNEOFF32 */
129 struct __xlocale_st_runelocale
*
133 struct __xlocale_st_runelocale
*data
;
140 if (_fstat(fileno(fp
), &sb
) < 0)
143 if (sb
.st_size
< SIZEOF32_RUNELOCALE
) {
149 /* will adjust later */
150 if ((data
= (struct __xlocale_st_runelocale
*)malloc(sizeof(struct __xlocale_st_runelocale
))) == NULL
)
151 #else /* !__LP64__ */
152 if ((data
= (struct __xlocale_st_runelocale
*)malloc(sizeof(struct __xlocale_st_runelocale
) - sizeof(_RuneLocale
) + sb
.st_size
)) == NULL
)
153 #endif /* __LP64__ */
155 data
->__refcount
= 1;
156 data
->__free_extra
= NULL
;
159 rewind(fp
); /* Someone might have read the magic number once already */
167 rl
= &data
->_CurrentRuneLocale
;
170 if (fread(rl
, SIZEOF32_RUNELOCALE
, 1, fp
) != 1)
171 #else /* !__LP64__ */
172 if (fread(rl
, sb
.st_size
, 1, fp
) != 1)
173 #endif /* __LP64__ */
182 lastp
= (char *)rl
+ sb
.st_size
;
184 rl
->__variable
= rl
+ 1;
185 #endif /* __LP64__ */
187 if (memcmp(rl
->__magic
, _RUNE_MAGIC_A
, sizeof(rl
->__magic
))) {
194 /* shift things into the right position */
195 for (x
= NOFF
- 2; x
>= 0; x
--)
196 memmove((char *)rl
+ off64
[x
] + (skip
[x
] ? BYTES64BITS
: 0),
197 (char *)rl
+ off32
[x
] + (skip
[x
] ? BYTES32BITS
: 0),
198 off32
[x
+ 1] - off32
[x
] - (skip
[x
] ? BYTES32BITS
: 0));
199 #endif /* __LP64__ */
200 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
201 rl
->__invalid_rune
= ntohl(rl
->__invalid_rune
);
202 rl
->__variable_len
= ntohl(rl
->__variable_len
);
203 rl
->__ncharclasses
= ntohl(rl
->__ncharclasses
);
204 rl
->__runetype_ext
.__nranges
= ntohl(rl
->__runetype_ext
.__nranges
);
205 rl
->__maplower_ext
.__nranges
= ntohl(rl
->__maplower_ext
.__nranges
);
206 rl
->__mapupper_ext
.__nranges
= ntohl(rl
->__mapupper_ext
.__nranges
);
208 for (x
= 0; x
< _CACHED_RUNES
; ++x
) {
209 rl
->__runetype
[x
] = ntohl(rl
->__runetype
[x
]);
210 rl
->__maplower
[x
] = ntohl(rl
->__maplower
[x
]);
211 rl
->__mapupper
[x
] = ntohl(rl
->__mapupper
[x
]);
213 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
217 int count
= rl
->__runetype_ext
.__nranges
+ rl
->__maplower_ext
.__nranges
218 + rl
->__mapupper_ext
.__nranges
;
219 int extra
= sb
.st_size
- SIZEOF32_RUNELOCALE
- count
* SIZEOF32_RUNEENTRY
- rl
->__ncharclasses
* sizeof(_RuneCharClass
);
228 if ((data
= (struct __xlocale_st_runelocale
*)reallocf(data
, sizeof(struct __xlocale_st_runelocale
)
229 + count
* sizeof(_RuneEntry
)
230 + rl
->__ncharclasses
* sizeof(_RuneCharClass
)
233 rl
= &data
->_CurrentRuneLocale
;
234 rl
->__variable
= rl
+ 1;
235 rp
= (_RuneEntry
*)rl
->__variable
;
236 for (x
= 0; x
< count
; x
++, rp
++)
237 if (fread(rp
, SIZEOF32_RUNEENTRY
, 1, fp
) != 1) {
243 if (rl
->__ncharclasses
> 0) {
244 if (fread(rp
, sizeof(_RuneCharClass
), rl
->__ncharclasses
, fp
) != rl
->__ncharclasses
) {
250 rp
= (_RuneEntry
*)((char *)rp
+ rl
->__ncharclasses
* sizeof(_RuneCharClass
));
252 if (extra
> 0 && fread(rp
, extra
, 1, fp
) != 1) {
258 lastp
= (char *)rp
+ extra
;
260 #endif /* __LP64__ */
261 rl
->__runetype_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
262 rl
->__variable
= rl
->__runetype_ext
.__ranges
+
263 rl
->__runetype_ext
.__nranges
;
264 if (rl
->__variable
> lastp
) {
270 rl
->__maplower_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
271 rl
->__variable
= rl
->__maplower_ext
.__ranges
+
272 rl
->__maplower_ext
.__nranges
;
273 if (rl
->__variable
> lastp
) {
279 rl
->__mapupper_ext
.__ranges
= (_RuneEntry
*)rl
->__variable
;
280 rl
->__variable
= rl
->__mapupper_ext
.__ranges
+
281 rl
->__mapupper_ext
.__nranges
;
282 if (rl
->__variable
> lastp
) {
288 for (x
= 0; x
< rl
->__runetype_ext
.__nranges
; ++x
) {
289 rr
= rl
->__runetype_ext
.__ranges
;
291 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
292 rr
[x
].__min
= ntohl(rr
[x
].__min
);
293 rr
[x
].__max
= ntohl(rr
[x
].__max
);
294 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
295 if ((rr
[x
].__map
= ntohl(rr
[x
].__map
)) == 0) {
296 int len
= rr
[x
].__max
- rr
[x
].__min
+ 1;
297 rr
[x
].__types
= rl
->__variable
;
298 rl
->__variable
= rr
[x
].__types
+ len
;
299 if (rl
->__variable
> lastp
) {
304 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
306 rr
[x
].__types
[len
] = ntohl(rr
[x
].__types
[len
]);
307 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
312 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
313 for (x
= 0; x
< rl
->__maplower_ext
.__nranges
; ++x
) {
314 rr
= rl
->__maplower_ext
.__ranges
;
316 rr
[x
].__min
= ntohl(rr
[x
].__min
);
317 rr
[x
].__max
= ntohl(rr
[x
].__max
);
318 rr
[x
].__map
= ntohl(rr
[x
].__map
);
321 for (x
= 0; x
< rl
->__mapupper_ext
.__nranges
; ++x
) {
322 rr
= rl
->__mapupper_ext
.__ranges
;
324 rr
[x
].__min
= ntohl(rr
[x
].__min
);
325 rr
[x
].__max
= ntohl(rr
[x
].__max
);
326 rr
[x
].__map
= ntohl(rr
[x
].__map
);
328 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
330 if (rl
->__ncharclasses
> 0) {
331 rl
->__charclasses
= (_RuneCharClass
*)rl
->__variable
;
332 rl
->__variable
= (void *)(rl
->__charclasses
+ rl
->__ncharclasses
);
333 if (rl
->__variable
> lastp
) {
338 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
339 for (x
= 0; x
< rl
->__ncharclasses
; ++x
)
340 rl
->__charclasses
[x
].__mask
= ntohl(rl
->__charclasses
[x
].__mask
);
341 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
344 if (((char *)rl
->__variable
) + rl
->__variable_len
> (char *)lastp
) {
351 * Go out and zero pointers that should be zero.
353 if (!rl
->__variable_len
)
356 if (!rl
->__runetype_ext
.__nranges
)
357 rl
->__runetype_ext
.__ranges
= 0;
359 if (!rl
->__maplower_ext
.__nranges
)
360 rl
->__maplower_ext
.__ranges
= 0;
362 if (!rl
->__mapupper_ext
.__nranges
)
363 rl
->__mapupper_ext
.__ranges
= 0;
365 data
->__datasize
= lastp
- (void *)data
;
368 #endif /* !RUNEOFF32 */
369 #pragma clang diagnostic pop