Coverage Report

Created: 2026-08-13 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libidn2/lib/context.c
Line
Count
Source
1
/* context.c - check contextual rule on label
2
   Copyright (C) 2011-2026 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
#include "tables.h"
33
#include <unictype.h>   /* uc_combining_class, UC_CCC_VR */
34
#include "context.h"
35
36
int
37
_idn2_contextj_rule (const uint32_t *label, size_t llen, size_t pos)
38
7.49k
{
39
7.49k
  uint32_t cp;
40
41
7.49k
  if (llen == 0)
42
0
    return IDN2_OK;
43
44
7.49k
  cp = label[pos];
45
46
7.49k
  if (!_idn2_contextj_p (cp))
47
7.22k
    return IDN2_OK;
48
49
270
  switch (cp)
50
270
    {
51
229
    case 0x200C:    /* ZERO WIDTH NON-JOINER */
52
229
      if (pos > 0)
53
228
  {
54
    /* If Canonical_Combining_Class(Before(cp)) .eq.  Virama Then True; */
55
228
    uint32_t before_cp = label[pos - 1];
56
228
    int cc = uc_combining_class (before_cp);
57
228
    if (cc == UC_CCC_VR)
58
35
      return IDN2_OK;
59
228
  }
60
61
      /* See http://permalink.gmane.org/gmane.ietf.idnabis/6980 for
62
         clarified rule. */
63
64
194
      if (pos == 0 || pos == llen - 1)
65
15
  return IDN2_CONTEXTJ;
66
67
179
      {
68
179
  int jt;
69
179
  size_t tmp;
70
71
  /* Search backwards. */
72
183
  for (tmp = pos - 1;; tmp--)
73
362
    {
74
362
      jt = uc_joining_type (label[tmp]);
75
362
      if (jt == UC_JOINING_TYPE_L || jt == UC_JOINING_TYPE_D)
76
111
        break;
77
251
      if (tmp == 0)
78
55
        return IDN2_CONTEXTJ;
79
196
      if (jt == UC_JOINING_TYPE_T)
80
183
        continue;
81
13
      return IDN2_CONTEXTJ;
82
196
    }
83
84
  /* Search forward. */
85
261
  for (tmp = pos + 1; tmp < llen; tmp++)
86
261
    {
87
261
      jt = uc_joining_type (label[tmp]);
88
261
      if (jt == UC_JOINING_TYPE_R || jt == UC_JOINING_TYPE_D)
89
90
        break;
90
171
      if (tmp == llen - 1)
91
17
        return IDN2_CONTEXTJ;
92
154
      if (jt == UC_JOINING_TYPE_T)
93
150
        continue;
94
4
      return IDN2_CONTEXTJ;
95
154
    }
96
111
      }
97
98
90
      return IDN2_OK;
99
0
      break;
100
101
41
    case 0x200D:    /* ZERO WIDTH JOINER */
102
41
      if (pos > 0)
103
40
  {
104
40
    uint32_t before_cp = label[pos - 1];
105
40
    int cc = uc_combining_class (before_cp);
106
40
    if (cc == UC_CCC_VR)
107
34
      return IDN2_OK;
108
40
  }
109
7
      return IDN2_CONTEXTJ;
110
270
    }
111
112
0
  return IDN2_CONTEXTJ_NO_RULE;
113
270
}
114
115
static const char *
116
_uc_script_name (ucs4_t uc)
117
18.2k
{
118
18.2k
  const uc_script_t *ucs = uc_script (uc);
119
120
18.2k
  if (!ucs)
121
1.94k
    return "";
122
123
16.3k
  return ucs->name;
124
18.2k
}
125
126
int
127
_idn2_contexto_rule (const uint32_t *label, size_t llen, size_t pos)
128
6.63k
{
129
6.63k
  uint32_t cp = label[pos];
130
131
6.63k
  if (!_idn2_contexto_p (cp))
132
3.98k
    return IDN2_OK;
133
134
2.65k
  switch (cp)
135
2.65k
    {
136
101
    case 0x00B7:
137
      /* MIDDLE DOT */
138
101
      if (llen < 3)
139
1
  return IDN2_CONTEXTO;
140
100
      if (pos == 0 || pos == llen - 1)
141
10
  return IDN2_CONTEXTO;
142
90
      if (label[pos - 1] == 0x006C && label[pos + 1] == 0x006C)
143
46
  return IDN2_OK;
144
44
      return IDN2_CONTEXTO;
145
0
      break;
146
147
91
    case 0x0375:
148
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
149
91
      if (pos == llen - 1)
150
8
  return IDN2_CONTEXTO;
151
83
      if (strcmp (_uc_script_name (label[pos + 1]), "Greek") == 0)
152
67
  return IDN2_OK;
153
16
      return IDN2_CONTEXTO;
154
0
      break;
155
156
138
    case 0x05F3:
157
      /* HEBREW PUNCTUATION GERESH */
158
214
    case 0x05F4:
159
      /* HEBREW PUNCTUATION GERSHAYIM */
160
214
      if (pos == 0)
161
2
  return IDN2_CONTEXTO;
162
212
      if (strcmp (_uc_script_name (label[pos - 1]), "Hebrew") == 0)
163
200
  return IDN2_OK;
164
12
      return IDN2_CONTEXTO;
165
0
      break;
166
167
81
    case 0x0660:
168
192
    case 0x0661:
169
286
    case 0x0662:
170
395
    case 0x0663:
171
487
    case 0x0664:
172
583
    case 0x0665:
173
661
    case 0x0666:
174
742
    case 0x0667:
175
823
    case 0x0668:
176
900
    case 0x0669:
177
900
      {
178
  /* ARABIC-INDIC DIGITS */
179
900
  size_t i;
180
20.2k
  for (i = 0; i < llen; i++)
181
19.3k
    if (label[i] >= 0x6F0 && label[i] <= 0x06F9)
182
5
      return IDN2_CONTEXTO;
183
895
  return IDN2_OK;
184
0
  break;
185
900
      }
186
187
134
    case 0x06F0:
188
253
    case 0x06F1:
189
358
    case 0x06F2:
190
487
    case 0x06F3:
191
576
    case 0x06F4:
192
647
    case 0x06F5:
193
724
    case 0x06F6:
194
803
    case 0x06F7:
195
874
    case 0x06F8:
196
946
    case 0x06F9:
197
946
      {
198
  /* EXTENDED ARABIC-INDIC DIGITS */
199
946
  size_t i;
200
19.5k
  for (i = 0; i < llen; i++)
201
18.6k
    if (label[i] >= 0x660 && label[i] <= 0x0669)
202
4
      return IDN2_CONTEXTO;
203
942
  return IDN2_OK;
204
0
  break;
205
946
      }
206
402
    case 0x30FB:
207
402
      {
208
  /* KATAKANA MIDDLE DOT */
209
402
  size_t i;
210
402
  bool script_ok = false;
211
212
6.50k
  for (i = 0; !script_ok && i < llen; i++)
213
6.10k
    if (strcmp (_uc_script_name (label[i]), "Hiragana") == 0
214
6.03k
        || strcmp (_uc_script_name (label[i]), "Katakana") == 0
215
5.86k
        || strcmp (_uc_script_name (label[i]), "Han") == 0)
216
358
      script_ok = true;
217
218
402
  if (script_ok)
219
358
    return IDN2_OK;
220
44
  return IDN2_CONTEXTO;
221
0
  break;
222
402
      }
223
2.65k
    }
224
225
0
  return IDN2_CONTEXTO_NO_RULE;
226
2.65k
}
227
228
bool
229
_idn2_contexto_with_rule (uint32_t cp)
230
0
{
231
0
  switch (cp)
232
0
    {
233
0
    case 0x00B7:
234
      /* MIDDLE DOT */
235
0
    case 0x0375:
236
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
237
0
    case 0x05F3:
238
      /* HEBREW PUNCTUATION GERESH */
239
0
    case 0x05F4:
240
      /* HEBREW PUNCTUATION GERSHAYIM */
241
0
    case 0x0660:
242
0
    case 0x0661:
243
0
    case 0x0662:
244
0
    case 0x0663:
245
0
    case 0x0664:
246
0
    case 0x0665:
247
0
    case 0x0666:
248
0
    case 0x0667:
249
0
    case 0x0668:
250
0
    case 0x0669:
251
      /* ARABIC-INDIC DIGITS */
252
0
    case 0x06F0:
253
0
    case 0x06F1:
254
0
    case 0x06F2:
255
0
    case 0x06F3:
256
0
    case 0x06F4:
257
0
    case 0x06F5:
258
0
    case 0x06F6:
259
0
    case 0x06F7:
260
0
    case 0x06F8:
261
0
    case 0x06F9:
262
      /* EXTENDED ARABIC-INDIC DIGITS */
263
0
    case 0x30FB:
264
      /* KATAKANA MIDDLE DOT */
265
0
      return true;
266
0
      break;
267
0
    }
268
269
0
  return false;
270
0
}