Annotation of rpm2html/compressor.c, revision 1.3

1.1       veillard    1: /*
                      2:  * Compressor.c : read an input file and store it compressed in
                      3:  *                a C source file containing a char array containing
                      4:  *                the compressed file.
                      5:  *
                      6:  * Copyright (c) 1997 Daniel Veillard <veillard@apocalypse.org>
                      7:  * See COPYING for the status of this software.
                      8:  *
1.3     ! veillard    9:  * $Id: compressor.c,v 1.2 1998/04/30 23:41:44 veillard Exp $
1.1       veillard   10:  */
                     11: 
                     12: #include <stdio.h>
                     13: #include <stdarg.h>
                     14: #include <stdlib.h>
                     15: #include <string.h>
                     16: #include <sys/types.h>
                     17: #include <sys/stat.h>
                     18: #include <unistd.h>
                     19: #include <fcntl.h>
                     20: #include <zlib.h>
                     21: 
                     22: #define MAX_PATH_LENGHT 512
                     23: 
1.3     ! veillard   24: char *data_name;
1.1       veillard   25: char *data_file = NULL;
                     26: int main_argc;
                     27: char **main_argv;
                     28: char pwd[MAX_PATH_LENGHT];
                     29: 
                     30: void create_gz_source(char *filename) {
                     31:     char outputfile[MAX_PATH_LENGHT];
                     32:     Bytef *input;
                     33:     int size;
                     34:     Bytef *output;
1.2       veillard   35:     uLongf outsize;
1.1       veillard   36:     int res;
                     37:     struct stat st;
                     38:     int fd;
                     39:     FILE *result;
                     40:     int i;
                     41:     
                     42:     /*
                     43:      * open and read the input file in a buffer.
                     44:      */
                     45:     fd = open(filename, O_RDONLY);
                     46:     if (fd < 0) {
                     47:            perror("mkdep: open");
                     48:            return;
                     49:     }
                     50:     fstat(fd, &st);
                     51:     size = st.st_size;
                     52:     input = malloc(size);
                     53:     if (input == NULL) {
                     54:        perror("malloc failed");
                     55:        exit(1);
                     56:     }
                     57:     res = read(fd, input, size);
                     58:     if (res < size) {
                     59:        perror("read didn't complete");
                     60:        exit(1);
                     61:     }
                     62:     close(fd);
                     63: 
                     64:     /*
                     65:      * allocate an output buffer for the compressed result.
                     66:      */
                     67:     outsize = size + (size / 8) + 12;
                     68:     output = malloc(outsize);
                     69:     if (output == NULL) {
                     70:        perror("malloc failed");
                     71:        exit(1);
                     72:     }
                     73: 
                     74:     /*
                     75:      * compress the whole stuff in one pass.
                     76:      */
                     77:     if (compress(output, (uLongf *) &outsize, input, size) != Z_OK) {
                     78:         fprintf(stderr, "compress failed\n");
                     79:        exit(1);
                     80:     }
                     81: 
                     82:     /*
                     83:      * save the whole stuff as a C source file.
                     84:      */
                     85:     if (data_file == NULL) {
                     86:         sprintf(outputfile, "%s.h", filename);
                     87:        data_file = &outputfile[0];
                     88:     }
                     89:     result = fopen(data_file, "w");
                     90:     if (result == NULL) {
                     91:         fprintf(stderr, "fopen(%s)", data_file);
                     92:        perror("failed");
                     93:        exit(1);
                     94:     }
                     95:     fprintf(result, "/* output from compressor, need #include <zlib.h>\" */\n");
                     96:     fprintf(result, "#define %s_size %d\n", data_name, size);
                     97:     fprintf(result, "unsigned char %s[%d] = {", data_name, outsize);
                     98:     for (i = 0;i < (outsize - 1);i++) {
                     99:         if (!(i % 15)) fprintf(result, "\n");
                    100:        fprintf(result, "0x%02x,", output[i]);
                    101:     }
                    102:     fprintf(result, "0x%02x\n};\n\n", output[outsize - 1]);
                    103:     fprintf(result, "char *read_%s(void) {\n", data_name);
                    104:     fprintf(result, "    uLongf size = %d;\n", size + 20);
                    105:     fprintf(result, "    Bytef *buffer = malloc(%d);\n\n", size + 20);
                    106:     fprintf(result, "    if (buffer == NULL) return(NULL);\n");
                    107:     fprintf(result, "    if (uncompress(buffer, &size, %s, %d) != Z_OK) {\n",
                    108:             data_name, outsize);
                    109:     fprintf(result, "        fprintf(stderr, \"uncompress failed\");\n");
                    110:     fprintf(result, "        free(buffer);\n");
                    111:     fprintf(result, "        return(NULL);\n");
                    112:     fprintf(result, "    }\n");
                    113:     fprintf(result, "    if (size != %d) {\n", size);
                    114:     fprintf(result, "        fprintf(stderr, \"uncompress failed\");\n");
                    115:     fprintf(result, "        free(buffer);\n");
                    116:     fprintf(result, "        return(NULL);\n");
                    117:     fprintf(result, "    }\n");
                    118:     fprintf(result, "    buffer[%d] = '\\0';\n", size);
                    119:     fprintf(result, "    return(buffer);\n}\n");
                    120: 
                    121:     fclose(result);
                    122: }
                    123: 
                    124: void message(char *fmt, ...)
                    125: {
                    126:     va_list ap;
                    127: 
                    128:     va_start(ap, fmt);
                    129:     vfprintf(stderr, fmt, ap);
                    130:     va_end(ap);
                    131: }
                    132: 
