git_internal/internal/object/
utils.rs

1use 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
10/// Parses a byte slice into a `usize` representing the size of a Git object.
11///
12/// This function is intended to be used for converting the bytes, which represent the size portion
13/// in a Git object, back into a `usize`. This size is typically compared with the actual length of
14/// the object's data part to ensure data integrity.
15///
16/// # Parameters
17/// * `bytes`: A byte slice (`&[u8]`) representing the size in a serialized Git object.
18///
19/// # Returns
20/// Returns a `Result` which is:
21/// * `Ok(usize)`: On successful parsing, returns the size as a `usize`.
22/// * `Err(Box<dyn std::error::Error>)`: On failure, returns an error in a Box. This error could be
23///   due to invalid UTF-8 encoding in the byte slice or a failure to parse the byte slice as a `usize`.
24///
25/// # Errors
26/// This function handles two main types of errors:
27/// 1. `Utf8Error`: If the byte slice is not a valid UTF-8 string, which is necessary for the size representation.
28/// 2. `ParseIntError`: If the byte slice does not represent a valid `usize` value.
29pub 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
34/// Preserve the last bits of value binary
35///
36fn keep_bits(value: usize, bits: u8) -> usize {
37    value & ((1 << bits) - 1)
38}
39/// Read the first few fields of the object and parse
40///
41pub fn read_type_and_size<R: Read>(stream: &mut R) -> io::Result<(u8, usize)> {
42    // Object type and uncompressed pack data size
43    // are stored in a "size-encoding" variable-length integer.
44    // Bits 4 through 6 store the type and the remaining bits store the size.
45    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
53/// Read the type and size of the object
54///
55pub 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
70/// Returns whether the first bit of u8 is 1 and returns the 7-bit truth value
71///
72pub 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/// Read the next N bytes from the reader
81///
82#[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}