2008-06-30 21:54:22 -04:00
|
|
|
u_int32_t *bitvector_resize(u_int32_t *b, size_t n)
|
|
|
|
{
|
|
|
|
u_int32_t *p;
|
2019-08-09 07:02:02 -04:00
|
|
|
size_t sz = ((n + 31) >> 5) * 4;
|
2008-06-30 21:54:22 -04:00
|
|
|
p = realloc(b, sz);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
2008-06-30 21:54:22 -04:00
|
|
|
memset(p, 0, sz);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
u_int32_t *mk_bitvector(size_t n) { return bitvector_resize(NULL, n); }
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
void bitvector_set(u_int32_t *b, u_int32_t n, u_int32_t c)
|
|
|
|
{
|
|
|
|
if (c)
|
2019-08-09 07:02:02 -04:00
|
|
|
b[n >> 5] |= (1 << (n & 31));
|
2008-06-30 21:54:22 -04:00
|
|
|
else
|
2019-08-09 07:02:02 -04:00
|
|
|
b[n >> 5] &= ~(1 << (n & 31));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t bitvector_get(u_int32_t *b, u_int32_t n)
|
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
return b[n >> 5] & (1 << (n & 31));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t n, maxsize;
|
|
|
|
unsigned long *items;
|
|
|
|
} ltable_t;
|
|
|
|
|
|
|
|
void ltable_init(ltable_t *t, size_t n)
|
|
|
|
{
|
|
|
|
t->n = 0;
|
|
|
|
t->maxsize = n;
|
2019-08-09 07:02:02 -04:00
|
|
|
t->items = (unsigned long *)malloc(n * sizeof(unsigned long));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
void ltable_clear(ltable_t *t) { t->n = 0; }
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
void ltable_insert(ltable_t *t, unsigned long item)
|
|
|
|
{
|
|
|
|
unsigned long *p;
|
|
|
|
|
|
|
|
if (t->n == t->maxsize) {
|
2019-08-09 07:02:02 -04:00
|
|
|
p = realloc(t->items, (t->maxsize * 2) * sizeof(unsigned long));
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
2008-06-30 21:54:22 -04:00
|
|
|
t->items = p;
|
|
|
|
t->maxsize *= 2;
|
|
|
|
}
|
|
|
|
t->items[t->n++] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NOTFOUND ((int)-1)
|
|
|
|
|
|
|
|
int ltable_lookup(ltable_t *t, unsigned long item)
|
|
|
|
{
|
|
|
|
int i;
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < (int)t->n; i++)
|
2008-06-30 21:54:22 -04:00
|
|
|
if (t->items[i] == item)
|
|
|
|
return i;
|
|
|
|
return NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ltable_adjoin(ltable_t *t, unsigned long item)
|
|
|
|
{
|
|
|
|
if (ltable_lookup(t, item) == NOTFOUND)
|
|
|
|
ltable_insert(t, item);
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
static const u_int32_t offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL,
|
|
|
|
0x000E2080UL, 0x03C82080UL,
|
|
|
|
0xFA082080UL, 0x82082080UL };
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
static const char trailingBytesForUTF8[256] = {
|
2019-08-09 07:02:02 -04:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
|
2008-06-30 21:54:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
int u8_seqlen(const char c)
|
|
|
|
{
|
|
|
|
return trailingBytesForUTF8[(unsigned int)(unsigned char)c] + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UEOF ((u_int32_t)EOF)
|
|
|
|
|
|
|
|
u_int32_t u8_fgetc(FILE *f)
|
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
int amt = 0, sz, c;
|
|
|
|
u_int32_t ch = 0;
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
c = fgetc(f);
|
|
|
|
if (c == EOF)
|
|
|
|
return UEOF;
|
|
|
|
ch = (u_int32_t)c;
|
|
|
|
amt = sz = u8_seqlen(ch);
|
|
|
|
while (--amt) {
|
|
|
|
ch <<= 6;
|
|
|
|
c = fgetc(f);
|
|
|
|
if (c == EOF)
|
|
|
|
return UEOF;
|
|
|
|
ch += (u_int32_t)c;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
ch -= offsetsFromUTF8[sz - 1];
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|