| 98 | |
| 99 | |
| 100 | static int |
| 101 | repack_brotli(trace::File *inFile, const char *outFileName, int quality) |
| 102 | { |
| 103 | BrotliEncoderState *s = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); |
| 104 | if (!s) { |
| 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | |
| 108 | // Brotli default quality is 11. There used to be problems using quality |
| 109 | // higher than 9: |
| 110 | // |
| 111 | // - Some traces cause compression to be extremely slow. Possibly the same |
| 112 | // issue as https://github.com/google/brotli/issues/330 |
| 113 | // - Some traces get lower compression ratio with 11 than 9. Possibly the |
| 114 | // same issue as https://github.com/google/brotli/issues/222 |
| 115 | // |
| 116 | // but not any more. |
| 117 | BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, quality); |
| 118 | |
| 119 | // The larger the window, the higher the compression ratio and |
| 120 | // decompression speeds, so choose the maximum. |
| 121 | BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, 24); |
| 122 | |
| 123 | FILE *fout = fopen(outFileName, "wb"); |
| 124 | if (!fout) { |
| 125 | return EXIT_FAILURE; |
| 126 | } |
| 127 | |
| 128 | uLong inCrc = crc32(0L, Z_NULL, 0); |
| 129 | static const size_t kFileBufferSize = 1 << 16; |
| 130 | uint8_t *input = (uint8_t *)malloc(kFileBufferSize * 2); |
| 131 | uint8_t *output = input + kFileBufferSize; |
| 132 | size_t available_in = 0; |
| 133 | const uint8_t *next_in = nullptr; |
| 134 | size_t available_out = kFileBufferSize; |
| 135 | uint8_t *next_out = output; |
| 136 | bool is_eof = false; |
| 137 | do { |
| 138 | if (available_in == 0 && !is_eof) { |
| 139 | available_in = inFile->read(input, kFileBufferSize); |
| 140 | next_in = input; |
| 141 | if (available_in == 0) { |
| 142 | is_eof = true; |
| 143 | } else { |
| 144 | crc32(inCrc, reinterpret_cast<const Bytef *>(input), available_in); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (!BrotliEncoderCompressStream(s, |
| 149 | is_eof ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS, |
| 150 | &available_in, &next_in, |
| 151 | &available_out, &next_out, nullptr)) { |
| 152 | std::cerr << "error: failed to compress data\n"; |
| 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | |
| 156 | if (available_out != kFileBufferSize) { |
| 157 | size_t out_size = kFileBufferSize - available_out; |