Coverage Report

Created: 2026-03-31 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/unistring/uninorm/canonical-decomposition.c
Line
Count
Source
1
/* Canonical decomposition of Unicode characters.
2
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2009.
4
5
   This file is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as
7
   published by the Free Software Foundation; either version 2.1 of the
8
   License, or (at your option) any later version.
9
10
   This file is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17
18
#include <config.h>
19
20
/* Specification.  */
21
#include "uninorm.h"
22
23
#include <stdlib.h>
24
25
#include "uninorm/decomposition-table.h"
26
27
int
28
uc_canonical_decomposition (ucs4_t uc, ucs4_t *decomposition)
29
0
{
30
0
  if (uc >= 0xAC00 && uc < 0xD7A4)
31
0
    {
32
      /* Hangul syllable.  See Unicode standard, chapter 3, section
33
         "Hangul Syllable Decomposition",  See also the clarification at
34
         <https://www.unicode.org/versions/Unicode5.1.0/>, section
35
         "Clarification of Hangul Jamo Handling".  */
36
0
      uc -= 0xAC00;
37
0
      unsigned int t = uc % 28;
38
39
0
      if (t == 0)
40
0
        {
41
0
          uc = uc / 28;
42
0
          unsigned int v = uc % 21;
43
0
          unsigned int l = uc / 21;
44
45
0
          decomposition[0] = 0x1100 + l;
46
0
          decomposition[1] = 0x1161 + v;
47
0
          return 2;
48
0
        }
49
0
      else
50
0
        {
51
0
#if 1 /* Return the pairwise decomposition, not the full decomposition.  */
52
0
          decomposition[0] = 0xAC00 + uc - t; /* = 0xAC00 + (l * 21 + v) * 28; */
53
0
          decomposition[1] = 0x11A7 + t;
54
0
          return 2;
55
#else
56
          uc = uc / 28;
57
          unsigned int v = uc % 21;
58
          unsigned int l = uc / 21;
59
60
          decomposition[0] = 0x1100 + l;
61
          decomposition[1] = 0x1161 + v;
62
          decomposition[2] = 0x11A7 + t;
63
          return 3;
64
#endif
65
0
        }
66
0
    }
67
0
  else if (uc < 0x110000)
68
0
    {
69
0
      unsigned short entry = decomp_index (uc);
70
      /* An entry of (unsigned short)(-1) denotes an absent entry.
71
         Otherwise, bit 15 of the entry tells whether the decomposition
72
         is a canonical one.  */
73
0
      if (entry < 0x8000)
74
0
        {
75
0
          const unsigned char *p = &gl_uninorm_decomp_chars_table[3 * entry];
76
0
          unsigned int element = (p[0] << 16) | (p[1] << 8) | p[2];
77
          /* The first element has 5 bits for the decomposition type.  */
78
0
          if (((element >> 18) & 0x1f) != UC_DECOMP_CANONICAL)
79
0
            abort ();
80
0
          unsigned int length = 1;
81
0
          for (;;)
82
0
            {
83
              /* Every element has an 18 bits wide Unicode code point.  */
84
0
              *decomposition = element & 0x3ffff;
85
              /* Bit 23 tells whether there are more elements,  */
86
0
              if ((element & (1 << 23)) == 0)
87
0
                break;
88
0
              p += 3;
89
0
              element = (p[0] << 16) | (p[1] << 8) | p[2];
90
0
              decomposition++;
91
0
              length++;
92
0
            }
93
0
          return length;
94
0
        }
95
0
    }
96
0
  return -1;
97
0
}