]> git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/rune.c.patch
Libc-391.4.1.tar.gz
[apple/libc.git] / locale / FreeBSD / rune.c.patch
1 --- rune.c.orig 2004-11-25 11:38:19.000000000 -0800
2 +++ rune.c 2005-02-14 19:25:48.000000000 -0800
3 @@ -34,28 +34,96 @@
4 * SUCH DAMAGE.
5 */
6
7 +#ifndef RUNEOFF32
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 $");
13
14 +#include "xlocale_private.h"
15 +
16 #include "namespace.h"
17 #include <arpa/inet.h>
18 #include <errno.h>
19 +#endif /* !RUNEOFF32 */
20 #include <runetype.h>
21 #include <stdio.h>
22 +#ifndef RUNEOFF32
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include "un-namespace.h"
28 +#endif /* !RUNEOFF32 */
29 +
30 +#if defined(__LP64__) || defined(RUNEOFF32)
31 +/*
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.
37 + */
38 +#include <stddef.h>
39 +#ifndef RUNEOFF32
40 +#include "rune32.h"
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[] = {
45 + 1,
46 + 1,
47 + 0,
48 + 1,
49 + 0,
50 + 1,
51 + 0,
52 + 1,
53 + 1,
54 + 0
55 +};
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),
67 + sizeof(_RuneLocale)
68 +};
69 +#define NOFF (sizeof(off64) / sizeof(int))
70 +#ifdef RUNEOFF32
71 +/*
72 + * This program generates a header file (on stdout) that containes the 32-bit
73 + * offsets, plus some 32-bit sizes
74 + */
75 +main()
76 +{
77 + int i;
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]);
83 + printf("};\n");
84 + return 0;
85 +}
86 +#endif /* RUNEOFF32 */
87 +#else /* !__LP64__ && !RUNEOFF32 */
88 +#define SIZEOF32_RUNELOCALE sizeof(_RuneLocale)
89 +#endif /* __LP64__ || RUNEOFF32 */
90
91 -_RuneLocale *
92 +#ifndef RUNEOFF32
93 +struct __xlocale_st_runelocale *
94 _Read_RuneMagi(fp)
95 FILE *fp;
96 {
97 - char *data;
98 + struct __xlocale_st_runelocale *data;
99 void *lastp;
100 _RuneLocale *rl;
101 _RuneEntry *rr;
102 @@ -65,13 +133,20 @@
103 if (_fstat(fileno(fp), &sb) < 0)
104 return (NULL);
105
106 - if (sb.st_size < sizeof(_RuneLocale)) {
107 + if (sb.st_size < SIZEOF32_RUNELOCALE) {
108 errno = EFTYPE;
109 return (NULL);
110 }
111
112 - if ((data = malloc(sb.st_size)) == NULL)
113 +#ifdef __LP64__
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__ */
119 return (NULL);
120 + data->__refcount = 1;
121 + data->__free_extra = NULL;
122
123 errno = 0;
124 rewind(fp); /* Someone might have read the magic number once already */
125 @@ -82,17 +157,25 @@
126 return (NULL);
127 }
128
129 - if (fread(data, sb.st_size, 1, fp) != 1) {
130 + rl = &data->_CurrentRuneLocale;
131 +
132 +#ifdef __LP64__
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__ */
137 + {
138 saverr = errno;
139 free(data);
140 errno = saverr;
141 return (NULL);
142 }
143
144 - rl = (_RuneLocale *)data;
145 - lastp = data + sb.st_size;
146 +#ifndef __LP64__
147 + lastp = (char *)rl + sb.st_size;
148
149 rl->__variable = rl + 1;
150 +#endif /* __LP64__ */
151
152 if (memcmp(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic))) {
153 free(data);
154 @@ -100,6 +183,14 @@
155 return (NULL);
156 }
157
158 +#ifdef __LP64__
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);
169 @@ -111,7 +202,43 @@
170 rl->__maplower[x] = ntohl(rl->__maplower[x]);
171 rl->__mapupper[x] = ntohl(rl->__mapupper[x]);
172 }
173 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
174 +
175 +#ifdef __LP64__
176 + {
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;
180 + _RuneEntry *rp;
181
182 + if (extra < 0) {
183 + saverr = errno;
184 + free(data);
185 + errno = saverr;
186 + return (NULL);
187 + }
188 + if ((data = (struct __xlocale_st_runelocale *)reallocf(data, sizeof(struct __xlocale_st_runelocale) +
189 + count * sizeof(_RuneEntry) + extra)) == NULL)
190 + return (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) {
196 + saverr = errno;
197 + free(data);
198 + errno = saverr;
199 + return (NULL);
200 + }
201 + if (extra > 0 && fread(rp, extra, 1, fp) != 1) {
202 + saverr = errno;
203 + free(data);
204 + errno = saverr;
205 + return (NULL);
206 + }
207 + lastp = (char *)rp + extra;
208 + }
209 +#endif /* __LP64__ */
210 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
211 rl->__variable = rl->__runetype_ext.__ranges +
212 rl->__runetype_ext.__nranges;
213 @@ -142,8 +269,10 @@
214 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
215 rr = rl->__runetype_ext.__ranges;
216
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 @@
225 errno = EFTYPE;
226 return (NULL);
227 }
228 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
229 while (len-- > 0)
230 rr[x].__types[len] = ntohl(rr[x].__types[len]);
231 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
232 } else
233 rr[x].__types = 0;
234 }
235
236 +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
237 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
238 rr = rl->__maplower_ext.__ranges;
239
240 @@ -174,6 +306,7 @@
241 rr[x].__max = ntohl(rr[x].__max);
242 rr[x].__map = ntohl(rr[x].__map);
243 }
244 +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
245 if (((char *)rl->__variable) + rl->__variable_len > (char *)lastp) {
246 free(data);
247 errno = EFTYPE;
248 @@ -195,5 +328,7 @@
249 if (!rl->__mapupper_ext.__nranges)
250 rl->__mapupper_ext.__ranges = 0;
251
252 - return (rl);
253 + data->__datasize = lastp - (void *)data;
254 + return (data);
255 }
256 +#endif /* !RUNEOFF32 */