Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/unicode-transform.h
Line
Count
Source
1
#ifndef UNICODE_NF_H
2
#define UNICODE_NF_H
3
4
#include "unicode-data-tables.h"
5
6
0
#define UNICODE_NF_STREAM_SAFE_NON_STARTER_LEN 30
7
/* Space for a starter, the non-starters after it, a CGJ and the longest
8
   decomposition */
9
#define UNICODE_NF_BUFFER_SIZE \
10
0
  (1 + UNICODE_NF_STREAM_SAFE_NON_STARTER_LEN + 1 + \
11
0
   UNICODE_DECOMPOSITION_MAX_LENGTH)
12
13
struct unicode_code_point_data;
14
15
/*
16
 * Transform API
17
 */
18
19
struct unicode_transform;
20
21
struct unicode_transform_buffer {
22
  const uint32_t *cp;
23
  const struct unicode_code_point_data *const *cp_data;
24
  size_t cp_count;
25
};
26
27
struct unicode_transform_def {
28
  ssize_t (*input)(struct unicode_transform *trans,
29
       const struct unicode_transform_buffer *buf,
30
       const char **error_r);
31
  int (*flush)(struct unicode_transform *trans, bool finished,
32
         const char **error_r);
33
};
34
35
struct unicode_transform {
36
  const struct unicode_transform_def *def;
37
  struct unicode_transform *next;
38
};
39
40
static inline void
41
unicode_transform_init(struct unicode_transform *trans,
42
           const struct unicode_transform_def *def)
43
0
{
44
0
  i_zero(trans);
45
0
  trans->def = def;
46
0
}
Unexecuted instantiation: unichar.c:unicode_transform_init
Unexecuted instantiation: unicode-transform.c:unicode_transform_init
47
48
static inline void
49
unicode_transform_chain(struct unicode_transform *trans,
50
      struct unicode_transform *next)
51
0
{
52
0
  i_assert(trans->next == NULL);
53
0
  trans->next = next;
54
0
}
Unexecuted instantiation: unichar.c:unicode_transform_chain
Unexecuted instantiation: unicode-transform.c:unicode_transform_chain
55
56
static inline struct unicode_transform *
57
unicode_transform_get_last(struct unicode_transform *trans)
58
0
{
59
0
  while (trans->next != NULL)
60
0
    trans = trans->next;
61
0
  return trans;
62
0
}
Unexecuted instantiation: unichar.c:unicode_transform_get_last
Unexecuted instantiation: unicode-transform.c:unicode_transform_get_last
63
64
ssize_t uniform_transform_forward(
65
  struct unicode_transform *trans, const uint32_t *out,
66
  const struct unicode_code_point_data *const *out_data, size_t out_len,
67
  const char **error_r);
68
69
ssize_t unicode_transform_input_buf(struct unicode_transform *trans,
70
            const struct unicode_transform_buffer *buf,
71
            const char **error_r);
72
static inline ssize_t
73
unicode_transform_input(struct unicode_transform *trans,
74
      const uint32_t *in, size_t in_len, const char **error_r)
75
0
{
76
0
  struct unicode_transform_buffer buf = {
77
0
    .cp = in,
78
0
    .cp_count = in_len,
79
0
  };
80
81
0
  return unicode_transform_input_buf(trans, &buf, error_r);
82
0
}
Unexecuted instantiation: unichar.c:unicode_transform_input
Unexecuted instantiation: unicode-transform.c:unicode_transform_input
83
84
int unicode_transform_flush(struct unicode_transform *trans,
85
          const char **error_r);
86
87
/* Buffer Sink */
88
89
struct unicode_buffer_sink {
90
  struct unicode_transform transform;
91
  buffer_t *buffer;
92
};
93
94
void unicode_buffer_sink_init(struct unicode_buffer_sink *sink,
95
            buffer_t *buffer);
