xref: /netbsd-src/external/gpl2/dtc/dist/tests/incbin.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: incbin.c,v 1.1.1.2 2017/06/08 15:59:26 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Testcase for string escapes in dtc
6  * Copyright (C) 2006 David Gibson, IBM Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <errno.h>
27 
28 #include <libfdt.h>
29 
30 #include "tests.h"
31 #include "testdata.h"
32 
33 #define CHUNKSIZE	1024
34 
35 static char *load_file(const char *name, int *len)
36 {
37 	FILE *f;
38 	char *buf = NULL;
39 	int bufsize = 0, n;
40 
41 	*len = 0;
42 
43 	f = fopen(name, "r");
44 	if (!f)
45 		FAIL("Couldn't open \"%s\": %s", name, strerror(errno));
46 
47 	while (!feof(f)) {
48 		if (bufsize < (*len + CHUNKSIZE)) {
49 			buf = xrealloc(buf, *len + CHUNKSIZE);
50 			bufsize = *len + CHUNKSIZE;
51 		}
52 
53 		n = fread(buf + *len, 1, CHUNKSIZE, f);
54 		if (ferror(f))
55 			FAIL("Error reading \"%s\": %s", name, strerror(errno));
56 		*len += n;
57 	}
58 
59 	return buf;
60 }
61 
62 int main(int argc, char *argv[])
63 {
64 	void *fdt;
65 	char *incbin;
66 	int len;
67 
68 	test_init(argc, argv);
69 
70 	incbin = load_file("incbin.bin", &len);
71 	fdt = load_blob_arg(argc, argv);
72 
73 	check_getprop(fdt, 0, "incbin", len, incbin);
74 	check_getprop(fdt, 0, "incbin-partial", 17, incbin + 13);
75 
76 	PASS();
77 }
78