initial checkin
[swftools.git] / installer / mkarchive.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <memory.h>
7 #include <zlib.h>
8 #define ZLIB_BUFFER_SIZE 16384
9
10 typedef struct _writer
11 {
12     int (*write)(struct _writer*, void*data, int len);
13     void (*finish)(struct _writer*);
14     void *internal;
15 } writer_t;
16
17 /* ---------------------------- include file filter ------------------------------- */
18
19 typedef struct _ifwrite
20 {
21     FILE*fi;
22     int bp;
23     int pos;
24     int last;
25     char buf[1024];
26     char*filename;
27 } ifwrite_t;
28
29 static int writer_ifwrite_write(writer_t*out, void*data, int len)
30 {
31     ifwrite_t*i = (ifwrite_t*)out->internal;
32     FILE*fo = i->fi;
33     int t;
34     for(t=0;t<len;t++) {
35         unsigned char c = ((unsigned char*)data)[t];
36         if(i->bp>=80) {
37             fprintf(fo, "\"%s\"\n", i->buf);
38             i->bp = 0;
39         }
40         if(c<=32 || c>0x7e || c=='"' || c == '\\' || c == '%'
41         || (i->last && c>='0' && c<='9') 
42         || (i->last && c>='a' && c<='f')
43         || (i->last && c>='A' && c<='F')
44         ) {
45             i->bp += sprintf(i->buf+i->bp, "\\x%x",c);
46             i->last = 1;
47         } else {
48             i->buf[i->bp] = c;
49             i->buf[i->bp+1] = 0;
50             i->last = 0;
51             i->bp++;
52         }
53         i->pos++;
54     }
55 }
56 static void writer_ifwrite_finish(writer_t*w)
57 {
58     ifwrite_t *i= (ifwrite_t*)w->internal;
59     fprintf(i->fi, "\"%s\"\n", i->buf);
60     fprintf(i->fi, ";\n");
61     fclose(i->fi);
62     printf("wrote file %s\n", i->filename);
63     free(w->internal);w->internal = 0;
64 }
65
66 writer_t*writer_init_includewriter(char*filename)
67 {
68     writer_t*w = malloc(sizeof(writer_t));
69     FILE*fi = fopen(filename, "wb");
70     memset(w, 0, sizeof(writer_t));
71     w->write = writer_ifwrite_write;
72     w->finish = writer_ifwrite_finish;
73     ifwrite_t*i = (ifwrite_t *)malloc(sizeof(ifwrite_t));
74     i->fi = fi;
75     i->pos = 0;
76     i->bp=0;
77     i->last = 0;
78     i->filename = filename;
79     w->internal = i;
80     
81     fprintf(i->fi, "char* crndata =\n");
82     return w;
83
84 }
85
86 /* ---------------------------- zlibdeflate writer -------------------------- */
87
88 struct zlibdeflate_t
89 {
90     z_stream zs;
91     writer_t*output;
92     int written;
93     int zwritten;
94     unsigned char writebuffer[ZLIB_BUFFER_SIZE];
95 };
96
97 static void zlib_error(int ret, char* msg, z_stream*zs)
98 {
99     fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
100           msg,
101           ret,
102           zs->msg?zs->msg:"unknown");
103     perror("errno:");
104     exit(1);
105 }
106
107 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) 
108 {
109     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
110     int ret;
111     if(!z) {
112         fprintf(stderr, "zlib not initialized!\n");
113         return 0;
114     }
115     if(!len)
116         return 0;
117     
118     z->zs.next_in = (Bytef *)data;
119     z->zs.avail_in = len;
120
121     while(1) {
122         ret = deflate(&z->zs, Z_NO_FLUSH);
123         
124         if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
125
126         if(z->zs.next_out != z->writebuffer) {
127             z->zwritten += z->zs.next_out - (Bytef*)z->writebuffer;
128             z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
129             z->zs.next_out = z->writebuffer;
130             z->zs.avail_out = ZLIB_BUFFER_SIZE;
131         }
132
133         if(!z->zs.avail_in) {
134             break;
135         }
136     }
137     z->written += len;
138     return len;
139 }
140 static void writer_zlibdeflate_finish(writer_t*writer)
141 {
142     struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
143     writer_t*output;
144     int ret;
145     if(!z)
146         return;
147     output= z->output;
148     while(1) {
149         ret = deflate(&z->zs, Z_FINISH);
150         if (ret != Z_OK &&
151             ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
152
153         if(z->zs.next_out != z->writebuffer) {
154             z->zwritten += z->zs.next_out - (Bytef*)z->writebuffer;
155             z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer);
156             z->zs.next_out = z->writebuffer;
157             z->zs.avail_out = ZLIB_BUFFER_SIZE;
158         }
159
160         if (ret == Z_STREAM_END) {
161             break;
162
163         }
164     }
165     ret = deflateEnd(&z->zs);
166     if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
167
168     if(z->written) {
169         printf("Compression ratio: %.2f%% (%d bytes of input data, %d bytes of output data)\n", 
170                 z->zwritten*100.0 / z->written,
171                 z->written, z->zwritten);
172     } else {
173         printf("Compression ratio: 0%% (0 bytes of input data, %d bytes of output data)\n", z->zwritten);
174     }
175
176     free(writer->internal);
177     memset(writer, 0, sizeof(writer_t));
178     output->finish(output); 
179 }
180 writer_t*writer_init_zwriter(writer_t*output)
181 {
182     writer_t*w = malloc(sizeof(writer_t));
183     struct zlibdeflate_t*z;
184     int ret;
185     memset(w, 0, sizeof(writer_t));
186     z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t));
187     memset(z, 0, sizeof(struct zlibdeflate_t));
188     w->internal = z;
189     w->write = writer_zlibdeflate_write;
190     w->finish = writer_zlibdeflate_finish;
191     z->output = output;
192     memset(&z->zs,0,sizeof(z_stream));
193     z->zs.zalloc = Z_NULL;
194     z->zs.zfree  = Z_NULL;
195     z->zs.opaque = Z_NULL;
196     ret = deflateInit(&z->zs, 9);
197     if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs);
198     z->zs.next_out = z->writebuffer;
199     z->zs.avail_out = ZLIB_BUFFER_SIZE;
200     z->written = 0;
201     z->zwritten = 0;
202     return w;
203 }
204
205 /* ------------------------------- main ----------- ------------------------------- */
206
207 static int compare_filenames(const void*_n1, const void*_n2)
208 {
209     const char*s1 = *(const char**)_n1;
210     const char*s2 = *(const char**)_n2;
211
212     char*e1 = strrchr(s1, '.');
213     char*e2 = strrchr(s2, '.');
214     if(!e1 && !e2)
215         return 0;
216     if(e1 && !e2)
217         return 1;
218     if(!e1 && e2)
219         return -1;
220     int c = strcasecmp(e1+1,e2+1);
221     if(c)
222         return c;
223     return strcasecmp(s1,s2);
224 }
225
226 int main (int argn, char*argv[])
227 {
228     int t;
229     char buf[320];
230
231     writer_t*include_writer = writer_init_includewriter("crnfiles.c");
232     writer_t*zwriter = writer_init_zwriter(include_writer);
233
234     qsort(argv+1, argn-1, sizeof(argv[0]), compare_filenames);
235
236     for(t=1;t<argn;t++)
237     {
238             if(argv[t][0]!='-')
239             {
240                     FILE*fi=fopen(argv[t],"rb");
241                     if(fi)
242                     {
243                             fseek(fi,0,SEEK_END);
244                             long l=ftell(fi);
245                             fseek(fi,0,SEEK_SET);
246
247                             char*buf=(char*)malloc(l);
248                             char*id="NUL";
249                             struct stat st;
250                             fstat(fileno(fi),&st);
251                             if(S_ISDIR(st.st_mode)) {id ="DIR";l=0;}
252                             else if(strstr(argv[t],".swf")) id="SWF";
253                             else if(strstr(argv[t],".gif")) id="PIC";
254                             else if(strstr(argv[t],".png")) id="PIC";
255                             else if(strstr(argv[t],".jpeg")) id="PIC";
256                             else if(strstr(argv[t],".jpg")) id="PIC";
257                             else if(strstr(argv[t],".tif")) id="PIC";
258                             else if(strstr(argv[t],".c")) id="SRC";
259                             else if(strstr(argv[t],".cc")) id="SRC";
260                             else if(strstr(argv[t],".cpp")) id="SRC";
261                             else if(strstr(argv[t],".h")) id="SRC";
262                             else if(strstr(argv[t],".class")) id="CLA";
263                             else if(strstr(argv[t],".java")) id="SRC";
264                             else if(strstr(argv[t],".exe")) id="EXE";
265                             else if(strstr(argv[t],".ttf")) id="FON";
266                             else if(strstr(argv[t],".pfa")) id="FON";
267                             else if(strstr(argv[t],".pfb")) id="FON";
268                             else if(strstr(argv[t],".afm")) id="FON";
269                             else if(strstr(argv[t],".1")) id="DOC";
270                             else if(strstr(argv[t],".doc")) id="DOC";
271                             else if(strstr(argv[t],".hlp")) id="DOC";
272                             else if(strstr(argv[t],".dll")) id="DLL";
273
274                             fread(buf,l,1,fi);
275                             zwriter->write(zwriter,id,3);
276                             zwriter->write(zwriter,&l,4);
277                             int sl=strlen(argv[t]);
278                             zwriter->write(zwriter,argv[t],sl+1); //write filename
279                             zwriter->write(zwriter,buf,l); //write data
280                             fprintf(stderr,"[%s] %s: %d bytes written.\n", id, argv[t], l);
281                             fclose(fi);
282                             free(buf);
283                     }
284                     else
285                     {
286                             fprintf(stderr,"%s not found.\n", argv[t]);
287                     }
288             }
289     }
290     zwriter->finish(zwriter);
291 }
292