Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: strlist.c,v 1.2 2021/01/23 19:41:16 thorpej Exp $	*/

/*-
 * Copyright (c) 2021 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * strlist --
 *
 *	A set of routines for interacting with IEEE 1275 (OpenFirmware)
 *	style string lists.
 *
 *	An OpenFirmware string list is simply a buffer containing
 *	multiple NUL-terminated strings concatenated together.
 *
 *	So, for example, the a string list consisting of the strings
 *	"foo", "bar", and "baz" would be represented in memory like:
 *
 *		foo\0bar\0baz\0
 */

#include <sys/types.h>

/*
 * Memory allocation wrappers to handle different environments.
 */
#if defined(_KERNEL)
#include <sys/kmem.h>
#include <sys/systm.h>

static void *
strlist_alloc(size_t const size)
{
	return kmem_zalloc(size, KM_SLEEP);
}

static void
strlist_free(void * const v, size_t const size)
{
	kmem_free(v, size);
}
#elif defined(_STANDALONE)
#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>

static void *
strlist_alloc(size_t const size)
{
	char *cp = alloc(size);
	if (cp != NULL) {
		memset(cp, 0, size);
	}
	return cp;
}

static void
strlist_free(void * const v, size_t const size)
{
	dealloc(v, size);
}
#else /* user-space */
#include <stdlib.h>
#include <string.h>

extern int pmatch(const char *, const char *, const char **);

static void *
strlist_alloc(size_t const size)
{
	return calloc(1, size);
}

static void
strlist_free(void * const v, size_t const size __unused)
{
	free(v);
}
#endif

#include "strlist.h"

/*
 * strlist_next --
 *
 *	Return a pointer to the next string in the strlist,
 *	or NULL if there are no more strings.
 */
const char *
strlist_next(const char * const sl, size_t const slsize, size_t * const cursorp)
{

	if (sl == NULL || slsize == 0 || cursorp == NULL) {
		return NULL;
	}

	size_t cursor = *cursorp;

	if (cursor >= slsize) {
		/* No more strings in the list. */
		return NULL;
	}

	const char *cp = sl + cursor;
	*cursorp = cursor + strlen(cp) + 1;

	return cp;
}

/*
 * strlist_count --
 *
 *	Return the number of strings in the strlist.
 */
unsigned int
strlist_count(const char *sl, size_t slsize)
{

	if (sl == NULL || slsize == 0) {
		return 0;
	}

	size_t cursize;
	unsigned int count;

	for (count = 0; slsize != 0;
	     count++, sl += cursize, slsize -= cursize) {
		cursize = strlen(sl) + 1;
	}
	return count;
}

/*
 * strlist_string --
 *
 *	Returns the string in the strlist at the specified index.
 *	Returns NULL if the index is beyond the strlist range.
 */
const char *
strlist_string(const char * sl, size_t slsize, unsigned int const idx)
{

	if (sl == NULL || slsize == 0) {
		return NULL;
	}

	size_t cursize;
	unsigned int i;

	for (i = 0; slsize != 0; i++, slsize -= cursize, sl += cursize) {
		cursize = strlen(sl) + 1;
		if (i == idx) {
			return sl;
		}
	}

	return NULL;
}

static bool
match_strcmp(const char * const s1, const char * const s2)
{
	return strcmp(s1, s2) == 0;
}

#if !defined(_STANDALONE)
static bool
match_pmatch(const char * const s1, const char * const s2)
{
	return pmatch(s1, s2, NULL) == 2;
}
#endif /* _STANDALONE */

static bool
strlist_match_internal(const char * const sl, size_t slsize,
    const char * const str, int * const indexp, unsigned int * const countp,
    bool (*match_fn)(const char *, const char *))
{
	const char *cp;
	size_t l;
	int i;
	bool rv = false;

	if (sl == NULL || slsize == 0) {
		return false;
	}

	cp = sl;

	for (i = 0; slsize != 0;
	     l = strlen(cp) + 1, slsize -= l, cp += l, i++) {
		if (rv) {
			/*
			 * We've already matched. We must be
			 * counting to the end.
			 */
			continue;
		}
		if ((*match_fn)(cp, str)) {
			/*
			 * Matched!  Get the index.  If we don't
			 * also want the total count, then get
			 * out early.
			 */
			*indexp = i;
			rv = true;
			if (countp == NULL) {
				break;
			}
		}
	}

	if (countp != NULL) {
		*countp = i;
	}

	return rv;
}

