Coverage Report

Created: 2025-07-23 06:43

/src/libidn2/lib/tr46map.c
Line
Count
Source (jump to first uncovered line)
1
/* tr46map.c - implementation of IDNA2008 TR46 functions
2
   Copyright (C) 2016-2017 Tim Rühsen
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 <stdint.h>
32
#include <stdlib.h>   /* bsearch */
33
#include <string.h>   /* memset */
34
35
#include "tr46map.h"
36
#include "tr46map_data.c"
37
38
39.5k
#define countof(a) (sizeof(a)/sizeof(*(a)))
39
40
static void
41
_fill_map (uint32_t c, const uint8_t *p, IDNAMap *map)
42
88.3M
{
43
88.3M
  uint32_t value;
44
45
88.3M
  if (c <= 0xFF)
46
87.0M
    {
47
87.0M
      map->cp1 = *p++;
48
87.0M
      map->range = *p++;
49
87.0M
    }
50
1.34M
  else if (c <= 0xFFFF)
51
1.26M
    {
52
1.26M
      map->cp1 = (p[0] << 8) | p[1];
53
1.26M
      map->range = (p[2] << 8) | p[3];
54
1.26M
      p += 4;
55
1.26M
    }
56
81.9k
  else
57
81.9k
    {
58
81.9k
      map->cp1 = (p[0] << 16) | (p[1] << 8) | p[2];
59
81.9k
      map->range = (p[3] << 8) | p[4];
60
81.9k
      p += 5;
61
81.9k
    }
62
63
88.3M
  value = (p[0] << 16) | (p[1] << 8) | p[2];
64
65
  /* deconstruct value, construction was
66
   *   value = (((map->nmappings << 14) | map->offset) << 3) | map->flag_index; */
67
88.3M
  map->flag_index = value & 0x7;
68
88.3M
  map->offset = (value >> 3) & 0x3FFF;
69
88.3M
  map->nmappings = (value >> 17) & 0x1F;
70
88.3M
}
71
72
static int
73
_compare_idna_map (const uint32_t *c, const uint8_t *p)
74
77.0M
{
75
77.0M
  IDNAMap map;
76
77
77.0M
  _fill_map (*c, p, &map);
78
79
77.0M
  if (*c < map.cp1)
80
57.9M
    return -1;
81
19.0M
  if (*c > map.cp1 + map.range)
82
7.66M
    return 1;
83
11.3M
  return 0;
84
19.0M
}
85
86
/*
87
static int
88
_compare_idna_map(uint32_t *c, IDNAMap *m2)
89
{
90
  if (*c < m2->cp1)
91
    return -1;
92
  if (*c > m2->cp1 + m2->range)
93
    return 1;
94
  return 0;
95
}
96
97
IDNAMap
98
*get_idna_map(uint32_t c)
99
{
100
  return bsearch(&c, idna_map, countof(idna_map), sizeof(IDNAMap), (int(*)(const void *, const void *))_compare_idna_map);
101
}
102
*/
103
104
int
105
get_idna_map (uint32_t c, IDNAMap *map)
106
11.3M
{
107
11.3M
  uint8_t *p;
108
109
11.3M
  if (c <= 0xFF)
110
11.2M
    p =
111
11.2M
      (uint8_t *) bsearch (&c, idna_map_8, sizeof (idna_map_8) / 5, 5,
112
11.2M
         (int (*)(const void *, const void *))
113
11.2M
         _compare_idna_map);
114
103k
  else if (c <= 0xFFFF)
115
96.6k
    p =
116
96.6k
      (uint8_t *) bsearch (&c, idna_map_16, sizeof (idna_map_16) / 7, 7,
117
96.6k
         (int (*)(const void *, const void *))
118
96.6k
         _compare_idna_map);
119
6.58k
  else if (c <= 0xFFFFFF)
120
6.58k
    p =
121
6.58k
      (uint8_t *) bsearch (&c, idna_map_24, sizeof (idna_map_24) / 8, 8,
122
6.58k
         (int (*)(const void *, const void *))
123
6.58k
         _compare_idna_map);
124
0
  else
125
0
    p = NULL;
126
127
11.3M
  if (!p)
128
0
    {
129
0
      memset (map, 0, sizeof (IDNAMap));
130
0
      return -1;
131
0
    }
132
133
11.3M
  _fill_map (c, p, map);
134
11.3M
  return 0;
135
11.3M
}
136
137
int
138
map_is (const IDNAMap *map, unsigned flags)
139
47.3M
{
140
47.3M
  return (idna_flags[map->flag_index] & flags) == flags;
141
47.3M
}
142
143
static int G_GNUC_IDN2_ATTRIBUTE_PURE
144
_compare_nfcqc_map (uint32_t *c, NFCQCMap *m2)
145
260k
{
146
260k
  if (*c < m2->cp1)
147
140k
    return -1;
148
120k
  if (*c > m2->cp2)
149
119k
    return 1;
150
947
  return 0;
151
120k
}
152
153
NFCQCMap *
154
get_nfcqc_map (uint32_t c)
155
39.5k
{
156
39.5k
  return (NFCQCMap *) bsearch (&c, nfcqc_map, countof (nfcqc_map),
157
39.5k
             sizeof (NFCQCMap),
158
39.5k
             (int (*)(const void *, const void *))
159
39.5k
             _compare_nfcqc_map);
160
39.5k
}
161
162
/* copy 'n' codepoints from mapdata stream */
163
int
164
get_map_data (uint32_t *dst, const IDNAMap *map)
165
25.8k
{
166
25.8k
  int n = map->nmappings;
167
25.8k
  const uint8_t *src = mapdata + map->offset;
168
169
70.4k
  for (; n > 0; n--)
170
44.5k
    {
171
44.5k
      uint32_t cp = 0;
172
44.5k
      do
173
73.8k
  cp = (cp << 7) | (*src & 0x7F);
174
73.8k
      while (*src++ & 0x80);
175
44.5k
      *dst++ = cp;
176
44.5k
    }
177
178
25.8k
  return map->nmappings;
179
25.8k
}