Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Approved by: TBD
   1 /*

   2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2002-2004 Tim J. Robbins. 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  *


  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 <errno.h>
  37 #include <limits.h>
  38 #include <stddef.h>
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <string.h>
  42 #include <wchar.h>
  43 #include <note.h>
  44 #include "runetype.h"
  45 #include "mblocal.h"

  46 
  47 static size_t   _none_mbrtowc(wchar_t *_RESTRICT_KYWD,
  48     const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD);
  49 
  50 static int      _none_mbsinit(const mbstate_t *);
  51 static size_t   _none_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst,
  52     const char **_RESTRICT_KYWD src, size_t nms, size_t len,
  53     mbstate_t *_RESTRICT_KYWD);
  54 static size_t   _none_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
  55     mbstate_t *_RESTRICT_KYWD);
  56 static size_t   _none_wcsnrtombs(char *_RESTRICT_KYWD,
  57     const wchar_t **_RESTRICT_KYWD,
  58     size_t, size_t, mbstate_t *_RESTRICT_KYWD);
  59 
  60 /* setup defaults */
  61 
  62 int
  63 _none_init(_RuneLocale *rl)
  64 {
  65         charset_is_ascii = 1;
  66 
  67         __mbrtowc = _none_mbrtowc;
  68         __mbsinit = _none_mbsinit;
  69         __mbsnrtowcs = _none_mbsnrtowcs;
  70         __wcrtomb = _none_wcrtomb;
  71         __wcsnrtombs = _none_wcsnrtombs;
  72         _CurrentRuneLocale = rl;
  73         return (0);
  74 }
  75 
  76 static int
  77 _none_mbsinit(const mbstate_t *unused)
  78 {
  79         _NOTE(ARGUNUSED(unused));
  80 
  81         /*
  82          * Encoding is not state dependent - we are always in the
  83          * initial state.
  84          */
  85         return (1);
  86 }
  87 
  88 static size_t
  89 _none_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
  90     size_t n, mbstate_t *_RESTRICT_KYWD unused)
  91 {
  92         _NOTE(ARGUNUSED(unused));
  93 
  94         if (s == NULL)
  95                 /* Reset to initial shift state (no-op) */
  96                 return (0);
  97         if (n == 0)
  98                 /* Incomplete multibyte sequence */
  99                 return ((size_t)-2);
 100         if (pwc != NULL)
 101                 *pwc = (unsigned char)*s;
 102         return (*s == '\0' ? 0 : 1);
 103 }
 104 
 105 static size_t
 106 _none_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc,
 107     mbstate_t *_RESTRICT_KYWD unused)
 108 {
 109         _NOTE(ARGUNUSED(unused));
 110 
 111         if (s == NULL)
 112                 /* Reset to initial shift state (no-op) */
 113                 return (1);
 114         if (wc < 0 || wc > UCHAR_MAX) {
 115                 errno = EILSEQ;
 116                 return ((size_t)-1);
 117         }
 118         *s = (unsigned char)wc;
 119         return (1);
 120 }
 121 
 122 static size_t
 123 _none_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
 124     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
 125 {
 126         const char *s;
 127         size_t nchr;
 128 
 129         _NOTE(ARGUNUSED(unused));
 130 
 131         if (dst == NULL) {
 132                 s = memchr(*src, '\0', nms);
 133                 return (s != NULL ? s - *src : nms);
 134         }
 135 
 136         s = *src;
 137         nchr = 0;
 138         while (len-- > 0 && nms-- > 0) {
 139                 if ((*dst++ = (unsigned char)*s++) == L'\0') {
 140                         *src = NULL;
 141                         return (nchr);
 142                 }
 143                 nchr++;
 144         }
 145         *src = s;
 146         return (nchr);
 147 }
 148 
 149 static size_t
 150 _none_wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
 151     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
 152 {
 153         const wchar_t *s;
 154         size_t nchr;
 155 
 156         _NOTE(ARGUNUSED(unused));
 157 
 158         if (dst == NULL) {
 159                 for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
 160                         if (*s < 0 || *s > UCHAR_MAX) {
 161                                 errno = EILSEQ;
 162                                 return ((size_t)-1);
 163                         }
 164                 }
 165                 return (s - *src);
 166         }
 167 
 168         s = *src;
 169         nchr = 0;
 170         while (len-- > 0 && nwc-- > 0) {
 171                 if (*s < 0 || *s > UCHAR_MAX) {
 172                         errno = EILSEQ;
 173                         return ((size_t)-1);
 174                 }
 175                 if ((*dst++ = *s++) == '\0') {
 176                         *src = NULL;
 177                         return (nchr);
 178                 }
 179                 nchr++;
 180         }
 181         *src = s;
 182         return (nchr);
 183 }
 184 
 185 /* setup defaults */
 186 
 187 size_t (*__mbrtowc)(wchar_t *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
 188     size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbrtowc;
 189 
 190 int (*__mbsinit)(const mbstate_t *) = _none_mbsinit;
 191 
 192 size_t (*__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, const char **_RESTRICT_KYWD,
 193     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbsnrtowcs;
 194 
 195 size_t (*__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, mbstate_t *_RESTRICT_KYWD) =
 196     _none_wcrtomb;
 197 
 198 size_t (*__wcsnrtombs)(char *_RESTRICT_KYWD, const wchar_t **_RESTRICT_KYWD,
 199     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_wcsnrtombs;
   1 /*
   2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
   3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
   4  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
   5  * Copyright (c) 1993
   6  *      The Regents of the University of California.  All rights reserved.
   7  *
   8  * This code is derived from software contributed to Berkeley by
   9  * Paul Borman at Krystal Technologies.
  10  *
  11  * Redistribution and use in source and binary forms, with or without
  12  * modification, are permitted provided that the following conditions
  13  * are met:
  14  * 1. Redistributions of source code must retain the above copyright
  15  *    notice, this list of conditions and the following disclaimer.
  16  * 2. Redistributions in binary form must reproduce the above copyright
  17  *    notice, this list of conditions and the following disclaimer in the
  18  *    documentation and/or other materials provided with the distribution.
  19  * 4. Neither the name of the University nor the names of its contributors
  20  *    may be used to endorse or promote products derived from this software
  21  *    without specific prior written permission.
  22  *


  25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33  * SUCH DAMAGE.
  34  */
  35 
  36 #include "lint.h"
  37 #include <errno.h>
  38 #include <limits.h>
  39 #include <stddef.h>
  40 #include <stdio.h>
  41 #include <stdlib.h>
  42 #include <string.h>
  43 #include <wchar.h>
  44 #include <note.h>

  45 #include "mblocal.h"
  46 #include "lctype.h"
  47 













  48 /* setup defaults */
  49 
  50 void
  51 _none_init(struct lc_ctype *lct)
  52 {
  53         lct->lc_is_ascii = 1;
  54         lct->lc_mbrtowc = __mbrtowc_ascii;
  55         lct->lc_mbsinit = __mbsinit_ascii;
  56         lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
  57         lct->lc_wcrtomb = __wcrtomb_ascii;
  58         lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
  59         lct->lc_max_mblen = 1;


  60 }
  61 
  62 int
  63 __mbsinit_ascii(const mbstate_t *unused)
  64 {
  65         _NOTE(ARGUNUSED(unused));
  66 
  67         /*
  68          * Encoding is not state dependent - we are always in the
  69          * initial state.
  70          */
  71         return (1);
  72 }
  73 
  74 size_t
  75 __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
  76     size_t n, mbstate_t *_RESTRICT_KYWD unused)
  77 {
  78         _NOTE(ARGUNUSED(unused));
  79 
  80         if (s == NULL)
  81                 /* Reset to initial shift state (no-op) */
  82                 return (0);
  83         if (n == 0)
  84                 /* Incomplete multibyte sequence */
  85                 return ((size_t)-2);
  86         if (pwc != NULL)
  87                 *pwc = (unsigned char)*s;
  88         return (*s == '\0' ? 0 : 1);
  89 }
  90 
  91 size_t
  92 __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
  93     mbstate_t *_RESTRICT_KYWD unused)
  94 {
  95         _NOTE(ARGUNUSED(unused));
  96 
  97         if (s == NULL)
  98                 /* Reset to initial shift state (no-op) */
  99                 return (1);
 100         if (wc < 0 || wc > UCHAR_MAX) {
 101                 errno = EILSEQ;
 102                 return ((size_t)-1);
 103         }
 104         *s = (unsigned char)wc;
 105         return (1);
 106 }
 107 
 108 size_t
 109 __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
 110     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
 111 {
 112         const char *s;
 113         size_t nchr;
 114 
 115         _NOTE(ARGUNUSED(unused));
 116 
 117         if (dst == NULL) {
 118                 s = memchr(*src, '\0', nms);
 119                 return (s != NULL ? s - *src : nms);
 120         }
 121 
 122         s = *src;
 123         nchr = 0;
 124         while (len-- > 0 && nms-- > 0) {
 125                 if ((*dst++ = (unsigned char)*s++) == L'\0') {
 126                         *src = NULL;
 127                         return (nchr);
 128                 }
 129                 nchr++;
 130         }
 131         *src = s;
 132         return (nchr);
 133 }
 134 
 135 size_t
 136 __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
 137     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
 138 {
 139         const wchar_t *s;
 140         size_t nchr;
 141 
 142         _NOTE(ARGUNUSED(unused));
 143 
 144         if (dst == NULL) {
 145                 for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
 146                         if (*s < 0 || *s > UCHAR_MAX) {
 147                                 errno = EILSEQ;
 148                                 return ((size_t)-1);
 149                         }
 150                 }
 151                 return (s - *src);
 152         }
 153 
 154         s = *src;
 155         nchr = 0;
 156         while (len-- > 0 && nwc-- > 0) {
 157                 if (*s < 0 || *s > UCHAR_MAX) {
 158                         errno = EILSEQ;
 159                         return ((size_t)-1);
 160                 }
 161                 if ((*dst++ = *s++) == '\0') {
 162                         *src = NULL;
 163                         return (nchr);
 164                 }
 165                 nchr++;
 166         }
 167         *src = s;
 168         return (nchr);
 169 }