Coverage Report

Created: 2026-06-08 06:48

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