1*b68bfe3fSad /* $NetBSD: bin2blob.c,v 1.1 2008/05/04 23:50:32 ad Exp $ */
2*b68bfe3fSad
3*b68bfe3fSad /*-
4*b68bfe3fSad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*b68bfe3fSad * All rights reserved.
6*b68bfe3fSad *
7*b68bfe3fSad * Redistribution and use in source and binary forms, with or without
8*b68bfe3fSad * modification, are permitted provided that the following conditions
9*b68bfe3fSad * are met:
10*b68bfe3fSad * 1. Redistributions of source code must retain the above copyright
11*b68bfe3fSad * notice, this list of conditions and the following disclaimer.
12*b68bfe3fSad * 2. Redistributions in binary form must reproduce the above copyright
13*b68bfe3fSad * notice, this list of conditions and the following disclaimer in the
14*b68bfe3fSad * documentation and/or other materials provided with the distribution.
15*b68bfe3fSad *
16*b68bfe3fSad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*b68bfe3fSad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*b68bfe3fSad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*b68bfe3fSad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*b68bfe3fSad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*b68bfe3fSad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*b68bfe3fSad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*b68bfe3fSad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*b68bfe3fSad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*b68bfe3fSad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*b68bfe3fSad * POSSIBILITY OF SUCH DAMAGE.
27*b68bfe3fSad */
28*b68bfe3fSad
29*b68bfe3fSad /*
30*b68bfe3fSad * Reads data from stdin (must be a regular file so we can do stat()), and
31*b68bfe3fSad * outputs a 32-bit array of format:
32*b68bfe3fSad *
33*b68bfe3fSad * [0] signature: BLOB
34*b68bfe3fSad * [1] original, uncompressed length
35*b68bfe3fSad * [2] length of compressed data that follows, unpadded
36*b68bfe3fSad * [n] zlib compressed data, padded to 4 bytes
37*b68bfe3fSad */
38*b68bfe3fSad
39*b68bfe3fSad #include <sys/cdefs.h>
40*b68bfe3fSad #ifndef lint
41*b68bfe3fSad __RCSID("$NetBSD: bin2blob.c,v 1.1 2008/05/04 23:50:32 ad Exp $");
42*b68bfe3fSad #endif /* !lint */
43*b68bfe3fSad
44*b68bfe3fSad #include <sys/module.h>
45*b68bfe3fSad #include <sys/stat.h>
46*b68bfe3fSad
47*b68bfe3fSad #include <stdio.h>
48*b68bfe3fSad #include <stdlib.h>
49*b68bfe3fSad #include <unistd.h>
50*b68bfe3fSad #include <string.h>
51*b68bfe3fSad #include <err.h>
52*b68bfe3fSad #include <zlib.h>
53*b68bfe3fSad
54*b68bfe3fSad int main(int, char **);
55*b68bfe3fSad
56*b68bfe3fSad #define SIG 0x424c4f42
57*b68bfe3fSad
58*b68bfe3fSad void
put(uint32_t val)59*b68bfe3fSad put(uint32_t val)
60*b68bfe3fSad {
61*b68bfe3fSad static int n;
62*b68bfe3fSad
63*b68bfe3fSad switch (n) {
64*b68bfe3fSad case 6:
65*b68bfe3fSad putc('\n', stdout);
66*b68bfe3fSad n = 0;
67*b68bfe3fSad /* FALLTHROUGH */
68*b68bfe3fSad case 0:
69*b68bfe3fSad putc('\t', stdout);
70*b68bfe3fSad break;
71*b68bfe3fSad }
72*b68bfe3fSad printf("0x%08x,", val);
73*b68bfe3fSad n++;
74*b68bfe3fSad }
75*b68bfe3fSad
76*b68bfe3fSad int
main(int argc,char ** argv)77*b68bfe3fSad main(int argc, char **argv)
78*b68bfe3fSad {
79*b68bfe3fSad struct stat sb;
80*b68bfe3fSad uint8_t *src, *dst;
81*b68bfe3fSad u_long slen, dlen;
82*b68bfe3fSad uint32_t *dp;
83*b68bfe3fSad
84*b68bfe3fSad if (fstat(STDIN_FILENO, &sb) < 0)
85*b68bfe3fSad err(1, "stat(stdin)");
86*b68bfe3fSad
87*b68bfe3fSad src = malloc(sb.st_size);
88*b68bfe3fSad dlen = sb.st_size * 1002 / 1000 + 12 + 3 + 4*2;
89*b68bfe3fSad dst = malloc(dlen);
90*b68bfe3fSad if (src == NULL || dst == NULL)
91*b68bfe3fSad errx(1, "malloc");
92*b68bfe3fSad
93*b68bfe3fSad if (read(STDIN_FILENO, src, sb.st_size) != sb.st_size)
94*b68bfe3fSad errx(1, "read");
95*b68bfe3fSad
96*b68bfe3fSad slen = sb.st_size;
97*b68bfe3fSad memset(dst, 0, dlen);
98*b68bfe3fSad if (compress2(dst, &dlen, src, slen, 9) != Z_OK)
99*b68bfe3fSad errx(1, "compress2");
100*b68bfe3fSad
101*b68bfe3fSad printf("static const uint32_t blob[] = {\n");
102*b68bfe3fSad put(SIG);
103*b68bfe3fSad put(slen);
104*b68bfe3fSad put(dlen);
105*b68bfe3fSad dlen = ((dlen + 3) & ~3) >> 2;
106*b68bfe3fSad dp = (uint32_t *)dst;
107*b68bfe3fSad while (dlen--)
108*b68bfe3fSad put(*dp++);
109*b68bfe3fSad printf("\n};\n");
110*b68bfe3fSad
111*b68bfe3fSad exit(EXIT_SUCCESS);
112*b68bfe3fSad }
113