96
97
/* Static Array Sink */
98
99
struct unicode_static_array_sink {
100
  struct unicode_transform transform;
101
  uint32_t *array;
102
  size_t array_size;
103
  size_t *array_pos;
104
};
105
106
void unicode_static_array_sink_init(struct unicode_static_array_sink *sink,
107
            uint32_t *array, size_t array_size,
108
            size_t *array_pos);
109
110
/*
111
 * NFD, NFKD, NFC, NFKC
112
 */
113
114
/* Unicode Standard Annex #15, Section 1.2:
115
116
   Unicode Normalization Forms are formally defined normalizations of Unicode
117
   strings which make it possible to determine whether any two Unicode strings
118
   are equivalent to each other. Depending on the particular Unicode
119
   Normalization Form, that equivalence can either be a canonical equivalence or
120
   a compatibility equivalence.
121
122
   Essentially, the Unicode Normalization Algorithm puts all combining marks in
123
   a specified order, and uses rules for decomposition and composition to
124
   transform each string into one of the Unicode Normalization Forms. A binary
125
   comparison of the transformed strings will then determine equivalence.
126
127
   The four Unicode Normalization Forms are summarized as follows:
128
129
     Normalization Form D  (NFD)   - Canonical Decomposition
130
     Normalization Form KD (NFKD)  - Compatibility Decomposition
131
     Normalization Form C  (NFC)   - Canonical Decomposition, followed by
132
                                     Canonical Composition
133
     Normalization Form KC (NFKC)  - Compatibility Decomposition, followed by
134
                                     Canonical Composition
135
136
   There are two forms of normalization that convert to composite characters:
137
   Normalization Form C and Normalization Form KC. The difference between these
138
   depends on whether the resulting text is to be a canonical equivalent to the
139
   original unnormalized text or a compatibility equivalent to the original
140
   unnormalized text. (In NFKC and NFKD, a K is used to stand for compatibility
141
   to avoid confusion with the C standing for composition.) Both types of
142
   normalization can be useful in different circumstances.
143
 */
144
145
enum unicode_nf_type {
146
  UNICODE_NFD,
147
  UNICODE_NFKD,
148
  UNICODE_NFC,
149
  UNICODE_NFKC,
150
};
151
152
struct unicode_nf_context {
153
  struct unicode_transform transform;
154
155
  size_t nonstarter_count;
156
  size_t buffer_len;
157
  /* Index after the last starter in the buffer. The non-starters after
158
     it may still be reordered. */
159
  size_t run_pos;
160
  /* NFC/NFKC: [0, composed_pos) has been composed */
161
  size_t composed_pos;
162
  /* NFC/NFKC: Index of the last starter in [0, composed_pos), if
163
     have_starter=TRUE. The following code points may compose with it. */
164
  size_t starter_pos;
165
  /* [0, buffer_output_max) is final and can be forwarded */
166
  size_t buffer_output_max;
167
  /* NFC/NFKC: Combining class of the last code point after starter_pos
168
     that didn't compose with it, or -1 if there is none. */
169
  int last_ccc;
170
171
  bool compose:1;
172
  bool canonical:1;
173
  bool have_starter:1;
174
175
  /* These are last, so they don't need to be zeroed at init */
176
  uint32_t cp_buffer[UNICODE_NF_BUFFER_SIZE];
177
  const struct unicode_code_point_data *
178
    cpd_buffer[UNICODE_NF_BUFFER_SIZE];
179
};
180
181
void unicode_nf_init(struct unicode_nf_context *ctx_r,
182
         enum unicode_nf_type type);
183
void unicode_nf_reset(struct unicode_nf_context *ctx);
184
185
/*
186
 * Normalization check
187
 */
