aboutsummaryrefslogtreecommitdiff
path: root/lib/slist.c
blob: b758c9b007d7870155aadda42a4da22c077837c1 (plain)
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
/* This file is part of pam-modules.
   Copyright (C) 2008, 2010-2012 Sergey Poznyakoff
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License along
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <graypam.h>

#define GRAY_SLIST_BUCKET_SIZE 1024

struct gray_slist_bucket {
	struct gray_slist_bucket *next;
	char *buf;
	size_t level;
	size_t size;
};

struct gray_slist {
	struct gray_slist_bucket *head, *tail;
	struct gray_slist_bucket *free;
};

static struct gray_slist_bucket *
alloc_bucket(size_t size)
{
	struct gray_slist_bucket *p = gray_malloc(sizeof(*p) + size);
	p->buf = (char*)(p + 1);
	p->level = 0;
	p->size = size;
	p->next = NULL;
	return p;
}

static void
alloc_pool(gray_slist_t slist, size_t size)
{
	struct gray_slist_bucket *p = alloc_bucket(GRAY_SLIST_BUCKET_SIZE);
	if (slist->tail)
		slist->tail->next = p;
	else
		slist->head = p;
	slist->tail = p;
}

static size_t
copy_chars(gray_slist_t slist, const char *str, size_t n)
{
	size_t rest;


	if (!slist->head || slist->tail->level == slist->tail->size)
		alloc_pool(slist, GRAY_SLIST_BUCKET_SIZE);
	rest = slist->tail->size - slist->tail->level;
	if (n > rest)
		n = rest;
	memcpy(slist->tail->buf + slist->tail->level, str, n);
	slist->tail->level += n;
	return n;
}

gray_slist_t 
gray_slist_create()
{
	gray_slist_t slist = gray_malloc(sizeof(*slist));
	slist->head = slist->tail = slist->free = 0;
	return slist;
}

void
gray_slist_clear(gray_slist_t slist)
{
	if (slist->tail) {
		slist->tail->next = slist->free;
		slist->free = slist->head;
		slist->head = slist->tail = NULL;
	}
}	


void
gray_slist_free(gray_slist_t *slist)
{
	struct gray_slist_bucket *p;
	if (*slist) {
		gray_slist_clear(*slist);
		for (p = (*slist)->free; p; ) {
			struct gray_slist_bucket *next = p->next;
			free(p);
			p = next;
		}
	}
	free(*slist);
	*slist = NULL;
}

void
gray_slist_append(gray_slist_t slist, const char *str, size_t n)
{
	const char *ptr = str;
	while (n) {
		size_t s = copy_chars(slist, ptr, n);
		ptr += s;
		n -= s;
	}
}

void
gray_slist_append_char(gray_slist_t slist, char c)
{
	gray_slist_append(slist, &c, 1);
}	

size_t
gray_slist_size(gray_slist_t slist)
{
	size_t size = 0;
	struct gray_slist_bucket *p;
	for (p = slist->head; p; p = p->next)
		size += p->level;
	return size;
}

size_t
gray_slist_coalesce(gray_slist_t slist)
{
	size_t size;

	if (slist->head && slist->head->next == NULL)
		size = slist->head->level;
	else {
		size = gray_slist_size(slist);
		struct gray_slist_bucket *bucket = alloc_bucket(size);
		struct gray_slist_bucket *p;
	
		for (p = slist->head; p; ) {
			struct gray_slist_bucket *next = p->next;
			memcpy(bucket->buf + bucket->level, p->buf, p->level);
			bucket->level += p->level;
			free(p);
			p = next;
		}
		slist->head = slist->tail = bucket;
	}
	return size;
}

void *
gray_slist_head(gray_slist_t slist, size_t *psize)
{
	if (*psize) 
		*psize = slist->head ? slist->head->level : 0;
	return slist->head ? slist->head->buf : NULL;
}

void *
gray_slist_finish(gray_slist_t slist)
{
	gray_slist_coalesce(slist);
	gray_slist_clear(slist);
	return slist->free->buf;
}


#define to_num(c) \
  (isdigit(c) ? c - '0' : (isxdigit(c) ? toupper(c) - 'A' + 10 : 255 ))

void
gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
			      int len, int base)
{
	int i;
	int val = 0;
	char *start = text;
	
	if (text[0] == '\\') {
		text++;
		if (base == 16)
			text++;
	}
	
	for (i = 0; i < len; i++) {
		int n = (unsigned char)text[i];
		if (n > 127 || (n = to_num(n)) >= base)
			break;
		val = val*base + n;
	}
	
	if (i == 0) {
		gray_slist_append(slist, start, 1);
		if (pend)
			*pend = start + 1;
	} else {
		gray_slist_append_char(slist, val);
		if (pend)
			*pend = text + i;
	}
}

int
gray_decode_backslash(int c)
{
        static char transtab[] = "a\ab\bf\fn\nr\rt\t";
        char *p;

        for (p = transtab; *p; p += 2) {
                if (*p == c)
                        return p[1];
        }
        return c;
}

void
gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp)
{
	if (text[1] == '\\' || (unsigned char)text[1] > 127) {
		gray_slist_append_char(slist, text[1]);
		text += 2;
	} else if (isdigit(text[1])) 
		gray_slist_grow_backslash_num(slist, text, &text, 3, 8);
	else if (text[1] == 'x' || text[1] == 'X')
		gray_slist_grow_backslash_num(slist, text, &text, 2, 16);
	else {
		int c = gray_decode_backslash(text[1]);
		gray_slist_append_char(slist, c);
		text += 2;
	}
		
	*endp = text;
}

Return to:

Send suggestions and report system problems to the System administrator.