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) 2004 Tim J. Robbins. All rights reserved.
   4  * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
   5  * All rights reserved.
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  * 1. Redistributions of source code must retain the above copyright
  11  *    notice, this list of conditions and the following disclaimer.
  12  * 2. Redistributions in binary form must reproduce the above copyright
  13  *    notice, this list of conditions and the following disclaimer in the
  14  *    documentation and/or other materials provided with the distribution.
  15  *
  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26  * SUCH DAMAGE.
  27  */
  28 
  29 #include "lint.h"
  30 #include <sys/types.h>
  31 #include <errno.h>
  32 #include "runetype.h"
  33 #include <stdlib.h>
  34 #include <string.h>
  35 #include <wchar.h>
  36 #include "mblocal.h"

  37 
  38 static size_t   _GB2312_mbrtowc(wchar_t *_RESTRICT_KYWD,
  39                     const char *_RESTRICT_KYWD,
  40                     size_t, mbstate_t *_RESTRICT_KYWD);
  41 static int      _GB2312_mbsinit(const mbstate_t *);
  42 static size_t   _GB2312_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
  43                     mbstate_t *_RESTRICT_KYWD);






  44 

  45 typedef struct {
  46         int     count;
  47         uchar_t bytes[2];
  48 } _GB2312State;
  49 
  50 int
  51 _GB2312_init(_RuneLocale *rl)
  52 {
  53 
  54         _CurrentRuneLocale = rl;
  55         __mbrtowc = _GB2312_mbrtowc;
  56         __wcrtomb = _GB2312_wcrtomb;
  57         __mbsinit = _GB2312_mbsinit;
  58         __ctype[520] = 2;
  59         charset_is_ascii = 0;
  60         return (0);
  61 }
  62 
  63 static int
  64 _GB2312_mbsinit(const mbstate_t *ps)
  65 {
  66 
  67         return (ps == NULL || ((const _GB2312State *)ps)->count == 0);
  68 }
  69 
  70 static int
  71 _GB2312_check(const char *str, size_t n)
  72 {
  73         const uchar_t *s = (const uchar_t *)str;
  74 
  75         if (n == 0)
  76                 /* Incomplete multibyte sequence */
  77                 return (-2);
  78         if (s[0] >= 0xa1 && s[0] <= 0xfe) {
  79                 if (n < 2)
  80                         /* Incomplete multibyte sequence */


 137 {
 138         _GB2312State *gs;
 139 
 140         gs = (_GB2312State *)ps;
 141 
 142         if (gs->count != 0) {
 143                 errno = EINVAL;
 144                 return ((size_t)-1);
 145         }
 146 
 147         if (s == NULL)
 148                 /* Reset to initial shift state (no-op) */
 149                 return (1);
 150         if (wc & 0x8000) {
 151                 *s++ = (wc >> 8) & 0xff;
 152                 *s = wc & 0xff;
 153                 return (2);
 154         }
 155         *s = wc & 0xff;
 156         return (1);
















 157 }
   1 /*
   2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
   3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   4  * Copyright (c) 2004 Tim J. Robbins. All rights reserved.
   5  * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
   6  * All rights reserved.
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 1. Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  * 2. Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in the
  15  *    documentation and/or other materials provided with the distribution.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 #include "lint.h"
  31 #include <sys/types.h>
  32 #include <errno.h>
  33 #include "runetype.h"
  34 #include <stdlib.h>
  35 #include <string.h>
  36 #include <wchar.h>
  37 #include "mblocal.h"
  38 #include "lctype.h"
  39 
  40 static size_t   _GB2312_mbrtowc(wchar_t *_RESTRICT_KYWD,
  41                     const char *_RESTRICT_KYWD,
  42                     size_t, mbstate_t *_RESTRICT_KYWD);
  43 static int      _GB2312_mbsinit(const mbstate_t *);
  44 static size_t   _GB2312_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
  45                     mbstate_t *_RESTRICT_KYWD);
  46 static size_t   _GB2312_mbsnrtowcs(wchar_t *_RESTRICT_KYWD,
  47                     const char **_RESTRICT_KYWD, size_t, size_t,
  48                     mbstate_t *_RESTRICT_KYWD);
  49 static size_t   _GB2312_wcsnrtombs(char *_RESTRICT_KYWD,
  50                     const wchar_t **_RESTRICT_KYWD, size_t, size_t,
  51                     mbstate_t *_RESTRICT_KYWD);
  52 
  53 
  54 typedef struct {
  55         int     count;
  56         uchar_t bytes[2];
  57 } _GB2312State;
  58 
  59 void
  60 _GB2312_init(struct lc_ctype *lct)
  61 {
  62 
  63         lct->lc_mbrtowc = _GB2312_mbrtowc;
  64         lct->lc_wcrtomb = _GB2312_wcrtomb;
  65         lct->lc_mbsinit = _GB2312_mbsinit;
  66         lct->lc_mbsnrtowcs = _GB2312_mbsnrtowcs;
  67         lct->lc_wcsnrtombs = _GB2312_wcsnrtombs;
  68         lct->lc_max_mblen = 2;
  69         lct->lc_is_ascii = 0;
  70 }
  71 
  72 static int
  73 _GB2312_mbsinit(const mbstate_t *ps)
  74 {
  75 
  76         return (ps == NULL || ((const _GB2312State *)ps)->count == 0);
  77 }
  78 
  79 static int
  80 _GB2312_check(const char *str, size_t n)
  81 {
  82         const uchar_t *s = (const uchar_t *)str;
  83 
  84         if (n == 0)
  85                 /* Incomplete multibyte sequence */
  86                 return (-2);
  87         if (s[0] >= 0xa1 && s[0] <= 0xfe) {
  88                 if (n < 2)
  89                         /* Incomplete multibyte sequence */


 146 {
 147         _GB2312State *gs;
 148 
 149         gs = (_GB2312State *)ps;
 150 
 151         if (gs->count != 0) {
 152                 errno = EINVAL;
 153                 return ((size_t)-1);
 154         }
 155 
 156         if (s == NULL)
 157                 /* Reset to initial shift state (no-op) */
 158                 return (1);
 159         if (wc & 0x8000) {
 160                 *s++ = (wc >> 8) & 0xff;
 161                 *s = wc & 0xff;
 162                 return (2);
 163         }
 164         *s = wc & 0xff;
 165         return (1);
 166 }
 167 
 168 static size_t
 169 _GB2312_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst,
 170     const char **_RESTRICT_KYWD src, size_t nms, size_t len,
 171     mbstate_t *_RESTRICT_KYWD ps)
 172 {
 173         return (__mbsnrtowcs_std(dst, src, nms, len, ps, _GB2312_mbrtowc));
 174 }
 175 
 176 static size_t
 177 _GB2312_wcsnrtombs(char *_RESTRICT_KYWD dst,
 178     const wchar_t **_RESTRICT_KYWD src, size_t nwc, size_t len,
 179     mbstate_t *_RESTRICT_KYWD ps)
 180 {
 181         return (__wcsnrtombs_std(dst, src, nwc, len, ps, _GB2312_wcrtomb));
 182 }