1 | /* Copyright (C) 1996-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Written by Ulrich Drepper, <drepper@cygnus.com>. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _WEIGHT_H_ |
20 | #define _WEIGHT_H_ 1 |
21 | |
22 | #include <libc-diag.h> |
23 | |
24 | /* Find index of weight. */ |
25 | static inline int32_t __attribute__ ((always_inline)) |
26 | findidx (const int32_t *table, |
27 | const int32_t *indirect, |
28 | const unsigned char *, |
29 | const unsigned char **cpp, size_t len) |
30 | { |
31 | int_fast32_t i = table[*(*cpp)++]; |
32 | const unsigned char *cp; |
33 | const unsigned char *usrc; |
34 | |
35 | if (i >= 0) |
36 | /* This is an index into the weight table. Cool. */ |
37 | return i; |
38 | |
39 | /* Oh well, more than one sequence starting with this byte. |
40 | Search for the correct one. */ |
41 | cp = &extra[-i]; |
42 | usrc = *cpp; |
43 | --len; |
44 | while (1) |
45 | { |
46 | size_t nhere; |
47 | |
48 | /* The first thing is the index. */ |
49 | i = *((const int32_t *) cp); |
50 | cp += sizeof (int32_t); |
51 | |
52 | /* Next is the length of the byte sequence. These are always |
53 | short byte sequences so there is no reason to call any |
54 | function (even if they are inlined). */ |
55 | nhere = *cp++; |
56 | |
57 | if (i >= 0) |
58 | { |
59 | /* It is a single character. If it matches we found our |
60 | index. Note that at the end of each list there is an |
61 | entry of length zero which represents the single byte |
62 | sequence. The first (and here only) byte was tested |
63 | already. */ |
64 | size_t cnt; |
65 | |
66 | /* With GCC 5.3 when compiling with -Os the compiler warns |
67 | that seq2.back_us, which becomes usrc, might be used |
68 | uninitialized. This can't be true because we pass a length |
69 | of -1 for len at the same time which means that this loop |
70 | never executes. */ |
71 | DIAG_PUSH_NEEDS_COMMENT; |
72 | DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized" ); |
73 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
74 | if (cp[cnt] != usrc[cnt]) |
75 | break; |
76 | DIAG_POP_NEEDS_COMMENT; |
77 | |
78 | if (cnt == nhere) |
79 | { |
80 | /* Found it. */ |
81 | *cpp += nhere; |
82 | return i; |
83 | } |
84 | |
85 | /* Up to the next entry. */ |
86 | cp += nhere; |
87 | if (!LOCFILE_ALIGNED_P (1 + nhere)) |
88 | cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN; |
89 | } |
90 | else |
91 | { |
92 | /* This is a range of characters. First decide whether the |
93 | current byte sequence lies in the range. */ |
94 | size_t cnt; |
95 | size_t offset = 0; |
96 | |
97 | for (cnt = 0; cnt < nhere && cnt < len; ++cnt) |
98 | if (cp[cnt] != usrc[cnt]) |
99 | break; |
100 | |
101 | if (cnt != nhere) |
102 | { |
103 | if (cnt == len || cp[cnt] > usrc[cnt]) |
104 | { |
105 | /* Cannot be in this range. */ |
106 | cp += 2 * nhere; |
107 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
108 | cp += (LOCFILE_ALIGN |
109 | - (1 + 2 * nhere) % LOCFILE_ALIGN); |
110 | continue; |
111 | } |
112 | |
113 | /* Test against the end of the range. */ |
114 | for (cnt = 0; cnt < nhere; ++cnt) |
115 | if (cp[nhere + cnt] != usrc[cnt]) |
116 | break; |
117 | |
118 | if (cnt != nhere && cp[nhere + cnt] < usrc[cnt]) |
119 | { |
120 | /* Cannot be in this range. */ |
121 | cp += 2 * nhere; |
122 | if (!LOCFILE_ALIGNED_P (1 + 2 * nhere)) |
123 | cp += (LOCFILE_ALIGN |
124 | - (1 + 2 * nhere) % LOCFILE_ALIGN); |
125 | continue; |
126 | } |
127 | |
128 | /* This range matches the next characters. Now find |
129 | the offset in the indirect table. */ |
130 | for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt); |
131 | |
132 | do |
133 | { |
134 | offset <<= 8; |
135 | /* With GCC 7 when compiling with -Os the compiler |
136 | warns that seq1.back_us and seq2.back_us, which |
137 | become usrc, might be used uninitialized. This |
138 | is impossible for the same reason as described |
139 | above. */ |
140 | DIAG_PUSH_NEEDS_COMMENT; |
141 | DIAG_IGNORE_Os_NEEDS_COMMENT (7, "-Wmaybe-uninitialized" ); |
142 | offset += usrc[cnt] - cp[cnt]; |
143 | DIAG_POP_NEEDS_COMMENT; |
144 | } |
145 | while (++cnt < nhere); |
146 | } |
147 | |
148 | *cpp += nhere; |
149 | return indirect[-i + offset]; |
150 | } |
151 | } |
152 | |
153 | /* NOTREACHED */ |
154 | return 0x43219876; |
155 | } |
156 | |
157 | #endif /* weight.h */ |
158 | |