1/* Copyright (C) 1996-2016 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 <http://www.gnu.org/licenses/>. */
18
19#ifndef _WEIGHT_H_
20#define _WEIGHT_H_ 1
21
22/* Find index of weight. */
23static inline int32_t __attribute__ ((always_inline))
24findidx (const int32_t *table,
25 const int32_t *indirect,
26 const unsigned char *extra,
27 const unsigned char **cpp, size_t len)
28{
29 int_fast32_t i = table[*(*cpp)++];
30 const unsigned char *cp;
31 const unsigned char *usrc;
32
33 if (i >= 0)
34 /* This is an index into the weight table. Cool. */
35 return i;
36
37 /* Oh well, more than one sequence starting with this byte.
38 Search for the correct one. */
39 cp = &extra[-i];
40 usrc = *cpp;
41 --len;
42 while (1)
43 {
44 size_t nhere;
45
46 /* The first thing is the index. */
47 i = *((const int32_t *) cp);
48 cp += sizeof (int32_t);
49
50 /* Next is the length of the byte sequence. These are always
51 short byte sequences so there is no reason to call any
52 function (even if they are inlined). */
53 nhere = *cp++;
54
55 if (i >= 0)
56 {
57 /* It is a single character. If it matches we found our
58 index. Note that at the end of each list there is an
59 entry of length zero which represents the single byte
60 sequence. The first (and here only) byte was tested
61 already. */
62 size_t cnt;
63
64 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
65 if (cp[cnt] != usrc[cnt])
66 break;
67
68 if (cnt == nhere)
69 {
70 /* Found it. */
71 *cpp += nhere;
72 return i;
73 }
74
75 /* Up to the next entry. */
76 cp += nhere;
77 if (!LOCFILE_ALIGNED_P (1 + nhere))
78 cp += LOCFILE_ALIGN - (1 + nhere) % LOCFILE_ALIGN;
79 }
80 else
81 {
82 /* This is a range of characters. First decide whether the
83 current byte sequence lies in the range. */
84 size_t cnt;
85 size_t offset = 0;
86
87 for (cnt = 0; cnt < nhere && cnt < len; ++cnt)
88 if (cp[cnt] != usrc[cnt])
89 break;
90
91 if (cnt != nhere)
92 {
93 if (cnt == len || cp[cnt] > usrc[cnt])
94 {
95 /* Cannot be in this range. */
96 cp += 2 * nhere;
97 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
98 cp += (LOCFILE_ALIGN
99 - (1 + 2 * nhere) % LOCFILE_ALIGN);
100 continue;
101 }
102
103 /* Test against the end of the range. */
104 for (cnt = 0; cnt < nhere; ++cnt)
105 if (cp[nhere + cnt] != usrc[cnt])
106 break;
107
108 if (cnt != nhere && cp[nhere + cnt] < usrc[cnt])
109 {
110 /* Cannot be in this range. */
111 cp += 2 * nhere;
112 if (!LOCFILE_ALIGNED_P (1 + 2 * nhere))
113 cp += (LOCFILE_ALIGN
114 - (1 + 2 * nhere) % LOCFILE_ALIGN);
115 continue;
116 }
117
118 /* This range matches the next characters. Now find
119 the offset in the indirect table. */
120 for (cnt = 0; cp[cnt] == usrc[cnt]; ++cnt);
121
122 do
123 {
124 offset <<= 8;
125 offset += usrc[cnt] - cp[cnt];
126 }
127 while (++cnt < nhere);
128 }
129
130 *cpp += nhere;
131 return indirect[-i + offset];
132 }
133 }
134
135 /* NOTREACHED */
136 return 0x43219876;
137}
138
139#endif /* weight.h */
140