Add SnappyEncoder/Decoder

The encoder/decoder should work. Haven't actually hooked it into the log
namer yet, though.

Change-Id: Id20be8161d46fe080f89eee8032103977574e7ad
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/aos/events/logging/BUILD b/aos/events/logging/BUILD
index 952c16d..ef2ffd7 100644
--- a/aos/events/logging/BUILD
+++ b/aos/events/logging/BUILD
@@ -122,6 +122,44 @@
 )
 
 cc_library(
+    name = "snappy_encoder",
+    srcs = [
+        "snappy_encoder.cc",
+    ],
+    hdrs = [
+        "snappy_encoder.h",
+    ],
+    target_compatible_with = ["@platforms//os:linux"],
+    visibility = ["//visibility:public"],
+    deps = [
+        ":buffer_encoder",
+        ":crc32",
+        ":logger_fbs",
+        "//aos:configuration_fbs",
+        "//aos/containers:resizeable_buffer",
+        "@com_github_google_flatbuffers//:flatbuffers",
+        "@com_github_google_glog//:glog",
+        "@com_google_absl//absl/types:span",
+        "@snappy//:snappy",
+    ],
+)
+
+cc_test(
+    name = "snappy_encoder_test",
+    srcs = [
+        "snappy_encoder_test.cc",
+    ],
+    shard_count = 4,
+    target_compatible_with = ["@platforms//os:linux"],
+    deps = [
+        ":buffer_encoder_param_test",
+        ":snappy_encoder",
+        "//aos/testing:googletest",
+        "@com_github_google_glog//:glog",
+    ],
+)
+
+cc_library(
     name = "lzma_encoder",
     srcs = [
         "lzma_encoder.cc",
@@ -408,3 +446,12 @@
     gen_reflections = 1,
     target_compatible_with = ["@platforms//os:linux"],
 )
+
+cc_library(
+    name = "crc32",
+    srcs = ["crc32.cc"],
+    hdrs = ["crc32.h"],
+    deps = [
+        "@com_google_absl//absl/types:span",
+    ],
+)
diff --git a/aos/events/logging/buffer_encoder_param_test.cc b/aos/events/logging/buffer_encoder_param_test.cc
index 825696e..e464a52 100644
--- a/aos/events/logging/buffer_encoder_param_test.cc
+++ b/aos/events/logging/buffer_encoder_param_test.cc
@@ -70,7 +70,9 @@
           decoder->Read(chunk.data(), chunk.data() + chunk_size);
       if (read_result + total_decoded_size != total_encoded_size) {
         // We didn't read anything, so we should've read the complete chunk.
-        ASSERT_EQ(read_result, chunk_size);
+        ASSERT_EQ(read_result, chunk_size)
+            << "Read " << read_result + total_decoded_size << " of "
+            << total_encoded_size << " expected bytes.";
       }
       // Eventually we'll get here, once the decoder is really sure it's done.
       if (read_result == 0) {
diff --git a/aos/events/logging/crc32.cc b/aos/events/logging/crc32.cc
new file mode 100644
index 0000000..cd9d9fb
--- /dev/null
+++ b/aos/events/logging/crc32.cc
@@ -0,0 +1,67 @@
+#include "aos/events/logging/crc32.h"
+
+namespace aos {
+
+namespace {
+const uint32_t kCrc32Table[256] = {
+    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+    0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+    0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+    0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+    0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+    0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+    0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+    0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+    0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+    0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+    0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+    0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+    0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+    0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+    0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+    0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+    0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+    0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
+}
+
+uint32_t ComputeCrc32(const absl::Span<uint8_t> data) {
+  return AccumulateCrc32(data, std::nullopt);
+}
+
+uint32_t AccumulateCrc32(
+    const absl::Span<uint8_t> data,
+    std::optional<uint32_t> current_checksum) {
+  uint32_t crc =
+      current_checksum.has_value() ? current_checksum.value() : 0xFF'FF'FF'FF;
+  for (const uint8_t n : data) {
+    crc = kCrc32Table[(crc ^ n) & 0xFF] ^ (crc >> 8);
+  }
+  return crc;
+}
+
+}  // namespace aos
diff --git a/aos/events/logging/crc32.h b/aos/events/logging/crc32.h
new file mode 100644
index 0000000..7d21704
--- /dev/null
+++ b/aos/events/logging/crc32.h
@@ -0,0 +1,11 @@
+#include "absl/types/span.h"
+
+namespace aos {
+
+uint32_t ComputeCrc32(const absl::Span<uint8_t> data);
+
+uint32_t AccumulateCrc32(
+    const absl::Span<uint8_t> data,
+    std::optional<uint32_t> current_checksum = std::nullopt);
+
+}  // namespace aos
diff --git a/aos/events/logging/snappy_encoder.cc b/aos/events/logging/snappy_encoder.cc
new file mode 100644
index 0000000..d57b3dd
--- /dev/null
+++ b/aos/events/logging/snappy_encoder.cc
@@ -0,0 +1,249 @@
+#include "aos/events/logging/snappy_encoder.h"
+
+#include "aos/events/logging/crc32.h"
+#include "external/snappy/snappy.h"
+
+namespace aos::logger {
+// Snappy file format is a series of chunks. Each chunk consists of:
+// 1-byte: Format identifier.
+// 3-bytes: Little-endian chunk length, not counting the four bytes of format
+//          + length.
+//
+// The relevant chunk identifiers are 0xff (header) and 0x00 (compressed data).
+// Compressed data's first four bytes are a CRC-32C that is masked by some
+// magic function. Technically the size of the uncompressed data per chunk
+// is supposed to be no more than 65536 bytes, but I am too lazy to enforce
+// that at the moment.
+// 0x01 is the type for uncompressed data, which has not been implemented here.
+namespace {
+const std::vector<uint8_t> kSnappyIdentifierChunk = {
+    0xFF, 0x06, 0x00, 0x00, 's', 'N', 'a', 'P', 'p', 'Y'};
+
+// Magic mask that snappy's framing format requires for some reason.
+uint32_t MaskChecksum(uint32_t x) {
+  return ((x >> 15) | (x << 17)) + 0xa282ead8;
+}
+
+uint32_t SnappyChecksum(uint8_t *data, size_t size) {
+  return MaskChecksum(ComputeCrc32({data, size}));
+}
+}  // namespace
+
+SnappyEncoder::SnappyEncoder(size_t chunk_size) : chunk_size_(chunk_size) {
+  queue_.emplace_back();
+  queue_.back().resize(kSnappyIdentifierChunk.size());
+  std::copy(kSnappyIdentifierChunk.begin(), kSnappyIdentifierChunk.end(),
+            queue_.back().begin());
+  total_bytes_ += queue_.back().size();
+}
+
+void SnappyEncoder::Finish() { EncodeCurrentBuffer(); }
+
+void SnappyEncoder::Encode(flatbuffers::DetachedBuffer &&in) {
+  accumulated_checksum_ =
+      AccumulateCrc32({in.data(), in.size()}, accumulated_checksum_);
+  buffer_source_.AppendBuffer(std::move(in));
+
+  if (buffer_source_.Available() >= chunk_size_) {
+    EncodeCurrentBuffer();
+  }
+}
+
+void SnappyEncoder::EncodeCurrentBuffer() {
+  if (buffer_source_.Available() == 0) {
+    return;
+  }
+  const uint32_t uncompressed_checksum =
+      MaskChecksum(accumulated_checksum_.value());
+  queue_.emplace_back();
+  constexpr size_t kPrefixSize = 8;
+  queue_.back().resize(kPrefixSize +
+                       snappy::MaxCompressedLength(buffer_source_.Available()));
+  queue_.back().data()[0] = '\x00';
+  char *const compressed_start =
+      reinterpret_cast<char *>(queue_.back().data()) + kPrefixSize;
+  snappy::UncheckedByteArraySink snappy_sink(compressed_start);
+  const size_t compressed_size =
+      snappy::Compress(&buffer_source_, &snappy_sink);
+  CHECK_LT(compressed_size + kPrefixSize, queue_.back().size());
+  queue_.back().resize(compressed_size + kPrefixSize);
+  const size_t chunk_size = compressed_size + 4;
+  CHECK_LT(chunk_size, 1U << 24);
+  queue_.back().data()[1] = chunk_size & 0xFF;
+  queue_.back().data()[2] = (chunk_size >> 8) & 0xFF;
+  queue_.back().data()[3] = (chunk_size >> 16) & 0xFF;
+  queue_.back().data()[4] = uncompressed_checksum & 0xFF;
+  queue_.back().data()[5] = (uncompressed_checksum >> 8) & 0xFF;
+  queue_.back().data()[6] = (uncompressed_checksum >> 16) & 0xFF;
+  queue_.back().data()[7] = (uncompressed_checksum >> 24) & 0xFF;
+
+  total_bytes_ += queue_.back().size();
+
+  accumulated_checksum_.reset();
+  CHECK_EQ(0u, buffer_source_.Available());
+}
+
+void SnappyEncoder::Clear(int n) {
+  CHECK_GE(n, 0);
+  CHECK_LE(static_cast<size_t>(n), queue_size());
+  queue_.erase(queue_.begin(), queue_.begin() + n);
+}
+
+size_t SnappyEncoder::queued_bytes() const {
+  size_t bytes = 0;
+  for (const auto &buffer : queue_) {
+    bytes += buffer.size();
+  }
+  return bytes;
+}
+
+std::vector<absl::Span<const uint8_t>> SnappyEncoder::queue() const {
+  std::vector<absl::Span<const uint8_t>> queue;
+  queue.reserve(queue_.size());
+  for (const auto &buffer : queue_) {
+    queue.emplace_back(buffer.data(), buffer.size());
+  }
+  return queue;
+}
+
+size_t SnappyEncoder::DetachedBufferSource::Available() const {
+  size_t total_available = 0;
+  for (const flatbuffers::DetachedBuffer &buffer : buffers_) {
+    total_available += buffer.size();
+  }
+  return total_available;
+}
+
+const char *SnappyEncoder::DetachedBufferSource::Peek(size_t *length) {
+  *CHECK_NOTNULL(length) = buffers_[0].size() - index_into_first_buffer_;
+  return reinterpret_cast<char *>(buffers_[0].data()) +
+         index_into_first_buffer_;
+}
+
+void SnappyEncoder::DetachedBufferSource::Skip(size_t n) {
+  if (n == 0) {
+    return;
+  }
+
+  CHECK(!buffers_.empty());
+
+  index_into_first_buffer_ += n;
+  CHECK_LE(index_into_first_buffer_, buffers_[0].size())
+      << ": " << n << " is too large a skip.";
+  if (index_into_first_buffer_ == buffers_[0].size()) {
+    buffers_.erase(buffers_.begin());
+    index_into_first_buffer_ = 0;
+  }
+}
+
+void SnappyEncoder::DetachedBufferSource::AppendBuffer(
+    flatbuffers::DetachedBuffer &&buffer) {
+  buffers_.emplace_back(std::move(buffer));
+}
+
+size_t SnappyDecoder::Read(uint8_t *begin, uint8_t *end) {
+  // Decoding process:
+  // We maintain an uncompressed_buffer_ of data that may have been uncompressed
+  // in the past but which hasn't been read yet.
+  //
+  // When we run out of that buffer and need to actually read new data, we
+  // need to read things from the file in one-chunk units. Snappy expects to
+  // uncompress an entire chunk at once, so there isn't any value in attempting
+  // to be more incremental. Each chunk will be of some variable size (specified
+  // at the start of the chunk). Once we read and uncompress the chunk, we can
+  // then copy it into the user's buffer until either we run out of data and
+  // need to uncompress a new chunk or user's buffer runs out in which case
+  // we fill up their buffer and store the extra data in uncompressed_buffer_
+  // to be populated later.
+
+  uint8_t *current_output = begin;
+  if (uncompressed_buffer_.size() > 0) {
+    const size_t amount_to_copy =
+        std::min<size_t>(uncompressed_buffer_.size(), end - current_output);
+    std::memcpy(current_output, uncompressed_buffer_.data(), amount_to_copy);
+    current_output += amount_to_copy;
+    uncompressed_buffer_.erase_front(amount_to_copy);
+  }
+
+  while (current_output != end) {
+    uint8_t header[4];
+    const size_t read_length = underlying_decoder_->Read(header, header + 4);
+    if (read_length == 0) {
+      break;
+    }
+    if (read_length != 4u) {
+      LOG(WARNING) << "Logfile data is truncated.";
+      break;
+    }
+    const uint8_t chunk_type = header[0];
+    const size_t chunk_size = header[1] + (header[2] << 8) + (header[3] << 16);
+    compressed_buffer_.resize(chunk_size);
+    if (chunk_size !=
+        underlying_decoder_->Read(compressed_buffer_.data(),
+                                  compressed_buffer_.data() + chunk_size)) {
+      LOG(WARNING) << "Logfile data is truncated.";
+      break;
+    }
+    if (chunk_type == 0xFF) {
+      // Start of file, don't bother
+      // checking the contents.
+      continue;
+    } else if (chunk_type == 0x00) {
+      // Note: This section uses CHECKs rather than terminating more silently
+      // because any of these CHECK's failing implies outright data corruption
+      // rather than mere truncation.
+      CHECK_LT(4u, compressed_buffer_.size()) << ": Missing checksum.";
+      const uint32_t checksum = compressed_buffer_.data()[0] +
+                                (compressed_buffer_.data()[1] << 8) +
+                                (compressed_buffer_.data()[2] << 16) +
+                                (compressed_buffer_.data()[3] << 24);
+
+      const char *input_data =
+          reinterpret_cast<char *>(compressed_buffer_.data() + 4);
+      const size_t input_size = compressed_buffer_.size() - 4;
+
+      size_t uncompressed_length;
+      CHECK(snappy::GetUncompressedLength(input_data, input_size,
+                                          &uncompressed_length));
+
+      // If the user's buffer can fit the entire uncompressed data, we
+      // will uncompress directly into their buffer. Otherwise, we uncompress
+      // into a separate buffer and then copy the correct number of bytes out
+      // to fill up the user's buffer.
+      // TODO(james): In the future, overload a snappy::Sink to avoid
+      // unnecessary copies by extracting the initial N bytes directly into
+      // the user's buffer.
+      if (end > (uncompressed_length + current_output)) {
+        CHECK(snappy::RawUncompress(input_data, input_size,
+                                    reinterpret_cast<char *>(current_output)))
+            << ": Corrupted snappy chunk.";
+        CHECK_EQ(checksum, SnappyChecksum(current_output, uncompressed_length))
+            << ": Checksum mismatch.";
+
+        current_output += uncompressed_length;
+      } else {
+        uncompressed_buffer_.resize(uncompressed_length);
+        CHECK(snappy::RawUncompress(
+            input_data, input_size,
+            reinterpret_cast<char *>(uncompressed_buffer_.data())))
+            << ": Corrupted snappy chunk.";
+        CHECK_EQ(checksum, SnappyChecksum(uncompressed_buffer_.data(),
+                                          uncompressed_buffer_.size()))
+            << ": Checksum mismatch.";
+        std::memcpy(current_output, uncompressed_buffer_.data(),
+                    end - current_output);
+        uncompressed_buffer_.erase_front(end - current_output);
+        current_output = end;
+      }
+    } else {
+      // Unimplemented.
+      LOG(FATAL) << "Unsupported snappy chunk type "
+                 << static_cast<int>(chunk_type);
+    }
+  }
+  total_output_ += current_output - begin;
+
+  return current_output - begin;
+}
+
+}  // namespace aos::logger
diff --git a/aos/events/logging/snappy_encoder.h b/aos/events/logging/snappy_encoder.h
new file mode 100644
index 0000000..57a9a21
--- /dev/null
+++ b/aos/events/logging/snappy_encoder.h
@@ -0,0 +1,92 @@
+#ifndef AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_
+#define AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_
+
+#include <string_view>
+
+#include "absl/types/span.h"
+#include "aos/containers/resizeable_buffer.h"
+#include "aos/events/logging/buffer_encoder.h"
+#include "aos/events/logging/logger_generated.h"
+#include "external/snappy/snappy-sinksource.h"
+#include "external/snappy/snappy.h"
+#include "flatbuffers/flatbuffers.h"
+
+namespace aos::logger {
+
+// Encodes buffers using snappy.
+class SnappyEncoder final : public DetachedBufferEncoder {
+ public:
+  explicit SnappyEncoder(size_t chunk_size = 32768);
+
+  void Encode(flatbuffers::DetachedBuffer &&in) final;
+  void Finish() final;
+  void Clear(int n) final;
+  std::vector<absl::Span<const uint8_t>> queue() const final;
+  size_t queued_bytes() const final;
+  size_t total_bytes() const final { return total_bytes_; }
+  size_t queue_size() const final { return queue_.size(); }
+
+ private:
+  class DetachedBufferSource : public snappy::Source {
+   public:
+    size_t Available() const final;
+    const char *Peek(size_t *length) final;
+    void Skip(size_t n) final;
+    void AppendBuffer(flatbuffers::DetachedBuffer &&buffer);
+
+   private:
+    std::vector<flatbuffers::DetachedBuffer> buffers_;
+    size_t index_into_first_buffer_ = 0;
+  };
+
+  // Flushes buffer_source_ and stores the compressed buffer in queue_.
+  void EncodeCurrentBuffer();
+
+  // To queue up data:
+  // 1) When Encode is called, we use AppendBuffer to store the DetachedBuffer
+  //    in buffer_source_.
+  // 2) Once we've queued up at least chunk_size_ data in buffer_source_, we
+  //    use snappy to compress all the data. This flushes everything out of
+  //    buffer_source_ and adds a single buffer to queue_. Note that we do
+  //    not split up flatbuffer buffers to ensure that we produce chunks of
+  //    exactly chunk_size_ uncompressed data--if we get a 1MB DetachedBuffer
+  //    we will compress it all at once.
+  // 3) queue_ is the data that is actually read by queue() and cleared by
+  //    Clear() to be written to disk.
+  const size_t chunk_size_;
+  DetachedBufferSource buffer_source_;
+  std::optional<uint32_t> accumulated_checksum_;
+  std::vector<ResizeableBuffer> queue_;
+  size_t total_bytes_ = 0;
+};
+
+// Decompresses data with snappy.
+class SnappyDecoder final : public DataDecoder {
+ public:
+  explicit SnappyDecoder(std::unique_ptr<DataDecoder> underlying_decoder)
+      : underlying_decoder_(std::move(underlying_decoder)) {}
+  explicit SnappyDecoder(std::string_view filename)
+      : SnappyDecoder(std::make_unique<DummyDecoder>(filename)) {}
+
+  size_t Read(uint8_t *begin, uint8_t *end) final;
+  std::string_view filename() const final {
+    return underlying_decoder_->filename();
+  }
+
+ private:
+  // decoder to use for reading data out of the file itself.
+  std::unique_ptr<DataDecoder> underlying_decoder_;
+  // Buffer to use for reading data from the file. This being a member variable
+  // is purely an optimization to avoid constant reallocations on every call to
+  // Read().
+  ResizeableBuffer compressed_buffer_;
+  // Buffer of any uncompressed data that we've read but which hasn't yet been
+  // consumed by a call to Read().
+  ResizeableBuffer uncompressed_buffer_;
+
+  size_t total_output_ = 0;
+};
+
+}  // namespace aos::logger
+
+#endif  // AOS_EVENTS_LOGGING_SNAPPY_ENCODER_H_
diff --git a/aos/events/logging/snappy_encoder_test.cc b/aos/events/logging/snappy_encoder_test.cc
new file mode 100644
index 0000000..3334602
--- /dev/null
+++ b/aos/events/logging/snappy_encoder_test.cc
@@ -0,0 +1,20 @@
+#include "aos/events/logging/snappy_encoder.h"
+
+#include "aos/events/logging/buffer_encoder_param_test.h"
+#include "aos/util/file.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace aos::logger::testing {
+
+INSTANTIATE_TEST_SUITE_P(
+    Snappy, BufferEncoderTest,
+    ::testing::Combine(::testing::Values([]() {
+                         return std::make_unique<SnappyEncoder>();
+                       }),
+                       ::testing::Values([](std::string_view filename) {
+                         return std::make_unique<SnappyDecoder>(filename);
+                       }),
+                       ::testing::Range(0, 100)));
+
+}  // namespace aos::logger::testing