msgcat.c.patch   [plain text]


--- msgcat.c.orig	2007-02-07 01:54:34.000000000 -0800
+++ msgcat.c	2007-02-07 02:03:33.000000000 -0800
@@ -45,16 +45,22 @@
 #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"
 
@@ -87,7 +93,7 @@
 		return (loadCat(name));
 
 	if (type == NL_CAT_LOCALE)
-		lang = setlocale(LC_MESSAGES, NULL);
+		lang = (char *)querylocale(LC_MESSAGES_MASK, NULL);
 	else
 		lang = getenv("LANG");
 
@@ -210,21 +216,21 @@
 
 #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;                                \
@@ -240,32 +246,28 @@
 }
 
 static MCSetT *
-MCGetSet(cat, setId)
-	MCCatT  *cat;
-	int     setId;
+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);
 }
 
 static MCMsgT *
-MCGetMsg(set, msgId)
-	MCSetT  *set;
-	int     msgId;
+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);
 }
 
@@ -357,7 +359,7 @@
 	MCHeaderT       header;
 	MCCatT          *cat;
 	MCSetT          *set;
-	long            i;
+	int32_t         i;
 	off_t           nextSet;
 	int             saverr;
 
@@ -377,27 +379,30 @@
 	    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);
@@ -414,7 +419,7 @@
 		/* if it's invalid, skip over it (and backup 'i') */
 		if (set->invalid) {
 			--i;
-			nextSet = set->nextSet;
+			nextSet = ntohll(set->nextSet);
 			continue;
 		}
 #if 0
@@ -432,7 +437,7 @@
 		} else
 #endif
 			set->invalid = TRUE;
-		nextSet = set->nextSet;
+		nextSet = ntohll(set->nextSet);
 	}
 #if 0
 	if (cat->loadType == MCLoadAll) {
@@ -453,11 +458,11 @@
 	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;
@@ -465,13 +470,13 @@
 	}
 
 	/* 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);
@@ -479,7 +484,7 @@
 		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;
@@ -492,7 +497,7 @@
 			--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);