Decode Delta-compressed bytes back to i64 values.
(data: &[u8])
| 110 | |
| 111 | /// Decode Delta-compressed bytes back to i64 values. |
| 112 | pub fn decode(data: &[u8]) -> Result<Vec<i64>, CodecError> { |
| 113 | if data.len() < 4 { |
| 114 | return Err(CodecError::Truncated { |
| 115 | expected: 4, |
| 116 | actual: data.len(), |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | let count = u32::from_le_bytes(data[0..4].try_into().map_err(|_| CodecError::Corrupt { |
| 121 | detail: "invalid header".into(), |
| 122 | })?) as usize; |
| 123 | |
| 124 | if count == 0 { |
| 125 | return Ok(Vec::new()); |
| 126 | } |
| 127 | |
| 128 | if data.len() < 12 { |
| 129 | return Err(CodecError::Truncated { |
| 130 | expected: 12, |
| 131 | actual: data.len(), |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | let first_value = |
| 136 | i64::from_le_bytes(data[4..12].try_into().map_err(|_| CodecError::Corrupt { |
| 137 | detail: "invalid first value".into(), |
| 138 | })?); |
| 139 | |
| 140 | let mut values = Vec::with_capacity(count); |
| 141 | values.push(first_value); |
| 142 | |
| 143 | let mut offset = 12; |
| 144 | for _ in 1..count { |
| 145 | if offset >= data.len() { |
| 146 | return Err(CodecError::Truncated { |
| 147 | expected: offset + 1, |
| 148 | actual: data.len(), |
| 149 | }); |
| 150 | } |
| 151 | let (encoded_delta, consumed) = read_varint(&data[offset..])?; |
| 152 | let delta = zigzag_decode(encoded_delta); |
| 153 | let value = values[values.len() - 1].wrapping_add(delta); |
| 154 | values.push(value); |
| 155 | offset += consumed; |
| 156 | } |
| 157 | |
| 158 | Ok(values) |
| 159 | } |
| 160 | |
| 161 | // --------------------------------------------------------------------------- |
| 162 | // Streaming encoder / decoder types |