aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-11-26 16:15:23 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-11-26 16:15:23 +0100
commit7a10cd38214bc6e244d89333ed23d0c36855bb4a (patch)
treeb7348cb763c8fe3b83fa082e4d2d4fac59a5e325 /src
parent76705a7ac6d404a9db709d45190767b3e8be24c9 (diff)
downloadcbconv-7a10cd38214bc6e244d89333ed23d0c36855bb4a.tar.gz
cbconv-7a10cd38214bc6e244d89333ed23d0c36855bb4a.tar.bz2
cbconv-7a10cd38214bc6e244d89333ed23d0c36855bb4a.zip
Get back to where we were.
Only, now the root_chunk parser will return the value it has parsed, so it can be used outside of the parser. Not used for anything other than printing the information for now, though.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs43
1 files changed, 24 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs
index 414f1c0..5a3bf36 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,7 +26,6 @@ use nom::{
number::complete::*,
sequence::tuple,
Finish,
- InputTake,
IResult,
};
use std::{
@@ -263,10 +262,14 @@ fn fourcc<'a>(data: &'a [u8]) -> IResult<&'a [u8], &'a str> {
Ok((rest, std::str::from_utf8(tag).unwrap()))
}
-fn root_chunk<'a>(data: &'a [u8]) -> IResult<&'a [u8], ()> {
- let (rest, (key, val)) = tuple((cmstring, cmstring))(data)?;
- println!(" {}: {}", key, val);
- Ok((rest, ()))
+/**
+ * Root chunks allways have the same layout, two length prefixed strings.
+ *
+ * These seem to be a mapping between a field name, and the data type the field contains.
+ * The actual data follows in the ARCH chunk following this ROOT chunk.
+ */
+fn root_chunk<'a>(data: &'a [u8]) -> IResult<&'a [u8], (&str, &str)> {
+ tuple((cmstring, cmstring))(data)
}
@@ -304,20 +307,6 @@ fn arch_chunk<'a>(data: &'a [u8]) -> IResult<&'a [u8], ()> {
Ok((rest, ()))
}
-fn chunk<'a>(data: &'a [u8]) -> IResult<&'a [u8], ()> {
- let (rest, (tag, payload)) = tuple((fourcc, length_data(be_u32)))(data)?;
-
- println!(" {}: {}", tag, payload.len());
-
- match tag {
- "ROOT" => root_chunk(payload)?,
- "ARCH" => arch_chunk(payload)?,
- &_ => (payload, ())
- };
-
- Ok((rest, ()))
-}
-
struct RiffChunk<'a> {
fourcc: &'a str,
payload: &'a [u8],
@@ -383,6 +372,22 @@ where
for chunk in chunks {
println!("{}: {}", chunk.fourcc, chunk.payload.len());
+
+ match chunk.fourcc {
+ "ROOT" => {
+ let (r, (k, v)) = root_chunk(chunk.payload).finish().map_err(|e| format!("{:?}", e))?;
+ assert_eq!(r.len(), 0);
+ println!(" {}: {}", k, v);
+ },
+ "ARCH" => {
+ arch_chunk(chunk.payload).finish().map_err(|e| format!("{:?}", e))?;
+ },
+ _ => {
+ eprintln!("[-] Warning: ignoring unknown chunk \"{}\" of length {} bytes.",
+ chunk.fourcc,
+ chunk.payload.len());
+ }
+ };
}
Ok(proj)