|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cassert> |
| 16 | +#include <cstdio> |
| 17 | +#include <cstdlib> |
| 18 | + |
| 19 | +#include "absl/base/log_severity.h" |
| 20 | +#include "absl/flags/parse.h" |
| 21 | +#include "absl/log/globals.h" |
| 22 | +#include "absl/log/initialize.h" |
| 23 | +#include "absl/log/log.h" |
| 24 | +#include "zlib.h" |
| 25 | + |
| 26 | +int main(int argc, char* argv[]) { |
| 27 | + absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo); |
| 28 | + absl::ParseCommandLine(argc, argv); |
| 29 | + absl::InitializeLog(); |
| 30 | + |
| 31 | + int ret; |
| 32 | + int flush; |
| 33 | + unsigned have; |
| 34 | + z_stream strm = {}; |
| 35 | + |
| 36 | + constexpr int kChunk = 16384; |
| 37 | + unsigned char in[kChunk] = {0}; |
| 38 | + unsigned char out[kChunk] = {0}; |
| 39 | + |
| 40 | + const char* kZlibVersion = "1.2.11"; |
| 41 | + // Allocate deflate state. |
| 42 | + |
| 43 | + ret = deflateInit_(&strm, Z_DEFAULT_COMPRESSION, kZlibVersion, sizeof(strm)); |
| 44 | + |
| 45 | + if (ret != Z_OK) { |
| 46 | + return ret; |
| 47 | + } |
| 48 | + |
| 49 | + LOG(INFO) << "Starting compression"; |
| 50 | + |
| 51 | + // Compress until end of file. |
| 52 | + do { |
| 53 | + strm.avail_in = fread(in, 1, kChunk, stdin); |
| 54 | + if (ferror(stdin)) { |
| 55 | + LOG(INFO) << "Error reading from stdin"; |
| 56 | + deflateEnd(&strm); |
| 57 | + return Z_ERRNO; |
| 58 | + } |
| 59 | + flush = feof(stdin) ? Z_FINISH : Z_NO_FLUSH; |
| 60 | + strm.next_in = in; |
| 61 | + |
| 62 | + // Run deflate() on input until output buffer not full, finish compression |
| 63 | + // if all of source has been read in. |
| 64 | + do { |
| 65 | + strm.avail_out = kChunk; |
| 66 | + strm.next_out = out; |
| 67 | + |
| 68 | + ret = deflate(&strm, flush); // no bad return value. |
| 69 | + assert(ret != Z_STREAM_ERROR); // state not clobbered. |
| 70 | + have = kChunk - strm.avail_out; |
| 71 | + |
| 72 | + if (fwrite(out, 1, have, stdout) != have || ferror(stdout)) { |
| 73 | + // Not really necessary as strm did not change from last transfer. |
| 74 | + deflateEnd(&strm); |
| 75 | + return Z_ERRNO; |
| 76 | + } |
| 77 | + } while (strm.avail_out == 0); |
| 78 | + assert(strm.avail_in == 0); // all input will be used. |
| 79 | + |
| 80 | + // done when last data in file processed. |
| 81 | + } while (flush != Z_FINISH); |
| 82 | + assert(ret == Z_STREAM_END); // stream will be complete. |
| 83 | + |
| 84 | + // clean up and return. |
| 85 | + deflateEnd(&strm); |
| 86 | + |
| 87 | + return EXIT_SUCCESS; |
| 88 | +} |
0 commit comments