Coverage Report

Created: 2025-08-23 06:26

/src/libunistring/lib/unistr/u8-mbtouc-unsafe-aux.c
Line
Count
Source (jump to first uncovered line)
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
22.9M
{
28
22.9M
  uint8_t c = *s;
29
30
22.9M
  if (c >= 0xc2)
31
22.2M
    {
32
22.2M
      if (c < 0xe0)
33
20.0M
        {
34
20.0M
          if (n >= 2)
35
20.0M
            {
36
20.0M
              if ((s[1] ^ 0x80) < 0x40)
37
19.8M
                {
38
19.8M
                  *puc = ((unsigned int) (c & 0x1f) << 6)
39
19.8M
                         | (unsigned int) (s[1] ^ 0x80);
40
19.8M
                  return 2;
41
19.8M
                }
42
              /* invalid multibyte character */
43
20.0M
            }
44
0
          else
45
0
            {
46
              /* incomplete multibyte character */
47
0
              *puc = 0xfffd;
48
0
              return 1;
49
0
            }
50
20.0M
        }
51
2.13M
      else if (c < 0xf0)
52
1.94M
        {
53
1.94M
          if (n >= 3)
54
1.94M
            {
55
1.94M
              if ((s[1] ^ 0x80) < 0x40)
56
1.92M
                {
57
1.92M
                  if ((s[2] ^ 0x80) < 0x40)
58
1.92M
                    {
59
1.92M
                      if ((c >= 0xe1 || s[1] >= 0xa0)
60
1.92M
                          && (c != 0xed || s[1] < 0xa0))
61
1.92M
                        {
62
1.92M
                          *puc = ((unsigned int) (c & 0x0f) << 12)
63
1.92M
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
64
1.92M
                                 | (unsigned int) (s[2] ^ 0x80);
65
1.92M
                          return 3;
66
1.92M
                        }
67
                      /* invalid multibyte character */
68
427
                      *puc = 0xfffd;
69
427
                      return 3;
70
1.92M
                    }
71
                  /* invalid multibyte character */
72
3.82k
                  *puc = 0xfffd;
73
3.82k
                  return 2;
74
1.92M
                }
75
              /* invalid multibyte character */
76
1.94M
            }
77
2.04k
          else
78
2.04k
            {
79
              /* incomplete multibyte character */
80
2.04k
              *puc = 0xfffd;
81
2.04k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
82
2.04k
                return 1;
83
0
              else
84
0
                return 2;
85
2.04k
            }
86
1.94M
        }
87
191k
      else if (c < 0xf8)
88
36.0k
        {
89
36.0k
          if (n >= 4)
90
34.3k
            {
91
34.3k
              if ((s[1] ^ 0x80) < 0x40)
92
23.9k
                {
93
23.9k
                  if ((s[2] ^ 0x80) < 0x40)
94
22.9k
                    {
95
22.9k
                      if ((s[3] ^ 0x80) < 0x40)
96
22.5k
                        {
97
22.5k
                          if ((c >= 0xf1 || s[1] >= 0x90)
98
22.5k
                              && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
99
22.5k
                             )
100
21.1k
                            {
101
21.1k
                              *puc = ((unsigned int) (c & 0x07) << 18)
102
21.1k
                                     | ((unsigned int) (s[1] ^ 0x80) << 12)
103
21.1k
                                     | ((unsigned int) (s[2] ^ 0x80) << 6)
104
21.1k
                                     | (unsigned int) (s[3] ^ 0x80);
105
21.1k
                              return 4;
106
21.1k
                            }
107
                          /* invalid multibyte character */
108
1.41k
                          *puc = 0xfffd;
109
1.41k
                          return 4;
110
22.5k
                        }
111
                      /* invalid multibyte character */
112
434
                      *puc = 0xfffd;
113
434
                      return 3;
114
22.9k
                    }
115
                  /* invalid multibyte character */
116
965
                  *puc = 0xfffd;
117
965
                  return 2;
118
23.9k
                }
119
              /* invalid multibyte character */
120
34.3k
            }
121
1.62k
          else
122
1.62k
            {
123
              /* incomplete multibyte character */
124
1.62k
              *puc = 0xfffd;
125
1.62k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
126
1.25k
                return 1;
127
370
              else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
128
370
                return 2;
129
0
              else
130
0
                return 3;
131
1.62k
            }
132
36.0k
        }
133
22.2M
    }
134
  /* invalid multibyte character */
135
1.18M
  *puc = 0xfffd;
136
1.18M
  return 1;
137
22.9M
}
138
139
#endif