]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/rune.c.patch
Libc-763.12.tar.gz
[apple/libc.git] / locale / FreeBSD / rune.c.patch
1 Index: rune.c
2 ===================================================================
3 --- rune.c (revision 59751)
4 +++ rune.c (working copy)
5 @@ -34,28 +34,100 @@
6 * SUCH DAMAGE.
7 */
8
9 +#ifndef RUNEOFF32
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 $");
15
16 +#include "xlocale_private.h"
17 +
18 #include "namespace.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <runetype.h>
22 +#else
23 +#include "runetype.h"
24 +#endif /* !RUNEOFF32 */
25 #include <stdio.h>
26 +#ifndef RUNEOFF32
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include "un-namespace.h"
32 +#endif /* !RUNEOFF32 */
33
34 -_RuneLocale *
35 +#if defined(__LP64__) || defined(RUNEOFF32)
36 +/*
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.
42 + */
43 +#include <stddef.h>
44 +#ifndef RUNEOFF32
45 +#include "rune32.h"
46 +#define BYTES32BITS 4
47 +#define BYTES64BITS 8
48 +/* whether to skip over a pointer or not (one-to-one with off64) */
49 +int skip[] = {
50 + 1,
51 + 1,
52 + 0,
53 + 1,
54 + 0,
55 + 1,
56 + 0,
57 + 1,
58 + 1,
59 + 1,
60 + 0
61 +};
62 +#endif /* !RUNEOFF32 */
63 +int off64[] = {
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),
74 + sizeof(_RuneLocale)
75 +};
76 +#define NOFF (sizeof(off64) / sizeof(int))
77 +#ifdef RUNEOFF32
78 +/*
79 + * This program generates a header file (on stdout) that containes the 32-bit
80 + * offsets, plus some 32-bit sizes
81 + */
82 +main()
83 +{
84 + int i;
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]);
90 + printf("};\n");
91 + return 0;
92 +}
93 +#endif /* RUNEOFF32 */
94 +#else /* !__LP64__ && !RUNEOFF32 */
95 +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
96 +#endif /* __LP64__ || RUNEOFF32 */
97 +
98 +#ifndef RUNEOFF32
99 +struct __xlocale_st_runelocale *
100 _Read_RuneMagi(fp)
101 FILE *fp;
102 {
103 - char *data;
104 + struct __xlocale_st_runelocale *data;
105 void *lastp;
106 _RuneLocale *rl;
107 _RuneEntry *rr;
108 @@ -65,13 +137,20 @@
109 if (_fstat(fileno(fp), &sb) < 0)
110 return (NULL);
111
112 - if (sb.st_size < sizeof(_RuneLocale)) {
113 + if (sb.st_size < SIZEOF32_RUNELOCALE) {
114 errno = EFTYPE;
115 return (NULL);
116 }
117
118 - if ((data = malloc(sb.st_size)) == NULL)
119 +#ifdef __LP64__
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__ */
125 return (NULL);
126 + data->__refcount = 1;
127 + data->__free_extra = NULL;
128
129 errno = 0;
130 rewind(fp); /* Someone might have read the magic number once already */
131 @@ -82,26 +161,43 @@
132 return (NULL);
133 }
134
135 - if (fread(data, sb.st_size, 1, fp) != 1) {
136 + rl = &data->_CurrentRuneLocale;
137 +
138 +#ifdef __LP64__
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__ */
143 + {
144 saverr = errno;
145 free(data);
146 errno = saverr;
147 return (NULL);
148 }
149
150 - rl = (_RuneLocale *)data;
151 - lastp = data + sb.st_size;
152 +#ifndef __LP64__
153 + lastp = (char *)rl + sb.st_size;
154
155 rl->__variable = rl + 1;
156 +#endif /* __LP64__ */
157
158 - if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) {
159 + if (memcmp(rl->__magic, _RUNE_MAGIC_A, sizeof(rl->__magic))) {
160 free(data);
161 errno = EFTYPE;
162 return (NULL);
163 }
164
165 +#ifdef __LP64__
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);
179 @@ -111,7 +207,54 @@
180 rl->__maplower[x] = ntohl(rl->__maplower[x]);
181 rl->__mapupper[x] = ntohl(rl->__mapupper[x]);
182 }
183 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
184
185 +#ifdef __LP64__
186 + {
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);
190 + _RuneEntry *rp;
191 +
192 + if (extra < 0) {
193 + saverr = errno;
194 + free(data);
195 + errno = saverr;
196 + return (NULL);
197 + }
198 + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale)
199 + + count * sizeof(_RuneEntry)
200 + + rl->__ncharclasses * sizeof(_RuneCharClass)
201 + + extra)) == NULL)
202 + return (NULL);
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) {
208 + saverr = errno;
209 + free(data);
210 + errno = saverr;
211 + return (NULL);
212 + }
213 + if (rl->__ncharclasses > 0) {
214 + if (fread(rp, sizeof(_RuneCharClass), rl->__ncharclasses, fp) != rl->__ncharclasses) {
215 + saverr = errno;
216 + free(data);
217 + errno = saverr;
218 + return (NULL);
219 + }
220 + rp = (_RuneEntry *)((char *)rp + rl->__ncharclasses * sizeof(_RuneCharClass));
221 + }
222 + if (extra > 0 && fread(rp, extra, 1, fp) != 1) {
223 + saverr = errno;
224 + free(data);
225 + errno = saverr;
226 + return (NULL);
227 + }
228 + lastp = (char *)rp + extra;
229 + }
230 +#endif /* __LP64__ */
231 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
232 rl->__variable = rl->__runetype_ext.__ranges +
233 rl->__runetype_ext.__nranges;
234 @@ -142,8 +285,10 @@
235 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
236 rr = rl->__runetype_ext.__ranges;
237
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 @@
246 errno = EFTYPE;
247 return (NULL);
248 }
249 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
250 while (len-- > 0)
251 rr[x].__types[len] = ntohl(rr[x].__types[len]);
252 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
253 } else
254 rr[x].__types = 0;
255 }
256
257 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
258 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
259 rr = rl->__maplower_ext.__ranges;
260
261 @@ -174,6 +322,22 @@
262 rr[x].__max = ntohl(rr[x].__max);
263 rr[x].__map = ntohl(rr[x].__map);
264 }
265 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
266 +
267 + if (rl->__ncharclasses > 0) {
268 + rl->__charclasses = (_RuneCharClass *)rl->__variable;
269 + rl->__variable = (void *)(rl->__charclasses + rl->__ncharclasses);
270 + if (rl->__variable > lastp) {
271 + free(data);
272 + errno = EFTYPE;
273 + return (NULL);
274 + }
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 */
279 + }
280 +
281 if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) {
282 free(data);
283 errno = EFTYPE;
284 @@ -195,5 +359,7 @@
285 if (!rl->__mapupper_ext.__nranges)
286 rl->__mapupper_ext.__ranges = 0;
287
288 - return (rl);
289 + data->__datasize = lastp - (void *)data;
290 + return (data);
291 }
292 +#endif /* !RUNEOFF32 */