@@ -5438,6 +5438,53 @@ impl<const L: usize, const R: usize> DecoderConfig<L, R> {
54385438 }
54395439}
54405440
5441+ /// Given the blob's bytes, take into account the first byte, i.e. enable_encoding? and either spit
5442+ /// out the raw bytes or zstd decode them.
5443+ pub fn decode_bytes ( bytes : & [ u8 ] ) -> std:: io:: Result < Vec < u8 > > {
5444+ let enable_encoding = bytes[ 0 ] . eq ( & 1 ) ;
5445+
5446+ // If not encoded, spit out the rest of the bytes, as it is.
5447+ if !enable_encoding {
5448+ return Ok ( bytes[ 1 ..] . to_vec ( ) ) ;
5449+ }
5450+
5451+ // The bytes following the first byte represent the zstd-encoded bytes.
5452+ let mut encoded_bytes = bytes[ 1 ..] . to_vec ( ) ;
5453+ let mut encoded_len = encoded_bytes. len ( ) ;
5454+ let mut decoded_bytes = Vec :: with_capacity ( 5 * 4096 * 32 ) ;
5455+ loop {
5456+ let mut decoder = zstd_encoder:: zstd:: stream:: read:: Decoder :: new ( encoded_bytes. as_slice ( ) ) ?;
5457+ decoder. include_magicbytes ( false ) ?;
5458+ decoder. window_log_max ( 30 ) ?;
5459+
5460+ decoded_bytes. clear ( ) ;
5461+
5462+ if std:: io:: copy ( & mut decoder, & mut decoded_bytes) . is_ok ( ) {
5463+ break ;
5464+ }
5465+
5466+ // The error above means we need to truncate the suffix 0-byte.
5467+ encoded_len -= 1 ;
5468+ encoded_bytes. truncate ( encoded_len) ;
5469+ }
5470+
5471+ Ok ( decoded_bytes)
5472+ }
5473+
5474+ /// The inverse of decode_bytes.
5475+ pub fn encode_bytes ( batch_bytes : & [ u8 ] ) -> Vec < u8 > {
5476+ let mut blob_bytes = crate :: witgen:: zstd_encode ( batch_bytes) ;
5477+
5478+ // Whether we encode batch -> blob or not.
5479+ let enable_encoding = blob_bytes. len ( ) < batch_bytes. len ( ) ;
5480+ if !enable_encoding {
5481+ blob_bytes = batch_bytes. to_vec ( ) ;
5482+ }
5483+ blob_bytes. insert ( 0 , enable_encoding as u8 ) ;
5484+
5485+ blob_bytes
5486+ }
5487+
54415488#[ cfg( test) ]
54425489mod tests {
54435490 use crate :: {
@@ -5793,51 +5840,4 @@ mod tests {
57935840
57945841 Ok ( ( ) )
57955842 }
5796- }
5797-
5798- /// Given the blob's bytes, take into account the first byte, i.e. enable_encoding? and either spit
5799- /// out the raw bytes or zstd decode them.
5800- pub fn decode_bytes ( bytes : & [ u8 ] ) -> std:: io:: Result < Vec < u8 > > {
5801- let enable_encoding = bytes[ 0 ] . eq ( & 1 ) ;
5802-
5803- // If not encoded, spit out the rest of the bytes, as it is.
5804- if !enable_encoding {
5805- return Ok ( bytes[ 1 ..] . to_vec ( ) ) ;
5806- }
5807-
5808- // The bytes following the first byte represent the zstd-encoded bytes.
5809- let mut encoded_bytes = bytes[ 1 ..] . to_vec ( ) ;
5810- let mut encoded_len = encoded_bytes. len ( ) ;
5811- let mut decoded_bytes = Vec :: with_capacity ( 5 * 4096 * 32 ) ;
5812- loop {
5813- let mut decoder = zstd_encoder:: zstd:: stream:: read:: Decoder :: new ( encoded_bytes. as_slice ( ) ) ?;
5814- decoder. include_magicbytes ( false ) ?;
5815- decoder. window_log_max ( 30 ) ?;
5816-
5817- decoded_bytes. clear ( ) ;
5818-
5819- if std:: io:: copy ( & mut decoder, & mut decoded_bytes) . is_ok ( ) {
5820- break ;
5821- }
5822-
5823- // The error above means we need to truncate the suffix 0-byte.
5824- encoded_len -= 1 ;
5825- encoded_bytes. truncate ( encoded_len) ;
5826- }
5827-
5828- Ok ( decoded_bytes)
5829- }
5830-
5831- /// The inverse of decode_bytes.
5832- pub fn encode_bytes ( batch_bytes : & [ u8 ] ) -> Vec < u8 > {
5833- let mut blob_bytes = crate :: witgen:: zstd_encode ( batch_bytes) ;
5834-
5835- // Whether we encode batch -> blob or not.
5836- let enable_encoding = blob_bytes. len ( ) < batch_bytes. len ( ) ;
5837- if !enable_encoding {
5838- blob_bytes = batch_bytes. to_vec ( ) ;
5839- }
5840- blob_bytes. insert ( 0 , enable_encoding as u8 ) ;
5841-
5842- blob_bytes
5843- }
5843+ }
0 commit comments