git_internal/internal/object/
utils.rs1use std::io::{self, Read, Write};
2
3use flate2::{Compression, write::ZlibEncoder};
4
5const TYPE_BITS: u8 = 3;
6const VAR_INT_ENCODING_BITS: u8 = 7;
7const TYPE_BYTE_SIZE_BITS: u8 = VAR_INT_ENCODING_BITS - TYPE_BITS;
8const VAR_INT_CONTINUE_FLAG: u8 = 1 << VAR_INT_ENCODING_BITS;
9
10pub fn parse_size_from_bytes(bytes: &[u8]) -> Result<usize, Box<dyn std::error::Error>> {
30 let size_str = std::str::from_utf8(bytes)?;
31 Ok(size_str.parse::<usize>()?)
32}
33
34fn keep_bits(value: usize, bits: u8) -> usize {
37 value & ((1 << bits) - 1)
38}
39pub fn read_type_and_size<R: Read>(stream: &mut R) -> io::Result<(u8, usize)> {
42 let value = read_size_encoding(stream)?;
46 let object_type = keep_bits(value >> TYPE_BYTE_SIZE_BITS, TYPE_BITS) as u8;
47 let size = keep_bits(value, TYPE_BYTE_SIZE_BITS)
48 | (value >> VAR_INT_ENCODING_BITS << TYPE_BYTE_SIZE_BITS);
49
50 Ok((object_type, size))
51}
52
53pub fn read_size_encoding<R: Read>(stream: &mut R) -> io::Result<usize> {
56 let mut value = 0;
57 let mut length = 0;
58
59 loop {
60 let (byte_value, more_bytes) = read_var_int_byte(stream).unwrap();
61 value |= (byte_value as usize) << length;
62 if !more_bytes {
63 return Ok(value);
64 }
65
66 length += VAR_INT_ENCODING_BITS;
67 }
68}
69
70pub fn read_var_int_byte<R: Read>(stream: &mut R) -> io::Result<(u8, bool)> {
73 let [byte] = read_bytes(stream)?;
74 let value = byte & !VAR_INT_CONTINUE_FLAG;
75 let more_bytes = byte & VAR_INT_CONTINUE_FLAG != 0;
76
77 Ok((value, more_bytes))
78}
79
80#[inline]
83pub fn read_bytes<R: Read, const N: usize>(stream: &mut R) -> io::Result<[u8; N]> {
84 let mut bytes = [0; N];
85 stream.read_exact(&mut bytes)?;
86
87 Ok(bytes)
88}
89
90pub fn compress_zlib(data: &[u8]) -> io::Result<Vec<u8>> {
91 let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
92 encoder.write_all(data)?;
93 let compressed_data = encoder.finish()?;
94 Ok(compressed_data)
95}
96
97#[cfg(test)]
98mod tests {
99 use crate::internal::object::utils::parse_size_from_bytes;
100
101 #[test]
102 fn test_parse_size_from_bytes() -> Result<(), Box<dyn std::error::Error>> {
103 let size: usize = 12345;
104 let size_bytes = size.to_string().as_bytes().to_vec();
105
106 let parsed_size = parse_size_from_bytes(&size_bytes)?;
107
108 assert_eq!(size, parsed_size);
109 Ok(())
110 }
111}