]> git.saurik.com Git - apple/libc.git/blobdiff - nls/FreeBSD/msgcat.c
Libc-1244.1.7.tar.gz
[apple/libc.git] / nls / FreeBSD / msgcat.c
index 0ce69e0079af29c4252fa93b9cd9445c8937d482..542368e6573ed4c0ff477e54059d3a26733a465c 100644 (file)
@@ -45,16 +45,22 @@ __FBSDID("$FreeBSD: src/lib/libc/nls/msgcat.c,v 1.49 2005/02/01 16:04:55 phantom
 #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,46 +347,57 @@ loadCat(__const char *catpath)
        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);
@@ -397,11 +414,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 +432,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 +444,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 +458,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 +471,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);