in java/src/main/java/com/epam/deltix/zstd/ZstdFrameDecompressor.java [843:920]
private static FrameHeader readFrameHeader(final ByteBuffer inputBase, final int inputAddress, final int inputLimit) {
int input = inputAddress;
verify(input < inputLimit, input, "Not enough input bytes");
final int frameHeaderDescriptor = inputBase.get(input++) & 0xFF;
final boolean singleSegment = (frameHeaderDescriptor & 0b100000) != 0;
final int dictionaryDescriptor = frameHeaderDescriptor & 0b11;
final int contentSizeDescriptor = frameHeaderDescriptor >>> 6;
final int headerSize = 1 +
(singleSegment ? 0 : 1) +
(dictionaryDescriptor == 0 ? 0 : (1 << (dictionaryDescriptor - 1))) +
(contentSizeDescriptor == 0 ? (singleSegment ? 1 : 0) : (1 << contentSizeDescriptor));
verify(headerSize <= inputLimit - inputAddress, input, "Not enough input bytes");
// decode window size
int windowSize = -1;
if (!singleSegment) {
final int windowDescriptor = inputBase.get(input++) & 0xFF;
final int exponent = windowDescriptor >>> 3;
final int mantissa = windowDescriptor & 0b111;
final int base = 1 << (MIN_WINDOW_LOG + exponent);
windowSize = base + (base / 8) * mantissa;
}
// decode dictionary id
long dictionaryId = -1;
switch (dictionaryDescriptor) {
case 1:
dictionaryId = inputBase.get(input) & 0xFF;
input += SIZE_OF_BYTE;
break;
case 2:
dictionaryId = inputBase.getShort(input) & 0xFFFF;
input += SIZE_OF_SHORT;
break;
case 3:
dictionaryId = inputBase.getInt(input) & 0xFFFF_FFFFL;
input += SIZE_OF_INT;
break;
}
verify(dictionaryId == -1, input, "Custom dictionaries not supported");
// decode content size
long contentSize = -1;
switch (contentSizeDescriptor) {
case 0:
if (singleSegment) {
contentSize = inputBase.get(input) & 0xFF;
input += SIZE_OF_BYTE;
}
break;
case 1:
contentSize = inputBase.getShort(input) & 0xFFFF;
contentSize += 256;
input += SIZE_OF_SHORT;
break;
case 2:
contentSize = inputBase.getInt(input) & 0xFFFF_FFFFL;
input += SIZE_OF_INT;
break;
case 3:
contentSize = inputBase.getLong(input);
input += SIZE_OF_LONG;
break;
}
final boolean hasChecksum = (frameHeaderDescriptor & 0b100) != 0;
return new FrameHeader(
input - inputAddress,
windowSize,
contentSize,
dictionaryId,
hasChecksum);
}