188
189
struct unicode_nf_checker {
190
  const struct unicode_code_point_data *cpd_last;
191
  /* The last code point, if it's a starter with quick check Yes that
192
     wasn't given to the normalization transform (last_starter=TRUE) */
193
  uint32_t last_cp;
194
195
  uint8_t nf_qc_mask;
196
  uint8_t nf_qc_yes;
197
  uint8_t nf_qc_no;
198
199
  /* Code points given to the normalization transform that haven't been
200
     compared to its output yet */
201
  uint32_t cp_buffer[UNICODE_NF_BUFFER_SIZE];
202
  size_t buffer_len;
203
  struct unicode_nf_context nf;
204
  struct unicode_transform sink;
205
206
  bool compose:1;
207
  bool canonical:1;
208
  bool last_starter:1;
209
  /* The normalization transform has been given input since it was
210
     reset */
211
  bool nf_used:1;
212
};
213
214
void unicode_nf_checker_init(struct unicode_nf_checker *unc_r,
215
           enum unicode_nf_type type);
216
void unicode_nf_checker_reset(struct unicode_nf_checker *unc);
217
218
int unicode_nf_checker_input(struct unicode_nf_checker *unc, uint32_t cp,
219
           const struct unicode_code_point_data **cp_data);
220
int unicode_nf_checker_finish(struct unicode_nf_checker *unc);
221
222
/*
223
 * Casemap Transform
224
 */
225
226
/* Maximum number of mapped code points buffered by the casemap transform.
227
   Final_Sigma: If more case-ignorable code points follow a capital sigma
228
   than fit into the buffer after it, it's lowercased as final sigma. */
229
0
#define UNICODE_CASEMAP_BUFFER_SIZE 32
230
231
struct unicode_casemap {
232
  struct unicode_transform transform;
233
234
  size_t (*map)(const struct unicode_code_point_data *cp_data,
235
          const uint32_t **map_r);
236
237
  unsigned int buffer_len;
238
  /* Final_Sigma: Position of the pending sigma in cp_buffer */
239
  unsigned int sigma_pos;
240
241
  /* Lowercase a capital sigma to final sigma when it's preceded by a
242
     cased letter and not followed by one (Final_Sigma condition in
243
     SpecialCasing.txt) */
244
  bool final_sigma:1;
245
  /* Final_Sigma: The last code point that isn't case-ignorable was
246
     cased */
247
  bool preceded_by_cased:1;
248
  /* Final_Sigma: It's still unknown whether a cased letter follows the
249
     sigma at sigma_pos. It and the code points after it aren't
250
     forwarded until it's known. */
251
  bool sigma_pending:1;
252
253
  /* Mapped code points that haven't been forwarded yet. These are last,
254
     so they don't need to be zeroed at init. */
255
  uint32_t cp_buffer[UNICODE_CASEMAP_BUFFER_SIZE];
256
  const struct unicode_code_point_data *
257
    cpd_buffer[UNICODE_CASEMAP_BUFFER_SIZE];
258
};
259
260
void unicode_casemap_init_uppercase(struct unicode_casemap *map);
261
void unicode_casemap_init_lowercase(struct unicode_casemap *map);
262
/* Like unicode_casemap_init_lowercase(), but also handle the Final_Sigma
263
   condition like libicu's Any-Lower transliterator. A case-ignorable code
264
   point is skipped when checking the condition, even if it's also cased.
265
   The condition is checked separately for each string, which ends when the
266
   transform is flushed. */
267
void unicode_casemap_init_lowercase_final_sigma(struct unicode_casemap *map);
268
void unicode_casemap_init_casefold(struct unicode_casemap *map);
269
270
/*
271
 * RFC 5051 - Simple Unicode Collation Algorithm
272
 */
273
274
struct unicode_rfc5051_context {
275
  uint32_t buffer[3];
276
};
277
278
void unicode_rfc5051_init(struct unicode_rfc5051_context *ctx);
279
size_t unicode_rfc5051_normalize(struct unicode_rfc5051_context *ctx,
280
         uint32_t cp, const uint32_t **norm_r);
281
282
#endif