File: context.c

package info (click to toggle)
libidn2 2.3.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,768 kB
  • sloc: ansic: 6,049; sh: 1,480; makefile: 499; xml: 50; perl: 15
file content (270 lines) | stat: -rw-r--r-- 5,861 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* context.c - check contextual rule on label
   Copyright (C) 2011-2025 Simon Josefsson

   Libidn2 is free software: you can redistribute it and/or modify it
   under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version.

   or both in parallel, as here.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include "idn2.h"
#include "tables.h"
#include <unictype.h>		/* uc_combining_class, UC_CCC_VR */
#include "context.h"

int
_idn2_contextj_rule (const uint32_t *label, size_t llen, size_t pos)
{
  uint32_t cp;

  if (llen == 0)
    return IDN2_OK;

  cp = label[pos];

  if (!_idn2_contextj_p (cp))
    return IDN2_OK;

  switch (cp)
    {
    case 0x200C:		/* ZERO WIDTH NON-JOINER */
      if (pos > 0)
	{
	  /* If Canonical_Combining_Class(Before(cp)) .eq.  Virama Then True; */
	  uint32_t before_cp = label[pos - 1];
	  int cc = uc_combining_class (before_cp);
	  if (cc == UC_CCC_VR)
	    return IDN2_OK;
	}

      /* See http://permalink.gmane.org/gmane.ietf.idnabis/6980 for
         clarified rule. */

      if (pos == 0 || pos == llen - 1)
	return IDN2_CONTEXTJ;

      {
	int jt;
	size_t tmp;

	/* Search backwards. */
	for (tmp = pos - 1;; tmp--)
	  {
	    jt = uc_joining_type (label[tmp]);
	    if (jt == UC_JOINING_TYPE_L || jt == UC_JOINING_TYPE_D)
	      break;
	    if (tmp == 0)
	      return IDN2_CONTEXTJ;
	    if (jt == UC_JOINING_TYPE_T)
	      continue;
	    return IDN2_CONTEXTJ;
	  }

	/* Search forward. */
	for (tmp = pos + 1; tmp < llen; tmp++)
	  {
	    jt = uc_joining_type (label[tmp]);
	    if (jt == UC_JOINING_TYPE_R || jt == UC_JOINING_TYPE_D)
	      break;
	    if (tmp == llen - 1)
	      return IDN2_CONTEXTJ;
	    if (jt == UC_JOINING_TYPE_T)
	      continue;
	    return IDN2_CONTEXTJ;
	  }
      }

      return IDN2_OK;
      break;

    case 0x200D:		/* ZERO WIDTH JOINER */
      if (pos > 0)
	{
	  uint32_t before_cp = label[pos - 1];
	  int cc = uc_combining_class (before_cp);
	  if (cc == UC_CCC_VR)
	    return IDN2_OK;
	}
      return IDN2_CONTEXTJ;
    }

  return IDN2_CONTEXTJ_NO_RULE;
}

static const char *
_uc_script_name (ucs4_t uc)
{
  const uc_script_t *ucs = uc_script (uc);

  if (!ucs)
    return "";

  return ucs->name;
}

int
_idn2_contexto_rule (const uint32_t *label, size_t llen, size_t pos)
{
  uint32_t cp = label[pos];

  if (!_idn2_contexto_p (cp))
    return IDN2_OK;

  switch (cp)
    {
    case 0x00B7:
      /* MIDDLE DOT */
      if (llen < 3)
	return IDN2_CONTEXTO;
      if (pos == 0 || pos == llen - 1)
	return IDN2_CONTEXTO;
      if (label[pos - 1] == 0x006C && label[pos + 1] == 0x006C)
	return IDN2_OK;
      return IDN2_CONTEXTO;
      break;

    case 0x0375:
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
      if (pos == llen - 1)
	return IDN2_CONTEXTO;
      if (strcmp (_uc_script_name (label[pos + 1]), "Greek") == 0)
	return IDN2_OK;
      return IDN2_CONTEXTO;
      break;

    case 0x05F3:
      /* HEBREW PUNCTUATION GERESH */
    case 0x05F4:
      /* HEBREW PUNCTUATION GERSHAYIM */
      if (pos == 0)
	return IDN2_CONTEXTO;
      if (strcmp (_uc_script_name (label[pos - 1]), "Hebrew") == 0)
	return IDN2_OK;
      return IDN2_CONTEXTO;
      break;

    case 0x0660:
    case 0x0661:
    case 0x0662:
    case 0x0663:
    case 0x0664:
    case 0x0665:
    case 0x0666:
    case 0x0667:
    case 0x0668:
    case 0x0669:
      {
	/* ARABIC-INDIC DIGITS */
	size_t i;
	for (i = 0; i < llen; i++)
	  if (label[i] >= 0x6F0 && label[i] <= 0x06F9)
	    return IDN2_CONTEXTO;
	return IDN2_OK;
	break;
      }

    case 0x06F0:
    case 0x06F1:
    case 0x06F2:
    case 0x06F3:
    case 0x06F4:
    case 0x06F5:
    case 0x06F6:
    case 0x06F7:
    case 0x06F8:
    case 0x06F9:
      {
	/* EXTENDED ARABIC-INDIC DIGITS */
	size_t i;
	for (i = 0; i < llen; i++)
	  if (label[i] >= 0x660 && label[i] <= 0x0669)
	    return IDN2_CONTEXTO;
	return IDN2_OK;
	break;
      }
    case 0x30FB:
      {
	/* KATAKANA MIDDLE DOT */
	size_t i;
	bool script_ok = false;

	for (i = 0; !script_ok && i < llen; i++)
	  if (strcmp (_uc_script_name (label[i]), "Hiragana") == 0
	      || strcmp (_uc_script_name (label[i]), "Katakana") == 0
	      || strcmp (_uc_script_name (label[i]), "Han") == 0)
	    script_ok = true;

	if (script_ok)
	  return IDN2_OK;
	return IDN2_CONTEXTO;
	break;
      }
    }

  return IDN2_CONTEXTO_NO_RULE;
}

bool
_idn2_contexto_with_rule (uint32_t cp)
{
  switch (cp)
    {
    case 0x00B7:
      /* MIDDLE DOT */
    case 0x0375:
      /* GREEK LOWER NUMERAL SIGN (KERAIA) */
    case 0x05F3:
      /* HEBREW PUNCTUATION GERESH */
    case 0x05F4:
      /* HEBREW PUNCTUATION GERSHAYIM */
    case 0x0660:
    case 0x0661:
    case 0x0662:
    case 0x0663:
    case 0x0664:
    case 0x0665:
    case 0x0666:
    case 0x0667:
    case 0x0668:
    case 0x0669:
      /* ARABIC-INDIC DIGITS */
    case 0x06F0:
    case 0x06F1:
    case 0x06F2:
    case 0x06F3:
    case 0x06F4:
    case 0x06F5:
    case 0x06F6:
    case 0x06F7:
    case 0x06F8:
    case 0x06F9:
      /* EXTENDED ARABIC-INDIC DIGITS */
    case 0x30FB:
      /* KATAKANA MIDDLE DOT */
      return true;
      break;
    }

  return false;
}