Debian Codegraph @

src/util.c

raw symbols SCIP
1 /*
2 the DASM macro assembler (aka small systems cross assembler)
3
4 Copyright (c) 1988-2002 by Matthew Dillon.
5 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
6 Copyright (c) 2003-2008 by Andrew Davie.
7 Copyright (c) 2008 by Peter H. Froehlich.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /**
25 * @file
26 */
27
28 /*
29 The functions strlcat() and strlcpy() are not distributed under the GNU
30 General Public License but a custom license reproduced below. Although
31 not strictly necessary, inclusion of these functions in a GPLed project
32 has been explicitly allowed by their author, Todd C. Miller.
33
34 The functions were downloaded from these URLs on 2008/04/07 and had the
35 indicated versioning information attached:
36
37 ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcat.c
38 $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $
39 ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.c
40 $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $
41
42 The code was modified to include assertions and to make splint happier
43 but is otherwise unchanged.
44 The documentation comments were moved into the util.h header file for
45 consistency.
46
47 The function hash_string() is based on code fragments posted by Daniel
48 Bernstein to comp.lang.c on 1990/12/04. As far as we know, the code is
49 in the public domain.
50 */
51
52 #include "util.h"
53
54 #include "asm.h"
55 #include "errors.h"
56 #include "version.h"
57
58 #include <assert.h>
59 #include <ctype.h>
60 #include <stdint.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sys/types.h>
64
65 /*@out@*/
66 void *checked_malloc(size_t bytes)
67 {
68 void *p = NULL;
69 assert(bytes > 0); /* rule out 0! */
70
71 p = malloc(bytes);
72 if (p == NULL)
73 {
74 panic_fmt(PANIC_MEMORY, bytes, SOURCE_LOCATION);
75 exit(EXIT_FAILURE); /* makes splint happier */
76 }
77
78 return p;
79 }
80
81 void *zero_malloc(size_t bytes)
82 {
83 void *p = NULL;
84 assert(bytes > 0); /* rule out 0! */
85
86 p = checked_malloc(bytes);
87 p = memset(p, 0, bytes);
88
89 return p;
90 }
91
92 struct new_perm_block
93 {
94 /*@temp@*/ struct new_perm_block *next;
95 char data[];
96 };
97
98 /*@null@*/ /*@temp@*/
99 static struct new_perm_block *new_permalloc_stack = NULL;
100
101 #define ALLOCSIZE 16384
102 #define ROUNDUP(x) ((x + alignment-1) & ~(alignment-1))
103
104 void *small_alloc(size_t bytes)
105 {
106 /* Assume sizeof(union align) is a power of 2 */
107 union align { long l; void *p; void (*fp)(void); };
108
109 static void *buf;
110 static size_t left = 0;
111 void *ptr;
112 struct new_perm_block *block;
113 size_t alignment = sizeof(union align);
114
115 assert(bytes > 0); /* rule out 0! */
116 /* could sanity check upper bound here, but we're doing it below anyway */
117
118 debug_fmt(DEBUG_ENTER, SOURCE_LOCATION);
119
120 /* round up bytes for proper alignment */
121 bytes = ROUNDUP(bytes);
122
123 /* do we not have enough left in the current block? */
124 if (bytes > left)
125 {
126 debug_fmt("%s: new block needed", SOURCE_LOCATION);
127
128 /* allocate a new block */
129 block = zero_malloc(ALLOCSIZE);
130 debug_fmt("%s: block @ %p", SOURCE_LOCATION, (void*) block);
131
132 /* calculate bytes we have left */
133 left = ALLOCSIZE - ROUNDUP(sizeof(block->next));
134
135 /* check again if we have enough space */
136 if (bytes > left)
137 {
138 panic_fmt(PANIC_SMALL_MEMORY, bytes, SOURCE_LOCATION);
139 exit(EXIT_FAILURE); /* makes splint happier */
140 }
141
142 /* insert at top of stack */
143 block->next = new_permalloc_stack;
144 new_permalloc_stack = block;
145
146 /* setup buf to point to actual memory area */
147 buf = ((char*)block) + ROUNDUP(sizeof(block->next)); /* char cast important! */
148 debug_fmt("%s: initial buf @ %p", SOURCE_LOCATION, (void*) buf);
149 }
150
151 ptr = buf;
152 buf = ((char*)buf) + bytes; /* char cast important! */
153 debug_fmt("%s: adjusted buf @ %p", SOURCE_LOCATION, (void*) buf);
154 assert(ptr < buf); /* TODO: good idea? [phf] */
155 left -= bytes;
156 debug_fmt(DEBUG_LEAVE, SOURCE_LOCATION);
157 return ptr;
158 }
159
160 void small_free_all(void)
161 {
162 /* the block we are about to free() */
163 struct new_perm_block *current = NULL;
164
165 debug_fmt(DEBUG_ENTER, SOURCE_LOCATION);
166
167 /* as long as we have block left */
168 while (new_permalloc_stack != NULL)
169 {
170 /* remember the top block */
171 current = new_permalloc_stack;
172 /* pop the top block, stack possibly empty after this */
173 new_permalloc_stack = current->next;
174 /* free() the block we popped */
175 debug_fmt("%s: freed block @ %p", SOURCE_LOCATION, (void*) current);
176 free(current);
177 }
178
179 debug_fmt(DEBUG_LEAVE, SOURCE_LOCATION);
180 }
181
182 unsigned int hash_string(const char *string, size_t length)
183 {
184 /*
185 Why is this a better hash function than Matt's original?
186 For MNEMONICs we go from 14 to 5 collisons, woohoo. :-)
187 For SYMBOLs we go from 29 to 11 collisons, woohoo. :-)
188 */
189
190 unsigned int hash = 5381;
191
192 assert(string != NULL);
193 assert(length > 0);
194 assert(length <= strlen(string));
195
196 while (length-- != 0)
197 {
198 hash = ((hash << 5) + hash) + (unsigned int) *string++;
199 }
200
201 return hash;
202 }
203
204 char *checked_strdup(const char *s)
205 {
206 assert(s != NULL);
207 /* v_seg() doesn't enforce this so we can't either for now [phf] */
208 /*assert(strlen(s) > 0);*/
209 return strcpy(checked_malloc(strlen(s)+1), s);
210 }
211
212 size_t strlower(/*@out@*/ char *dst, const char *src, size_t size)
213 {
214 /* strlcpy checks the assertions anyway */
215 size_t result = strlcpy(dst, src, size);
216
217 for (/* blank */; *dst != '\0'; dst++) {
218 *dst = (char) tolower((int)*dst);
219 }
220
221 return result;
222 }
223
224 size_t strupper(/*@out@*/ char *dst, const char *src, size_t size)
225 {
226 /* strlcpy checks the assertions anyway */
227 size_t result = strlcpy(dst, src, size);
228
229 for (/* blank */; *dst != '\0'; dst++) {
230 *dst = (char) toupper((int)*dst);
231 }
232
233 return result;
234 }
235
236 size_t strip_whitespace(/*@out@*/ char *dst, const char *src, size_t size)
237 {
238 /* TODO: there must be a simpler way to write this... */
239 size_t n = size;
240 size_t needed = 0;
241 const char *t;
242
243 assert(dst != NULL);
244 assert(src != NULL);
245 assert(size > 0);
246
247 /* pre-count how many non-space characters we have */
248 for (t = src; *t != '\0'; t++) {
249 if (isspace((int)*t) == 0) {
250 needed++;
251 }
252 }
253
254 /* copy while still room and src not over */
255 while (n > 1 && *src != '\0') {
256 if (isspace((int)*src) != 0) {
257 src++;
258 }
259 else {
260 *dst++ = *src++;
261 n--;
262 }
263 }
264
265 *dst = '\0';
266 n--;
267
268 return needed;
269 }
270
271 bool match_either_case(const char *string, const char *either)
272 {
273 char buffer[MAX_SYM_LEN];
274 size_t res;
275
276 assert(string != NULL);
277 assert(either != NULL);
278
279 res = strlower(buffer, either, sizeof(buffer));
280 assert(res < sizeof(buffer));
281
282 if (strcmp(string, buffer) == 0) {
283 return true;
284 }
285
286 res = strupper(buffer, either, sizeof(buffer));
287 assert(res < sizeof(buffer));
288
289 if (strcmp(string, buffer) == 0) {
290 return true;
291 }
292
293 return false;
294 }
295
296 #if !defined(__APPLE__) && !defined(__BSD__)
297
298 /*
299 * Copyright (c) 1998 Todd C. Miller <[email protected]>
300 *
301 * Permission to use, copy, modify, and distribute this software for any
302 * purpose with or without fee is hereby granted, provided that the above
303 * copyright notice and this permission notice appear in all copies.
304 *
305 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
306 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
307 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
308 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
309 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
310 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
311 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
312 */
313
314 size_t
315 strlcat(/*@in@*/ /*@out@*/ char *dst, const char *src, size_t siz)
316 {
317 char *d = dst;
318 const char *s = src;
319 size_t n = siz;
320 size_t dlen;
321
322 /* added [phf] */
323 assert(dst != NULL);
324 assert(src != NULL);
325 assert(siz > 0);
326
327 /* Find the end of dst and adjust bytes left but don't go past end */
328 while (n-- != 0 && *d != '\0')
329 d++;
330 assert(d - dst >= 0); /* added [phf] */
331 dlen = d - dst;
332 n = siz - dlen;
333
334 if (n == 0)
335 return(dlen + strlen(s));
336 while (*s != '\0') {
337 if (n != 1) {
338 *d++ = *s;
339 n--;
340 }
341 s++;
342 }
343 *d = '\0';
344
345 return(dlen + (s - src)); /* count does not include NUL */
346 }
347
348 /*
349 * Copyright (c) 1998 Todd C. Miller <[email protected]>
350 *
351 * Permission to use, copy, modify, and distribute this software for any
352 * purpose with or without fee is hereby granted, provided that the above
353 * copyright notice and this permission notice appear in all copies.
354 *
355 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
356 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
357 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
358 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
359 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
360 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
361 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
362 */
363
364 size_t
365 strlcpy(/*@out@*/ char *dst, const char *src, size_t siz)
366 {
367 char *d = dst;
368 const char *s = src;
369 size_t n = siz;
370
371 /* added [phf] */
372 assert(dst != NULL);
373 assert(src != NULL);
374 assert(siz > 0);
375
376 /* Copy as many bytes as will fit */
377 if (n != 0) {
378 while (--n != 0) {
379 if ((*d++ = *s++) == '\0')
380 break;
381 }
382 }
383
384 /* Not enough room in dst, add NUL and traverse rest of src */
385 if (n == 0) {
386 if (siz != 0)
387 *d = '\0'; /* NUL-terminate dst */
388 while (*s++ != '\0') /* splinted [phf] */
389 ;
390 }
391
392 assert(s - src - 1 >= 0); /* added [phf] */
393 return(s - src - 1); /* count does not include NUL */
394 }
395
396 /*@null@*/
397 static const char *__dasm_progname = NULL;
398
399 /*@temp@*/
400 const char *getprogname(void)
401 {
402 return (__dasm_progname != NULL) ? __dasm_progname : "(unknown progname)";
403 }
404
405 void setprogname(const char *name)
406 {
407 char *slash = NULL;
408 assert(name != NULL);
409
410 slash = strrchr(name, DASM_PATH_SEPARATOR);
411 if (slash != NULL)
412 {
413 name = slash+1;
414 }
415 __dasm_progname = name;
416 }
417
418 #endif /* !defined(__APPLE__) && !defined(__BSD__) */
419
420 /* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4 autoindent: */