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