summaryrefslogtreecommitdiffstats
path: root/rust/src/lzma.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
commita0aa2307322cd47bbf416810ac0292925e03be87 (patch)
tree37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/src/lzma.rs
parentInitial commit. (diff)
downloadsuricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz
suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/src/lzma.rs')
-rw-r--r--rust/src/lzma.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/rust/src/lzma.rs b/rust/src/lzma.rs
new file mode 100644
index 0000000..e10d803
--- /dev/null
+++ b/rust/src/lzma.rs
@@ -0,0 +1,87 @@
+/* Copyright (C) 2022 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+//! lzma decompression utility module.
+
+use lzma_rs::decompress::{Options, Stream};
+use lzma_rs::error::Error;
+use std::io::{Cursor, Write};
+
+/// Propagate lzma crate errors
+#[repr(C)]
+pub enum LzmaStatus {
+ LzmaOk,
+ LzmaIoError,
+ LzmaHeaderTooShortError,
+ LzmaError,
+ LzmaMemoryError,
+ LzmaXzError,
+}
+
+impl From<Error> for LzmaStatus {
+ fn from(e: Error) -> LzmaStatus {
+ match e {
+ Error::IoError(_) => LzmaStatus::LzmaIoError,
+ Error::HeaderTooShort(_) => LzmaStatus::LzmaHeaderTooShortError,
+ Error::LzmaError(e) => {
+ if e.contains("exceeded memory limit") {
+ LzmaStatus::LzmaMemoryError
+ } else {
+ LzmaStatus::LzmaError
+ }
+ }
+ Error::XzError(_) => LzmaStatus::LzmaXzError,
+ }
+ }
+}
+
+impl From<std::io::Error> for LzmaStatus {
+ fn from(_e: std::io::Error) -> LzmaStatus {
+ LzmaStatus::LzmaIoError
+ }
+}
+
+/// Use the lzma algorithm to decompress a chunk of data.
+#[no_mangle]
+pub unsafe extern "C" fn lzma_decompress(
+ input: *const u8, input_len: &mut usize, output: *mut u8, output_len: &mut usize,
+ memlimit: usize,
+) -> LzmaStatus {
+ let input = std::slice::from_raw_parts(input, *input_len);
+ let output = std::slice::from_raw_parts_mut(output, *output_len);
+ let output = Cursor::new(output);
+
+ let options = Options {
+ memlimit: Some(memlimit),
+ allow_incomplete: true,
+ ..Default::default()
+ };
+
+ let mut stream = Stream::new_with_options(&options, output);
+
+ if let Err(e) = stream.write_all(input) {
+ return e.into();
+ }
+
+ match stream.finish() {
+ Ok(output) => {
+ *output_len = output.position() as usize;
+ LzmaStatus::LzmaOk
+ }
+ Err(e) => e.into(),
+ }
+}