Coverage Report

Created: 2025-11-05 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unistr/u8-mbtouc-unsafe-aux.c
Line
Count
Source
1
/* Conversion UTF-8 to UCS-4.
2
   Copyright (C) 2001-2002, 2006-2007, 2009-2025 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2001.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
#include <config.h>
19
20
/* Specification.  */
21
#include "unistr.h"
22
23
#if defined IN_LIBUNISTRING || HAVE_INLINE
24
25
int
26
u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n)
27
18.6M
{
28
18.6M
  uint8_t c = *s;
29
30
18.6M
  if (c >= 0xc2)
31
17.9M
    {
32
17.9M
      if (c < 0xe0)
33
16.1M
        {
34
16.1M
          if (n >= 2)
35
16.1M
            {
36
16.1M
              if ((s[1] ^ 0x80) < 0x40)
37
15.9M
                {
38
15.9M
                  *puc = ((unsigned int) (c & 0x1f) << 6)
39
15.9M
                         | (unsigned int) (s[1] ^ 0x80);
40
15.9M
                  return 2;
41
15.9M
                }
42
              /* invalid multibyte character */
43
16.1M
            }
44
0
          else
45
0
            {
46
              /* incomplete multibyte character */
47
0
              *puc = 0xfffd;
48
0
              return 1;
49
0
            }
50
16.1M
        }
51
1.74M
      else if (c < 0xf0)
52
1.57M
        {
53
1.57M
          if (n >= 3)
54
1.57M
            {
55
1.57M
              if ((s[1] ^ 0x80) < 0x40)
56
1.56M
                {
57
1.56M
                  if ((s[2] ^ 0x80) < 0x40)
58
1.56M
                    {
59
1.56M
                      if ((c >= 0xe1 || s[1] >= 0xa0)
60
1.56M
                          && (c != 0xed || s[1] < 0xa0))
61
1.56M
                        {
62
1.56M
                          *puc = ((unsigned int) (c & 0x0f) << 12)
63
1.56M
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
64
1.56M
                                 | (unsigned int) (s[2] ^ 0x80);
65
1.56M
                          return 3;
66
1.56M
                        }
67
                      /* invalid multibyte character */
68
404
                      *puc = 0xfffd;
69
404
                      return 3;
70
1.56M
                    }
71
                  /* invalid multibyte character */
72
1.17k
                  *puc = 0xfffd;
73
1.17k
                  return 2;
74
1.56M
                }
75
              /* invalid multibyte character */
76
1.57M
            }
77
1.16k
          else
78
1.16k
            {
79
              /* incomplete multibyte character */
80
1.16k
              *puc = 0xfffd;
81
1.16k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
82
1.16k
                return 1;
83
0
              else
84
0
                return 2;
85
1.16k
            }
86
1.57M
        }
87
176k
      else if (c < 0xf8)
88
25.3k
        {
89
25.3k
          if (n >= 4)
90
24.1k
            {
91
24.1k
              if ((s[1] ^ 0x80) < 0x40)
92
18.5k
                {
93
18.5k
                  if ((s[2] ^ 0x80) < 0x40)
94
18.1k
                    {
95
18.1k
                      if ((s[3] ^ 0x80) < 0x40)
96
17.8k
                        {
97
17.8k
                          if ((c >= 0xf1 || s[1] >= 0x90)
98
17.6k
                              && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
99
17.8k
                             )
100
16.2k
                            {
101
16.2k
                              *puc = ((unsigned int) (c & 0x07) << 18)
102
16.2k
                                     | ((unsigned int) (s[1] ^ 0x80) << 12)
103
16.2k
                                     | ((unsigned int) (s[2] ^ 0x80) << 6)
104
16.2k
                                     | (unsigned int) (s[3] ^ 0x80);
105
16.2k
                              return 4;
106
16.2k
                            }
107
                          /* invalid multibyte character */
108
1.60k
                          *puc = 0xfffd;
109
1.60k
                          return 4;
110
17.8k
                        }
111
                      /* invalid multibyte character */
112
280
                      *puc = 0xfffd;
113
280
                      return 3;
114
18.1k
                    }
115
                  /* invalid multibyte character */
116
378
                  *puc = 0xfffd;
117
378
                  return 2;
118
18.5k
                }
119
              /* invalid multibyte character */
120
24.1k
            }
121
1.19k
          else
122
1.19k
            {
123
              /* incomplete multibyte character */
124
1.19k
              *puc = 0xfffd;
125
1.19k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
126
878
                return 1;
127
316
              else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
128
316
                return 2;
129
0
              else
130
0
                return 3;
131
1.19k
            }
132
25.3k
        }
133
17.9M
    }
134
  /* invalid multibyte character */
135
1.05M
  *puc = 0xfffd;
136
1.05M
  return 1;
137
18.6M
}
138
139
#endif