From 28908a4b6137c76a8fc481f830933f9088d007d1 Mon Sep 17 00:00:00 2001 From: kramm Date: Tue, 13 Jan 2009 22:15:24 +0000 Subject: [PATCH] initial checkin --- installer/mkarchive.c | 292 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 installer/mkarchive.c diff --git a/installer/mkarchive.c b/installer/mkarchive.c new file mode 100644 index 0000000..c9c0d49 --- /dev/null +++ b/installer/mkarchive.c @@ -0,0 +1,292 @@ +#include +#include +#include +#include +#include +#include +#include +#define ZLIB_BUFFER_SIZE 16384 + +typedef struct _writer +{ + int (*write)(struct _writer*, void*data, int len); + void (*finish)(struct _writer*); + void *internal; +} writer_t; + +/* ---------------------------- include file filter ------------------------------- */ + +typedef struct _ifwrite +{ + FILE*fi; + int bp; + int pos; + int last; + char buf[1024]; + char*filename; +} ifwrite_t; + +static int writer_ifwrite_write(writer_t*out, void*data, int len) +{ + ifwrite_t*i = (ifwrite_t*)out->internal; + FILE*fo = i->fi; + int t; + for(t=0;tbp>=80) { + fprintf(fo, "\"%s\"\n", i->buf); + i->bp = 0; + } + if(c<=32 || c>0x7e || c=='"' || c == '\\' || c == '%' + || (i->last && c>='0' && c<='9') + || (i->last && c>='a' && c<='f') + || (i->last && c>='A' && c<='F') + ) { + i->bp += sprintf(i->buf+i->bp, "\\x%x",c); + i->last = 1; + } else { + i->buf[i->bp] = c; + i->buf[i->bp+1] = 0; + i->last = 0; + i->bp++; + } + i->pos++; + } +} +static void writer_ifwrite_finish(writer_t*w) +{ + ifwrite_t *i= (ifwrite_t*)w->internal; + fprintf(i->fi, "\"%s\"\n", i->buf); + fprintf(i->fi, ";\n"); + fclose(i->fi); + printf("wrote file %s\n", i->filename); + free(w->internal);w->internal = 0; +} + +writer_t*writer_init_includewriter(char*filename) +{ + writer_t*w = malloc(sizeof(writer_t)); + FILE*fi = fopen(filename, "wb"); + memset(w, 0, sizeof(writer_t)); + w->write = writer_ifwrite_write; + w->finish = writer_ifwrite_finish; + ifwrite_t*i = (ifwrite_t *)malloc(sizeof(ifwrite_t)); + i->fi = fi; + i->pos = 0; + i->bp=0; + i->last = 0; + i->filename = filename; + w->internal = i; + + fprintf(i->fi, "char* crndata =\n"); + return w; + +} + +/* ---------------------------- zlibdeflate writer -------------------------- */ + +struct zlibdeflate_t +{ + z_stream zs; + writer_t*output; + int written; + int zwritten; + unsigned char writebuffer[ZLIB_BUFFER_SIZE]; +}; + +static void zlib_error(int ret, char* msg, z_stream*zs) +{ + fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n", + msg, + ret, + zs->msg?zs->msg:"unknown"); + perror("errno:"); + exit(1); +} + +static int writer_zlibdeflate_write(writer_t*writer, void* data, int len) +{ + struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal; + int ret; + if(!z) { + fprintf(stderr, "zlib not initialized!\n"); + return 0; + } + if(!len) + return 0; + + z->zs.next_in = (Bytef *)data; + z->zs.avail_in = len; + + while(1) { + ret = deflate(&z->zs, Z_NO_FLUSH); + + if (ret != Z_OK) zlib_error(ret, "bitio:deflate_deflate", &z->zs); + + if(z->zs.next_out != z->writebuffer) { + z->zwritten += z->zs.next_out - (Bytef*)z->writebuffer; + z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer); + z->zs.next_out = z->writebuffer; + z->zs.avail_out = ZLIB_BUFFER_SIZE; + } + + if(!z->zs.avail_in) { + break; + } + } + z->written += len; + return len; +} +static void writer_zlibdeflate_finish(writer_t*writer) +{ + struct zlibdeflate_t*z = (struct zlibdeflate_t*)writer->internal; + writer_t*output; + int ret; + if(!z) + return; + output= z->output; + while(1) { + ret = deflate(&z->zs, Z_FINISH); + if (ret != Z_OK && + ret != Z_STREAM_END) zlib_error(ret, "bitio:deflate_finish", &z->zs); + + if(z->zs.next_out != z->writebuffer) { + z->zwritten += z->zs.next_out - (Bytef*)z->writebuffer; + z->output->write(z->output, z->writebuffer, z->zs.next_out - (Bytef*)z->writebuffer); + z->zs.next_out = z->writebuffer; + z->zs.avail_out = ZLIB_BUFFER_SIZE; + } + + if (ret == Z_STREAM_END) { + break; + + } + } + ret = deflateEnd(&z->zs); + if (ret != Z_OK) zlib_error(ret, "bitio:deflate_end", &z->zs); + + if(z->written) { + printf("Compression ratio: %.2f%% (%d bytes of input data, %d bytes of output data)\n", + z->zwritten*100.0 / z->written, + z->written, z->zwritten); + } else { + printf("Compression ratio: 0%% (0 bytes of input data, %d bytes of output data)\n", z->zwritten); + } + + free(writer->internal); + memset(writer, 0, sizeof(writer_t)); + output->finish(output); +} +writer_t*writer_init_zwriter(writer_t*output) +{ + writer_t*w = malloc(sizeof(writer_t)); + struct zlibdeflate_t*z; + int ret; + memset(w, 0, sizeof(writer_t)); + z = (struct zlibdeflate_t*)malloc(sizeof(struct zlibdeflate_t)); + memset(z, 0, sizeof(struct zlibdeflate_t)); + w->internal = z; + w->write = writer_zlibdeflate_write; + w->finish = writer_zlibdeflate_finish; + z->output = output; + memset(&z->zs,0,sizeof(z_stream)); + z->zs.zalloc = Z_NULL; + z->zs.zfree = Z_NULL; + z->zs.opaque = Z_NULL; + ret = deflateInit(&z->zs, 9); + if (ret != Z_OK) zlib_error(ret, "bitio:deflate_init", &z->zs); + z->zs.next_out = z->writebuffer; + z->zs.avail_out = ZLIB_BUFFER_SIZE; + z->written = 0; + z->zwritten = 0; + return w; +} + +/* ------------------------------- main ----------- ------------------------------- */ + +static int compare_filenames(const void*_n1, const void*_n2) +{ + const char*s1 = *(const char**)_n1; + const char*s2 = *(const char**)_n2; + + char*e1 = strrchr(s1, '.'); + char*e2 = strrchr(s2, '.'); + if(!e1 && !e2) + return 0; + if(e1 && !e2) + return 1; + if(!e1 && e2) + return -1; + int c = strcasecmp(e1+1,e2+1); + if(c) + return c; + return strcasecmp(s1,s2); +} + +int main (int argn, char*argv[]) +{ + int t; + char buf[320]; + + writer_t*include_writer = writer_init_includewriter("crnfiles.c"); + writer_t*zwriter = writer_init_zwriter(include_writer); + + qsort(argv+1, argn-1, sizeof(argv[0]), compare_filenames); + + for(t=1;twrite(zwriter,id,3); + zwriter->write(zwriter,&l,4); + int sl=strlen(argv[t]); + zwriter->write(zwriter,argv[t],sl+1); //write filename + zwriter->write(zwriter,buf,l); //write data + fprintf(stderr,"[%s] %s: %d bytes written.\n", id, argv[t], l); + fclose(fi); + free(buf); + } + else + { + fprintf(stderr,"%s not found.\n", argv[t]); + } + } + } + zwriter->finish(zwriter); +} + -- 1.7.10.4