Coverage Report

Created: 2023-06-07 07:17

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