Coverage Report

Created: 2026-02-26 06:52

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
3.76k
    ((Char) < 0x110000 &&                     \
35
3.76k
     (((Char) & 0xFFFFF800) != 0xD800) &&     \
36
3.76k
     ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
37
3.76k
     ((Char) & 0xFFFE) != 0xFFFE)
38
39
40
#define CONTINUATION_CHAR                           \
41
9.37k
 do {                                     \
42
9.37k
  if ((*(const unsigned char *)p & 0xc0) != 0x80) /* 10xxxxxx */ \
43
9.37k
    goto error;                                     \
44
9.37k
  val <<= 6;                                        \
45
9.10k
  val |= (*(const unsigned char *)p) & 0x3f;                     \
46
9.10k
 } while(0)
47
48
49
const char *
50
avahi_utf8_valid (const char *str)
51
52
27.6k
{
53
27.6k
  unsigned val = 0;
54
27.6k
  unsigned min = 0;
55
27.6k
  const char *p;
56
57
193k
  for (p = str; *p; p++)
58
167k
    {
59
167k
      if (*(const unsigned char *)p < 128)
60
161k
  /* done */;
61
5.75k
      else
62
5.75k
  {
63
5.75k
    if ((*(const unsigned char *)p & 0xe0) == 0xc0) /* 110xxxxx */
64
1.36k
      {
65
1.36k
        if ( ((*(const unsigned char *)p & 0x1e) == 0))
66
29
    goto error;
67
1.33k
        p++;
68
1.33k
        if ( ((*(const unsigned char *)p & 0xc0) != 0x80)) /* 10xxxxxx */
69
142
    goto error;
70
1.33k
      }
71
4.39k
    else
72
4.39k
      {
73
4.39k
        if ((*(const unsigned char *)p & 0xf0) == 0xe0) /* 1110xxxx */
74
2.53k
    {
75
2.53k
      min = (1 << 11);
76
2.53k
      val = *(const unsigned char *)p & 0x0f;
77
2.53k
      goto TWO_REMAINING;
78
2.53k
    }
79
1.86k
        else if ((*(const unsigned char *)p & 0xf8) == 0xf0) /* 11110xxx */
80
1.53k
    {
81
1.53k
      min = (1 << 16);
82
1.53k
      val = *(const unsigned char *)p & 0x07;
83
1.53k
    }
84
326
        else
85
326
    goto error;
86
87
1.53k
        p++;
88
1.53k
        CONTINUATION_CHAR;
89
3.98k
      TWO_REMAINING:
90
3.98k
        p++;
91
3.98k
        CONTINUATION_CHAR;
92
3.84k
        p++;
93
3.84k
        CONTINUATION_CHAR;
94
95
3.79k
        if ( (val < min))
96
31
    goto error;
97
98
3.76k
        if ( (!UNICODE_VALID(val)))
99
153
    goto error;
100
3.76k
      }
101
102
4.80k
    continue;
103
104
4.80k
  error:
105
955
    return NULL;
106
5.75k
  }
107
167k
    }
108
109
26.6k
  return str;
110
27.6k
}