mzabaluev commented on code in PR #12423:
URL: https://github.com/apache/arrow-rs/pull/12423#discussion_r4670254503
##########
arrow-ipc/src/compression.rs:
##########
@@ -418,6 +418,152 @@ fn compress_lz4(
#[cfg(feature = "lz4")]
fn decompress_lz4(input: &[u8], decompressed_size: usize) -> Result<Vec<u8>,
ArrowError> {
+ match decompress_lz4_blocks(input, decompressed_size) {
+ Some(result) => result,
+ // A frame this does not take apart itself: linked blocks, a block or
content checksum,
+ // or a dictionary. Independent blocks with no checksums are what
every Arrow writer
+ // produces, so the rest take the streaming decoder rather than more
parsing here.
+ None => decompress_lz4_streaming(input, decompressed_size),
+ }
+}
+
+/// Decodes a frame of independent blocks straight into its output buffer.
+///
+/// `lz4_flex`'s `FrameDecoder` is a streaming decoder: it holds source and
destination buffers
+/// sized from the frame's *declared* maximum block size, and zero-fills the
destination up to
+/// that maximum before each block, whatever the block actually holds. The
format lets a writer
+/// declare 4 MiB blocks for a buffer of any size, so a decoder can cost
megabytes of `memset`
+/// before decompressing a few KiB -- and an IPC message decodes one buffer
after another, each
+/// with a decoder of its own.
+///
+/// None of that is needed here: the blocks decompress one after another
straight into the output,
+/// with no scratch buffer and nothing zeroed beyond the output itself.
Returns `None` for a frame
+/// outside the shape it handles, which the caller sends to the streaming
decoder.
+///
+/// Like the streaming path, this never sizes the output from
`decompressed_size` alone: that
+/// length comes from the IPC prefix, which is untrusted, so the output starts
at no more than
+/// `MAX_PREALLOC_BYTES` and grows only as the blocks fill it.
+#[cfg(feature = "lz4")]
+fn decompress_lz4_blocks(
+ input: &[u8],
+ decompressed_size: usize,
+) -> Option<Result<Vec<u8>, ArrowError>> {
+ const MAGIC: u32 = 0x184D2204;
+ const VERSION: u8 = 0b01;
+ const INDEPENDENT_BLOCKS: u8 = 0b0010_0000;
+ const BLOCK_CHECKSUM: u8 = 0b0001_0000;
+ const CONTENT_SIZE: u8 = 0b0000_1000;
+ const CONTENT_CHECKSUM: u8 = 0b0000_0100;
+ const DICTIONARY_ID: u8 = 0b0000_0001;
+ const UNCOMPRESSED_BLOCK: u32 = 0x8000_0000;
+
+ if input.len() < 7 || u32::from_le_bytes(input[0..4].try_into().ok()?) !=
MAGIC {
+ return None;
+ }
+ let flg = input[4];
+ if flg >> 6 != VERSION
+ || flg & INDEPENDENT_BLOCKS == 0
+ || flg & (BLOCK_CHECKSUM | CONTENT_CHECKSUM | DICTIONARY_ID) != 0
+ {
+ return None;
+ }
+ // Magic, FLG, BD, the content size when the frame carries one, then the
header checksum. The
+ // checksum is left to the blocks and to the caller's length check: a
header corrupt enough to
+ // matter cannot also describe blocks that decode to exactly the expected
size.
+ let at = 6 + if flg & CONTENT_SIZE != 0 { 8 } else { 0 } + 1;
+ if input.len() < at {
+ return None;
+ }
+ // The maximum size a single block of this frame may decompress to. A
block size the format
+ // does not define goes to the streaming decoder along with the other
shapes declined above.
+ let block_max = match (input[5] >> 4) & 0b111 {
+ 4 => 64 * 1024,
+ 5 => 256 * 1024,
+ 6 => 1024 * 1024,
+ 7 => 4 * 1024 * 1024,
+ _ => return None,
+ };
+
+ Some(decode_independent_blocks(
+ input,
+ at,
+ decompressed_size,
+ block_max,
+ UNCOMPRESSED_BLOCK,
Review Comment:
Does this const value need to be passed as an argument to the private
function defined in the same module?
##########
arrow-ipc/src/compression.rs:
##########
@@ -418,6 +418,152 @@ fn compress_lz4(
#[cfg(feature = "lz4")]
fn decompress_lz4(input: &[u8], decompressed_size: usize) -> Result<Vec<u8>,
ArrowError> {
+ match decompress_lz4_blocks(input, decompressed_size) {
+ Some(result) => result,
+ // A frame this does not take apart itself: linked blocks, a block or
content checksum,
+ // or a dictionary. Independent blocks with no checksums are what
every Arrow writer
+ // produces, so the rest take the streaming decoder rather than more
parsing here.
+ None => decompress_lz4_streaming(input, decompressed_size),
+ }
+}
+
+/// Decodes a frame of independent blocks straight into its output buffer.
+///
+/// `lz4_flex`'s `FrameDecoder` is a streaming decoder: it holds source and
destination buffers
+/// sized from the frame's *declared* maximum block size, and zero-fills the
destination up to
+/// that maximum before each block, whatever the block actually holds. The
format lets a writer
+/// declare 4 MiB blocks for a buffer of any size, so a decoder can cost
megabytes of `memset`
+/// before decompressing a few KiB -- and an IPC message decodes one buffer
after another, each
+/// with a decoder of its own.
+///
+/// None of that is needed here: the blocks decompress one after another
straight into the output,
+/// with no scratch buffer and nothing zeroed beyond the output itself.
Returns `None` for a frame
+/// outside the shape it handles, which the caller sends to the streaming
decoder.
+///
+/// Like the streaming path, this never sizes the output from
`decompressed_size` alone: that
+/// length comes from the IPC prefix, which is untrusted, so the output starts
at no more than
+/// `MAX_PREALLOC_BYTES` and grows only as the blocks fill it.
+#[cfg(feature = "lz4")]
+fn decompress_lz4_blocks(
+ input: &[u8],
+ decompressed_size: usize,
+) -> Option<Result<Vec<u8>, ArrowError>> {
+ const MAGIC: u32 = 0x184D2204;
+ const VERSION: u8 = 0b01;
+ const INDEPENDENT_BLOCKS: u8 = 0b0010_0000;
+ const BLOCK_CHECKSUM: u8 = 0b0001_0000;
+ const CONTENT_SIZE: u8 = 0b0000_1000;
+ const CONTENT_CHECKSUM: u8 = 0b0000_0100;
+ const DICTIONARY_ID: u8 = 0b0000_0001;
+ const UNCOMPRESSED_BLOCK: u32 = 0x8000_0000;
+
+ if input.len() < 7 || u32::from_le_bytes(input[0..4].try_into().ok()?) !=
MAGIC {
+ return None;
+ }
+ let flg = input[4];
+ if flg >> 6 != VERSION
+ || flg & INDEPENDENT_BLOCKS == 0
+ || flg & (BLOCK_CHECKSUM | CONTENT_CHECKSUM | DICTIONARY_ID) != 0
+ {
+ return None;
+ }
+ // Magic, FLG, BD, the content size when the frame carries one, then the
header checksum. The
+ // checksum is left to the blocks and to the caller's length check: a
header corrupt enough to
+ // matter cannot also describe blocks that decode to exactly the expected
size.
+ let at = 6 + if flg & CONTENT_SIZE != 0 { 8 } else { 0 } + 1;
+ if input.len() < at {
+ return None;
+ }
+ // The maximum size a single block of this frame may decompress to. A
block size the format
+ // does not define goes to the streaming decoder along with the other
shapes declined above.
+ let block_max = match (input[5] >> 4) & 0b111 {
+ 4 => 64 * 1024,
+ 5 => 256 * 1024,
+ 6 => 1024 * 1024,
+ 7 => 4 * 1024 * 1024,
+ _ => return None,
+ };
+
+ Some(decode_independent_blocks(
+ input,
+ at,
+ decompressed_size,
+ block_max,
+ UNCOMPRESSED_BLOCK,
+ ))
+}
+
+#[cfg(feature = "lz4")]
+fn decode_independent_blocks(
+ input: &[u8],
+ mut at: usize,
+ decompressed_size: usize,
+ block_max: usize,
+ uncompressed_block: u32,
+) -> Result<Vec<u8>, ArrowError> {
+ let corrupt = |detail: &str| ArrowError::IpcError(format!("Corrupt lz4
frame: {detail}"));
+ let exceeds = || {
+ ArrowError::IpcError(format!(
+ "LZ4 decompressed buffer exceeds advertised size of
{decompressed_size}"
+ ))
+ };
+ let mut output = vec![0u8;
decompressed_size.min(crate::reader::MAX_PREALLOC_BYTES)];
+ let mut written = 0usize;
+
+ loop {
+ let header = input
+ .get(at..at + 4)
+ .ok_or_else(|| corrupt("it ends before its end mark"))?;
+ let word = u32::from_le_bytes(header.try_into().unwrap());
+ at += 4;
+ if word == 0 {
+ break;
+ }
+ let len = (word & !uncompressed_block) as usize;
+ let block = input
+ .get(at..at + len)
+ .ok_or_else(|| corrupt("a block runs past its end"))?;
+ at += len;
+
+ // A block decompresses to at most the frame's declared maximum, and
the frame as a whole
+ // to at most the advertised size, so this is every byte the next
block can legitimately
+ // need. Growing to it leaves the output at the size the blocks
actually reach, and keeps
+ // a bogus advertised length from sizing the allocation.
+ let room = block_max.min(decompressed_size - written);
+ if output.len() < written + room {
+ output.resize(written + room, 0);
+ }
+ let dst = &mut output[written..written + room];
+
+ if word & uncompressed_block != 0 {
+ dst.get_mut(..len)
+ .ok_or_else(exceeds)?
+ .copy_from_slice(block);
+ written += len;
+ } else {
+ written += lz4_flex::block::decompress_into(block,
dst).map_err(|e| {
+ // `room` ran out at the advertised size rather than at the
block maximum, so the
+ // frame holds more than the IPC prefix said it would.
+ if room == decompressed_size - written {
+ exceeds()
Review Comment:
Should the error be inspected before deciding this is the cause?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]