Coverage Report

Created: 2025-07-11 06:23

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