blob: 57cb2d8f2ddc977f587ddb732c14878d7daaba64 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Generic string table handling.
2 Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <assert.h>
20#include <inttypes.h>
21#include <libelf.h>
22#include <stddef.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/param.h>
27
28#include "libebl.h"
29
30#ifndef MIN
31# define MIN(a, b) ((a) < (b) ? (a) : (b))
32#endif
33
34
35struct Ebl_GStrent
36{
37 const char *string;
38 size_t len;
39 struct Ebl_GStrent *next;
40 struct Ebl_GStrent *left;
41 struct Ebl_GStrent *right;
42 size_t offset;
43 unsigned int width;
44 char reverse[0];
45};
46
47
48struct memoryblock
49{
50 struct memoryblock *next;
51 char memory[0];
52};
53
54
55struct Ebl_GStrtab
56{
57 struct Ebl_GStrent *root;
58 struct memoryblock *memory;
59 char *backp;
60 size_t left;
61 size_t total;
62 unsigned int width;
63 bool nullstr;
64
65 struct Ebl_GStrent null;
66};
67
68
69/* Cache for the pagesize. We correct this value a bit so that `malloc'
70 is not allocating more than a page. */
71static size_t ps;
72
73
74struct Ebl_GStrtab *
75ebl_gstrtabinit (unsigned int width, bool nullstr)
76{
77 struct Ebl_GStrtab *ret;
78
79 if (ps == 0)
80 {
81 ps = sysconf (_SC_PAGESIZE) - 2 * sizeof (void *);
82 assert (sizeof (struct memoryblock) < ps);
83 }
84
85 ret = (struct Ebl_GStrtab *) calloc (1, sizeof (struct Ebl_GStrtab));
86 if (ret != NULL)
87 {
88 ret->width = width;
89 ret->nullstr = nullstr;
90
91 if (nullstr)
92 {
93 ret->null.len = 1;
94 ret->null.string = (char *) calloc (1, width);
95 }
96 }
97
98 return ret;
99}
100
101
102static void
103morememory (struct Ebl_GStrtab *st, size_t len)
104{
105 struct memoryblock *newmem;
106
107 if (len < ps)
108 len = ps;
109 newmem = (struct memoryblock *) malloc (len);
110 if (newmem == NULL)
111 abort ();
112
113 newmem->next = st->memory;
114 st->memory = newmem;
115 st->backp = newmem->memory;
116 st->left = len - offsetof (struct memoryblock, memory);
117}
118
119
120void
121ebl_gstrtabfree (struct Ebl_GStrtab *st)
122{
123 struct memoryblock *mb = st->memory;
124
125 while (mb != NULL)
126 {
127 void *old = mb;
128 mb = mb->next;
129 free (old);
130 }
131
132 if (st->null.string != NULL)
133 free ((char *) st->null.string);
134
135 free (st);
136}
137
138
139static struct Ebl_GStrent *
140newstring (struct Ebl_GStrtab *st, const char *str, size_t len)
141{
142 /* Compute the amount of padding needed to make the structure aligned. */
143 size_t align = ((__alignof__ (struct Ebl_GStrent)
144 - (((uintptr_t) st->backp)
145 & (__alignof__ (struct Ebl_GStrent) - 1)))
146 & (__alignof__ (struct Ebl_GStrent) - 1));
147
148 /* Make sure there is enough room in the memory block. */
149 if (st->left < align + sizeof (struct Ebl_GStrent) + len * st->width)
150 {
151 morememory (st, sizeof (struct Ebl_GStrent) + len * st->width);
152 align = 0;
153 }
154
155 /* Create the reserved string. */
156 struct Ebl_GStrent *newstr = (struct Ebl_GStrent *) (st->backp + align);
157 newstr->string = str;
158 newstr->len = len;
159 newstr->width = st->width;
160 newstr->next = NULL;
161 newstr->left = NULL;
162 newstr->right = NULL;
163 newstr->offset = 0;
164 for (int i = len - 2; i >= 0; --i)
165 for (int j = st->width - 1; j >= 0; --j)
166 newstr->reverse[i * st->width + j] = str[(len - 2 - i) * st->width + j];
167 for (size_t j = 0; j < st->width; ++j)
168 newstr->reverse[(len - 1) * st->width + j] = '\0';
169 st->backp += align + sizeof (struct Ebl_GStrent) + len * st->width;
170 st->left -= align + sizeof (struct Ebl_GStrent) + len * st->width;
171
172 return newstr;
173}
174
175
176/* XXX This function should definitely be rewritten to use a balancing
177 tree algorith (AVL, red-black trees). For now a simple, correct
178 implementation is enough. */
179static struct Ebl_GStrent **
180searchstring (struct Ebl_GStrent **sep, struct Ebl_GStrent *newstr)
181{
182 int cmpres;
183
184 /* More strings? */
185 if (*sep == NULL)
186 {
187 *sep = newstr;
188 return sep;
189 }
190
191 /* Compare the strings. */
192 cmpres = memcmp ((*sep)->reverse, newstr->reverse,
193 (MIN ((*sep)->len, newstr->len) - 1) * (*sep)->width);
194 if (cmpres == 0)
195 /* We found a matching string. */
196 return sep;
197 else if (cmpres > 0)
198 return searchstring (&(*sep)->left, newstr);
199 else
200 return searchstring (&(*sep)->right, newstr);
201}
202
203
204/* Add new string. The actual string is assumed to be permanent. */
205struct Ebl_GStrent *
206ebl_gstrtabadd (struct Ebl_GStrtab *st, const char *str, size_t len)
207{
208 struct Ebl_GStrent *newstr;
209 struct Ebl_GStrent **sep;
210
211 /* Compute the string length if the caller doesn't know it. */
212 if (len == 0)
213 {
214 size_t j;
215
216 do
217 for (j = 0; j < st->width; ++j)
218 if (str[len * st->width + j] != '\0')
219 break;
220 while (j == st->width && ++len);
221 }
222
223 /* Make sure all "" strings get offset 0 but only if the table was
224 created with a special null entry in mind. */
225 if (len == 1 && st->null.string != NULL)
226 return &st->null;
227
228 /* Allocate memory for the new string and its associated information. */
229 newstr = newstring (st, str, len);
230
231 /* Search in the array for the place to insert the string. If there
232 is no string with matching prefix and no string with matching
233 leading substring, create a new entry. */
234 sep = searchstring (&st->root, newstr);
235 if (*sep != newstr)
236 {
237 /* This is not the same entry. This means we have a prefix match. */
238 if ((*sep)->len > newstr->len)
239 {
240 struct Ebl_GStrent *subs;
241
242 /* Check whether we already know this string. */
243 for (subs = (*sep)->next; subs != NULL; subs = subs->next)
244 if (subs->len == newstr->len)
245 {
246 /* We have an exact match with a substring. Free the memory
247 we allocated. */
248 st->left += (st->backp - (char *) newstr) * st->width;
249 st->backp = (char *) newstr;
250
251 return subs;
252 }
253
254 /* We have a new substring. This means we don't need the reverse
255 string of this entry anymore. */
256 st->backp -= newstr->len;
257 st->left += newstr->len;
258
259 newstr->next = (*sep)->next;
260 (*sep)->next = newstr;
261 }
262 else if ((*sep)->len != newstr->len)
263 {
264 /* When we get here it means that the string we are about to
265 add has a common prefix with a string we already have but
266 it is longer. In this case we have to put it first. */
267 st->total += newstr->len - (*sep)->len;
268 newstr->next = *sep;
269 newstr->left = (*sep)->left;
270 newstr->right = (*sep)->right;
271 *sep = newstr;
272 }
273 else
274 {
275 /* We have an exact match. Free the memory we allocated. */
276 st->left += (st->backp - (char *) newstr) * st->width;
277 st->backp = (char *) newstr;
278
279 newstr = *sep;
280 }
281 }
282 else
283 st->total += newstr->len;
284
285 return newstr;
286}
287
288
289static void
290copystrings (struct Ebl_GStrent *nodep, char **freep, size_t *offsetp)
291{
292 struct Ebl_GStrent *subs;
293
294 if (nodep->left != NULL)
295 copystrings (nodep->left, freep, offsetp);
296
297 /* Process the current node. */
298 nodep->offset = *offsetp;
299 *freep = (char *) mempcpy (*freep, nodep->string, nodep->len * nodep->width);
300 *offsetp += nodep->len * nodep->width;
301
302 for (subs = nodep->next; subs != NULL; subs = subs->next)
303 {
304 assert (subs->len < nodep->len);
305 subs->offset = nodep->offset + (nodep->len - subs->len) * nodep->width;
306 assert (subs->offset != 0 || subs->string[0] == '\0');
307 }
308
309 if (nodep->right != NULL)
310 copystrings (nodep->right, freep, offsetp);
311}
312
313
314void
315ebl_gstrtabfinalize (struct Ebl_GStrtab *st, Elf_Data *data)
316{
317 size_t copylen;
318 char *endp;
319 size_t nulllen = st->nullstr ? st->width : 0;
320
321 /* Fill in the information. */
322 data->d_buf = malloc (st->total + nulllen);
323 if (data->d_buf == NULL)
324 abort ();
325
326 /* The first byte must always be zero if we created the table with a
327 null string. */
328 if (st->nullstr)
329 memset (data->d_buf, '\0', st->width);
330
331 data->d_type = ELF_T_BYTE;
332 data->d_size = st->total + nulllen;
333 data->d_off = 0;
334 data->d_align = 1;
335 data->d_version = EV_CURRENT;
336
337 /* Now run through the tree and add all the string while also updating
338 the offset members of the elfstrent records. */
339 endp = (char *) data->d_buf + nulllen;
340 copylen = nulllen;
341 copystrings (st->root, &endp, &copylen);
342 assert (copylen == st->total * st->width + nulllen);
343}
344
345
346size_t
347ebl_gstrtaboffset (struct Ebl_GStrent *se)
348{
349 return se->offset;
350}