Coverage Report

Created: 2026-09-28 06:58

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