Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>
   1 /*

   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 1993
   4  *      The Regents of the University of California.  All rights reserved.
   5  *
   6  * This code is derived from software contributed to Berkeley by
   7  * Paul Borman at Krystal Technologies.
   8  *
   9  * Redistribution and use in source and binary forms, with or without
  10  * modification, are permitted provided that the following conditions
  11  * are met:
  12  * 1. Redistributions of source code must retain the above copyright
  13  *    notice, this list of conditions and the following disclaimer.
  14  * 2. Redistributions in binary form must reproduce the above copyright
  15  *    notice, this list of conditions and the following disclaimer in the
  16  *    documentation and/or other materials provided with the distribution.
  17  * 4. Neither the name of the University nor the names of its contributors
  18  *    may be used to endorse or promote products derived from this software
  19  *    without specific prior written permission.
  20  *
  21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31  * SUCH DAMAGE.
  32  */
  33 
  34 #include "lint.h"
  35 #include "file64.h"
  36 #include <errno.h>
  37 #include <stdio.h>
  38 #include <string.h>
  39 #include <stdlib.h>
  40 #include <sys/types.h>
  41 #include <sys/stat.h>



  42 
  43 #include "runetype.h"
  44 #include "runefile.h"
  45 
  46 _RuneLocale *_Read_RuneMagi(FILE *);
  47 
  48 _RuneLocale *
  49 _Read_RuneMagi(FILE *fp)
  50 {
  51         char *fdata, *data;
  52         void *lastp;
  53         _FileRuneLocale *frl;
  54         _RuneLocale *rl;
  55         _FileRuneEntry *frr;
  56         _RuneEntry *rr;
  57         struct stat sb;
  58         int x, saverr;
  59         void *variable;
  60         _FileRuneEntry *runetype_ext_ranges;
  61         _FileRuneEntry *maplower_ext_ranges;
  62         _FileRuneEntry *mapupper_ext_ranges;
  63         int runetype_ext_len = 0;

  64 
  65         if (fstat(fileno(fp), &sb) < 0)

  66                 return (NULL);

  67 
  68         if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {

  69                 errno = EINVAL;
  70                 return (NULL);
  71         }
  72 
  73         if ((fdata = malloc(sb.st_size)) == NULL)


  74                 return (NULL);
  75 
  76         errno = 0;
  77         rewind(fp); /* Someone might have read the magic number once already */
  78         if (errno) {
  79                 saverr = errno;
  80                 free(fdata);
  81                 errno = saverr;
  82                 return (NULL);
  83         }
  84 
  85         if (fread(fdata, sb.st_size, 1, fp) != 1) {
  86                 saverr = errno;
  87                 free(fdata);
  88                 errno = saverr;

  89                 return (NULL);
  90         }
  91 
  92         frl = (_FileRuneLocale *)(void *)fdata;
  93         lastp = fdata + sb.st_size;
  94 
  95         variable = frl + 1;
  96 
  97         if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
  98                 free(fdata);
  99                 errno = EINVAL;
 100                 return (NULL);
 101         }
 102 
 103         runetype_ext_ranges = (_FileRuneEntry *)variable;
 104         variable = runetype_ext_ranges + frl->runetype_ext_nranges;
 105         if (variable > lastp) {
 106                 free(fdata);
 107                 errno = EINVAL;
 108                 return (NULL);
 109         }
 110 
 111         maplower_ext_ranges = (_FileRuneEntry *)variable;
 112         variable = maplower_ext_ranges + frl->maplower_ext_nranges;
 113         if (variable > lastp) {
 114                 free(fdata);
 115                 errno = EINVAL;
 116                 return (NULL);
 117         }
 118 
 119         mapupper_ext_ranges = (_FileRuneEntry *)variable;
 120         variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
 121         if (variable > lastp) {
 122                 free(fdata);
 123                 errno = EINVAL;
 124                 return (NULL);
 125         }
 126 
 127         frr = runetype_ext_ranges;
 128         for (x = 0; x < frl->runetype_ext_nranges; ++x) {
 129                 uint32_t *types;
 130 
 131                 if (frr[x].map == 0) {
 132                         int len = frr[x].max - frr[x].min + 1;
 133                         types = variable;
 134                         variable = types + len;
 135                         runetype_ext_len += len;
 136                         if (variable > lastp) {
 137                                 free(fdata);
 138                                 errno = EINVAL;
 139                                 return (NULL);
 140                         }
 141                 }
 142         }
 143 
 144         if ((char *)variable + frl->variable_len > (char *)lastp) {
 145                 free(fdata);
 146                 errno = EINVAL;
 147                 return (NULL);
 148         }
 149 
 150         /*
 151          * Convert from disk format to host format.
 152          */
 153         data = malloc(sizeof (_RuneLocale) +
 154             (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
 155             frl->mapupper_ext_nranges) * sizeof (_RuneEntry) +
 156             runetype_ext_len * sizeof (*rr->__types) +
 157             frl->variable_len);
 158         if (data == NULL) {
 159                 saverr = errno;
 160                 free(fdata);
 161                 errno = saverr;
 162                 return (NULL);
 163         }
 164 
 165         rl = (_RuneLocale *)(void *)data;
 166         rl->__variable = rl + 1;
 167 
 168         (void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic));
 169         (void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding));
 170 
 171         rl->__variable_len = frl->variable_len;
 172         rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
 173         rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
 174         rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
 175 
 176         for (x = 0; x < _CACHED_RUNES; ++x) {
 177                 rl->__runetype[x] = frl->runetype[x];
 178                 rl->__maplower[x] = frl->maplower[x];
 179                 rl->__mapupper[x] = frl->mapupper[x];
 180         }


 212                         rr[x].__types = NULL;
 213         }
 214 
 215         frr = maplower_ext_ranges;
 216         rr = rl->__maplower_ext.__ranges;
 217         for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
 218                 rr[x].__min = frr[x].min;
 219                 rr[x].__max = frr[x].max;
 220                 rr[x].__map = frr[x].map;
 221         }
 222 
 223         frr = mapupper_ext_ranges;
 224         rr = rl->__mapupper_ext.__ranges;
 225         for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
 226                 rr[x].__min = frr[x].min;
 227                 rr[x].__max = frr[x].max;
 228                 rr[x].__map = frr[x].map;
 229         }
 230 
 231         (void) memcpy(rl->__variable, variable, rl->__variable_len);
 232         free(fdata);
 233 
 234         /*
 235          * Go out and zero pointers that should be zero.
 236          */
 237         if (!rl->__variable_len)
 238                 rl->__variable = NULL;
 239 
 240         if (!rl->__runetype_ext.__nranges)
 241                 rl->__runetype_ext.__ranges = NULL;
 242 
 243         if (!rl->__maplower_ext.__nranges)
 244                 rl->__maplower_ext.__ranges = NULL;
 245 
 246         if (!rl->__mapupper_ext.__nranges)
 247                 rl->__mapupper_ext.__ranges = NULL;
 248 
 249         return (rl);





 250 }
   1 /*
   2  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
   3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   4  * Copyright (c) 1993
   5  *      The Regents of the University of California.  All rights reserved.
   6  *
   7  * This code is derived from software contributed to Berkeley by
   8  * Paul Borman at Krystal Technologies.
   9  *
  10  * Redistribution and use in source and binary forms, with or without
  11  * modification, are permitted provided that the following conditions
  12  * are met:
  13  * 1. Redistributions of source code must retain the above copyright
  14  *    notice, this list of conditions and the following disclaimer.
  15  * 2. Redistributions in binary form must reproduce the above copyright
  16  *    notice, this list of conditions and the following disclaimer in the
  17  *    documentation and/or other materials provided with the distribution.
  18  * 4. Neither the name of the University nor the names of its contributors
  19  *    may be used to endorse or promote products derived from this software
  20  *    without specific prior written permission.
  21  *
  22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32  * SUCH DAMAGE.
  33  */
  34 
  35 #include "lint.h"
  36 #include "file64.h"
  37 #include <errno.h>
  38 #include <stdio.h>
  39 #include <string.h>
  40 #include <stdlib.h>
  41 #include <sys/types.h>
  42 #include <sys/stat.h>
  43 #include <sys/mman.h>
  44 #include <fcntl.h>
  45 #include <unistd.h>
  46 
  47 #include "runetype.h"
  48 #include "runefile.h"
  49 


  50 _RuneLocale *
  51 _Read_RuneMagi(const char *fname)
  52 {
  53         char *fdata, *data;
  54         void *lastp;
  55         _FileRuneLocale *frl;
  56         _RuneLocale *rl;
  57         _FileRuneEntry *frr;
  58         _RuneEntry *rr;
  59         struct stat sb;
  60         int x, saverr;
  61         void *variable;
  62         _FileRuneEntry *runetype_ext_ranges;
  63         _FileRuneEntry *maplower_ext_ranges;
  64         _FileRuneEntry *mapupper_ext_ranges;
  65         int runetype_ext_len = 0;
  66         int fd;
  67 
  68         if ((fd = open(fname, O_RDONLY)) < 0) {
  69                 errno = EINVAL;
  70                 return (NULL);
  71         }
  72 
  73         if (fstat(fd, &sb) < 0) {
  74                 (void) close(fd);
  75                 errno = EINVAL;
  76                 return (NULL);
  77         }
  78 
  79         if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
  80                 (void) close(fd);
  81                 errno = EINVAL;
  82                 return (NULL);








  83         }
  84 
  85 
  86         fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  87         (void) close(fd);
  88         if (fdata == NULL) {
  89                 errno = EINVAL;
  90                 return (NULL);
  91         }
  92 
  93         frl = (_FileRuneLocale *)(void *)fdata;
  94         lastp = fdata + sb.st_size;
  95 
  96         variable = frl + 1;
  97 
  98         if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
  99                 goto invalid;


 100         }
 101 
 102         runetype_ext_ranges = (_FileRuneEntry *)variable;
 103         variable = runetype_ext_ranges + frl->runetype_ext_nranges;
 104         if (variable > lastp) {
 105                 goto invalid;


 106         }
 107 
 108         maplower_ext_ranges = (_FileRuneEntry *)variable;
 109         variable = maplower_ext_ranges + frl->maplower_ext_nranges;
 110         if (variable > lastp) {
 111                 goto invalid;


 112         }
 113 
 114         mapupper_ext_ranges = (_FileRuneEntry *)variable;
 115         variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
 116         if (variable > lastp) {
 117                 goto invalid;


 118         }
 119 
 120         frr = runetype_ext_ranges;
 121         for (x = 0; x < frl->runetype_ext_nranges; ++x) {
 122                 uint32_t *types;
 123 
 124                 if (frr[x].map == 0) {
 125                         int len = frr[x].max - frr[x].min + 1;
 126                         types = variable;
 127                         variable = types + len;
 128                         runetype_ext_len += len;
 129                         if (variable > lastp) {
 130                                 goto invalid;


 131                         }
 132                 }
 133         }
 134 
 135         if ((char *)variable + frl->variable_len > (char *)lastp) {
 136                 goto invalid;


 137         }
 138 
 139         /*
 140          * Convert from disk format to host format.
 141          */
 142         data = malloc(sizeof (_RuneLocale) +
 143             (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
 144             frl->mapupper_ext_nranges) * sizeof (_RuneEntry) +
 145             runetype_ext_len * sizeof (*rr->__types) +
 146             frl->variable_len);
 147         if (data == NULL) {
 148                 saverr = errno;
 149                 (void) munmap(fdata, sb.st_size);
 150                 errno = saverr;
 151                 return (NULL);
 152         }
 153 
 154         rl = (_RuneLocale *)(void *)data;
 155         rl->__variable = rl + 1;
 156 
 157         (void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic));
 158         (void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding));
 159 
 160         rl->__variable_len = frl->variable_len;
 161         rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
 162         rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
 163         rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
 164 
 165         for (x = 0; x < _CACHED_RUNES; ++x) {
 166                 rl->__runetype[x] = frl->runetype[x];
 167                 rl->__maplower[x] = frl->maplower[x];
 168                 rl->__mapupper[x] = frl->mapupper[x];
 169         }


 201                         rr[x].__types = NULL;
 202         }
 203 
 204         frr = maplower_ext_ranges;
 205         rr = rl->__maplower_ext.__ranges;
 206         for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
 207                 rr[x].__min = frr[x].min;
 208                 rr[x].__max = frr[x].max;
 209                 rr[x].__map = frr[x].map;
 210         }
 211 
 212         frr = mapupper_ext_ranges;
 213         rr = rl->__mapupper_ext.__ranges;
 214         for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
 215                 rr[x].__min = frr[x].min;
 216                 rr[x].__max = frr[x].max;
 217                 rr[x].__map = frr[x].map;
 218         }
 219 
 220         (void) memcpy(rl->__variable, variable, rl->__variable_len);
 221         (void) munmap(fdata, sb.st_size);
 222 
 223         /*
 224          * Go out and zero pointers that should be zero.
 225          */
 226         if (!rl->__variable_len)
 227                 rl->__variable = NULL;
 228 
 229         if (!rl->__runetype_ext.__nranges)
 230                 rl->__runetype_ext.__ranges = NULL;
 231 
 232         if (!rl->__maplower_ext.__nranges)
 233                 rl->__maplower_ext.__ranges = NULL;
 234 
 235         if (!rl->__mapupper_ext.__nranges)
 236                 rl->__mapupper_ext.__ranges = NULL;
 237 
 238         return (rl);
 239 
 240 invalid:
 241         (void) munmap(fdata, sb.st_size);
 242         errno = EINVAL;
 243         return (NULL);
 244 }