Coverage Report

Created: 2026-05-30 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libidn/lib/gl/unistr/u8-check.c
Line
Count
Source
1
/* Check UTF-8 string.
2
   Copyright (C) 2002, 2006-2007, 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2002.
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
/* Don't use the const-improved function macros in this compilation unit.  */
19
#define _LIBUNISTRING_NO_CONST_GENERICS
20
21
#include <config.h>
22
23
/* Specification.  */
24
#include "unistr.h"
25
26
const uint8_t *
27
u8_check (const uint8_t *s, size_t n)
28
184k
{
29
184k
  const uint8_t *s_end = s + n;
30
31
5.66M
  while (s < s_end)
32
5.48M
    {
33
      /* Keep in sync with unistr.h and u8-mbtouc-aux.c.  */
34
5.48M
      uint8_t c = *s;
35
36
5.48M
      if (c < 0x80)
37
975k
        {
38
975k
          s++;
39
975k
          continue;
40
975k
        }
41
4.51M
      if (c >= 0xc2)
42
4.50M
        {
43
4.50M
          if (c < 0xe0)
44
1.22M
            {
45
1.22M
              if (s + 2 <= s_end
46
1.22M
                  && (s[1] ^ 0x80) < 0x40)
47
1.22M
                {
48
1.22M
                  s += 2;
49
1.22M
                  continue;
50
1.22M
                }
51
1.22M
            }
52
3.28M
          else if (c < 0xf0)
53
3.23M
            {
54
3.23M
              if (s + 3 <= s_end
55
3.23M
                  && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
56
3.23M
                  && (c >= 0xe1 || s[1] >= 0xa0)
57
3.23M
                  && (c != 0xed || s[1] < 0xa0))
58
3.23M
                {
59
3.23M
                  s += 3;
60
3.23M
                  continue;
61
3.23M
                }
62
3.23M
            }
63
45.2k
          else if (c <= 0xf4)
64
38.8k
            {
65
38.8k
              if (s + 4 <= s_end
66
38.6k
                  && (s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
67
38.4k
                  && (s[3] ^ 0x80) < 0x40
68
38.3k
                  && (c >= 0xf1 || s[1] >= 0x90)
69
38.3k
                  && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))
70
38.0k
                {
71
38.0k
                  s += 4;
72
38.0k
                  continue;
73
38.0k
                }
74
38.8k
            }
75
4.50M
        }
76
      /* invalid or incomplete multibyte character */
77
10.3k
      return s;
78
4.51M
    }
79
174k
  return NULL;
80
184k
}