Coverage Report

Created: 2026-02-20 06:21

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-2026 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.0M
{
28
18.0M
  uint8_t c = *s;
29
30
18.0M
  if (c >= 0xc2)
31
17.1M
    {
32
17.1M
      if (c < 0xe0)
33
14.6M
        {
34
14.6M
          if (n >= 2)
35
14.6M
            {
36
14.6M
              if ((s[1] ^ 0x80) < 0x40)
37
14.3M
                {
38
14.3M
                  *puc = ((unsigned int) (c & 0x1f) << 6)
39
14.3M
                         | (unsigned int) (s[1] ^ 0x80);
40
14.3M
                  return 2;
41
14.3M
                }
42
              /* invalid multibyte character */
43
14.6M
            }
44
0
          else
45
0
            {
46
              /* incomplete multibyte character */
47
0
              *puc = 0xfffd;
48
0
              return 1;
49
0
            }
50
14.6M
        }
51
2.47M
      else if (c < 0xf0)
52
2.21M
        {
53
2.21M
          if (n >= 3)
54
2.21M
            {
55
2.21M
              if ((s[1] ^ 0x80) < 0x40)
56
2.15M
                {
57
2.15M
                  if ((s[2] ^ 0x80) < 0x40)
58
2.14M
                    {
59
2.14M
                      if ((c >= 0xe1 || s[1] >= 0xa0)
60
2.14M
                          && (c != 0xed || s[1] < 0xa0))
61
2.14M
                        {
62
2.14M
                          *puc = ((unsigned int) (c & 0x0f) << 12)
63
2.14M
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
64
2.14M
                                 | (unsigned int) (s[2] ^ 0x80);
65
2.14M
                          return 3;
66
2.14M
                        }
67
                      /* invalid multibyte character */
68
950
                      *puc = 0xfffd;
69
950
                      return 3;
70
2.14M
                    }
71
                  /* invalid multibyte character */
72
13.8k
                  *puc = 0xfffd;
73
13.8k
                  return 2;
74
2.15M
                }
75
              /* invalid multibyte character */
76
2.21M
            }
77
2.39k
          else
78
2.39k
            {
79
              /* incomplete multibyte character */
80
2.39k
              *puc = 0xfffd;
81
2.39k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
82
2.39k
                return 1;
83
0
              else
84
0
                return 2;
85
2.39k
            }
86
2.21M
        }
87
260k
      else if (c < 0xf8)
88
58.2k
        {
89
58.2k
          if (n >= 4)
90
55.3k
            {
91
55.3k
              if ((s[1] ^ 0x80) < 0x40)
92
26.8k
                {
93
26.8k
                  if ((s[2] ^ 0x80) < 0x40)
94
20.4k
                    {
95
20.4k
                      if ((s[3] ^ 0x80) < 0x40)
96
18.4k
                        {
97
18.4k
                          if ((c >= 0xf1 || s[1] >= 0x90)
98
17.2k
                              && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
99
18.4k
                             )
100
15.9k
                            {
101
15.9k
                              *puc = ((unsigned int) (c & 0x07) << 18)
102
15.9k
                                     | ((unsigned int) (s[1] ^ 0x80) << 12)
103
15.9k
                                     | ((unsigned int) (s[2] ^ 0x80) << 6)
104
15.9k
                                     | (unsigned int) (s[3] ^ 0x80);
105
15.9k
                              return 4;
106
15.9k
                            }
107
                          /* invalid multibyte character */
108
2.49k
                          *puc = 0xfffd;
109
2.49k
                          return 4;
110
18.4k
                        }
111
                      /* invalid multibyte character */
112
1.99k
                      *puc = 0xfffd;
113
1.99k
                      return 3;
114
20.4k
                    }
115
                  /* invalid multibyte character */
116
6.43k
                  *puc = 0xfffd;
117
6.43k
                  return 2;
118
26.8k
                }
119
              /* invalid multibyte character */
120
55.3k
            }
121
2.84k
          else
122
2.84k
            {
123
              /* incomplete multibyte character */
124
2.84k
              *puc = 0xfffd;
125
2.84k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
126
2.23k
                return 1;
127
611
              else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
128
611
                return 2;
129
0
              else
130
0
                return 3;
131
2.84k
            }
132
58.2k
        }
133
17.1M
    }
134
  /* invalid multibyte character */
135
1.49M
  *puc = 0xfffd;
136
1.49M
  return 1;
137
18.0M
}
138
139
#endif