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