lzma_rs/decode/
util.rs
1use std::io;
2
3pub fn read_tag<R: io::BufRead>(input: &mut R, tag: &[u8]) -> io::Result<bool> {
4 let mut buf = vec![0; tag.len()];
5 input.read_exact(buf.as_mut_slice())?;
6 Ok(buf.as_slice() == tag)
7}
8
9pub fn is_eof<R: io::BufRead>(input: &mut R) -> io::Result<bool> {
10 let buf = input.fill_buf()?;
11 Ok(buf.is_empty())
12}
13
14pub fn flush_zero_padding<R: io::BufRead>(input: &mut R) -> io::Result<bool> {
15 loop {
16 let len = {
17 let buf = input.fill_buf()?;
18 let len = buf.len();
19
20 if len == 0 {
21 return Ok(true);
22 }
23
24 for x in buf {
25 if *x != 0u8 {
26 return Ok(false);
27 }
28 }
29 len
30 };
31
32 input.consume(len);
33 }
34}
35
36pub struct CrcDigestRead<'a, 'b, R, S>
38where
39 R: 'a + io::Read,
40 S: crc::Width,
41{
42 read: &'a mut R, digest: &'a mut crc::Digest<'b, S>, }
45
46impl<'a, 'b, R, S> CrcDigestRead<'a, 'b, R, S>
47where
48 R: io::Read,
49 S: crc::Width,
50{
51 pub fn new(read: &'a mut R, digest: &'a mut crc::Digest<'b, S>) -> Self {
52 Self { read, digest }
53 }
54}
55
56impl<'a, 'b, R> io::Read for CrcDigestRead<'a, 'b, R, u32>
57where
58 R: io::Read,
59{
60 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
61 let result = self.read.read(buf)?;
62 self.digest.update(&buf[..result]);
63 Ok(result)
64 }
65}
66
67pub struct CountBufRead<'a, R>
69where
70 R: 'a + io::BufRead,
71{
72 read: &'a mut R, count: usize, }
75
76impl<'a, R> CountBufRead<'a, R>
77where
78 R: io::BufRead,
79{
80 pub fn new(read: &'a mut R) -> Self {
81 Self { read, count: 0 }
82 }
83
84 pub fn count(&self) -> usize {
85 self.count
86 }
87}
88
89impl<'a, R> io::Read for CountBufRead<'a, R>
90where
91 R: io::BufRead,
92{
93 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
94 let result = self.read.read(buf)?;
95 self.count += result;
96 Ok(result)
97 }
98}
99
100impl<'a, R> io::BufRead for CountBufRead<'a, R>
101where
102 R: io::BufRead,
103{
104 fn fill_buf(&mut self) -> io::Result<&[u8]> {
105 self.read.fill_buf()
106 }
107
108 fn consume(&mut self, amt: usize) {
109 self.read.consume(amt);
110 self.count += amt;
111 }
112}