Coverage Report

Created: 2026-12-01 06:42

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
29.3M
{
28
29.3M
  uint8_t c = *s;
29
30
29.3M
  if (c >= 0xc2)
31
27.7M
    {
32
27.7M
      if (c < 0xe0)
33
18.2M
        {
34
18.2M
          if (n >= 2)
35
18.2M
            {
36
18.2M
              if ((s[1] ^ 0x80) < 0x40)
37
18.1M
                {
38
18.1M
                  *puc = ((unsigned int) (c & 0x1f) << 6)
39
18.1M
                         | (unsigned int) (s[1] ^ 0x80);
40
18.1M
                  return 2;
41
18.1M
                }
42
              /* invalid multibyte character */
43
18.2M
            }
44
0
          else
45
0
            {
46
              /* incomplete multibyte character */
47
0
              *puc = 0xfffd;
48
0
              return 1;
49
0
            }
50
18.2M
        }
51
9.41M
      else if (c < 0xf0)
52
8.92M
        {
53
8.92M
          if (n >= 3)
54
8.92M
            {
55
8.92M
              if ((s[1] ^ 0x80) < 0x40)
56
8.92M
                {
57
8.92M
                  if ((s[2] ^ 0x80) < 0x40)
58
8.91M
                    {
59
8.91M
                      if ((c >= 0xe1 || s[1] >= 0xa0)
60
8.91M
                          && (c != 0xed || s[1] < 0xa0))
61
8.91M
                        {
62
8.91M
                          *puc = ((unsigned int) (c & 0x0f) << 12)
63
8.91M
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
64
8.91M
                                 | (unsigned int) (s[2] ^ 0x80);
65
8.91M
                          return 3;
66
8.91M
                        }
67
                      /* invalid multibyte character */
68
918
                      *puc = 0xfffd;
69
918
                      return 3;
70
8.91M
                    }
71
                  /* invalid multibyte character */
72
5.51k
                  *puc = 0xfffd;
73
5.51k
                  return 2;
74
8.92M
                }
75
              /* invalid multibyte character */
76
8.92M
            }
77
1.24k
          else
78
1.24k
            {
79
              /* incomplete multibyte character */
80
1.24k
              *puc = 0xfffd;
81
1.24k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
82
1.24k
                return 1;
83
0
              else
84
0
                return 2;
85
1.24k
            }
86
8.92M
        }
87
490k
      else if (c < 0xf8)
88
36.2k
        {
89
36.2k
          if (n >= 4)
90
35.1k
            {
91
35.1k
              if ((s[1] ^ 0x80) < 0x40)
92
32.7k
                {
93
32.7k
                  if ((s[2] ^ 0x80) < 0x40)
94
31.9k
                    {
95
31.9k
                      if ((s[3] ^ 0x80) < 0x40)
96
31.3k
                        {
97
31.3k
                          if ((c >= 0xf1 || s[1] >= 0x90)
98
30.9k
                              && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
99
31.3k
                             )
100
29.6k
                            {
101
29.6k
                              *puc = ((unsigned int) (c & 0x07) << 18)
102
29.6k
                                     | ((unsigned int) (s[1] ^ 0x80) << 12)
103
29.6k
                                     | ((unsigned int) (s[2] ^ 0x80) << 6)
104
29.6k
                                     | (unsigned int) (s[3] ^ 0x80);
105
29.6k
                              return 4;
106
29.6k
                            }
107
                          /* invalid multibyte character */
108
1.68k
                          *puc = 0xfffd;
109
1.68k
                          return 4;
110
31.3k
                        }
111
                      /* invalid multibyte character */
112
609
                      *puc = 0xfffd;
113
609
                      return 3;
114
31.9k
                    }
115
                  /* invalid multibyte character */
116
837
                  *puc = 0xfffd;
117
837
                  return 2;
118
32.7k
                }
119
              /* invalid multibyte character */
120
35.1k
            }
121
1.15k
          else
122
1.15k
            {
123
              /* incomplete multibyte character */
124
1.15k
              *puc = 0xfffd;
125
1.15k
              if (n == 1 || (s[1] ^ 0x80) >= 0x40)
126
843
                return 1;
127
314
              else if (n == 2 || (s[2] ^ 0x80) >= 0x40)
128
314
                return 2;
129
0
              else
130
0
                return 3;
131
1.15k
            }
132
36.2k
        }
133
27.7M
    }
134
  /* invalid multibyte character */
135
2.23M
  *puc = 0xfffd;
136
2.23M
  return 1;
137
29.3M
}
138
139
#endif