1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/* ========================================================================
* Copyright 2008 Mark Crispin
* ========================================================================
*/
/*
* Program: Miscellaneous utility routines
*
* Author: Mark Crispin
*
* Date: 5 July 1988
* Last Edited: 19 November 2008
*
* Previous versions of this file were
*
* Copyright 1988-2006 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This original version of this file is
* Copyright 1988 Stanford University
* and was developed in the Symbolic Systems Resources Group of the Knowledge
* Systems Laboratory at Stanford University in 1987-88, and was funded by the
* Biomedical Research Technology Program of the NationalInstitutes of Health
* under grant number RR-00785.
*/
#include <ctype.h>
#include "c-client.h"
/* Convert ASCII string to all uppercase
* Accepts: string pointer
* Returns: string pointer
*
* Don't use islower/toupper since this function must be ASCII only.
*/
unsigned char *ucase (unsigned char *s)
{
unsigned char *t;
/* if lowercase covert to upper */
for (t = s; *t; t++) if ((*t >= 'a') && (*t <= 'z')) *t -= ('a' - 'A');
return s; /* return string */
}
/* Convert string to all lowercase
* Accepts: string pointer
* Returns: string pointer
*
* Don't use isupper/tolower since this function must be ASCII only.
*/
unsigned char *lcase (unsigned char *s)
{
unsigned char *t;
/* if uppercase covert to lower */
for (t = s; *t; t++) if ((*t >= 'A') && (*t <= 'Z')) *t += ('a' - 'A');
return s; /* return string */
}
/* Copy string to free storage
* Accepts: source string
* Returns: free storage copy of string
*/
char *cpystr (const char *string)
{
return string ? strcpy ((char *) fs_get (1 + strlen (string)),string) : NIL;
}
/* Copy text/size to free storage as sized text
* Accepts: destination sized text
* pointer to source text
* size of source text
* Returns: text as a char *
*/
char *cpytxt (SIZEDTEXT *dst,char *text,unsigned long size)
{
/* flush old space */
if (dst->data) fs_give ((void **) &dst->data);
/* copy data in sized text */
memcpy (dst->data = (unsigned char *)
fs_get ((size_t) (dst->size = size) + 1),text,(size_t) size);
dst->data[size] = '\0'; /* tie off text */
return (char *) dst->data; /* convenience return */
}
/* Copy sized text to free storage as sized text
* Accepts: destination sized text
* source sized text
* Returns: text as a char *
*/
char *textcpy (SIZEDTEXT *dst,SIZEDTEXT *src)
{
/* flush old space */
if (dst->data) fs_give ((void **) &dst->data);
/* copy data in sized text */
memcpy (dst->data = (unsigned char *)
fs_get ((size_t) (dst->size = src->size) + 1),
src->data,(size_t) src->size);
dst->data[dst->size] = '\0'; /* tie off text */
return (char *) dst->data; /* convenience return */
}
/* Copy stringstruct to free storage as sized text
* Accepts: destination sized text
* source stringstruct
* Returns: text as a char *
*/
char *textcpystring (SIZEDTEXT *text,STRING *bs)
{
unsigned long i = 0;
/* clear old space */
if (text->data) fs_give ((void **) &text->data);
/* make free storage space in sized text */
text->data = (unsigned char *) fs_get ((size_t) (text->size = SIZE (bs)) +1);
while (i < text->size) text->data[i++] = SNX (bs);
text->data[i] = '\0'; /* tie off text */
return (char *) text->data; /* convenience return */
}
/* Copy stringstruct from offset to free storage as sized text
* Accepts: destination sized text
* source stringstruct
* offset into stringstruct
* size of source text
* Returns: text as a char *
*/
char *textcpyoffstring (SIZEDTEXT *text,STRING *bs,unsigned long offset,
unsigned long size)
{
unsigned long i = 0;
/* clear old space */
if (text->data) fs_give ((void **) &text->data);
SETPOS (bs,offset); /* offset the string */
/* make free storage space in sized text */
text->data = (unsigned char *) fs_get ((size_t) (text->size = size) + 1);
while (i < size) text->data[i++] = SNX (bs);
text->data[i] = '\0'; /* tie off text */
return (char *) text->data; /* convenience return */
}
/* Returns index of rightmost bit in word
* Accepts: pointer to a 32 bit value
* Returns: -1 if word is 0, else index of rightmost bit
*
* Bit is cleared in the word
*/
unsigned long find_rightmost_bit (unsigned long *valptr)
{
unsigned long value = *valptr;
unsigned long bit = 0;
if (!(value & 0xffffffff)) return 0xffffffff;
/* binary search for rightmost bit */
if (!(value & 0xffff)) value >>= 16, bit += 16;
if (!(value & 0xff)) value >>= 8, bit += 8;
if (!(value & 0xf)) value >>= 4, bit += 4;
if (!(value & 0x3)) value >>= 2, bit += 2;
if (!(value & 0x1)) value >>= 1, bit += 1;
*valptr ^= (1 << bit); /* clear specified bit */
return bit;
}
/* Return minimum of two integers
* Accepts: integer 1
* integer 2
* Returns: minimum
*/
long min (long i,long j)
{
return ((i < j) ? i : j);
}
/* Return maximum of two integers
* Accepts: integer 1
* integer 2
* Returns: maximum
*/
long max (long i,long j)
{
return ((i > j) ? i : j);
}
/* Search, case-insensitive for ASCII characters
* Accepts: base string
* length of base string
* pattern string
* length of pattern string
* Returns: T if pattern exists inside base, else NIL
*/
long search (unsigned char *base,long basec,unsigned char *pat,long patc)
{
long i,j,k;
int c;
unsigned char mask[256];
static unsigned char alphatab[256] = {
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,
223,223,223,223,223,223,223,223,223,223,223,255,255,255,255,255,
255,223,223,223,223,223,223,223,223,223,223,223,223,223,223,223,
223,223,223,223,223,223,223,223,223,223,223,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
};
/* validate arguments */
if (base && (basec > 0) && pat && (basec >= patc)) {
if (patc <= 0) return T; /* empty pattern always succeeds */
memset (mask,0,256); /* initialize search validity mask */
for (i = 0; i < patc; i++) if (!mask[c = pat[i]]) {
/* mark single character if non-alphabetic */
if (alphatab[c] & 0x20) mask[c] = T;
/* else mark both cases */
else mask[c & 0xdf] = mask[c | 0x20] = T;
}
/* Boyer-Moore type search */
for (i = --patc; i < basec; i += (mask[c] ? 1 : (j + 1)))
for (j = patc,c = base[k = i]; !((c ^ pat[j]) & alphatab[c]);
j--,c = base[--k])
if (!j) return T; /* found a match! */
}
return NIL; /* pattern not found */
}
/* Boyer-Moore string search
* Accepts: base string
* length of base string
* pattern string
* length of pattern string
* Returns: T if pattern exists inside base, else NIL
*/
long ssearch (unsigned char *base,long basec,unsigned char *pat,long patc)
{
long i,j,k;
int c;
unsigned char mask[256];
/* validate arguments */
if (base && (basec > 0) && pat && (basec >= patc)) {
if (patc <= 0) return T; /* empty pattern always succeeds */
memset (mask,0,256); /* initialize search validity mask */
for (i = 0; i < patc; i++) mask[pat[i]] = T;
/* Boyer-Moore type search */
for (i = --patc, c = pat[i]; i < basec; i += (mask[c] ? 1 : (j + 1)))
for (j = patc,c = base[k = i]; c == pat[j]; j--,c = base[--k])
if (!j) return T; /* found a match! */
}
return NIL; /* pattern not found */
}
/* Create a hash table
* Accepts: size of new table (note: should be a prime)
* Returns: hash table
*/
HASHTAB *hash_create (size_t size)
{
size_t i = sizeof (size_t) + size * sizeof (HASHENT *);
HASHTAB *ret = (HASHTAB *) memset (fs_get (i),0,i);
ret->size = size;
return ret;
}
/* Destroy hash table
* Accepts: hash table
*/
void hash_destroy (HASHTAB **hashtab)
{
if (*hashtab) {
hash_reset (*hashtab); /* reset hash table */
fs_give ((void **) hashtab);
}
}
/* Reset hash table
* Accepts: hash table
*/
void hash_reset (HASHTAB *hashtab)
{
size_t i;
HASHENT *ent,*nxt;
/* free each hash entry */
for (i = 0; i < hashtab->size; i++) if ((ent = hashtab->table[i]) != NULL)
for (hashtab->table[i] = NIL; ent; ent = nxt) {
nxt = ent->next; /* get successor */
fs_give ((void **) &ent); /* flush this entry */
}
}
/* Calculate index into hash table
* Accepts: hash table
* entry name
* Returns: index
*/
unsigned long hash_index (HASHTAB *hashtab,char *key)
{
unsigned long i,ret;
/* polynomial of letters of the word */
for (ret = 0; (i = (unsigned int) *key++) != 0L; ret += i) ret *= HASHMULT;
return ret % (unsigned long) hashtab->size;
}
/* Look up name in hash table
* Accepts: hash table
* key
* Returns: associated data
*/
void **hash_lookup (HASHTAB *hashtab,char *key)
{
HASHENT *ret;
for (ret = hashtab->table[hash_index (hashtab,key)]; ret; ret = ret->next)
if (!strcmp (key,ret->name)) return ret->data;
return NIL;
}
/* Add entry to hash table
* Accepts: hash table
* key
* associated data
* number of extra data slots
* Returns: hash entry
* Caller is responsible for ensuring that entry isn't already in table
*/
HASHENT *hash_add (HASHTAB *hashtab,char *key,void *data,long extra)
{
unsigned long i = hash_index (hashtab,key);
size_t j = sizeof (HASHENT) + (extra * sizeof (void *));
HASHENT *ret = (HASHENT *) memset (fs_get (j),0,j);
ret->next = hashtab->table[i];/* insert as new head in this index */
ret->name = key; /* set up hash key */
ret->data[0] = data; /* and first data */
return hashtab->table[i] = ret;
}
/* Look up name in hash table
* Accepts: hash table
* key
* associated data
* number of extra data slots
* Returns: associated data
*/
void **hash_lookup_and_add (HASHTAB *hashtab,char *key,void *data,long extra)
{
HASHENT *ret;
unsigned long i = hash_index (hashtab,key);
size_t j = sizeof (HASHENT) + (extra * sizeof (void *));
for (ret = hashtab->table[i]; ret; ret = ret->next)
if (!strcmp (key,ret->name)) return ret->data;
ret = (HASHENT *) memset (fs_get (j),0,j);
ret->next = hashtab->table[i];/* insert as new head in this index */
ret->name = key; /* set up hash key */
ret->data[0] = data; /* and first data */
return (hashtab->table[i] = ret)->data;
}
/* Convert two hex characters into byte
* Accepts: char for high nybble
* char for low nybble
* Returns: byte
*
* Arguments must be isxdigit validated
*/
unsigned char hex2byte (unsigned char c1,unsigned char c2)
{
/* merge the two nybbles */
return ((c1 -= (isdigit (c1) ? '0' : ((c1 <= 'Z') ? 'A' : 'a') - 10)) << 4) +
(c2 - (isdigit (c2) ? '0' : ((c2 <= 'Z') ? 'A' : 'a') - 10));
}
/* Compare two unsigned longs
* Accepts: first value
* second value
* Returns: -1 if l1 < l2, 0 if l1 == l2, 1 if l1 > l2
*/
int compare_ulong (unsigned long l1,unsigned long l2)
{
if (l1 < l2) return -1;
if (l1 > l2) return 1;
return 0;
}
/* Compare two unsigned chars, case-independent
* Accepts: first value
* second value
* Returns: -1 if c1 < c2, 0 if c1 == c2, 1 if c1 > c2
*
* Don't use isupper/tolower since this function must be ASCII only.
*/
int compare_uchar (unsigned char c1,unsigned char c2)
{
return compare_ulong (((c1 >= 'a') && (c1 <= 'z')) ? c1 - ('a' - 'A') : c1,
((c2 >= 'a') && (c2 <= 'z')) ? c2 - ('a' - 'A') : c2);
}
/* Compare two strings by octet
* Accepts: first string
* second string
* Returns: -1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2
*
* Effectively strcmp() but without locales
*/
int compare_string (unsigned char *s1,unsigned char *s2)
{
int i;
if (!s1) return s2 ? -1 : 0; /* empty string cases */
else if (!s2) return 1;
for (; *s1 && *s2; s1++,s2++) if ((i = (compare_ulong (*s1,*s2))) != 0) return i;
if (*s1) return 1; /* first string is longer */
return *s2 ? -1 : 0; /* second string longer : strings identical */
}
/* Compare two case-independent ASCII strings
* Accepts: first string
* second string
* Returns: -1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2
*
* Effectively strcasecmp() but without locales
*/
int compare_cstring (unsigned char *s1,unsigned char *s2)
{
int i;
if (!s1) return s2 ? -1 : 0; /* empty string cases */
else if (!s2) return 1;
for (; *s1 && *s2; s1++,s2++) if ((i = (compare_uchar (*s1,*s2))) != 0) return i;
if (*s1) return 1; /* first string is longer */
return *s2 ? -1 : 0; /* second string longer : strings identical */
}
/* Compare case-independent string with sized text
* Accepts: first string
* sized text
* Returns: -1 if s1 < s2, 0 if s1 == s2, 1 if s1 > s2
*/
int compare_csizedtext (unsigned char *s1,SIZEDTEXT *s2)
{
int i;
unsigned char *s;
unsigned long j;
if (!s1) return s2 ? -1 : 0; /* null string cases */
else if (!s2) return 1;
for (s = (char *) s2->data,j = s2->size; *s1 && j; ++s1,++s,--j)
if ((i = (compare_uchar (*s1,*s))) != 0) return i;
if (*s1) return 1; /* first string is longer */
return j ? -1 : 0; /* second string longer : strings identical */
}
|