Coverage Report

Created: 2026-11-12 07:00

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-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
#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
24.9k
{
39
24.9k
  uint32_t cp;
40
41
24.9k
  if (llen == 0)
42
0
    return IDN2_OK;
43
44
24.9k
  cp = label[pos];
45
46
24.9k
  if (!_idn2_contextj_p (cp))
47
24.4k
    return IDN2_OK;
48
49
471
  switch (cp)
50
471
    {
51
393
    case 0x200C:    /* ZERO WIDTH NON-JOINER */
52
393
      if (pos > 0)
53
391
  {
54
    /* If Canonical_Combining_Class(Before(cp)) .eq.  Virama Then True; */
55
391
    uint32_t before_cp = label[pos - 1];
56
391
    int cc = uc_combining_class (before_cp);
57
391
    if (cc == UC_CCC_VR)
58
66
      return IDN2_OK;
59
391
  }
60
61
      /* See http://permalink.gmane.org/gmane.ietf.idnabis/6980 for
62
         clarified rule. */
63
64
327
      if (pos == 0 || pos == llen - 1)
65
20
  return IDN2_CONTEXTJ;
66
67
307
      {
68
307
  int jt;
69
307
  size_t tmp;
70
71
  /* Search backwards. */
72
307
  for (tmp = pos - 1;; tmp--)
73
570
    {
74
570
      jt = uc_joining_type (label[tmp]);
75
570
      if (jt == UC_JOINING_TYPE_L || jt == UC_JOINING_TYPE_D)
76
253
        break;
77
317
      if (tmp == 0)
78
50
        return IDN2_CONTEXTJ;
79
267
      if (jt == UC_JOINING_TYPE_T)
80
263
        continue;
81
4
      return IDN2_CONTEXTJ;
82
267
    }
83
84
  /* Search forward. */
85
811
  for (tmp = pos + 1; tmp < llen; tmp++)
86
811
    {
87
811
      jt = uc_joining_type (label[tmp]);
88
811
      if (jt == UC_JOINING_TYPE_R || jt == UC_JOINING_TYPE_D)
89
212
        break;
90
599
      if (tmp == llen - 1)
91
39
        return IDN2_CONTEXTJ;
92
560
      if (jt == UC_JOINING_TYPE_T)
93
558
        continue;
94
2
      return IDN2_CONTEXTJ;
95
560
    }
96
253
      }
97
98
212
      return IDN2_OK;
99
0
      break;
100
101
78
    case 0x200D:    /* ZERO WIDTH JOINER */
102
78
      if (pos > 0)
103
77
  {
104
77
    uint32_t before_cp = label[pos - 1];
105
77
    int cc = uc_combining_class (before_cp);
106
77
    if (cc == UC_CCC_VR)
107
66
      return IDN2_OK;
108
77
  }
109
12
      return IDN2_CONTEXTJ;
110
471
    }
111
112
0
  return IDN2_CONTEXTJ_NO_RULE;
113
471
}
114
115
static const char *
116
_uc_script_name (ucs4_t uc)
117
0
{
118
0
  const uc_script_t *ucs = uc_script (uc);
119
120
0
  if (!ucs)
121
0
    return "";
122
123
0
  return ucs->name;
124
0
}
125
126
int
127
_idn2_contexto_rule (const uint32_t *label, size_t llen, size_t pos)
128
0
{
129
0
  uint32_t cp = label[pos];
130
131
0
  if (!_idn2_contexto_p (cp))
132
0
    return IDN2_OK;
133
134
0
  switch (cp)
135
0
    {
136
0
    case 0x00B7:
137
      /* MIDDLE DOT */
138
0
      if (llen < 3)
139
0
  return IDN2_CONTEXTO;
140
0
      if (pos == 0 || pos == llen - 1)
141
0
  return IDN2_CONTEXTO;
142
0
      if (label[pos - 1] == 0x006C && label[pos + 1] == 0x006C)
143
0
  return IDN2_OK;
144
0
      return IDN2_CONTEXTO;
145
0
      break;
146
147
0
    case 0x0375:
148
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
149
0
      if (pos == llen - 1)
150
0
  return IDN2_CONTEXTO;
151
0
      if (strcmp (_uc_script_name (label[pos + 1]), "Greek") == 0)
152
0
  return IDN2_OK;
153
0
      return IDN2_CONTEXTO;
154
0
      break;
155
156
0
    case 0x05F3:
157
      /* HEBREW PUNCTUATION GERESH */
158
0
    case 0x05F4:
159
      /* HEBREW PUNCTUATION GERSHAYIM */
160
0
      if (pos == 0)
161
0
  return IDN2_CONTEXTO;
162
0
      if (strcmp (_uc_script_name (label[pos - 1]), "Hebrew") == 0)
163
0
  return IDN2_OK;
164
0
      return IDN2_CONTEXTO;
165
0
      break;
166
167
0
    case 0x0660:
168
0
    case 0x0661:
169
0
    case 0x0662:
170
0
    case 0x0663:
171
0
    case 0x0664:
172
0
    case 0x0665:
173
0
    case 0x0666:
174
0
    case 0x0667:
175
0
    case 0x0668:
176
0
    case 0x0669:
177
0
      {
178
  /* ARABIC-INDIC DIGITS */
179
0
  size_t i;
180
0
  for (i = 0; i < llen; i++)
181
0
    if (label[i] >= 0x6F0 && label[i] <= 0x06F9)
182
0
      return IDN2_CONTEXTO;
183
0
  return IDN2_OK;
184
0
  break;
185
0
      }
186
187
0
    case 0x06F0:
188
0
    case 0x06F1:
189
0
    case 0x06F2:
190
0
    case 0x06F3:
191
0
    case 0x06F4:
192
0
    case 0x06F5:
193
0
    case 0x06F6:
194
0
    case 0x06F7:
195
0
    case 0x06F8:
196
0
    case 0x06F9:
197
0
      {
198
  /* EXTENDED ARABIC-INDIC DIGITS */
199
0
  size_t i;
200
0
  for (i = 0; i < llen; i++)
201
0
    if (label[i] >= 0x660 && label[i] <= 0x0669)
202
0
      return IDN2_CONTEXTO;
203
0
  return IDN2_OK;
204
0
  break;
205
0
      }
206
0
    case 0x30FB:
207
0
      {
208
  /* KATAKANA MIDDLE DOT */
209
0
  size_t i;
210
0
  bool script_ok = false;
211
212
0
  for (i = 0; !script_ok && i < llen; i++)
213
0
    if (strcmp (_uc_script_name (label[i]), "Hiragana") == 0
214
0
        || strcmp (_uc_script_name (label[i]), "Katakana") == 0
215
0
        || strcmp (_uc_script_name (label[i]), "Han") == 0)
216
0
      script_ok = true;
217
218
0
  if (script_ok)
219
0
    return IDN2_OK;
220
0
  return IDN2_CONTEXTO;
221
0
  break;
222
0
      }
223
0
    }
224
225
0
  return IDN2_CONTEXTO_NO_RULE;
226
0
}
227
228
bool
229
_idn2_contexto_with_rule (uint32_t cp)
230
6.12k
{
231
6.12k
  switch (cp)
232
6.12k
    {
233
488
    case 0x00B7:
234
      /* MIDDLE DOT */
235
780
    case 0x0375:
236
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
237
997
    case 0x05F3:
238
      /* HEBREW PUNCTUATION GERESH */
239
1.24k
    case 0x05F4:
240
      /* HEBREW PUNCTUATION GERSHAYIM */
241
1.46k
    case 0x0660:
242
1.66k
    case 0x0661:
243
1.87k
    case 0x0662:
244
2.08k
    case 0x0663:
245
2.29k
    case 0x0664:
246
2.49k
    case 0x0665:
247
2.72k
    case 0x0666:
248
3.01k
    case 0x0667:
249
3.27k
    case 0x0668:
250
3.49k
    case 0x0669:
251
      /* ARABIC-INDIC DIGITS */
252
3.69k
    case 0x06F0:
253
3.89k
    case 0x06F1:
254
4.09k
    case 0x06F2:
255
4.47k
    case 0x06F3:
256
4.71k
    case 0x06F4:
257
4.93k
    case 0x06F5:
258
5.16k
    case 0x06F6:
259
5.49k
    case 0x06F7:
260
5.73k
    case 0x06F8:
261
5.93k
    case 0x06F9:
262
      /* EXTENDED ARABIC-INDIC DIGITS */
263
6.12k
    case 0x30FB:
264
      /* KATAKANA MIDDLE DOT */
265
6.12k
      return true;
266
0
      break;
267
6.12k
    }
268
269
0
  return false;
270
6.12k
}