Coverage Report

Created: 2025-07-23 06:43

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