]>
Commit | Line | Data |
---|---|---|
1f2f436a A |
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 | |
3d9156a7 A |
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> | |
31185420 A |
12 | #include <string.h> |
13 | #include <unistd.h> | |
14 | +#include <machine/endian.h> | |
15 | +#include <libkern/OSByteOrder.h> | |
9385eb3d A |
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 */ | |
31185420 | 21 | + |
224c7076 | 22 | +#ifndef ntohll |
31185420 A |
23 | +#define ntohll(x) OSSwapBigToHostInt64(x) |
24 | +#endif | |
9385eb3d A |
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 | ||
1f2f436a | 28 | @@ -85,7 +91,7 @@ catopen(__const char *name, int type) |
3d9156a7 A |
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 | ||
1f2f436a | 37 | @@ -208,21 +214,21 @@ catopen(__const char *name, int type) |
31185420 A |
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) \ | |
224c7076 | 53 | + if (ntohl(CHILD->ID) == ID) \ |
31185420 A |
54 | break; \ |
55 | - if (CHILD->ID < ID) { \ | |
224c7076 | 56 | + if (ntohl(CHILD->ID) < ID) { \ |
31185420 A |
57 | lo = cur + 1; \ |
58 | - if (hi > cur + (ID - CHILD->ID) + 1) \ | |
59 | - hi = cur + (ID - CHILD->ID) + 1; \ | |
224c7076 A |
60 | + if (hi > cur + (ID - ntohl(CHILD->ID)) + 1) \ |
61 | + hi = cur + (ID - ntohl(CHILD->ID)) + 1; \ | |
31185420 A |
62 | dir = 1; \ |
63 | } else { \ | |
64 | hi = cur; \ | |
1f2f436a A |
65 | @@ -241,11 +247,11 @@ static MCSetT * |
66 | MCGetSet(MCCatT *cat, int setId) | |
31185420 A |
67 | { |
68 | MCSetT *set; | |
224c7076 A |
69 | - long lo, hi, cur, dir; |
70 | + int32_t lo, hi, cur, dir; | |
31185420 A |
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); | |
1f2f436a A |
79 | @@ -255,11 +261,11 @@ static MCMsgT * |
80 | MCGetMsg(MCSetT *set, int msgId) | |
31185420 A |
81 | { |
82 | MCMsgT *msg; | |
224c7076 A |
83 | - long lo, hi, cur, dir; |
84 | + int32_t lo, hi, cur, dir; | |
31185420 A |
85 | |
86 | if (set == NULL || set->invalid || msgId <= 0) | |
87 | return (NULL); | |
88 | - LOOKUP(set, msg, msgId, numMsgs, u.msgs); | |
224c7076 | 89 | + LOOKUP(set, msg, msgId, ntohl(set->numMsgs), u.msgs); |
31185420 A |
90 | return (msg); |
91 | } | |
92 | ||
1f2f436a | 93 | @@ -341,7 +347,7 @@ loadCat(__const char *catpath) |
224c7076 A |
94 | MCHeaderT header; |
95 | MCCatT *cat; | |
96 | MCSetT *set; | |
97 | - long i; | |
98 | + int32_t i; | |
99 | off_t nextSet; | |
100 | int saverr; | |
101 | ||
1f2f436a | 102 | @@ -360,27 +366,30 @@ loadCat(__const char *catpath) |
31185420 A |
103 | strncmp(header.magic, MCMagic, MCMagicLen) != 0) |
104 | CORRUPT(); | |
105 | ||
106 | - if (header.majorVer != MCMajorVer) { | |
224c7076 | 107 | + if (ntohl(header.majorVer) != MCMajorVer) { |
31185420 A |
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); | |
224c7076 | 112 | + if (OSSwapInt32(ntohl(header.majorVer)) == MCMajorVer) { |
31185420 A |
113 | + (void)fprintf(stderr, "%s: %s is the wrong byte ordering.\n", _errowner, catpath); |
114 | + } else { | |
224c7076 | 115 | + (void)fprintf(stderr, "%s: %s is version %d, we need %d.\n", _errowner, catpath, (int)ntohl(header.majorVer), MCMajorVer); |
31185420 A |
116 | + } |
117 | NLRETERR(EFTYPE); | |
118 | } | |
119 | - if (header.numSets <= 0) { | |
224c7076 | 120 | + if (ntohl(header.numSets) <= 0) { |
31185420 A |
121 | (void)fclose(cat->fp); |
122 | free(cat); | |
224c7076 | 123 | - (void)fprintf(stderr, "%s: %s has %ld sets!\n", |
31185420 | 124 | - _errowner, catpath, header.numSets); |
224c7076 A |
125 | + (void)fprintf(stderr, "%s: %s has %d sets!\n", |
126 | + _errowner, catpath, (int)ntohl(header.numSets)); | |
31185420 A |
127 | NLRETERR(EFTYPE); |
128 | } | |
129 | ||
130 | - cat->numSets = header.numSets; | |
131 | - if ((cat->sets = (MCSetT *)malloc(sizeof(MCSetT) * header.numSets)) == | |
224c7076 | 132 | + cat->numSets = ntohl(header.numSets); |
31185420 A |
133 | + if ((cat->sets = (MCSetT *)malloc(sizeof(MCSetT) * cat->numSets)) == |
134 | NULL) | |
135 | NOSPACE(); | |
136 | ||
137 | - nextSet = header.firstSet; | |
224c7076 | 138 | + nextSet = ntohll(header.firstSet); |
31185420 A |
139 | for (i = 0; i < cat->numSets; ++i) { |
140 | if (fseeko(cat->fp, nextSet, SEEK_SET) == -1) { | |
141 | __nls_free_resources(cat, i); | |
1f2f436a | 142 | @@ -397,11 +406,11 @@ loadCat(__const char *catpath) |
31185420 A |
143 | /* if it's invalid, skip over it (and backup 'i') */ |
144 | if (set->invalid) { | |
145 | --i; | |
146 | - nextSet = set->nextSet; | |
224c7076 | 147 | + nextSet = ntohll(set->nextSet); |
31185420 A |
148 | continue; |
149 | } | |
1f2f436a | 150 | set->invalid = TRUE; |
31185420 | 151 | - nextSet = set->nextSet; |
224c7076 | 152 | + nextSet = ntohll(set->nextSet); |
31185420 | 153 | } |
1f2f436a A |
154 | |
155 | return ((nl_catd) cat); | |
156 | @@ -415,11 +424,11 @@ loadSet(MCCatT *cat, MCSetT *set) | |
31185420 A |
157 | int saverr; |
158 | ||
159 | /* Get the data */ | |
160 | - if (fseeko(cat->fp, set->data.off, SEEK_SET) == -1) | |
224c7076 | 161 | + if (fseeko(cat->fp, ntohll(set->data.off), SEEK_SET) == -1) |
31185420 A |
162 | return (0); |
163 | - if ((set->data.str = malloc(set->dataLen)) == NULL) | |
224c7076 | 164 | + if ((set->data.str = malloc(ntohl(set->dataLen))) == NULL) |
31185420 A |
165 | return (-1); |
166 | - if (fread(set->data.str, set->dataLen, 1, cat->fp) != 1) { | |
224c7076 | 167 | + if (fread(set->data.str, ntohl(set->dataLen), 1, cat->fp) != 1) { |
31185420 A |
168 | saverr = errno; |
169 | free(set->data.str); | |
170 | errno = saverr; | |
1f2f436a | 171 | @@ -427,13 +436,13 @@ loadSet(MCCatT *cat, MCSetT *set) |
31185420 A |
172 | } |
173 | ||
174 | /* Get the messages */ | |
175 | - if (fseeko(cat->fp, set->u.firstMsg, SEEK_SET) == -1) { | |
224c7076 | 176 | + if (fseeko(cat->fp, ntohll(set->u.firstMsg), SEEK_SET) == -1) { |
31185420 A |
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)) == | |
224c7076 | 183 | + if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * ntohl(set->numMsgs))) == |
31185420 A |
184 | NULL) { |
185 | saverr = errno; | |
186 | free(set->data.str); | |
1f2f436a | 187 | @@ -441,7 +450,7 @@ loadSet(MCCatT *cat, MCSetT *set) |
31185420 A |
188 | return (-1); |
189 | } | |
190 | ||
191 | - for (i = 0; i < set->numMsgs; ++i) { | |
224c7076 | 192 | + for (i = 0; i < ntohl(set->numMsgs); ++i) { |
31185420 A |
193 | msg = set->u.msgs + i; |
194 | if (fread(msg, sizeof(*msg), 1, cat->fp) != 1) { | |
195 | saverr = errno; | |
1f2f436a | 196 | @@ -454,7 +463,7 @@ loadSet(MCCatT *cat, MCSetT *set) |
31185420 A |
197 | --i; |
198 | continue; | |
199 | } | |
200 | - msg->msg.str = (char *)(set->data.str + msg->msg.off); | |
224c7076 | 201 | + msg->msg.str = (char *)(set->data.str + ntohll(msg->msg.off)); |
31185420 A |
202 | } |
203 | set->invalid = FALSE; | |
204 | return (1); |