8 #define ZLIB_BUFFER_SIZE 16384
10 typedef struct _writer
12 int (*write)(struct _writer*, void*data, int len);
13 void (*finish)(struct _writer*);
17 /* ---------------------------- include file filter ------------------------------- */
19 typedef struct _ifwrite
29 static int writer_ifwrite_write(writer_t*out, void*data, int len)
31 ifwrite_t*i = (ifwrite_t*)out->internal;
35 unsigned char c = ((unsigned char*)data)[t];
37 fprintf(fo, "\"%s\"\n", i->buf);
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')
45 i->bp += sprintf(i->buf+i->bp, "\\x%x",c);
56 static void writer_ifwrite_finish(writer_t*w)
58 ifwrite_t *i= (ifwrite_t*)w->internal;
59 fprintf(i->fi, "\"%s\"\n", i->buf);
60 fprintf(i->fi, ";\n");
62 printf("wrote file %s\n", i->filename);
63 free(w->internal);w->internal = 0;
66 writer_t*writer_init_includewriter(char*filename)
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));
78 i->filename = filename;
81 fprintf(i->fi, "char* crndata =\n");
86 /* ---------------------------- zlibdeflate writer -------------------------- */
94 unsigned char writebuffer[ZLIB_BUFFER_SIZE];
97 static void zlib_error(int ret, char* msg, z_stream*zs)
99 fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n",
102 zs->msg?zs->msg:"unknown");
107 static int writer_zlibdeflate_write(writer_t*writer, void* data, int len)
109 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
112 fprintf(stderr, "zlib not initialized!\n");
118 z->zs.next_in = (Bytef *)data;
119 z->zs.avail_in = len;
122 ret = deflate(&z->zs, Z_NO_FLUSH);
124 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs);
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;
133 if(!z->zs.avail_in) {
140 static void writer_zlibdeflate_finish(writer_t*writer)
142 struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal;
149 ret = deflate(&z->zs, Z_FINISH);
151 ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs);
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;
160 if (ret == Z_STREAM_END) {
165 ret = deflateEnd(&z->zs);
166 if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs);
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);
173 printf("Compression ratio: 0%% (0 bytes of input data, %d bytes of output data)\n", z->zwritten);
176 free(writer->internal);
177 memset(writer, 0, sizeof(writer_t));
178 output->finish(output);
180 writer_t*writer_init_zwriter(writer_t*output)
182 writer_t*w = malloc(sizeof(writer_t));
183 struct zlibdeflate_t*z;
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));
189 w->write = writer_zlibdeflate_write;
190 w->finish = writer_zlibdeflate_finish;
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;
205 /* ------------------------------- main ----------- ------------------------------- */
207 static int compare_filenames(const void*_n1, const void*_n2)
209 const char*s1 = *(const char**)_n1;
210 const char*s2 = *(const char**)_n2;
212 char*e1 = strrchr(s1, '.');
213 char*e2 = strrchr(s2, '.');
220 int c = strcasecmp(e1+1,e2+1);
223 return strcasecmp(s1,s2);
226 int main (int argn, char*argv[])
231 writer_t*include_writer = writer_init_includewriter("crnfiles.c");
232 writer_t*zwriter = writer_init_zwriter(include_writer);
234 qsort(argv+1, argn-1, sizeof(argv[0]), compare_filenames);
240 FILE*fi=fopen(argv[t],"rb");
243 fseek(fi,0,SEEK_END);
245 fseek(fi,0,SEEK_SET);
247 char*buf=(char*)malloc(l);
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";
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);
286 fprintf(stderr,"%s not found.\n", argv[t]);
290 zwriter->finish(zwriter);