"Fossies" - the Fresh Open Source Software Archive

Member "rpm2html-1.11.2/compressor.c" (25 May 1998, 5010 Bytes) of package /linux/privat/rpm2html-1.11.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "compressor.c" see the Fossies "Dox" file reference documentation.

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