mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-23 07:29:54 +00:00
Create the big endian uint parsers (8, 16, 32, 64 bits)
This commit is contained in:
parent
1b4f270544
commit
d863278e93
36
src/nom.rs
36
src/nom.rs
@ -726,6 +726,42 @@ macro_rules! take_until_either_and_leave(
|
||||
)
|
||||
);
|
||||
|
||||
pub fn be_u8(i: &[u8]) -> IResult<&[u8], u8> {
|
||||
if i.len() < 1 {
|
||||
Incomplete(0)
|
||||
} else {
|
||||
Done(&i[1..], i[0])
|
||||
}
|
||||
}
|
||||
|
||||
pub fn be_u16(i: &[u8]) -> IResult<&[u8], u16> {
|
||||
if i.len() < 2 {
|
||||
Incomplete(0)
|
||||
} else {
|
||||
let res = ((i[0] as u16) << 8) + i[1] as u16;
|
||||
Done(&i[2..], res)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn be_u32(i: &[u8]) -> IResult<&[u8], u32> {
|
||||
if i.len() < 4 {
|
||||
Incomplete(0)
|
||||
} else {
|
||||
let res = ((i[0] as u32) << 24) + ((i[1] as u32) << 16) + ((i[2] as u32) << 8) + i[3] as u32;
|
||||
Done(&i[4..], res)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn be_u64(i: &[u8]) -> IResult<&[u8], u64> {
|
||||
if i.len() < 8 {
|
||||
Incomplete(0)
|
||||
} else {
|
||||
let res = ((i[0] as u64) << 56) + ((i[1] as u64) << 48) + ((i[2] as u64) << 40) + ((i[3] as u64) << 32) +
|
||||
((i[4] as u64) << 24) + ((i[5] as u64) << 16) + ((i[6] as u64) << 8) + i[7] as u64;
|
||||
Done(&i[8..], res)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
15
tests/mp4.rs
15
tests/mp4.rs
@ -1,26 +1,23 @@
|
||||
#[macro_use]
|
||||
extern crate nom;
|
||||
|
||||
use nom::{HexDisplay,IResult,FlatMap,FlatMapOpt,Functor,Producer,ProducerState,FileProducer};
|
||||
use nom::{HexDisplay,IResult,FlatMap,FlatMapOpt,Functor,Producer,ProducerState,FileProducer,be_u8,be_u32,be_u64};
|
||||
use nom::IResult::*;
|
||||
|
||||
use std::str;
|
||||
|
||||
fn mp4_box(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
|
||||
take!(offset_parser 4);
|
||||
match offset_parser(input) {
|
||||
Done(i, offset_bytes) => {
|
||||
let offset:u32 = (offset_bytes[3] as u32) + (offset_bytes[2] as u32) * 0x100 + (offset_bytes[1] as u32) * 0x10000 + (offset_bytes[0] as u32) * 0x1000000;
|
||||
match be_u32(input) {
|
||||
Done(i, offset) => {
|
||||
let sz: usize = offset as usize;
|
||||
//println!("box size: {} -> {} (input: {})", offset_bytes.to_hex(8), sz, i.len());
|
||||
if i.len() >= sz - 4 {
|
||||
return Done(&i[(sz-4)..], &i[0..(sz-4)])
|
||||
} else {
|
||||
return Incomplete(1234)
|
||||
}
|
||||
},
|
||||
e => e
|
||||
}
|
||||
Error(e) => Error(e),
|
||||
Incomplete(e) => Incomplete(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user