Coverage Report

Created: 2025-07-11 06:23

/src/libidn2/lib/bidi.c
Line
Count
Source (jump to first uncovered line)
1
/* bidi.c - IDNA right to left checking functions
2
   Copyright (C) 2011-2025 Simon Josefsson
3
4
   Libidn2 is free software: you can redistribute it and/or modify it
5
   under the terms of either:
6
7
     * the GNU Lesser General Public License as published by the Free
8
       Software Foundation; either version 3 of the License, or (at
9
       your option) any later version.
10
11
   or
12
13
     * the GNU General Public License as published by the Free
14
       Software Foundation; either version 2 of the License, or (at
15
       your option) any later version.
16
17
   or both in parallel, as here.
18
19
   This program is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
   GNU General Public License for more details.
23
24
   You should have received copies of the GNU General Public License and
25
   the GNU Lesser General Public License along with this program.  If
26
   not, see <http://www.gnu.org/licenses/>.
27
*/
28
29
#include <config.h>
30
31
#include "idn2.h"
32
33
#include <sys/types.h>
34
#include <stdbool.h>
35
36
#include "bidi.h"
37
38
#include <unictype.h>
39
40
static bool
41
_isBidi (const uint32_t *label, size_t llen)
42
2.37M
{
43
3.50M
  for (; (ssize_t) llen > 0; llen--)
44
2.50M
    {
45
2.50M
      int bc = uc_bidi_category (*label++);
46
47
2.50M
      if (bc == UC_BIDI_R || bc == UC_BIDI_AL || bc == UC_BIDI_AN)
48
1.37M
  return 1;
49
2.50M
    }
50
51
1.00M
  return 0;
52
2.37M
}
53
54
/* IDNA2008 BIDI check (RFC 5893) */
55
int
56
_idn2_bidi (const uint32_t *label, size_t llen)
57
2.37M
{
58
2.37M
  int bc;
59
2.37M
  int endok = 1;
60
61
2.37M
  if (!_isBidi (label, llen))
62
1.00M
    return IDN2_OK;
63
64
  // 2.1
65
1.37M
  switch ((bc = uc_bidi_category (*label)))
66
1.37M
    {
67
2.30k
    case UC_BIDI_L:
68
      // check 2.5 & 2.6
69
6.19k
      for (size_t it = 1; it < llen; it++)
70
6.19k
  {
71
6.19k
    bc = uc_bidi_category (label[it]);
72
73
6.19k
    if (bc == UC_BIDI_L || bc == UC_BIDI_EN || bc == UC_BIDI_NSM)
74
2.33k
      {
75
2.33k
        endok = 1;
76
2.33k
      }
77
3.85k
    else
78
3.85k
      {
79
3.85k
        if (bc != UC_BIDI_ES && bc != UC_BIDI_CS && bc != UC_BIDI_ET
80
3.85k
      && bc != UC_BIDI_ON && bc != UC_BIDI_BN)
81
2.30k
    {
82
      /* printf("LTR label contains invalid code point\n"); */
83
2.30k
      return IDN2_BIDI;
84
2.30k
    }
85
1.55k
        endok = 0;
86
1.55k
      }
87
6.19k
  }
88
      /* printf("LTR label ends with invalid code point\n"); */
89
0
      return endok ? IDN2_OK : IDN2_BIDI;
90
91
111k
    case UC_BIDI_R:
92
1.36M
    case UC_BIDI_AL:
93
      // check 2.2, 2.3, 2.4
94
      /* printf("Label[0]=%04X: %s\n", label[0], uc_bidi_category_name(bc)); */
95
1.52M
      for (size_t it = 1; it < llen; it++)
96
157k
  {
97
157k
    bc = uc_bidi_category (label[it]);
98
99
    /* printf("Label[%d]=%04X: %s\n", (int) it, label[it], uc_bidi_category_name(bc)); */
100
157k
    if (bc == UC_BIDI_R || bc == UC_BIDI_AL || bc == UC_BIDI_EN
101
157k
        || bc == UC_BIDI_AN || bc == UC_BIDI_NSM)
102
149k
      {
103
149k
        endok = 1;
104
149k
      }
105
8.02k
    else
106
8.02k
      {
107
8.02k
        if (bc != UC_BIDI_ES && bc != UC_BIDI_CS && bc != UC_BIDI_ET
108
8.02k
      && bc != UC_BIDI_ON && bc != UC_BIDI_BN)
109
2.63k
    {
110
      /* printf("RTL label contains invalid code point\n"); */
111
2.63k
      return IDN2_BIDI;
112
2.63k
    }
113
5.39k
        endok = 0;
114
5.39k
      }
115
157k
  }
116
      /* printf("RTL label ends with invalid code point\n"); */
117
1.36M
      return endok ? IDN2_OK : IDN2_BIDI;
118
119
5.84k
    default:
120
      /* printf("Label begins with invalid BIDI class %s\n", uc_bidi_category_name(bc)); */
121
5.84k
      return IDN2_BIDI;
122
1.37M
    }
123
1.37M
}