/*
 * strlist_match --
 *
 *	Returns a weighted match value (1 <= match <= sl->count) if the
 *	specified string appears in the strlist.  A match at the
 *	beginning of the list carriest the greatest weight (i.e. sl->count)
 *	and a match at the end of the list carriest the least (i.e. 1).
 *	Returns 0 if there is no match.
 *
 *	This routine operates independently of the cursor used to enumerate
 *	a strlist.
 */
int
strlist_match(const char * const sl, size_t const slsize,
    const char * const str)
{
	unsigned int count;
	int idx;

	if (strlist_match_internal(sl, slsize, str, &idx, &count,
				   match_strcmp)) {
		return count - idx;
	}
	return 0;
}

#if !defined(_STANDALONE)
/*
 * strlist_pmatch --
 *
 *	Like strlist_match(), but uses pmatch(9) to match the
 *	strings.
 */
int
strlist_pmatch(const char * const sl, size_t const slsize,
    const char * const pattern)
{
	unsigned int count;
	int idx;

	if (strlist_match_internal(sl, slsize, pattern, &idx, &count,
				   match_pmatch)) {
		return count - idx;
	}
	return 0;
}
#endif /* _STANDALONE */

/*
 * strlist_index --
 *
 *	Returns the index of the specified string if it appears
 *	in the strlist.  Returns -1 if the string is not found.
 *
 *	This routine operates independently of the cursor used to enumerate
 *	a strlist.
 */
int
strlist_index(const char * const sl, size_t const slsize,
    const char * const str)
{
	int idx;

	if (strlist_match_internal(sl, slsize, str, &idx, NULL,
				   match_strcmp)) {
		return idx;
	}
	return -1;
}

/*
 * strlist_append --
 *
 *	Append the specified string to a mutable strlist.  Turns
 *	true if successful, false upon failure for any reason.
 */
bool
strlist_append(char ** const slp, size_t * const slsizep,
    const char * const str)
{
	size_t const slsize = *slsizep;
	char * const sl = *slp;

	size_t const addsize = strlen(str) + 1;
	size_t const newsize = slsize + addsize;
	char * const newbuf = strlist_alloc(newsize);

	if (newbuf == NULL) {
		return false;
	}

	if (sl != NULL) {
		memcpy(newbuf, sl, slsize);
	}

	memcpy(newbuf + slsize, str, addsize);

	if (sl != NULL) {
		strlist_free(sl, slsize);
	}

	*slp = newbuf;
	*slsizep = newsize;

	return true;
}

#ifdef STRLIST_TEST
/*
 * To build and run the tests:
 *
 * % cc -DSTRLIST_TEST -Os pmatch.c strlist.c
 * % ./a.out
 * Testing basic properties.
 * Testing enumeration.
 * Testing weighted matching.
 * Testing pattern matching.
 * Testing index return.
 * Testing string-at-index.
 * Testing gross blob count.
 * Testing gross blob indexing.
 * Testing creating a strlist.
 * Verifying new strlist.
 * All tests completed successfully.
 * %
 */

static char nice_blob[] = "zero\0one\0two\0three\0four\0five";
static char gross_blob[] = "zero\0\0two\0\0four\0\0";

