#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"
return (loadCat(name));
if (type == NL_CAT_LOCALE)
- lang = setlocale(LC_MESSAGES, NULL);
+ lang = (char *)querylocale(LC_MESSAGES_MASK, NULL);
else
lang = getenv("LANG");
#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; \
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);
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);
}
MCHeaderT header;
MCCatT *cat;
MCSetT *set;
- long i;
+ int32_t i;
off_t nextSet;
int saverr;
+ int fd;
if ((cat = (MCCatT *)malloc(sizeof(MCCatT))) == NULL)
return (NLERR);
- if ((cat->fp = fopen(catpath, "r")) == NULL) {
+ if ((fd = open(catpath, O_RDONLY | O_CLOEXEC)) == -1) {
saverr = errno;
free(cat);
errno = saverr;
return (NLERR);
}
- (void)_fcntl(fileno(cat->fp), F_SETFD, FD_CLOEXEC);
+
+ if ((cat->fp = fdopen(fd, "r")) == NULL) {
+ saverr = errno;
+ close(fd);
+ free(cat);
+ errno = saverr;
+ return (NLERR);
+ }
if (fread(&header, sizeof(header), 1, cat->fp) != 1 ||
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);
/* 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);
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;
}
/* 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);
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;
--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);