Coverage Report

Created: 2026-02-05 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unistr/u8-mbtoucr.c
Line
Count
Source
1
/* Look at first character in UTF-8 string, returning an error code.
2
   Copyright (C) 1999-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
int
24
u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)
25
7.09M
{
26
7.09M
  uint8_t c = *s;
27
28
7.09M
  if (c < 0x80)
29
3.06M
    {
30
3.06M
      *puc = c;
31
3.06M
      return 1;
32
3.06M
    }
33
4.02M
  else if (c >= 0xc2)
34
4.02M
    {
35
4.02M
      if (c < 0xe0)
36
3.70M
        {
37
3.70M
          if (n >= 2)
38
3.70M
            {
39
3.70M
              if ((s[1] ^ 0x80) < 0x40)
40
3.70M
                {
41
3.70M
                  *puc = ((unsigned int) (c & 0x1f) << 6)
42
3.70M
                         | (unsigned int) (s[1] ^ 0x80);
43
3.70M
                  return 2;
44
3.70M
                }
45
              /* invalid multibyte character */
46
3.70M
            }
47
0
          else
48
0
            {
49
              /* incomplete multibyte character */
50
0
              *puc = 0xfffd;
51
0
              return -2;
52
0
            }
53
3.70M
        }
54
326k
      else if (c < 0xf0)
55
302k
        {
56
302k
          if (n >= 2)
57
302k
            {
58
302k
              if ((s[1] ^ 0x80) < 0x40
59
302k
                  && (c >= 0xe1 || s[1] >= 0xa0)
60
302k
                  && (c != 0xed || s[1] < 0xa0))
61
302k
                {
62
302k
                  if (n >= 3)
63
302k
                    {
64
302k
                      if ((s[2] ^ 0x80) < 0x40)
65
302k
                        {
66
302k
                          *puc = ((unsigned int) (c & 0x0f) << 12)
67
302k
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
68
302k
                                 | (unsigned int) (s[2] ^ 0x80);
69
302k
                          return 3;
70
302k
                        }
71
                      /* invalid multibyte character */
72
302k
                    }
73
0
                  else
74
0
                    {
75
                      /* incomplete multibyte character */
76
0
                      *puc = 0xfffd;
77
0
                      return -2;
78
0
                    }
79
302k
                }
80
              /* invalid multibyte character */
81
302k
            }
82
0
          else
83
0
            {
84
              /* incomplete multibyte character */
85
0
              *puc = 0xfffd;
86
0
              return -2;
87
0
            }
88
302k
        }
89
23.7k
      else if (c <= 0xf4)
90
23.7k
        {
91
23.7k
          if (n >= 2)
92
23.7k
            {
93
23.7k
              if ((s[1] ^ 0x80) < 0x40
94
23.7k
                  && (c >= 0xf1 || s[1] >= 0x90)
95
23.7k
                  && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))
96
23.7k
                {
97
23.7k
                  if (n >= 3)
98
23.7k
                    {
99
23.7k
                      if ((s[2] ^ 0x80) < 0x40)
100
23.7k
                        {
101
23.7k
                          if (n >= 4)
102
23.7k
                            {
103
23.7k
                              if ((s[3] ^ 0x80) < 0x40)
104
23.7k
                                {
105
23.7k
                                  *puc = ((unsigned int) (c & 0x07) << 18)
106
23.7k
                                         | ((unsigned int) (s[1] ^ 0x80) << 12)
107
23.7k
                                         | ((unsigned int) (s[2] ^ 0x80) << 6)
108
23.7k
                                         | (unsigned int) (s[3] ^ 0x80);
109
23.7k
                                  return 4;
110
23.7k
                                }
111
                              /* invalid multibyte character */
112
23.7k
                            }
113
0
                          else
114
0
                            {
115
                              /* incomplete multibyte character */
116
0
                              *puc = 0xfffd;
117
0
                              return -2;
118
0
                            }
119
23.7k
                        }
120
                      /* invalid multibyte character */
121
23.7k
                    }
122
0
                  else
123
0
                    {
124
                      /* incomplete multibyte character */
125
0
                      *puc = 0xfffd;
126
0
                      return -2;
127
0
                    }
128
23.7k
                }
129
              /* invalid multibyte character */
130
23.7k
            }
131
0
          else
132
0
            {
133
              /* incomplete multibyte character */
134
0
              *puc = 0xfffd;
135
0
              return -2;
136
0
            }
137
23.7k
        }
138
4.02M
    }
139
  /* invalid multibyte character */
140
0
  *puc = 0xfffd;
141
0
  return -1;
142
7.09M
}