Coverage Report

Created: 2025-01-28 06:58

/src/libunistring/lib/unistr/u8-mbtoucr.c
Line
Count
Source (jump to first uncovered line)
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
1.11M
{
26
1.11M
  uint8_t c = *s;
27
28
1.11M
  if (c < 0x80)
29
213k
    {
30
213k
      *puc = c;
31
213k
      return 1;
32
213k
    }
33
900k
  else if (c >= 0xc2)
34
898k
    {
35
898k
      if (c < 0xe0)
36
838k
        {
37
838k
          if (n >= 2)
38
838k
            {
39
838k
              if ((s[1] ^ 0x80) < 0x40)
40
836k
                {
41
836k
                  *puc = ((unsigned int) (c & 0x1f) << 6)
42
836k
                         | (unsigned int) (s[1] ^ 0x80);
43
836k
                  return 2;
44
836k
                }
45
              /* invalid multibyte character */
46
838k
            }
47
0
          else
48
0
            {
49
              /* incomplete multibyte character */
50
0
              *puc = 0xfffd;
51
0
              return -2;
52
0
            }
53
838k
        }
54
59.6k
      else if (c < 0xf0)
55
55.8k
        {
56
55.8k
          if (n >= 2)
57
55.8k
            {
58
55.8k
              if ((s[1] ^ 0x80) < 0x40
59
55.8k
                  && (c >= 0xe1 || s[1] >= 0xa0)
60
55.8k
                  && (c != 0xed || s[1] < 0xa0))
61
55.4k
                {
62
55.4k
                  if (n >= 3)
63
55.4k
                    {
64
55.4k
                      if ((s[2] ^ 0x80) < 0x40)
65
55.2k
                        {
66
55.2k
                          *puc = ((unsigned int) (c & 0x0f) << 12)
67
55.2k
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
68
55.2k
                                 | (unsigned int) (s[2] ^ 0x80);
69
55.2k
                          return 3;
70
55.2k
                        }
71
                      /* invalid multibyte character */
72
55.4k
                    }
73
0
                  else
74
0
                    {
75
                      /* incomplete multibyte character */
76
0
                      *puc = 0xfffd;
77
0
                      return -2;
78
0
                    }
79
55.4k
                }
80
              /* invalid multibyte character */
81
55.8k
            }
82
0
          else
83
0
            {
84
              /* incomplete multibyte character */
85
0
              *puc = 0xfffd;
86
0
              return -2;
87
0
            }
88
55.8k
        }
89
3.77k
      else if (c <= 0xf4)
90
3.11k
        {
91
3.11k
          if (n >= 2)
92
3.11k
            {
93
3.11k
              if ((s[1] ^ 0x80) < 0x40
94
3.11k
                  && (c >= 0xf1 || s[1] >= 0x90)
95
3.11k
                  && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))
96
2.95k
                {
97
2.95k
                  if (n >= 3)
98
2.95k
                    {
99
2.95k
                      if ((s[2] ^ 0x80) < 0x40)
100
2.86k
                        {
101
2.86k
                          if (n >= 4)
102
2.86k
                            {
103
2.86k
                              if ((s[3] ^ 0x80) < 0x40)
104
2.80k
                                {
105
2.80k
                                  *puc = ((unsigned int) (c & 0x07) << 18)
106
2.80k
                                         | ((unsigned int) (s[1] ^ 0x80) << 12)
107
2.80k
                                         | ((unsigned int) (s[2] ^ 0x80) << 6)
108
2.80k
                                         | (unsigned int) (s[3] ^ 0x80);
109
2.80k
                                  return 4;
110
2.80k
                                }
111
                              /* invalid multibyte character */
112
2.86k
                            }
113
0
                          else
114
0
                            {
115
                              /* incomplete multibyte character */
116
0
                              *puc = 0xfffd;
117
0
                              return -2;
118
0
                            }
119
2.86k
                        }
120
                      /* invalid multibyte character */
121
2.95k
                    }
122
0
                  else
123
0
                    {
124
                      /* incomplete multibyte character */
125
0
                      *puc = 0xfffd;
126
0
                      return -2;
127
0
                    }
128
2.95k
                }
129
              /* invalid multibyte character */
130
3.11k
            }
131
0
          else
132
0
            {
133
              /* incomplete multibyte character */
134
0
              *puc = 0xfffd;
135
0
              return -2;
136
0
            }
137
3.11k
        }
138
898k
    }
139
  /* invalid multibyte character */
140
5.69k
  *puc = 0xfffd;
141
5.69k
  return -1;
142
1.11M
}