summaryrefslogtreecommitdiffabout
path: root/lib/slist.c
blob: 46e516e72c374df3bb37c57030c62579fcce0901 (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
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
/* This file is part of pam-modules.
   Copyright (C) 2008, 2010-2012, 2014-2015, 2018 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;
	int ec; /* error code */
};

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

static int
alloc_pool(gray_slist_t slist, size_t size)
{
	struct gray_slist_bucket *p = alloc_bucket(GRAY_SLIST_BUCKET_SIZE);
	if (!p) {
		slist->ec = errno;
		return 1;
	}
	if (slist->tail)
		slist->tail->next = p;
	else
		slist->head = p;
	slist->tail = p;
	return 0;
}

static ssize_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) {
		if (alloc_pool(slist, GRAY_SLIST_BUCKET_SIZE))
			return -1;
	}
	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(void)
{
	gray_slist_t slist = malloc(sizeof(*slist));
	if (slist) {
		slist->head = slist->tail = slist->free = 0;
		slist->ec = 0;
	}
	return slist;
}

int
gray_slist_err(gray_slist_t slist)
{
	return slist->ec;
}

void
gray_slist_clerr(gray_slist_t slist)
{
	slist->ec = 0;
}

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;
	}
	gray_slist_clerr(slist);
}	


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

ssize_t
gray_slist_append(gray_slist_t slist, const char *str, size_t n)
{
	ssize_t total;

	if (slist->ec)
		return -1;
	total = 0;
	while (total < n) {
		ssize_t s = copy_chars(slist, str + total, n - total);
		if (s == -1)
			return -1;
		total += s;
	}
	return total;
}

ssize_t
gray_slist_append_char(gray_slist_t slist, char c)
{
	return 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;
}

ssize_t
gray_slist_coalesce(gray_slist_t slist)
{
	size_t size;

	if (slist->ec)
		return -1;
	else if (slist->head && slist->head->next == NULL)
		size = slist->head->level;
	else {
		size = gray_slist_size(slist);
		struct gray_slist_bucket *bucket, *p;

		bucket = alloc_bucket(size);
		if (!bucket)
			return -1;
	
		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)
{
	if (slist->ec)
		return NULL;
	if (gray_slist_coalesce(slist) == -1)
		return NULL;
	gray_slist_clear(slist);
	return slist->free->buf;
}

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

int
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 (slist->ec)
		return -1;
	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) {
		if (gray_slist_append(slist, start, 1) != 1)
			return -1;
		if (pend)
			*pend = start + 1;
	} else {
		if (gray_slist_append_char(slist, val) != 1)
			return -1;
		if (pend)
			*pend = text + i;
	}
	return 0;
}

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;
}

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

Return to:

Send suggestions and report system problems to the System administrator.