File: bidi.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 (123 lines) | stat: -rw-r--r-- 3,166 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
/* bidi.c - IDNA right to left checking functions
   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 <sys/types.h>
#include <stdbool.h>

#include "bidi.h"

#include <unictype.h>

static bool
_isBidi (const uint32_t *label, size_t llen)
{
  for (; (ssize_t) llen > 0; llen--)
    {
      int bc = uc_bidi_category (*label++);

      if (bc == UC_BIDI_R || bc == UC_BIDI_AL || bc == UC_BIDI_AN)
	return 1;
    }

  return 0;
}

/* IDNA2008 BIDI check (RFC 5893) */
int
_idn2_bidi (const uint32_t *label, size_t llen)
{
  int bc;
  int endok = 1;

  if (!_isBidi (label, llen))
    return IDN2_OK;

  // 2.1
  switch ((bc = uc_bidi_category (*label)))
    {
    case UC_BIDI_L:
      // check 2.5 & 2.6
      for (size_t it = 1; it < llen; it++)
	{
	  bc = uc_bidi_category (label[it]);

	  if (bc == UC_BIDI_L || bc == UC_BIDI_EN || bc == UC_BIDI_NSM)
	    {
	      endok = 1;
	    }
	  else
	    {
	      if (bc != UC_BIDI_ES && bc != UC_BIDI_CS && bc != UC_BIDI_ET
		  && bc != UC_BIDI_ON && bc != UC_BIDI_BN)
		{
		  /* printf("LTR label contains invalid code point\n"); */
		  return IDN2_BIDI;
		}
	      endok = 0;
	    }
	}
      /* printf("LTR label ends with invalid code point\n"); */
      return endok ? IDN2_OK : IDN2_BIDI;

    case UC_BIDI_R:
    case UC_BIDI_AL:
      // check 2.2, 2.3, 2.4
      /* printf("Label[0]=%04X: %s\n", label[0], uc_bidi_category_name(bc)); */
      for (size_t it = 1; it < llen; it++)
	{
	  bc = uc_bidi_category (label[it]);

	  /* printf("Label[%d]=%04X: %s\n", (int) it, label[it], uc_bidi_category_name(bc)); */
	  if (bc == UC_BIDI_R || bc == UC_BIDI_AL || bc == UC_BIDI_EN
	      || bc == UC_BIDI_AN || bc == UC_BIDI_NSM)
	    {
	      endok = 1;
	    }
	  else
	    {
	      if (bc != UC_BIDI_ES && bc != UC_BIDI_CS && bc != UC_BIDI_ET
		  && bc != UC_BIDI_ON && bc != UC_BIDI_BN)
		{
		  /* printf("RTL label contains invalid code point\n"); */
		  return IDN2_BIDI;
		}
	      endok = 0;
	    }
	}
      /* printf("RTL label ends with invalid code point\n"); */
      return endok ? IDN2_OK : IDN2_BIDI;

    default:
      /* printf("Label begins with invalid BIDI class %s\n", uc_bidi_category_name(bc)); */
      return IDN2_BIDI;
    }
}