1.3     ! veillard  133: char *get_data_name(char *filename) {
        !           134:     char *res;
        !           135:     char *cur;
        !           136: 
        !           137:     res = (char *) malloc((strlen(filename) + 20) * sizeof(char));
        !           138:     sprintf(res, "data_buffer_%s", filename);
        !           139:     cur = res;
        !           140:     while (*cur != '\0') {
        !           141:         if (*cur == '.') *cur = '_';
        !           142:         else if (*cur == ' ') *cur = '_';
        !           143:         else if (*cur == '/') *cur = '_';
        !           144:         else if (*cur == '\t') *cur = '_';
        !           145:        cur++;
        !           146:     }
        !           147:     return(res);
        !           148: }
1.1       veillard  149: 
                    150: int main(int argc, char **argv)
                    151: {
1.3     ! veillard  152:        char fullname[MAX_PATH_LENGHT];
1.1       veillard  153: 
                    154:        main_argc = argc;
                    155:        main_argv = argv;
                    156:         getcwd(pwd, sizeof(pwd));
                    157: 
                    158:        while (--argc > 0) {
                    159:                char *name = *++argv;
                    160: 
                    161:                 if (name[0] == '-') {
                    162:                    switch (name[1]) {
                    163:                        case 'n':
                    164:                            data_name = *++argv;
                    165:                            argc--;
                    166:                            continue;
                    167:                        case 'o':
                    168:                            data_file = *++argv;
                    169:                            argc--;
                    170:                            continue;
                    171:                            
                    172:                        default:
                    173:                            /*
                    174:                             * NOTE : dump_h has to be modified accordingly
                    175:                             *   if new options are added !
                    176:                             */
                    177:                            fprintf(stderr,"%s [options]  includes ...\n",
                    178:                                    argv[0]);
                    179:                            fprintf(stderr,"Options :\n");
                    180:                            fprintf(stderr,"\t-n data_name\n");
                    181:                            fprintf(stderr,"\t-o file_name\n");
                    182:                            exit(1);
                    183:                    }
                    184:                    continue;
                    185:                }
                    186:                if (name[0] != '/') {
1.3     ! veillard  187:                   strcpy(fullname, pwd);
        !           188:                    strcat(fullname, "/");
        !           189:                    strcat(fullname, name);
        !           190:                } else {
        !           191:                   strcpy(fullname, name);
1.1       veillard  192:                }
                    193: 
                    194:                /*
1.3     ! veillard  195:                 * do the work !
1.1       veillard  196:                 */
1.3     ! veillard  197:                data_name = get_data_name(name);
        !           198:                create_gz_source(fullname);
1.1       veillard  199: 
                    200:        }
                    201:        return 0;
                    202: }
                    203: 

Webmaster