GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Codec-compression: Add Snappy frame decompressor by yawkat · Pull Request #17153 · netty/netty

Codec-compression: Add Snappy frame decompressor #17153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 3 commits into 4.2 from snappy-frame-decompressor
Jul 28, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Codec-compression: Add Snappy frame decompressor #17153

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
80753ae
Codec-compression: Add Snappy frame decompressor
yawkat Jul 27, 2026
65896c7
Codec-compression: Test Snappy decompressor checksums
yawkat Jul 27, 2026
6ee957a
Address comment
chrisvest Jul 27, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Codec-compression: Add Snappy frame decompressor
Co-Authored-By: multicode <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@yawkat @multicode-agent
yawkat and multicode-agent committed Jul 27, 2026
commit 80753ae317198dd9931453a2de6d1d3e324d9acb

There are no files selected for viewing

307 changes: 307 additions & 0 deletions 307 ...compression/src/main/java/io/netty/handler/codec/compression/SnappyFrameDecompressor.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
/*
* Copyright 2025 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.internal.UnstableApi;

import static io.netty.handler.codec.compression.Snappy.validateChecksum;

/**
* Uncompresses a {@link ByteBuf} encoded with the Snappy framing format.
*
* See <a href="https://github.com/google/snappy/blob/master/framing_format.txt">Snappy framing format</a>.
*/
@UnstableApi
public class SnappyFrameDecompressor extends InputBufferingDecompressor {
Comment thread
chrisvest marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


private enum ChunkType {
STREAM_IDENTIFIER,
COMPRESSED_DATA,
UNCOMPRESSED_DATA,
RESERVED_UNSKIPPABLE,
RESERVED_SKIPPABLE
}

private static final int SNAPPY_IDENTIFIER_LEN = 6;
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L95
private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65536 + 4;
// An uncompressed chunk contains a 4-byte masked checksum followed by the data.
private static final int MIN_UNCOMPRESSED_DATA_SIZE = 4;
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
private static final int MAX_DECOMPRESSED_DATA_SIZE = 65536;
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
private static final int MAX_COMPRESSED_CHUNK_SIZE = 16777216 - 1;
// A compressed chunk contains a 4-byte masked checksum followed by a Snappy stream.
private static final int MIN_COMPRESSED_CHUNK_SIZE = 5;

private final Snappy snappy = new Snappy();
private final boolean validateChecksums;

private boolean started;
private int numBytesToSkip;

private ByteBuf pendingOutput;
private boolean eof;

SnappyFrameDecompressor(Builder builder, ByteBufAllocator allocator) {
super(allocator);
this.validateChecksums = builder.validateChecksums;
}

@Override
public void close() {
super.close();
if (pendingOutput != null) {
pendingOutput.release();
pendingOutput = null;
}
}

@Override
public Status status() throws DecompressionException {
if (pendingOutput != null) {
return Status.NEED_OUTPUT;
}
if (eof) {
return Status.COMPLETE;
}
return Status.NEED_INPUT;
}

@Override
public void endOfInput() throws DecompressionException {
if (available() != 0 || numBytesToSkip != 0) {
throw new DecompressionException("Unexpected end of input");
}
eof = true;
}

@Override
void processInput(ByteBuf in) throws DecompressionException {
while (in.isReadable()) {
if (numBytesToSkip != 0) {
// The last chunkType we detected was RESERVED_SKIPPABLE and we still have some bytes to skip.
int skipBytes = Math.min(numBytesToSkip, in.readableBytes());
in.skipBytes(skipBytes);
numBytesToSkip -= skipBytes;

// Let's return and try again.
continue;
}

int idx = in.readerIndex();
final int inSize = in.readableBytes();
if (inSize < 4) {
// We need to be at least able to read the chunk type identifier (one byte),
// and the length of the chunk (3 bytes) in order to proceed
return;
}

final int chunkTypeVal = in.getUnsignedByte(idx);
final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
final int chunkLength = in.getUnsignedMediumLE(idx + 1);

switch (chunkType) {
case STREAM_IDENTIFIER:
if (chunkLength != SNAPPY_IDENTIFIER_LEN) {
throw new DecompressionException("Unexpected length of stream identifier: " + chunkLength);
}

if (inSize < 4 + SNAPPY_IDENTIFIER_LEN) {
return;
}

in.skipBytes(4);
int offset = in.readerIndex();
in.skipBytes(SNAPPY_IDENTIFIER_LEN);

checkByte(in.getByte(offset++), (byte) 's');
checkByte(in.getByte(offset++), (byte) 'N');
checkByte(in.getByte(offset++), (byte) 'a');
checkByte(in.getByte(offset++), (byte) 'P');
checkByte(in.getByte(offset++), (byte) 'p');
checkByte(in.getByte(offset), (byte) 'Y');

started = true;
break;
case RESERVED_SKIPPABLE:
if (!started) {
throw new DecompressionException("Received RESERVED_SKIPPABLE tag before STREAM_IDENTIFIER");
}

in.skipBytes(4);

int skipBytes = Math.min(chunkLength, in.readableBytes());
in.skipBytes(skipBytes);
if (skipBytes != chunkLength) {
// We could skip all bytes, let's store the remaining so we can do so once we receive more
// data.
numBytesToSkip = chunkLength - skipBytes;
}
break;
case RESERVED_UNSKIPPABLE:
// The spec mandates that reserved unskippable chunks must immediately
// return an error, as we must assume that we cannot decode the stream
// correctly
throw new DecompressionException(
"Found reserved unskippable chunk type: 0x" + Integer.toHexString(chunkTypeVal));
case UNCOMPRESSED_DATA:
if (!started) {
throw new DecompressionException("Received UNCOMPRESSED_DATA tag before STREAM_IDENTIFIER");
}
if (chunkLength > MAX_UNCOMPRESSED_DATA_SIZE) {
throw new DecompressionException("Received UNCOMPRESSED_DATA larger than " +
MAX_UNCOMPRESSED_DATA_SIZE + " bytes");
}
if (chunkLength < MIN_UNCOMPRESSED_DATA_SIZE) {
throw new DecompressionException("Received UNCOMPRESSED_DATA with invalid chunk length: " +
chunkLength);
}

if (inSize < 4 + chunkLength) {
return;
}

in.skipBytes(4);
if (validateChecksums) {
int checksum = in.readIntLE();
validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4);
} else {
in.skipBytes(4);
}
pendingOutput = in.readRetainedSlice(chunkLength - 4);
return;
case COMPRESSED_DATA:
if (!started) {
throw new DecompressionException("Received COMPRESSED_DATA tag before STREAM_IDENTIFIER");
}

if (chunkLength > MAX_COMPRESSED_CHUNK_SIZE) {
throw new DecompressionException("Received COMPRESSED_DATA that contains" +
" chunk that exceeds " + MAX_COMPRESSED_CHUNK_SIZE + " bytes");
}
if (chunkLength < MIN_COMPRESSED_CHUNK_SIZE) {
throw new DecompressionException("Received COMPRESSED_DATA with invalid chunk length: " +
chunkLength);
}

if (inSize < 4 + chunkLength) {
return;
}

in.skipBytes(4);
int checksum = in.readIntLE();

int uncompressedSize = snappy.getPreamble(in);
if (uncompressedSize > MAX_DECOMPRESSED_DATA_SIZE) {
throw new DecompressionException("Received COMPRESSED_DATA that contains" +
" uncompressed data that exceeds " + MAX_DECOMPRESSED_DATA_SIZE + " bytes");
}

ByteBuf uncompressed = allocator.buffer(uncompressedSize, MAX_DECOMPRESSED_DATA_SIZE);
try {
if (validateChecksums) {
int oldWriterIndex = in.writerIndex();
try {
in.writerIndex(in.readerIndex() + chunkLength - 4);
snappy.decode(in, uncompressed);
} finally {
in.writerIndex(oldWriterIndex);
}
validateChecksum(checksum, uncompressed, 0, uncompressed.writerIndex());
} else {
snappy.decode(in.readSlice(chunkLength - 4), uncompressed);
}
pendingOutput = uncompressed;
uncompressed = null;
} finally {
if (uncompressed != null) {
uncompressed.release();
}
}
snappy.reset();
return;
}
}
}

