source: trunk/src/exepack_mkdata.c@ 179

Last change on this file since 179 was 1, checked in by katerina, 19 years ago

Initial import

File size: 5.2 KB
Line 
1#include "config.h"
2
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <time.h>
7
8
9#include "minilzo.h"
10
11/* integer data type that is _exactly_ 32 bit
12 */
13#if defined(HAVE_INT_32)
14#define UINT32 unsigned int
15#elif defined(HAVE_LONG_32)
16#define UINT32 unsigned long
17#elif defined(HAVE_SHORT_32)
18#define UINT32 unsigned short
19#endif
20
21#if 0
22static UINT32 cstate[3], astate[3];
23
24/* interval [0, 4294967296]
25 */
26static UINT32 taus_get_long (UINT32 * state)
27{
28#define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
29
30 state[0] = TAUSWORTHE (state[0], 13, 19, 4294967294UL, 12);
31 state[1] = TAUSWORTHE (state[1], 2, 25, 4294967288UL, 4);
32 state[2] = TAUSWORTHE (state[2], 3, 11, 4294967280UL, 17);
33
34 return (state[0] ^ state[1] ^ state[2]);
35}
36
37void taus_set_from_state (UINT32 * state, UINT32 * state0)
38{
39 state[0] = state0[0] | (UINT32) 0x03;
40 state[1] = state0[1] | (UINT32) 0x09;
41 state[2] = state0[2] | (UINT32) 0x17;
42
43 /* 'warm up'
44 */
45 taus_get_long (state);
46 taus_get_long (state);
47 taus_get_long (state);
48 taus_get_long (state);
49 taus_get_long (state);
50 taus_get_long (state);
51
52 return;
53}
54#endif
55
56
57/* Work-memory needed for compression. Allocate memory in units
58 * of `long' (instead of `char') to make sure it is properly aligned.
59 */
60#define HEAP_ALLOC(var,size) \
61 long __LZO_MMODEL var [ ((size) + (sizeof(long) - 1)) / sizeof(long) ]
62
63static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS);
64
65#include <sys/stat.h>
66#include <unistd.h>
67
68int main(int argc, char **argv)
69{
70
71 FILE * fd;
72 FILE * fd_out;
73
74 struct stat sbuf;
75
76 int num = -1;
77
78 unsigned long i;
79 unsigned long len = 0;
80 unsigned long have = 0;
81
82 /* For compression.
83 */
84 lzo_byte * inbuf;
85 lzo_byte * outbuf;
86 int r;
87 lzo_uint in_len;
88 lzo_uint out_len;
89
90
91#if 0
92 astate[0] = EXEPACK_STATE_0;
93 astate[1] = EXEPACK_STATE_1;
94 astate[2] = EXEPACK_STATE_2;
95#endif
96
97 if (argc < 4 || (num = atoi(argv[3])) < 0)
98 {
99 fprintf(stderr,
100 "Usage: exepack_mkdata <infile> <outfile> <num> [ARGS]\n");
101 exit (EXIT_FAILURE);
102 }
103
104 /* the include file
105 */
106 if (NULL == (fd_out = fopen(argv[2], "w")) )
107 {
108 fprintf(stderr, "exepack_mkdata: Error opening %s for write.\n",
109 argv[2]);
110 exit (EXIT_FAILURE);
111 }
112
113
114 /* write data
115 */
116 fprintf (fd_out, "UINT32 programkey_%d[3] = { 0x%x, 0x%x, 0x%x };\n",
117 num, EXEPACK_STATE_0, EXEPACK_STATE_1, EXEPACK_STATE_2);
118
119 if (num == 0)
120 {
121 fprintf (fd_out, "UINT32 programlen_%d = %ld;\n\n",
122 num, 0x4C4C4C4CL);
123 fprintf (fd_out, "UINT32 programlen_compressed_%d = %ld;\n\n",
124 num, 0x43434343L);
125 }
126 else
127 {
128 fprintf (fd_out, "UINT32 programlen_%d = %ld;\n\n",
129 num, 0x4D4D4D4DL);
130 fprintf (fd_out, "UINT32 programlen_compressed_%d = %ld;\n\n",
131 num, 0x44444444L);
132 }
133
134
135 if (stat (argv[1], &sbuf) < 0)
136 {
137 perror ("exepack_mkdata");
138 exit (EXIT_FAILURE);
139 }
140
141 len = (unsigned long) sbuf.st_size;
142
143 /* Because the input block may be incompressible,
144 * we must provide a little more output space in case that compression
145 * is not possible.
146 */
147 inbuf = (lzo_byte *) malloc (sizeof(lzo_byte) * len);
148 outbuf = (lzo_byte *) malloc (sizeof(lzo_byte) * (len + len / 64 + 16 + 3));
149 in_len = len;
150
151 if (NULL == inbuf || NULL == outbuf)
152 {
153 fprintf(stderr, "exepack_mkdata: Out of memory.");
154 exit (EXIT_FAILURE);
155 }
156
157 if (NULL == (fd = fopen(argv[1], "r")))
158 {
159 perror ("exepack_mkdata");
160 exit (EXIT_FAILURE);
161 }
162
163 have = fread (inbuf, 1, len, fd);
164 fclose (fd);
165
166 if (have != len)
167 {
168 fprintf (stderr, "exepack_mkdata: Error reading %s", argv[1]);
169 exit (EXIT_FAILURE);
170 }
171
172 /*
173 * Step 1: initialize the LZO library
174 */
175 if (lzo_init() != LZO_E_OK)
176 {
177 fprintf(stderr, "exepack_mkdata: lzo_init() failed\n");
178 return 3;
179 }
180
181 /*
182 * Step 3: compress from `in' to `out' with LZO1X-1
183 */
184 r = lzo1x_1_compress(inbuf, in_len, outbuf, &out_len, wrkmem);
185
186 if (r == LZO_E_OK)
187 printf("exepack_mkdata: compressed %lu bytes into %lu bytes\n",
188 (long) in_len, (long) out_len);
189 else
190 {
191 /* this should NEVER happen */
192 printf("exepack_mkdata: internal error - compression failed: %d\n", r);
193 return 2;
194 }
195
196 /* check for an incompressible block
197 */
198 if (out_len >= in_len)
199 {
200 printf("exepack_mkdata: Incompressible data.\n");
201 }
202
203 fprintf (fd_out, "lzo_byte program_%d[] = {\n", num);
204
205 fprintf (fd_out, "0x43, 0x4F, 0x4E, 0x54, 0x41, 0x49, 0x4E, 0x45, 0x52,\n");
206
207 /*
208 taus_set_from_state (cstate, astate);
209 for (i = 0; i < out_len; ++i)
210 {
211 outbuf[i] =^ (taus_get_long (cstate) & 0xff);
212 }
213 */
214
215
216 for (i = 1; i <= out_len; ++i)
217 {
218 fprintf(fd_out, "0x00,");
219 if (0 == (i % 20))
220 fprintf(fd_out, "\n");
221 }
222
223 fprintf(fd_out, "\n");
224
225 for (i = 1; i <= 256; ++i)
226 {
227 fprintf(fd_out, "0x00,");
228 if (0 == (i % 20))
229 fprintf(fd_out, "\n");
230 }
231
232 fprintf (fd_out, "0x00 };\n\n");
233
234
235 fclose (fd_out);
236
237 if (argc > 4)
238 {
239 fprintf (fd_out, "char * const programargv_%d[]={\n", num);
240
241 for(len = 4; len < (unsigned int) argc; len++)
242 fprintf(fd_out, "\"%s\",", argv[len-4]);
243
244 fprintf(fd_out, "NULL };\n\n");
245 }
246
247 return 0;
248}
Note: See TracBrowser for help on using the repository browser.