#include #include #include #include #define INIT_BUFFER (64*1024) #define BUFFER_INCREMENT (64*1024) #define READ_SIZE (4*1024) #ifndef MIN static inline int MIN(int a, int b) { if(a < b) return a; else return b; } #endif /* !defined MIN */ static char *read_and_compress(FILE *fh, size_t *orig, size_t *ret) { unsigned long len; char *input,*output; size_t pos = 0,bufsize = INIT_BUFFER; input = malloc(bufsize); while(!feof(fh) && (*ret = fread(input+pos,1,READ_SIZE,fh)) != 0) { pos += *ret; if(pos >= (bufsize - READ_SIZE)) { bufsize += BUFFER_INCREMENT; input = realloc(input,bufsize); } } len = pos; *orig = len; output = malloc(len*2); compress2(output,&len,input,len,Z_BEST_COMPRESSION); free(input); *ret = (size_t) len; return output; } static void process(FILE *fh, const char *var_prefix) { size_t clen = 0; size_t tlen = 0; char *data; int i,j; data = read_and_compress(fh,&tlen,&clen); fprintf(stdout,"static char %s_data[] = {\n",var_prefix); for(i = 0; i < clen; i += j) { fprintf(stdout,"\t"); for(j = 0; j < MIN(12,clen-i); ++j) { fprintf(stdout,"0x%02x, ",(unsigned char) data[i+j]); } fprintf(stdout,"\n"); } fprintf(stdout,"\t0x%02x\n};\n",0); fprintf(stdout,"static size_t %s_size = %ld;\n", var_prefix,(long)clen); fprintf(stdout,"static size_t %s_original_size = %ld;\n", var_prefix,(long)tlen); } int main(int argc, char *argv[]) { char *file,*var_prefix; FILE *fh; if(argc < 3) { fprintf(stderr,"%s \n",argv[0]); return 1; } file = argv[1]; var_prefix = argv[2]; if(strcmp(file,"-") != 0) { fh = fopen(file,"rb"); if(fh == NULL) { fprintf(stderr,"Failed to open file: %s\n",file); return 1; } } else fh = stdin; process(fh,var_prefix); if(strcmp(file,"-") != 0) fclose(fh); return 0; }