@Override
ByteBuf processOutput(ByteBuf buf) throws DecompressionException {
ByteBuf p = pendingOutput;
pendingOutput = null;
return p;
}

private static void checkByte(byte actual, byte expect) {
if (actual != expect) {
throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy " +
"protocol version?");
}
}

/**
* Decodes the chunk type from the type tag byte.
*
* @param type The tag byte extracted from the stream
* @return The appropriate {@link ChunkType}, defaulting to {@link ChunkType#RESERVED_UNSKIPPABLE}
*/
private static ChunkType mapChunkType(byte type) {
if (type == 0) {
return ChunkType.COMPRESSED_DATA;
} else if (type == 1) {
return ChunkType.UNCOMPRESSED_DATA;
} else if (type == (byte) 0xff) {
return ChunkType.STREAM_IDENTIFIER;
} else if ((type & 0x80) == 0x80) {
return ChunkType.RESERVED_SKIPPABLE;
} else {
return ChunkType.RESERVED_UNSKIPPABLE;
}
}

@UnstableApi
public static Builder builder() {
return new Builder();
}

@UnstableApi
public static final class Builder extends AbstractDecompressorBuilder {
boolean validateChecksums;

Builder() {
}

/**
* If true, the checksum field will be validated against the actual
* uncompressed data, and if the checksums do not match, a suitable
* {@link DecompressionException} will be thrown. Off by default.
*
* @param validateChecksums Whether to validate the checksums
* @return This builder
*/
public Builder validateChecksums(boolean validateChecksums) {
this.validateChecksums = validateChecksums;
return this;
}

@Override
public Decompressor build(ByteBufAllocator allocator) throws DecompressionException {
return new DefensiveDecompressor(new SnappyFrameDecompressor(this, allocator));
}
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.