Coverage Report

Created: 2025-03-06 07:58

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