| 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 | #define _CRT_SECURE_NO_WARNINGS |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | |
| 33 | typedef unsigned int stb_uint; |
| 34 | typedef unsigned char stb_uchar; |
| 35 | stb_uint stb_compress(stb_uchar* out, stb_uchar* in, stb_uint len); |
| 36 | |
| 37 | enum SourceEncoding |
| 38 | { |
| 39 | SourceEncoding_U8, |
| 40 | SourceEncoding_U32, |
| 41 | SourceEncoding_Base85, |
| 42 | }; |
| 43 | |
| 44 | static bool binary_to_compressed_c(const char* filename, const char* symbol, SourceEncoding source_encoding, bool use_compression, bool use_static); |
| 45 | |
| 46 | int main(int argc, char** argv) |
| 47 | { |
| 48 | if (argc < 3) |
| 49 | { |
| 50 | printf("Syntax: %s [-u8|-u32|-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>\n", argv[0]); |
| 51 | printf("Source encoding types:\n"); |
| 52 | printf(" -u8 = ~12 bytes of source per 4 bytes of data. 4 bytes in binary.\n"); |
| 53 | printf(" -u32 = ~11 bytes of source per 4 bytes of data. 4 bytes in binary. Need endianness swapping on big-endian.\n"); |
| 54 | printf(" -base85 = ~5 bytes of source per 4 bytes of data. 5 bytes in binary. Need decoder.\n"); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int argn = 1; |
| 59 | bool use_compression = true; |
| 60 | bool use_static = true; |
| 61 | SourceEncoding source_encoding = SourceEncoding_U8; |
| 62 | while (argn < (argc - 2) && argv[argn][0] == '-') |
| 63 | { |
| 64 | if (strcmp(argv[argn], "-u8") == 0) { source_encoding = SourceEncoding_U8; argn++; } |
| 65 | else if (strcmp(argv[argn], "-u32") == 0) { source_encoding = SourceEncoding_U32; argn++; } |
| 66 | else if (strcmp(argv[argn], "-base85") == 0) { source_encoding = SourceEncoding_Base85; argn++; } |
| 67 | else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; } |
| 68 | else if (strcmp(argv[argn], "-nostatic") == 0) { use_static = false; argn++; } |
| 69 | else |
| 70 | { |
| 71 | fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]); |
| 72 | return 1; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], source_encoding, use_compression, use_static); |
| 77 | if (!ret) |
| 78 | fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]); |
| 79 | return ret ? 0 : 1; |
| 80 | } |
| 81 | |
| 82 | char Encode85Byte(unsigned int x) |
| 83 | { |
| 84 | x = (x % 85) + 35; |
| 85 | return (char)((x >= '\\') ? x + 1 : x); |
| 86 | } |
| 87 | |
| 88 | bool binary_to_compressed_c(const char* filename, const char* symbol, SourceEncoding source_encoding, bool use_compression, bool use_static) |
| 89 | { |
| 90 | |
| 91 | FILE* f = fopen(filename, "rb"); |
| 92 | if (!f) return false; |
| 93 | int data_sz; |
| 94 | if (fseek(f, 0, SEEK_END) || (data_sz = (int)ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) { fclose(f); return false; } |
| 95 | char* data = new char[data_sz + 4]; |
| 96 | if (fread(data, 1, data_sz, f) != (size_t)data_sz) { fclose(f); delete[] data; return false; } |
| 97 | memset((void*)(((char*)data) + data_sz), 0, 4); |
| 98 | fclose(f); |
| 99 | |
| 100 | |
| 101 | int maxlen = data_sz + 512 + (data_sz >> 2) + sizeof(int); |
| 102 | char* compressed = use_compression ? new char[maxlen] : data; |
| 103 | int compressed_sz = use_compression ? stb_compress((stb_uchar*)compressed, (stb_uchar*)data, data_sz) : data_sz; |
| 104 | if (use_compression) |
| 105 | memset(compressed + compressed_sz, 0, maxlen - compressed_sz); |
| 106 | |
| 107 | |
| 108 | FILE* out = stdout; |
| 109 | fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz); |
| 110 | const char* static_str = use_static ? "static " : ""; |
| 111 | const char* compressed_str = use_compression ? "compressed_" : ""; |
| 112 | if (source_encoding == SourceEncoding_Base85) |
| 113 | { |
| 114 | fprintf(out, "// Exported using binary_to_compressed_c.exe -base85 \"%s\" %s\n", filename, symbol); |
| 115 | fprintf(out, "%sconst char %s_%sdata_base85[%d+1] =\n \"", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5); |
| 116 | char prev_c = 0; |
| 117 | for (int src_i = 0; src_i < compressed_sz; src_i += 4) |
| 118 | { |
| 119 | |
| 120 | unsigned int d = *(unsigned int*)(compressed + src_i); |
| 121 | for (unsigned int n5 = 0; n5 < 5; n5++, d /= 85) |
| 122 | { |
| 123 | char c = Encode85Byte(d); |
| 124 | fprintf(out, (c == '?' && prev_c == '?') ? "\\%c" : "%c", c); |
| 125 | prev_c = c; |
| 126 | } |
| 127 | if ((src_i % 112) == 112 - 4) |
| 128 | fprintf(out, "\"\n \""); |
| 129 | } |
| 130 | fprintf(out, "\";\n\n"); |
| 131 | } |
| 132 | else if (source_encoding == SourceEncoding_U8) |
| 133 | { |
| 134 | |
| 135 | fprintf(out, "// Exported using binary_to_compressed_c.exe -u8 \"%s\" %s\n", filename, symbol); |
| 136 | fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz); |
| 137 | fprintf(out, "%sconst unsigned char %s_%sdata[%d] =\n{", static_str, symbol, compressed_str, (int)compressed_sz); |
| 138 | int column = 0; |
| 139 | for (int i = 0; i < compressed_sz; i++) |
| 140 | { |
| 141 | unsigned char d = *(unsigned char*)(compressed + i); |
| 142 | if (column == 0) |
| 143 | fprintf(out, "\n "); |
| 144 | column += fprintf(out, "%d,", d); |
| 145 | if (column >= 180) |
| 146 | column = 0; |
| 147 | } |
| 148 | fprintf(out, "\n};\n\n"); |
| 149 | } |
| 150 | else if (source_encoding == SourceEncoding_U32) |
| 151 | { |
| 152 | |
| 153 | fprintf(out, "// Exported using binary_to_compressed_c.exe -u32 \"%s\" %s\n", filename, symbol); |
| 154 | fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz); |
| 155 | fprintf(out, "%sconst unsigned int %s_%sdata[%d/4] =\n{", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4); |
| 156 | int column = 0; |
| 157 | for (int i = 0; i < compressed_sz; i += 4) |
| 158 | { |
| 159 | unsigned int d = *(unsigned int*)(compressed + i); |
| 160 | if ((column++ % 14) == 0) |
| 161 | fprintf(out, "\n 0x%08x, ", d); |
| 162 | else |
| 163 | fprintf(out, "0x%08x, ", d); |
| 164 | } |
| 165 | fprintf(out, "\n};\n\n"); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | delete[] data; |
| 170 | if (use_compression) |
| 171 | delete[] compressed; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | static stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen) |
| 180 | { |
| 181 | const unsigned long ADLER_MOD = 65521; |
| 182 | unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; |
| 183 | unsigned long blocklen, i; |
| 184 | |
| 185 | blocklen = buflen % 5552; |
| 186 | while (buflen) { |
| 187 | for (i=0; i + 7 < blocklen; i += 8) { |
| 188 | s1 += buffer[0], s2 += s1; |
| 189 | s1 += buffer[1], s2 += s1; |
| 190 | s1 += buffer[2], s2 += s1; |
| 191 | s1 += buffer[3], s2 += s1; |
| 192 | s1 += buffer[4], s2 += s1; |
| 193 | s1 += buffer[5], s2 += s1; |
| 194 | s1 += buffer[6], s2 += s1; |
| 195 | s1 += buffer[7], s2 += s1; |
| 196 | |
| 197 | buffer += 8; |
| 198 | } |
| 199 | |
| 200 | for (; i < blocklen; ++i) |
| 201 | s1 += *buffer++, s2 += s1; |
| 202 | |
| 203 | s1 %= ADLER_MOD, s2 %= ADLER_MOD; |
| 204 | buflen -= blocklen; |
| 205 | blocklen = 5552; |
| 206 | } |
| 207 | return (s2 << 16) + s1; |
| 208 | } |
| 209 | |
| 210 | static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2, stb_uint maxlen) |
| 211 | { |
| 212 | stb_uint i; |
| 213 | for (i=0; i < maxlen; ++i) |
| 214 | if (m1[i] != m2[i]) return i; |
| 215 | return i; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | static stb_uchar *stb__out; |
| 221 | static FILE *stb__outfile; |
| 222 | static stb_uint stb__outbytes; |
| 223 | |
| 224 | static void stb__write(unsigned char v) |
| 225 | { |
| 226 | fputc(v, stb__outfile); |
| 227 | ++stb__outbytes; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | #define stb_out(v) do { if (stb__out) *stb__out++ = (stb_uchar) (v); else stb__write((stb_uchar) (v)); } while (0) |
| 232 | |
| 233 | static void stb_out2(stb_uint v) { stb_out(v >> 8); stb_out(v); } |
| 234 | static void stb_out3(stb_uint v) { stb_out(v >> 16); stb_out(v >> 8); stb_out(v); } |
| 235 | static void stb_out4(stb_uint v) { stb_out(v >> 24); stb_out(v >> 16); stb_out(v >> 8 ); stb_out(v); } |
| 236 | |
| 237 | static void outliterals(stb_uchar *in, int numlit) |
| 238 | { |
| 239 | while (numlit > 65536) { |
| 240 | outliterals(in,65536); |
| 241 | in += 65536; |
| 242 | numlit -= 65536; |
| 243 | } |
| 244 | |
| 245 | if (numlit == 0) ; |
| 246 | else if (numlit <= 32) stb_out (0x000020 + numlit-1); |
| 247 | else if (numlit <= 2048) stb_out2(0x000800 + numlit-1); |
| 248 | else stb_out3(0x070000 + numlit-1); |
| 249 | |
| 250 | if (stb__out) { |
| 251 | memcpy(stb__out,in,numlit); |
| 252 | stb__out += numlit; |
| 253 | } else |
| 254 | fwrite(in, 1, numlit, stb__outfile); |
| 255 | } |
| 256 | |
| 257 | static int stb__window = 0x40000; |
| 258 | |
| 259 | static int stb_not_crap(int best, int dist) |
| 260 | { |
| 261 | return ((best > 2 && dist <= 0x00100) |
| 262 | || (best > 5 && dist <= 0x04000) |
| 263 | || (best > 7 && dist <= 0x80000)); |
| 264 | } |
| 265 | |
| 266 | static stb_uint stb__hashsize = 32768; |
| 267 | |
| 268 | |
| 269 | |
| 270 | #define stb__hc(q,h,c) (((h) << 7) + ((h) >> 25) + q[c]) |
| 271 | #define stb__hc2(q,h,c,d) (((h) << 14) + ((h) >> 18) + (q[c] << 7) + q[d]) |
| 272 | #define stb__hc3(q,c,d,e) ((q[c] << 14) + (q[d] << 7) + q[e]) |
| 273 | |
| 274 | static unsigned int stb__running_adler; |
| 275 | |
| 276 | static int stb_compress_chunk(stb_uchar *history, |
| 277 | stb_uchar *start, |
| 278 | stb_uchar *end, |
| 279 | int length, |
| 280 | int *pending_literals, |
| 281 | stb_uchar **chash, |
| 282 | stb_uint mask) |
| 283 | { |
| 284 | (void)history; |
| 285 | int window = stb__window; |
| 286 | stb_uint match_max; |
| 287 | stb_uchar *lit_start = start - *pending_literals; |
| 288 | stb_uchar *q = start; |
| 289 | |
| 290 | #define STB__SCRAMBLE(h) (((h) + ((h) >> 16)) & mask) |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | while (q < start+length && q+12 < end) { |
| 296 | int m; |
| 297 | stb_uint h1,h2,h3,h4, h; |
| 298 | stb_uchar *t; |
| 299 | int best = 2, dist=0; |
| 300 | |
| 301 | if (q+65536 > end) |
| 302 | match_max = (stb_uint)(end-q); |
| 303 | else |
| 304 | match_max = 65536; |
| 305 | |
| 306 | #define stb__nc(b,d) ((d) <= window && ((b) > 9 || stb_not_crap((int)(b),(int)(d)))) |
| 307 | |
| 308 | #define STB__TRY(t,p) \ |
| 309 | if (p ? dist != (int)(q-t) : 1) \ |
| 310 | if ((m = stb_matchlen(t, q, match_max)) > best) \ |
| 311 | if (stb__nc(m,q-(t))) \ |
| 312 | best = m, dist = (int)(q - (t)) |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | h = stb__hc3(q,0, 1, 2); h1 = STB__SCRAMBLE(h); |
| 319 | t = chash[h1]; if (t) STB__TRY(t,0); |
| 320 | h = stb__hc2(q,h, 3, 4); h2 = STB__SCRAMBLE(h); |
| 321 | h = stb__hc2(q,h, 5, 6); t = chash[h2]; if (t) STB__TRY(t,1); |
| 322 | h = stb__hc2(q,h, 7, 8); h3 = STB__SCRAMBLE(h); |
| 323 | h = stb__hc2(q,h, 9,10); t = chash[h3]; if (t) STB__TRY(t,1); |
| 324 | h = stb__hc2(q,h,11,12); h4 = STB__SCRAMBLE(h); |
| 325 | t = chash[h4]; if (t) STB__TRY(t,1); |
| 326 | |
| 327 | |
| 328 | |
| 329 | chash[h1] = chash[h2] = chash[h3] = chash[h4] = q; |
| 330 | |
| 331 | if (best > 2) |
| 332 | assert(dist > 0); |
| 333 | |
| 334 | |
| 335 | if (best < 3) { |
| 336 | ++q; |
| 337 | } else if (best > 2 && best <= 0x80 && dist <= 0x100) { |
| 338 | outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 339 | stb_out(0x80 + best-1); |
| 340 | stb_out(dist-1); |
| 341 | } else if (best > 5 && best <= 0x100 && dist <= 0x4000) { |
| 342 | outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 343 | stb_out2(0x4000 + dist-1); |
| 344 | stb_out(best-1); |
| 345 | } else if (best > 7 && best <= 0x100 && dist <= 0x80000) { |
| 346 | outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 347 | stb_out3(0x180000 + dist-1); |
| 348 | stb_out(best-1); |
| 349 | } else if (best > 8 && best <= 0x10000 && dist <= 0x80000) { |
| 350 | outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 351 | stb_out3(0x100000 + dist-1); |
| 352 | stb_out2(best-1); |
| 353 | } else if (best > 9 && dist <= 0x1000000) { |
| 354 | if (best > 65536) best = 65536; |
| 355 | outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 356 | if (best <= 0x100) { |
| 357 | stb_out(0x06); |
| 358 | stb_out3(dist-1); |
| 359 | stb_out(best-1); |
| 360 | } else { |
| 361 | stb_out(0x04); |
| 362 | stb_out3(dist-1); |
| 363 | stb_out2(best-1); |
| 364 | } |
| 365 | } else { |
| 366 | ++q; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
| 371 | if (q-start < length) |
| 372 | q = start+length; |
| 373 | |
| 374 | |
| 375 | *pending_literals = (int)(q - lit_start); |
| 376 | |
| 377 | stb__running_adler = stb_adler32(stb__running_adler, start, (stb_uint)(q - start)); |
| 378 | return (int)(q - start); |
| 379 | } |
| 380 | |
| 381 | static int stb_compress_inner(stb_uchar *input, stb_uint length) |
| 382 | { |
| 383 | int literals = 0; |
| 384 | stb_uint len,i; |
| 385 | |
| 386 | stb_uchar **chash; |
| 387 | chash = (stb_uchar**) malloc(stb__hashsize * sizeof(stb_uchar*)); |
| 388 | if (chash == nullptr) return 0; |
| 389 | for (i=0; i < stb__hashsize; ++i) |
| 390 | chash[i] = nullptr; |
| 391 | |
| 392 | |
| 393 | stb_out(0x57); stb_out(0xbc); |
| 394 | stb_out2(0); |
| 395 | |
| 396 | stb_out4(0); |
| 397 | stb_out4(length); |
| 398 | stb_out4(stb__window); |
| 399 | |
| 400 | stb__running_adler = 1; |
| 401 | |
| 402 | len = stb_compress_chunk(input, input, input+length, length, &literals, chash, stb__hashsize-1); |
| 403 | assert(len == length); |
| 404 | |
| 405 | outliterals(input+length - literals, literals); |
| 406 | |
| 407 | free(chash); |
| 408 | |
| 409 | stb_out2(0x05fa); |
| 410 | |
| 411 | stb_out4(stb__running_adler); |
| 412 | |
| 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | stb_uint stb_compress(stb_uchar *out, stb_uchar *input, stb_uint length) |
| 417 | { |
| 418 | stb__out = out; |
| 419 | stb__outfile = nullptr; |
| 420 | |
| 421 | stb_compress_inner(input, length); |
| 422 | |
| 423 | return (stb_uint)(stb__out - out); |
| 424 | } |