Coverage Report

Created: 2025-11-16 06:46

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-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
int
24
u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)
25
20.8M
{
26
20.8M
  uint8_t c = *s;
27
28
20.8M
  if (c < 0x80)
29
20.7M
    {
30
20.7M
      *puc = c;
31
20.7M
      return 1;
32
20.7M
    }
33
73.3k
  else if (c >= 0xc2)
34
73.2k
    {
35
73.2k
      if (c < 0xe0)
36
33.6k
        {
37
33.6k
          if (n >= 2)
38
33.6k
            {
39
33.6k
              if ((s[1] ^ 0x80) < 0x40)
40
33.6k
                {
41
33.6k
                  *puc = ((unsigned int) (c & 0x1f) << 6)
42
33.6k
                         | (unsigned int) (s[1] ^ 0x80);
43
33.6k
                  return 2;
44
33.6k
                }
45
              /* invalid multibyte character */
46
33.6k
            }
47
8
          else
48
8
            {
49
              /* incomplete multibyte character */
50
8
              *puc = 0xfffd;
51
8
              return -2;
52
8
            }
53
33.6k
        }
54
39.5k
      else if (c < 0xf0)
55
33.9k
        {
56
33.9k
          if (n >= 2)
57
33.9k
            {
58
33.9k
              if ((s[1] ^ 0x80) < 0x40
59
33.8k
                  && (c >= 0xe1 || s[1] >= 0xa0)
60
33.8k
                  && (c != 0xed || s[1] < 0xa0))
61
33.8k
                {
62
33.8k
                  if (n >= 3)
63
33.8k
                    {
64
33.8k
                      if ((s[2] ^ 0x80) < 0x40)
65
33.7k
                        {
66
33.7k
                          *puc = ((unsigned int) (c & 0x0f) << 12)
67
33.7k
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
68
33.7k
                                 | (unsigned int) (s[2] ^ 0x80);
69
33.7k
                          return 3;
70
33.7k
                        }
71
                      /* invalid multibyte character */
72
33.8k
                    }
73
33
                  else
74
33
                    {
75
                      /* incomplete multibyte character */
76
33
                      *puc = 0xfffd;
77
33
                      return -2;
78
33
                    }
79
33.8k
                }
80
              /* invalid multibyte character */
81
33.9k
            }
82
9
          else
83
9
            {
84
              /* incomplete multibyte character */
85
9
              *puc = 0xfffd;
86
9
              return -2;
87
9
            }
88
33.9k
        }
89
5.63k
      else if (c <= 0xf4)
90
5.60k
        {
91
5.60k
          if (n >= 2)
92
5.60k
            {
93
5.60k
              if ((s[1] ^ 0x80) < 0x40
94
5.58k
                  && (c >= 0xf1 || s[1] >= 0x90)
95
5.57k
                  && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))
96
5.56k
                {
97
5.56k
                  if (n >= 3)
98
5.53k
                    {
99
5.53k
                      if ((s[2] ^ 0x80) < 0x40)
100
5.50k
                        {
101
5.50k
                          if (n >= 4)
102
5.49k
                            {
103
5.49k
                              if ((s[3] ^ 0x80) < 0x40)
104
5.45k
                                {
105
5.45k
                                  *puc = ((unsigned int) (c & 0x07) << 18)
106
5.45k
                                         | ((unsigned int) (s[1] ^ 0x80) << 12)
107
5.45k
                                         | ((unsigned int) (s[2] ^ 0x80) << 6)
108
5.45k
                                         | (unsigned int) (s[3] ^ 0x80);
109
5.45k
                                  return 4;
110
5.45k
                                }
111
                              /* invalid multibyte character */
112
5.49k
                            }
113
9
                          else
114
9
                            {
115
                              /* incomplete multibyte character */
116
9
                              *puc = 0xfffd;
117
9
                              return -2;
118
9
                            }
119
5.50k
                        }
120
                      /* invalid multibyte character */
121
5.53k
                    }
122
25
                  else
123
25
                    {
124
                      /* incomplete multibyte character */
125
25
                      *puc = 0xfffd;
126
25
                      return -2;
127
25
                    }
128
5.56k
                }
129
              /* invalid multibyte character */
130
5.60k
            }
131
4
          else
132
4
            {
133
              /* incomplete multibyte character */
134
4
              *puc = 0xfffd;
135
4
              return -2;
136
4
            }
137
5.60k
        }
138
73.2k
    }
139
  /* invalid multibyte character */
140
331
  *puc = 0xfffd;
141
331
  return -1;
142
20.8M
}