Coverage Report

Created: 2026-01-10 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/avahi/avahi-common/utf8.c
Line
Count
Source
1
/* This file is based on the GLIB utf8 validation functions. The
2
 * original license text follows. */
3
4
/* gutf8.c - Operations on UTF-8 strings.
5
 *
6
 * Copyright (C) 1999 Tom Tromey
7
 * Copyright (C) 2000 Red Hat, Inc.
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the
21
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
 * Boston, MA 02111-1307, USA.
23
 */
24
25
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27
#endif
28
29
#include <stdlib.h>
30
31
#include "utf8.h"
32
33
#define UNICODE_VALID(Char)                   \
34
15.1k
    ((Char) < 0x110000 &&                     \
35
15.1k
     (((Char) & 0xFFFFF800) != 0xD800) &&     \
36
15.1k
     ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
37
15.1k
     ((Char) & 0xFFFE) != 0xFFFE)
38
39
40
#define CONTINUATION_CHAR                           \
41
35.7k
 do {                                     \
42
35.7k
  if ((*(const unsigned char *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
43
35.7k
    goto error;                                     \
44
35.7k
  val <<= 6;                                        \
45
35.4k
  val |= (*(const unsigned char *)p) & 0x3f;                     \
46
35.4k
 } while(0)
47
48
49
const char *
50
avahi_utf8_valid (const char *str)
51
52
1.68M
{
53
1.68M
  unsigned val = 0;
54
1.68M
  unsigned min = 0;
55
1.68M
  const char *p;
56
57
53.6M
  for (p = str; *p; p++)
58
51.9M
    {
59
51.9M
      if (*(const unsigned char *)p < 128)
60
51.9M
  /* done */;
61
21.7k
      else
62
21.7k
  {
63
21.7k
    if ((*(const unsigned char *)p & 0xe0) == 0xc0) /* 110xxxxx */
64
5.86k
      {
65
5.86k
        if ( ((*(const unsigned char *)p & 0x1e) == 0))
66
76
    goto error;
67
5.78k
        p++;
68
5.78k
        if ( ((*(const unsigned char *)p & 0xc0) != 0x80)) /* 10xxxxxx */
69
195
    goto error;
70
5.78k
      }
71
15.8k
    else
72
15.8k
      {
73
15.8k
        if ((*(const unsigned char *)p & 0xf0) == 0xe0) /* 1110xxxx */
74
10.2k
    {
75
10.2k
      min = (1 << 11);
76
10.2k
      val = *(const unsigned char *)p & 0x0f;
77
10.2k
      goto TWO_REMAINING;
78
10.2k
    }
79
5.58k
        else if ((*(const unsigned char *)p & 0xf8) == 0xf0) /* 11110xxx */
80
5.18k
    {
81
5.18k
      min = (1 << 16);
82
5.18k
      val = *(const unsigned char *)p & 0x07;
83
5.18k
    }
84
401
        else
85
401
    goto error;
86
87
5.18k
        p++;
88
5.18k
        CONTINUATION_CHAR;
89
15.3k
      TWO_REMAINING:
90
15.3k
        p++;
91
15.3k
        CONTINUATION_CHAR;
92
15.2k
        p++;
93
15.2k
        CONTINUATION_CHAR;
94
95
15.1k
        if ( (val < min))
96
23
    goto error;
97
98
15.1k
        if ( (!UNICODE_VALID(val)))
99
147
    goto error;
100
15.1k
      }
101
102
20.5k
    continue;
103
104
20.5k
  error:
105
1.15k
    return NULL;
106
21.7k
  }
107
51.9M
    }
108
109
1.68M
  return str;
110
1.68M
}