OpenCoverage

shmbchar.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/shmbchar.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* Copyright (C) 2001, 2006, 2009, 2010, 2012, 2015 Free Software Foundation, Inc.-
2-
3 This program is free software: you can redistribute it and/or modify-
4 it under the terms of the GNU General Public License as published by-
5 the Free Software Foundation; either version 3 of the License, or-
6 (at your option) any later version.-
7-
8 This program is distributed in the hope that it will be useful,-
9 but WITHOUT ANY WARRANTY; without even the implied warranty of-
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
11 GNU General Public License for more details.-
12-
13 You should have received a copy of the GNU General Public License-
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */-
15-
16-
17#include <config.h>-
18-
19#if defined (HANDLE_MULTIBYTE)-
20#include <stdlib.h>-
21#include <limits.h>-
22-
23#include <errno.h>-
24-
25#include <shmbutil.h>-
26#include <shmbchar.h>-
27-
28#ifndef errno-
29extern int errno;-
30#endif-
31-
32#if IS_BASIC_ASCII-
33-
34/* Bit table of characters in the ISO C "basic character set". */-
35const unsigned int is_basic_table [UCHAR_MAX / 32 + 1] =-
36{-
37 0x00001a00, /* '\t' '\v' '\f' */-
38 0xffffffef, /* ' '...'#' '%'...'?' */-
39 0xfffffffe, /* 'A'...'Z' '[' '\\' ']' '^' '_' */-
40 0x7ffffffe /* 'a'...'z' '{' '|' '}' '~' */-
41 /* The remaining bits are 0. */-
42};-
43-
44#endif /* IS_BASIC_ASCII */-
45-
46extern int locale_utf8locale;-
47-
48/* We can optimize this if we know the locale is UTF-8, but needs to handle-
49 malformed byte sequences. */-
50static inline size_t-
51utf8_mbstrlen(s)-
52 const char *s;-
53{-
54 size_t num = 0;-
55 register unsigned char c;-
56-
57 while ((c = *s++))
(c = *s++)Description
TRUEnever evaluated
FALSEnever evaluated
0
58 /* bytes 0xc0 through 0xff are first byte of multi-byte sequence */-
59 if ((c & 0xc0) != 0x80) /* skip continuation bytes */
(c & 0xc0) != 0x80Description
TRUEnever evaluated
FALSEnever evaluated
0
60 ++num;
never executed: ++num;
0
61 return (num);
never executed: return (num);
0
62}-
63-
64/* Adapted from GNU libutf8 */-
65static inline int-
66utf8_mblen (s, n)-
67 const char *s;-
68 int n;-
69{-
70 unsigned char c;-
71-
72 if (s == 0)
s == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
73 return 0;
never executed: return 0;
0
74 else if (n == 0)
n == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
75 return -1;
never executed: return -1;
0
76-
77 c = (unsigned char) *s;-
78 if (c < 0x80)
c < 0x80Description
TRUEnever evaluated
FALSEnever evaluated
0
79 return (c != 0);
never executed: return (c != 0);
0
80 else if (c < 0xc0)
c < 0xc0Description
TRUEnever evaluated
FALSEnever evaluated
0
81 goto return_error;
never executed: goto return_error;
0
82 else-
83 {-
84 const char *start = s;-
85 size_t count;-
86 int check_unsafe;-
87-
88 if (c < 0xe0)
c < 0xe0Description
TRUEnever evaluated
FALSEnever evaluated
0
89 {-
90 count = 1;-
91 if (c < 0xc2)
c < 0xc2Description
TRUEnever evaluated
FALSEnever evaluated
0
92 goto return_error;
never executed: goto return_error;
0
93 check_unsafe = 0;-
94 }
never executed: end of block
0
95 else if (c < 0xf0)
c < 0xf0Description
TRUEnever evaluated
FALSEnever evaluated
0
96 {-
97 count = 2;-
98 check_unsafe = (c == 0xe0);-
99 }
never executed: end of block
0
100#if SIZEOF_WCHAR_T == 4-
101 else if (c < 0xf8)
c < 0xf8Description
TRUEnever evaluated
FALSEnever evaluated
0
102 {-
103 count = 3;-
104 check_unsafe = (c == 0xe0);-
105 }
never executed: end of block
0
106 else if (c < 0xfc)
c < 0xfcDescription
TRUEnever evaluated
FALSEnever evaluated
0
107 {-
108 count = 4;-
109 check_unsafe = (c == 0xf8);-
110 }
never executed: end of block
0
111 else if (c < 0xfe)
c < 0xfeDescription
TRUEnever evaluated
FALSEnever evaluated
0
112 {-
113 count = 5;-
114 check_unsafe = (c == 0xfc);-
115 }
never executed: end of block
0
116#endif-
117 else-
118 goto return_error;
never executed: goto return_error;
0
119 if (n <= count)
n <= countDescription
TRUEnever evaluated
FALSEnever evaluated
0
120 return -1;
never executed: return -1;
0
121 s++;-
122 c = (unsigned char) *s++ ^ 0x80;-
123 if (c >= 0x40)
c >= 0x40Description
TRUEnever evaluated
FALSEnever evaluated
0
124 goto return_error;
never executed: goto return_error;
0
125 if (--count > 0)
--count > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
126 {-
127 if (check_unsafe && ((c >> (6 - count)) == 0))
check_unsafeDescription
TRUEnever evaluated
FALSEnever evaluated
((c >> (6 - count)) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
128 goto return_error;
never executed: goto return_error;
0
129 do-
130 {-
131 c = (unsigned char) *s++ ^ 0x80;-
132 if (c >= 0x40)
c >= 0x40Description
TRUEnever evaluated
FALSEnever evaluated
0
133 goto return_error;
never executed: goto return_error;
0
134 }
never executed: end of block
0
135 while (--count > 0);
--count > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
136 }
never executed: end of block
0
137 return s - start;
never executed: return s - start;
0
138 }-
139return_error:-
140 errno = EILSEQ;-
141 return -1;
never executed: return -1;
0
142}-
143 -
144/* Count the number of characters in S, counting multi-byte characters as a-
145 single character. */-
146size_t-
147mbstrlen (s)-
148 const char *s;-
149{-
150 size_t clen, nc;-
151 mbstate_t mbs = { 0 }, mbsbak = { 0 };-
152 int f, mb_cur_max;-
153-
154 nc = 0;-
155 mb_cur_max = MB_CUR_MAX;-
156 while (*s && (clen = (f = is_basic (*s)) ? 1 : mbrlen(s, mb_cur_max, &mbs)) != 0)
(f = is_basic (*s))Description
TRUEevaluated 7174 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 321 times by 1 test
Evaluated by:
  • Self test
*sDescription
TRUEevaluated 7495 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 650 times by 1 test
Evaluated by:
  • Self test
(clen = (f = i...x, &mbs)) != 0Description
TRUEevaluated 7495 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-7495
157 {-
158 if (MB_INVALIDCH(clen))
(clen) == (size_t)-1Description
TRUEnever evaluated
FALSEevaluated 7495 times by 1 test
Evaluated by:
  • Self test
(clen) == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 7495 times by 1 test
Evaluated by:
  • Self test
0-7495
159 {-
160 clen = 1; /* assume single byte */-
161 mbs = mbsbak;-
162 }
never executed: end of block
0
163-
164 if (f == 0)
f == 0Description
TRUEevaluated 321 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7174 times by 1 test
Evaluated by:
  • Self test
321-7174
165 mbsbak = mbs;
executed 321 times by 1 test: mbsbak = mbs;
Executed by:
  • Self test
321
166-
167 s += clen;-
168 nc++;-
169 }
executed 7495 times by 1 test: end of block
Executed by:
  • Self test
7495
170 return nc;
executed 650 times by 1 test: return nc;
Executed by:
  • Self test
650
171}-
172-
173static inline char *-
174utf8_mbsmbchar (str)-
175 const char *str;-
176{-
177 register char *s;-
178-
179 for (s = (char *)str; *s; s++)
*sDescription
TRUEevaluated 678369198 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 98412726 times by 1 test
Evaluated by:
  • Self test
98412726-678369198
180 if ((*s & 0xc0) == 0x80)
(*s & 0xc0) == 0x80Description
TRUEevaluated 19 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 678369179 times by 1 test
Evaluated by:
  • Self test
19-678369179
181 return s;
executed 19 times by 1 test: return s;
Executed by:
  • Self test
19
182 return (0);
executed 98412726 times by 1 test: return (0);
Executed by:
  • Self test
98412726
183}-
184-
185/* Return pointer to first multibyte char in S, or NULL if none. */-
186/* XXX - if we know that the locale is UTF-8, we can just check whether or-
187 not any byte has the eighth bit turned on */-
188char *-
189mbsmbchar (s)-
190 const char *s;-
191{-
192 char *t;-
193 size_t clen;-
194 mbstate_t mbs = { 0 };-
195 int mb_cur_max;-
196-
197 if (locale_utf8locale)
locale_utf8localeDescription
TRUEevaluated 98412745 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4506 times by 1 test
Evaluated by:
  • Self test
4506-98412745
198 return (utf8_mbsmbchar (s)); /* XXX */
executed 98412745 times by 1 test: return (utf8_mbsmbchar (s));
Executed by:
  • Self test
98412745
199-
200 mb_cur_max = MB_CUR_MAX;-
201 for (t = (char *)s; *t; t++)
*tDescription
TRUEevaluated 40022 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4506 times by 1 test
Evaluated by:
  • Self test
4506-40022
202 {-
203 if (is_basic (*t))
is_basic (*t)Description
TRUEevaluated 39796 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
226-39796
204 continue;
executed 39796 times by 1 test: continue;
Executed by:
  • Self test
39796
205-
206 if (locale_utf8locale) /* not used if above code active */
locale_utf8localeDescription
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-226
207 clen = utf8_mblen (t, mb_cur_max);
never executed: clen = utf8_mblen (t, mb_cur_max);
0
208 else-
209 clen = mbrlen (t, mb_cur_max, &mbs);
executed 226 times by 1 test: clen = mbrlen (t, mb_cur_max, &mbs);
Executed by:
  • Self test
226
210-
211 if (clen == 0)
clen == 0Description
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-226
212 return 0;
never executed: return 0;
0
213 if (MB_INVALIDCH(clen))
(clen) == (size_t)-1Description
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
(clen) == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-226
214 continue;
never executed: continue;
0
215-
216 if (clen > 1)
clen > 1Description
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-226
217 return t;
never executed: return t;
0
218 }
executed 226 times by 1 test: end of block
Executed by:
  • Self test
226
219 return 0;
executed 4506 times by 1 test: return 0;
Executed by:
  • Self test
4506
220}-
221-
222static inline int-
223utf_mbsnlen(src, srclen, maxlen)-
224 const char *src;-
225 size_t srclen;-
226 int maxlen;-
227{-
228 register int sind, count;-
229-
230 for (sind = count = 0; src[sind] && sind <= maxlen; sind++)
src[sind]Description
TRUEnever evaluated
FALSEnever evaluated
sind <= maxlenDescription
TRUEnever evaluated
FALSEnever evaluated
0
231 {-
232 if ((src[sind] & 0xc0) != 0x80)
(src[sind] & 0xc0) != 0x80Description
TRUEnever evaluated
FALSEnever evaluated
0
233 count++;
never executed: count++;
0
234 }
never executed: end of block
0
235 return (count);
never executed: return (count);
0
236}-
237-
238int-
239sh_mbsnlen(src, srclen, maxlen)-
240 const char *src;-
241 size_t srclen;-
242 int maxlen;-
243{-
244 int count;-
245 int sind;-
246 DECLARE_MBSTATE;-
247-
248 for (sind = count = 0; src[sind]; )
src[sind]Description
TRUEnever evaluated
FALSEnever evaluated
0
249 {-
250 count++; /* number of multibyte characters */-
251 ADVANCE_CHAR (src, srclen, sind);
never executed: mblength = 1;
never executed: mblength = 1;
never executed: end of block
never executed: end of block
never executed: (sind)++;
never executed: (sind) += mblength;
never executed: (sind)++;
locale_mb_cur_max > 1Description
TRUEnever evaluated
FALSEnever evaluated
_fDescription
TRUEnever evaluated
FALSEnever evaluated
mblength == 0Description
TRUEnever evaluated
FALSEnever evaluated
locale_utf8localeDescription
TRUEnever evaluated
FALSEnever evaluated
(((src)[sind] & 0x80) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
mblength == (size_t)-2Description
TRUEnever evaluated
FALSEnever evaluated
mblength == (size_t)-1Description
TRUEnever evaluated
FALSEnever evaluated
0
252 if (sind > maxlen)
sind > maxlenDescription
TRUEnever evaluated
FALSEnever evaluated
0
253 break;
never executed: break;
0
254 }
never executed: end of block
0
255-
256 return count;
never executed: return count;
0
257}-
258#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2