]>
Commit | Line | Data |
---|---|---|
1 | --- msgcat.c.orig 2009-12-05 13:47:14.000000000 -0800 | |
2 | +++ msgcat.c 2009-12-05 13:49:56.000000000 -0800 | |
3 | @@ -45,16 +45,22 @@ __FBSDID("$FreeBSD: src/lib/libc/nls/msg | |
4 | #include <errno.h> | |
5 | #include <fcntl.h> | |
6 | #include <limits.h> | |
7 | -#include <locale.h> | |
8 | +#include <xlocale.h> | |
9 | #include <nl_types.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <unistd.h> | |
14 | +#include <machine/endian.h> | |
15 | +#include <libkern/OSByteOrder.h> | |
16 | #include "un-namespace.h" | |
17 | ||
18 | #include "msgcat.h" | |
19 | -#include "../locale/setlocale.h" /* for ENCODING_LEN */ | |
20 | +#include "setlocale.h" /* for ENCODING_LEN */ | |
21 | + | |
22 | +#ifndef ntohll | |
23 | +#define ntohll(x) OSSwapBigToHostInt64(x) | |
24 | +#endif | |
25 | ||
26 | #define _DEFAULT_NLS_PATH "/usr/share/nls/%L/%N.cat:/usr/share/nls/%N/%L:/usr/local/share/nls/%L/%N.cat:/usr/local/share/nls/%N/%L" | |
27 | ||
28 | @@ -85,7 +91,7 @@ catopen(__const char *name, int type) | |
29 | return (loadCat(name)); | |
30 | ||
31 | if (type == NL_CAT_LOCALE) | |
32 | - lang = setlocale(LC_MESSAGES, NULL); | |
33 | + lang = (char *)querylocale(LC_MESSAGES_MASK, NULL); | |
34 | else | |
35 | lang = getenv("LANG"); | |
36 | ||
37 | @@ -208,21 +214,21 @@ catopen(__const char *name, int type) | |
38 | ||
39 | #define LOOKUP(PARENT, CHILD, ID, NUM, SET) { \ | |
40 | lo = 0; \ | |
41 | - if (ID - 1 < PARENT->NUM) { \ | |
42 | + if (ID - 1 < NUM) { \ | |
43 | cur = ID - 1; \ | |
44 | hi = ID; \ | |
45 | } else { \ | |
46 | - hi = PARENT->NUM; \ | |
47 | + hi = NUM; \ | |
48 | cur = (hi - lo) / 2; \ | |
49 | } \ | |
50 | while (TRUE) { \ | |
51 | CHILD = PARENT->SET + cur; \ | |
52 | - if (CHILD->ID == ID) \ | |
53 | + if (ntohl(CHILD->ID) == ID) \ | |
54 | break; \ | |
55 | - if (CHILD->ID < ID) { \ | |
56 | + if (ntohl(CHILD->ID) < ID) { \ | |
57 | lo = cur + 1; \ | |
58 | - if (hi > cur + (ID - CHILD->ID) + 1) \ | |
59 | - hi = cur + (ID - CHILD->ID) + 1; \ | |
60 | + if (hi > cur + (ID - ntohl(CHILD->ID)) + 1) \ | |
61 | + hi = cur + (ID - ntohl(CHILD->ID)) + 1; \ | |
62 | dir = 1; \ | |
63 | } else { \ | |
64 | hi = cur; \ | |
65 | @@ -241,11 +247,11 @@ static MCSetT * | |
66 | MCGetSet(MCCatT *cat, int setId) | |
67 | { | |
68 | MCSetT *set; | |
69 | - long lo, hi, cur, dir; | |
70 | + int32_t lo, hi, cur, dir; | |
71 | ||
72 | if (cat == NULL || setId <= 0) | |
73 | return (NULL); | |
74 | - LOOKUP(cat, set, setId, numSets, sets); | |
75 | + LOOKUP(cat, set, setId, cat->numSets, sets); | |
76 | if (set->invalid && loadSet(cat, set) <= 0) | |
77 | return (NULL); | |
78 | return (set); | |
79 | @@ -255,11 +261,11 @@ static MCMsgT * | |
80 | MCGetMsg(MCSetT *set, int msgId) | |
81 | { | |
82 | MCMsgT *msg; | |
83 | - long lo, hi, cur, dir; | |
84 | + int32_t lo, hi, cur, dir; | |
85 | ||
86 | if (set == NULL || set->invalid || msgId <= 0) | |
87 | return (NULL); | |
88 | - LOOKUP(set, msg, msgId, numMsgs, u.msgs); | |
89 | + LOOKUP(set, msg, msgId, ntohl(set->numMsgs), u.msgs); | |
90 | return (msg); | |
91 | } | |
92 | ||
93 | @@ -341,7 +347,7 @@ loadCat(__const char *catpath) | |
94 | MCHeaderT header; | |
95 | MCCatT *cat; | |
96 | MCSetT *set; | |
97 | - long i; | |
98 | + int32_t i; | |
99 | off_t nextSet; | |
100 | int saverr; | |
101 | ||
102 | @@ -360,27 +366,30 @@ loadCat(__const char *catpath) | |
103 | strncmp(header.magic, MCMagic, MCMagicLen) != 0) | |
104 | CORRUPT(); | |
105 | ||
106 | - if (header.majorVer != MCMajorVer) { | |
107 | + if (ntohl(header.majorVer) != MCMajorVer) { | |
108 | (void)fclose(cat->fp); | |
109 | free(cat); | |
110 | - (void)fprintf(stderr, "%s: %s is version %ld, we need %ld.\n", | |
111 | - _errowner, catpath, header.majorVer, MCMajorVer); | |
112 | + if (OSSwapInt32(ntohl(header.majorVer)) == MCMajorVer) { | |
113 | + (void)fprintf(stderr, "%s: %s is the wrong byte ordering.\n", _errowner, catpath); | |
114 | + } else { | |
115 | + (void)fprintf(stderr, "%s: %s is version %d, we need %d.\n", _errowner, catpath, (int)ntohl(header.majorVer), MCMajorVer); | |
116 | + } | |
117 | NLRETERR(EFTYPE); | |
118 | } | |
119 | - if (header.numSets <= 0) { | |
120 | + if (ntohl(header.numSets) <= 0) { | |
121 | (void)fclose(cat->fp); | |
122 | free(cat); | |
123 | - (void)fprintf(stderr, "%s: %s has %ld sets!\n", | |
124 | - _errowner, catpath, header.numSets); | |
125 | + (void)fprintf(stderr, "%s: %s has %d sets!\n", | |
126 | + _errowner, catpath, (int)ntohl(header.numSets)); | |
127 | NLRETERR(EFTYPE); | |
128 | } | |
129 | ||
130 | - cat->numSets = header.numSets; | |
131 | - if ((cat->sets = (MCSetT *)malloc(sizeof(MCSetT) * header.numSets)) == | |
132 | + cat->numSets = ntohl(header.numSets); | |
133 | + if ((cat->sets = (MCSetT *)malloc(sizeof(MCSetT) * cat->numSets)) == | |
134 | NULL) | |
135 | NOSPACE(); | |
136 | ||
137 | - nextSet = header.firstSet; | |
138 | + nextSet = ntohll(header.firstSet); | |
139 | for (i = 0; i < cat->numSets; ++i) { | |
140 | if (fseeko(cat->fp, nextSet, SEEK_SET) == -1) { | |
141 | __nls_free_resources(cat, i); | |
142 | @@ -397,11 +406,11 @@ loadCat(__const char *catpath) | |
143 | /* if it's invalid, skip over it (and backup 'i') */ | |
144 | if (set->invalid) { | |
145 | --i; | |
146 | - nextSet = set->nextSet; | |
147 | + nextSet = ntohll(set->nextSet); | |
148 | continue; | |
149 | } | |
150 | set->invalid = TRUE; | |
151 | - nextSet = set->nextSet; | |
152 | + nextSet = ntohll(set->nextSet); | |
153 | } | |
154 | ||
155 | return ((nl_catd) cat); | |
156 | @@ -415,11 +424,11 @@ loadSet(MCCatT *cat, MCSetT *set) | |
157 | int saverr; | |
158 | ||
159 | /* Get the data */ | |
160 | - if (fseeko(cat->fp, set->data.off, SEEK_SET) == -1) | |
161 | + if (fseeko(cat->fp, ntohll(set->data.off), SEEK_SET) == -1) | |
162 | return (0); | |
163 | - if ((set->data.str = malloc(set->dataLen)) == NULL) | |
164 | + if ((set->data.str = malloc(ntohl(set->dataLen))) == NULL) | |
165 | return (-1); | |
166 | - if (fread(set->data.str, set->dataLen, 1, cat->fp) != 1) { | |
167 | + if (fread(set->data.str, ntohl(set->dataLen), 1, cat->fp) != 1) { | |
168 | saverr = errno; | |
169 | free(set->data.str); | |
170 | errno = saverr; | |
171 | @@ -427,13 +436,13 @@ loadSet(MCCatT *cat, MCSetT *set) | |
172 | } | |
173 | ||
174 | /* Get the messages */ | |
175 | - if (fseeko(cat->fp, set->u.firstMsg, SEEK_SET) == -1) { | |
176 | + if (fseeko(cat->fp, ntohll(set->u.firstMsg), SEEK_SET) == -1) { | |
177 | saverr = errno; | |
178 | free(set->data.str); | |
179 | errno = saverr; | |
180 | return (0); | |
181 | } | |
182 | - if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * set->numMsgs)) == | |
183 | + if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * ntohl(set->numMsgs))) == | |
184 | NULL) { | |
185 | saverr = errno; | |
186 | free(set->data.str); | |
187 | @@ -441,7 +450,7 @@ loadSet(MCCatT *cat, MCSetT *set) | |
188 | return (-1); | |
189 | } | |
190 | ||
191 | - for (i = 0; i < set->numMsgs; ++i) { | |
192 | + for (i = 0; i < ntohl(set->numMsgs); ++i) { | |
193 | msg = set->u.msgs + i; | |
194 | if (fread(msg, sizeof(*msg), 1, cat->fp) != 1) { | |
195 | saverr = errno; | |
196 | @@ -454,7 +463,7 @@ loadSet(MCCatT *cat, MCSetT *set) | |
197 | --i; | |
198 | continue; | |
199 | } | |
200 | - msg->msg.str = (char *)(set->data.str + msg->msg.off); | |
201 | + msg->msg.str = (char *)(set->data.str + ntohll(msg->msg.off)); | |
202 | } | |
203 | set->invalid = FALSE; | |
204 | return (1); |