#include <assert.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
	const char *sl;
	size_t slsize;
	size_t cursor;
	const char *cp;
	size_t size;

	sl = nice_blob;
	slsize = sizeof(nice_blob);

	printf("Testing basic properties.\n");
	assert(strlist_count(sl, slsize) == 6);

	printf("Testing enumeration.\n");
	cursor = 0;
	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "zero") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "one") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "two") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "three") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "four") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) != NULL);
	assert(strcmp(cp, "five") == 0);

	assert((cp = strlist_next(sl, slsize, &cursor)) == NULL);

	printf("Testing weighted matching.\n");
	assert(strlist_match(sl, slsize, "non-existent") == 0);
	assert(strlist_match(sl, slsize, "zero") == 6);
	assert(strlist_match(sl, slsize, "one") == 5);
	assert(strlist_match(sl, slsize, "two") == 4);
	assert(strlist_match(sl, slsize, "three") == 3);
	assert(strlist_match(sl, slsize, "four") == 2);
	assert(strlist_match(sl, slsize, "five") == 1);

	printf("Testing pattern matching.\n");
	assert(strlist_pmatch(sl, slsize, "t?o") == 4);
	assert(strlist_pmatch(sl, slsize, "f[a-o][o-u][a-z]") == 2);

	printf("Testing index return.\n");
	assert(strlist_index(sl, slsize, "non-existent") == -1);
	assert(strlist_index(sl, slsize, "zero") == 0);
	assert(strlist_index(sl, slsize, "one") == 1);
	assert(strlist_index(sl, slsize, "two") == 2);
	assert(strlist_index(sl, slsize, "three") == 3);
	assert(strlist_index(sl, slsize, "four") == 4);
	assert(strlist_index(sl, slsize, "five") == 5);

	printf("Testing string-at-index.\n");
	assert(strcmp(strlist_string(sl, slsize, 0), "zero") == 0);
	assert(strcmp(strlist_string(sl, slsize, 1), "one") == 0);
	assert(strcmp(strlist_string(sl, slsize, 2), "two") == 0);
	assert(strcmp(strlist_string(sl, slsize, 3), "three") == 0);
	assert(strcmp(strlist_string(sl, slsize, 4), "four") == 0);
	assert(strcmp(strlist_string(sl, slsize, 5), "five") == 0);
	assert(strlist_string(sl, slsize, 6) == NULL);

	sl = gross_blob;
	slsize = sizeof(gross_blob);

	printf("Testing gross blob count.\n");
	assert(strlist_count(sl, slsize) == 7);

	printf("Testing gross blob indexing.\n");
	assert(strcmp(strlist_string(sl, slsize, 0), "zero") == 0);
	assert(strcmp(strlist_string(sl, slsize, 1), "") == 0);
	assert(strcmp(strlist_string(sl, slsize, 2), "two") == 0);
	assert(strcmp(strlist_string(sl, slsize, 3), "") == 0);
	assert(strcmp(strlist_string(sl, slsize, 4), "four") == 0);
	assert(strcmp(strlist_string(sl, slsize, 5), "") == 0);
	assert(strcmp(strlist_string(sl, slsize, 6), "") == 0);
	assert(strlist_string(sl, slsize, 7) == NULL);


	printf("Testing creating a strlist.\n");
	char *newsl = NULL;
	size_t newslsize = 0;
	assert(strlist_append(&newsl, &newslsize, "zero"));
	assert(strlist_append(&newsl, &newslsize, "one"));
	assert(strlist_append(&newsl, &newslsize, "two"));
	assert(strlist_append(&newsl, &newslsize, "three"));
	assert(strlist_append(&newsl, &newslsize, "four"));
	assert(strlist_append(&newsl, &newslsize, "five"));

	printf("Verifying new strlist.\n");
	assert(strlist_count(newsl, newslsize) == 6);
	assert(strcmp(strlist_string(newsl, newslsize, 0), "zero") == 0);
	assert(strcmp(strlist_string(newsl, newslsize, 1), "one") == 0);
	assert(strcmp(strlist_string(newsl, newslsize, 2), "two") == 0);
	assert(strcmp(strlist_string(newsl, newslsize, 3), "three") == 0);
	assert(strcmp(strlist_string(newsl, newslsize, 4), "four") == 0);
	assert(strcmp(strlist_string(newsl, newslsize, 5), "five") == 0);
	assert(strlist_string(newsl, newslsize, 6) == NULL);

	/* This should be equivalent to nice_blob. */
	assert(newslsize == sizeof(nice_blob));
	assert(memcmp(newsl, nice_blob, newslsize) == 0);


	printf("All tests completed successfully.\n");
	return 0;
}

#endif /* STRLIST_TEST */