1/*
2Copyright 2013 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18*/
19
20#include "gzip_container.h"
21#include "util.h"
22
23#include <stdio.h>
24
25#include "deflate.h"
26
27/* Table of CRCs of all 8-bit messages. */
28static unsigned long crc_table[256];
29
30/* Flag: has the table been computed? Initially false. */
31static int crc_table_computed = 0;
32
33/* Makes the table for a fast CRC. */
34static void MakeCRCTable() {
35  unsigned long c;
36  int n, k;
37  for (n = 0; n < 256; n++) {
38    c = (unsigned long) n;
39    for (k = 0; k < 8; k++) {
40      if (c & 1) {
41        c = 0xedb88320L ^ (c >> 1);
42      } else {
43        c = c >> 1;
44      }
45    }
46    crc_table[n] = c;
47  }
48  crc_table_computed = 1;
49}
50
51
52/*
53Updates a running crc with the bytes buf[0..len-1] and returns
54the updated crc. The crc should be initialized to zero.
55*/
56static unsigned long UpdateCRC(unsigned long crc,
57                               const unsigned char *buf, size_t len) {
58  unsigned long c = crc ^ 0xffffffffL;
59  unsigned n;
60
61  if (!crc_table_computed)
62    MakeCRCTable();
63  for (n = 0; n < len; n++) {
64    c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
65  }
66  return c ^ 0xffffffffL;
67}
68
69/* Returns the CRC of the bytes buf[0..len-1]. */
70static unsigned long CRC(const unsigned char* buf, int len) {
71  return UpdateCRC(0L, buf, len);
72}
73
74/*
75Compresses the data according to the gzip specification.
76*/
77void ZopfliGzipCompress(const ZopfliOptions* options,
78                        const unsigned char* in, size_t insize,
79                        unsigned char** out, size_t* outsize) {
80  unsigned long crcvalue = CRC(in, insize);
81  unsigned char bp = 0;
82
83  ZOPFLI_APPEND_DATA(31, out, outsize);  /* ID1 */
84  ZOPFLI_APPEND_DATA(139, out, outsize);  /* ID2 */
85  ZOPFLI_APPEND_DATA(8, out, outsize);  /* CM */
86  ZOPFLI_APPEND_DATA(0, out, outsize);  /* FLG */
87  /* MTIME */
88  ZOPFLI_APPEND_DATA(0, out, outsize);
89  ZOPFLI_APPEND_DATA(0, out, outsize);
90  ZOPFLI_APPEND_DATA(0, out, outsize);
91  ZOPFLI_APPEND_DATA(0, out, outsize);
92
93  ZOPFLI_APPEND_DATA(2, out, outsize);  /* XFL, 2 indicates best compression. */
94  ZOPFLI_APPEND_DATA(3, out, outsize);  /* OS follows Unix conventions. */
95
96  ZopfliDeflate(options, 2 /* Dynamic block */, 1,
97                in, insize, &bp, out, outsize);
98
99  /* CRC */
100  ZOPFLI_APPEND_DATA(crcvalue % 256, out, outsize);
101  ZOPFLI_APPEND_DATA((crcvalue >> 8) % 256, out, outsize);
102  ZOPFLI_APPEND_DATA((crcvalue >> 16) % 256, out, outsize);
103  ZOPFLI_APPEND_DATA((crcvalue >> 24) % 256, out, outsize);
104
105  /* ISIZE */
106  ZOPFLI_APPEND_DATA(insize % 256, out, outsize);
107  ZOPFLI_APPEND_DATA((insize >> 8) % 256, out, outsize);
108  ZOPFLI_APPEND_DATA((insize >> 16) % 256, out, outsize);
109  ZOPFLI_APPEND_DATA((insize >> 24) % 256, out, outsize);
110
111  if (options->verbose) {
112    fprintf(stderr,
113            "Original Size: %d, Gzip: %d, Compression: %f%% Removed\n",
114            (int)insize, (int)*outsize,
115            100.0 * (double)(insize - *outsize) / (double)insize);
116  }
117}
118