/src/suricata7/rust/src/lzma.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2022 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | //! lzma decompression utility module. |
19 | | |
20 | | use lzma_rs::decompress::{Options, Stream}; |
21 | | use lzma_rs::error::Error; |
22 | | use std::io::{Cursor, Write}; |
23 | | |
24 | | /// Propagate lzma crate errors |
25 | | #[repr(C)] |
26 | | pub enum LzmaStatus { |
27 | | LzmaOk, |
28 | | LzmaIoError, |
29 | | LzmaHeaderTooShortError, |
30 | | LzmaError, |
31 | | LzmaMemoryError, |
32 | | LzmaXzError, |
33 | | } |
34 | | |
35 | | impl From<Error> for LzmaStatus { |
36 | 0 | fn from(e: Error) -> LzmaStatus { |
37 | 0 | match e { |
38 | 0 | Error::IoError(_) => LzmaStatus::LzmaIoError, |
39 | 0 | Error::HeaderTooShort(_) => LzmaStatus::LzmaHeaderTooShortError, |
40 | 0 | Error::LzmaError(e) => { |
41 | 0 | if e.contains("exceeded memory limit") { |
42 | 0 | LzmaStatus::LzmaMemoryError |
43 | | } else { |
44 | 0 | LzmaStatus::LzmaError |
45 | | } |
46 | | } |
47 | 0 | Error::XzError(_) => LzmaStatus::LzmaXzError, |
48 | | } |
49 | 0 | } |
50 | | } |
51 | | |
52 | | impl From<std::io::Error> for LzmaStatus { |
53 | 0 | fn from(_e: std::io::Error) -> LzmaStatus { |
54 | 0 | LzmaStatus::LzmaIoError |
55 | 0 | } |
56 | | } |
57 | | |
58 | | /// Use the lzma algorithm to decompress a chunk of data. |
59 | | #[no_mangle] |
60 | 0 | pub unsafe extern "C" fn lzma_decompress( |
61 | 0 | input: *const u8, input_len: &mut usize, output: *mut u8, output_len: &mut usize, |
62 | 0 | memlimit: usize, |
63 | 0 | ) -> LzmaStatus { |
64 | 0 | let input = std::slice::from_raw_parts(input, *input_len); |
65 | 0 | let output = std::slice::from_raw_parts_mut(output, *output_len); |
66 | 0 | let output = Cursor::new(output); |
67 | 0 |
|
68 | 0 | let options = Options { |
69 | 0 | memlimit: Some(memlimit), |
70 | 0 | allow_incomplete: true, |
71 | 0 | ..Default::default() |
72 | 0 | }; |
73 | 0 |
|
74 | 0 | let mut stream = Stream::new_with_options(&options, output); |
75 | | |
76 | 0 | if let Err(e) = stream.write_all(input) { |
77 | 0 | return e.into(); |
78 | 0 | } |
79 | 0 |
|
80 | 0 | match stream.finish() { |
81 | 0 | Ok(output) => { |
82 | 0 | *output_len = output.position() as usize; |
83 | 0 | LzmaStatus::LzmaOk |
84 | | } |
85 | 0 | Err(e) => e.into(), |
86 | | } |
87 | 0 | } |