aboutsummaryrefslogtreecommitdiff
path: root/utils/data2c/data2c.c
blob: 9c2e997a884ed4ed39c3b7e7a9314b2fbe73e8d6 (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
#include <lib9.h>
#include <bio.h>

//#ifdef ANDROID
#undef malloc
#undef free
#undef realloc

void* malloc_(size_t l){
	return malloc(l);
}

void* realloc_(void*p, size_t l){
	return realloc(p, l);
}

void free_(void* p) {
	free(p);
}
//#endif


void
main(int argc, char *argv[])
{
	Biobuf bin, bout;
	long len;
	int n;
	uchar block[16], *c;

	if(argc != 2){
		fprint(2, "usage: data2s name\n");
		exits("usage");
	}
	setbinmode();
	Binit(&bin, 0, OREAD);
	Binit(&bout, 1, OWRITE);
	Bprint(&bout, "unsigned char %scode[] = {\n", argv[1]);
	for(len=0; (n=Bread(&bin, block, sizeof(block))) > 0; len += n){
		for(c=block; c < block+n; c++)
			if(*c)
				Bprint(&bout, "0x%ux,", *c);
			else
				Bprint(&bout, "0,");
		Bprint(&bout, "\n");
	}
	if(len == 0)
		Bprint(&bout, "0\n");	/* avoid empty initialiser */
	Bprint(&bout, "};\n");
	Bprint(&bout, "int %slen = %ld;\n", argv[1], len);
	exits(0);
}