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
14 +#include <machine/endian.h>
15 +#include <libkern/OSByteOrder.h>
16 #include "un-namespace.h"
19 -#include "../locale/setlocale.h" /* for ENCODING_LEN */
20 +#include "setlocale.h" /* for ENCODING_LEN */
23 +#define ntohll(x) OSSwapBigToHostInt64(x)
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"
28 @@ -85,7 +91,7 @@ catopen(__const char *name, int type)
29 return (loadCat(name));
31 if (type == NL_CAT_LOCALE)
32 - lang = setlocale(LC_MESSAGES, NULL);
33 + lang = (char *)querylocale(LC_MESSAGES_MASK, NULL);
35 lang = getenv("LANG");
37 @@ -208,21 +214,21 @@ catopen(__const char *name, int type)
39 #define LOOKUP(PARENT, CHILD, ID, NUM, SET) { \
41 - if (ID - 1 < PARENT->NUM) { \
42 + if (ID - 1 < NUM) { \
48 cur = (hi - lo) / 2; \
51 CHILD = PARENT->SET + cur; \
52 - if (CHILD->ID == ID) \
53 + if (ntohl(CHILD->ID) == ID) \
55 - if (CHILD->ID < ID) { \
56 + if (ntohl(CHILD->ID) < ID) { \
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; \
65 @@ -241,11 +247,11 @@ static MCSetT *
66 MCGetSet(MCCatT *cat, int setId)
69 - long lo, hi, cur, dir;
70 + int32_t lo, hi, cur, dir;
72 if (cat == NULL || setId <= 0)
74 - LOOKUP(cat, set, setId, numSets, sets);
75 + LOOKUP(cat, set, setId, cat->numSets, sets);
76 if (set->invalid && loadSet(cat, set) <= 0)
79 @@ -255,11 +261,11 @@ static MCMsgT *
80 MCGetMsg(MCSetT *set, int msgId)
83 - long lo, hi, cur, dir;
84 + int32_t lo, hi, cur, dir;
86 if (set == NULL || set->invalid || msgId <= 0)
88 - LOOKUP(set, msg, msgId, numMsgs, u.msgs);
89 + LOOKUP(set, msg, msgId, ntohl(set->numMsgs), u.msgs);
93 @@ -341,7 +347,7 @@ loadCat(__const char *catpath)
102 @@ -360,27 +366,30 @@ loadCat(__const char *catpath)
103 strncmp(header.magic, MCMagic, MCMagicLen) != 0)
106 - if (header.majorVer != MCMajorVer) {
107 + if (ntohl(header.majorVer) != MCMajorVer) {
108 (void)fclose(cat->fp);
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);
115 + (void)fprintf(stderr, "%s: %s is version %d, we need %d.\n", _errowner, catpath, (int)ntohl(header.majorVer), MCMajorVer);
119 - if (header.numSets <= 0) {
120 + if (ntohl(header.numSets) <= 0) {
121 (void)fclose(cat->fp);
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));
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)) ==
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') */
146 - nextSet = set->nextSet;
147 + nextSet = ntohll(set->nextSet);
151 - nextSet = set->nextSet;
152 + nextSet = ntohll(set->nextSet);
155 return ((nl_catd) cat);
156 @@ -415,11 +424,11 @@ loadSet(MCCatT *cat, MCSetT *set)
160 - if (fseeko(cat->fp, set->data.off, SEEK_SET) == -1)
161 + if (fseeko(cat->fp, ntohll(set->data.off), SEEK_SET) == -1)
163 - if ((set->data.str = malloc(set->dataLen)) == NULL)
164 + if ((set->data.str = malloc(ntohl(set->dataLen))) == NULL)
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) {
171 @@ -427,13 +436,13 @@ loadSet(MCCatT *cat, MCSetT *set)
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) {
182 - if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * set->numMsgs)) ==
183 + if ((set->u.msgs = (MCMsgT *)malloc(sizeof(MCMsgT) * ntohl(set->numMsgs))) ==
187 @@ -441,7 +450,7 @@ loadSet(MCCatT *cat, MCSetT *set)
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) {
196 @@ -454,7 +463,7 @@ loadSet(MCCatT *cat, MCSetT *set)
200 - msg->msg.str = (char *)(set->data.str + msg->msg.off);
201 + msg->msg.str = (char *)(set->data.str + ntohll(msg->msg.off));
203 set->invalid = FALSE;