1 /* $NetBSD: incbin.c,v 1.1.1.3 2019/12/22 12:34:07 skrll Exp $ */
2
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5 * libfdt - Flat Device Tree manipulation
6 * Testcase for string escapes in dtc
7 * Copyright (C) 2006 David Gibson, IBM Corporation.
8 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdint.h>
13 #include <errno.h>
14
15 #include <libfdt.h>
16
17 #include "tests.h"
18 #include "testdata.h"
19
20 #define CHUNKSIZE 1024
21
load_file(const char * name,int * len)22 static char *load_file(const char *name, int *len)
23 {
24 FILE *f;
25 char *buf = NULL;
26 int bufsize = 0, n;
27
28 *len = 0;
29
30 f = fopen(name, "r");
31 if (!f)
32 FAIL("Couldn't open \"%s\": %s", name, strerror(errno));
33
34 while (!feof(f)) {
35 if (bufsize < (*len + CHUNKSIZE)) {
36 buf = xrealloc(buf, *len + CHUNKSIZE);
37 bufsize = *len + CHUNKSIZE;
38 }
39
40 n = fread(buf + *len, 1, CHUNKSIZE, f);
41 if (ferror(f))
42 FAIL("Error reading \"%s\": %s", name, strerror(errno));
43 *len += n;
44 }
45
46 return buf;
47 }
48
main(int argc,char * argv[])49 int main(int argc, char *argv[])
50 {
51 void *fdt;
52 char *incbin;
53 int len;
54
55 test_init(argc, argv);
56
57 incbin = load_file("incbin.bin", &len);
58 fdt = load_blob_arg(argc, argv);
59
60 check_getprop(fdt, 0, "incbin", len, incbin);
61 check_getprop(fdt, 0, "incbin-partial", 17, incbin + 13);
62
63 PASS();
64 }
65