245 // header. |
245 // header. |
246 b'\0' => Ok(Cow::Borrowed(self.bytes)), |
246 b'\0' => Ok(Cow::Borrowed(self.bytes)), |
247 // Raw revision data follows. |
247 // Raw revision data follows. |
248 b'u' => Ok(Cow::Borrowed(&self.bytes[1..])), |
248 b'u' => Ok(Cow::Borrowed(&self.bytes[1..])), |
249 // zlib (RFC 1950) data. |
249 // zlib (RFC 1950) data. |
250 b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data())), |
250 b'x' => Ok(Cow::Owned(self.uncompressed_zlib_data()?)), |
251 // zstd data. |
251 // zstd data. |
252 b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data())), |
252 b'\x28' => Ok(Cow::Owned(self.uncompressed_zstd_data()?)), |
253 format_type => Err(RevlogError::UnknowDataFormat(format_type)), |
253 format_type => Err(RevlogError::UnknowDataFormat(format_type)), |
254 } |
254 } |
255 } |
255 } |
256 |
256 |
257 fn uncompressed_zlib_data(&self) -> Vec<u8> { |
257 fn uncompressed_zlib_data(&self) -> Result<Vec<u8>, RevlogError> { |
258 let mut decoder = ZlibDecoder::new(self.bytes); |
258 let mut decoder = ZlibDecoder::new(self.bytes); |
259 if self.is_delta() { |
259 if self.is_delta() { |
260 let mut buf = Vec::with_capacity(self.compressed_len); |
260 let mut buf = Vec::with_capacity(self.compressed_len); |
261 decoder.read_to_end(&mut buf).expect("corrupted zlib data"); |
261 decoder |
262 buf |
262 .read_to_end(&mut buf) |
|
263 .or(Err(RevlogError::Corrupted))?; |
|
264 Ok(buf) |
263 } else { |
265 } else { |
264 let mut buf = vec![0; self.uncompressed_len]; |
266 let mut buf = vec![0; self.uncompressed_len]; |
265 decoder.read_exact(&mut buf).expect("corrupted zlib data"); |
267 decoder |
266 buf |
268 .read_exact(&mut buf) |
267 } |
269 .or(Err(RevlogError::Corrupted))?; |
268 } |
270 Ok(buf) |
269 |
271 } |
270 fn uncompressed_zstd_data(&self) -> Vec<u8> { |
272 } |
|
273 |
|
274 fn uncompressed_zstd_data(&self) -> Result<Vec<u8>, RevlogError> { |
271 if self.is_delta() { |
275 if self.is_delta() { |
272 let mut buf = Vec::with_capacity(self.compressed_len); |
276 let mut buf = Vec::with_capacity(self.compressed_len); |
273 zstd::stream::copy_decode(self.bytes, &mut buf) |
277 zstd::stream::copy_decode(self.bytes, &mut buf) |
274 .expect("corrupted zstd data"); |
278 .or(Err(RevlogError::Corrupted))?; |
275 buf |
279 Ok(buf) |
276 } else { |
280 } else { |
277 let mut buf = vec![0; self.uncompressed_len]; |
281 let mut buf = vec![0; self.uncompressed_len]; |
278 let len = zstd::block::decompress_to_buffer(self.bytes, &mut buf) |
282 let len = zstd::block::decompress_to_buffer(self.bytes, &mut buf) |
279 .expect("corrupted zstd data"); |
283 .or(Err(RevlogError::Corrupted))?; |
280 assert_eq!(len, self.uncompressed_len, "corrupted zstd data"); |
284 if len != self.uncompressed_len { |
281 buf |
285 Err(RevlogError::Corrupted) |
|
286 } else { |
|
287 Ok(buf) |
|
288 } |
282 } |
289 } |
283 } |
290 } |
284 |
291 |
285 /// Tell if the entry is a snapshot or a delta |
292 /// Tell if the entry is a snapshot or a delta |
286 /// (influences on decompression). |
293 /// (influences on decompression). |