1pub mod complete;
2mod stream_buffer;
3pub mod streaming;
4
5pub use swf_types;
6
7pub use complete::tag::parse_tag;
8pub use complete::{parse_swf, SwfParseError};
9
10#[cfg(test)]
11mod tests {
12 use crate::parse_swf;
13 use ::swf_types::Movie;
14 use ::test_generator::test_resources;
15 use nom::IResult as NomResult;
16 use std::io::Write;
17 use std::path::Path;
18
19 #[test_resources("../tests/movies/*/")]
20 fn test_parse_movie(path: &str) {
21 use serde::Serialize;
22
23 let path: &Path = Path::new(path);
24 let _name = path
25 .components()
26 .last()
27 .unwrap()
28 .as_os_str()
29 .to_str()
30 .expect("Failed to retrieve sample name");
31 let movie_path = path.join("main.swf");
32 let movie_bytes: Vec<u8> = ::std::fs::read(movie_path).expect("Failed to read movie");
33
34 let actual_movie = parse_swf(&movie_bytes).expect("Failed to parse movie");
35
36 let actual_ast_path = path.join("local-ast.rs.json");
37 let actual_ast_file = ::std::fs::File::create(actual_ast_path).expect("Failed to create actual AST file");
38 let actual_ast_writer = ::std::io::BufWriter::new(actual_ast_file);
39
40 let mut ser = serde_json_v8::Serializer::pretty(actual_ast_writer);
41 actual_movie.serialize(&mut ser).expect("Failed to write actual AST");
42 ser.into_inner().write_all(b"\n").unwrap();
43
44 let ast_path = path.join("ast.json");
47 let ast_file = ::std::fs::File::open(ast_path).expect("Failed to open AST");
48 let ast_reader = ::std::io::BufReader::new(ast_file);
49 let expected_movie = serde_json_v8::from_reader::<_, Movie>(ast_reader).expect("Failed to read AST");
50
51 assert_eq!(actual_movie, expected_movie);
52 }
53
54 macro_rules! test_various_parser_impl_any {
55 ($(#[$meta:meta])* $name:ident<$type:ty>, $parser:path, $check: expr $(,)?) => {
56 $(#[$meta])*
57 fn $name(path: &str) {
58 let path: &Path = Path::new(path);
59 let _name = path
60 .components()
61 .last()
62 .unwrap()
63 .as_os_str()
64 .to_str()
65 .expect("Failed to retrieve sample name");
66 let input_path = path.join("input.bytes");
67 let input_bytes: Vec<u8> = ::std::fs::read(input_path).expect("Failed to read input");
68
69 let (remaining_bytes, actual_value): (&[u8], $type) = ($parser)(&input_bytes).expect("Failed to parse");
70
71 let expected_path = path.join("value.json");
72 let expected_file = ::std::fs::File::open(expected_path).expect("Failed to open expected value file");
73 let expected_reader = ::std::io::BufReader::new(expected_file);
74 let expected_value = serde_json_v8::from_reader::<_, $type>(expected_reader).expect("Failed to read AST");
75
76 #[allow(clippy::redundant_closure_call)]
77 ($check)(actual_value, expected_value);
78 assert_eq!(remaining_bytes, &[] as &[u8]);
79 }
80 };
81 }
82
83 macro_rules! test_various_parser_impl_eq {
84 ($(#[$meta:meta])* $name:ident<$type:ty>, $parser:path $(,)?) => {
85 test_various_parser_impl_any!(
86 $(#[$meta])* $name<$type>,
87 $parser,
88 |actual_value, expected_value| { assert_eq!(actual_value, expected_value) },
89 );
90 };
91 }
92
93 macro_rules! test_various_parser_impl_is {
94 ($(#[$meta:meta])* $name:ident<$type:ty>, $parser:path $(,)?) => {
95 test_various_parser_impl_any!(
96 $(#[$meta])* $name<$type>,
97 $parser,
98 |actual_value: $type, expected_value: $type| { assert!(crate::swf_types::float_is::Is::is(&actual_value, &expected_value)) },
99 );
100 };
101 }
102
103 test_various_parser_impl_is!(
104 #[test_resources("../tests/various/float16-le/*/")] test_parse_le_f16<f32>,
105 crate::streaming::basic_data_types::parse_le_f16,
106 );
107
108 test_various_parser_impl_eq!(
109 #[test_resources("../tests/various/matrix/*/")] test_parse_matrix<swf_types::Matrix>,
110 crate::streaming::basic_data_types::parse_matrix,
111 );
112
113 fn parse_header34(input: &[u8]) -> NomResult<&[u8], swf_types::Header> {
114 crate::streaming::movie::parse_header(input, 34)
115 }
116
117 test_various_parser_impl_eq!(
118 #[test_resources("../tests/various/header/*/")] test_parse_header<swf_types::Header>,
119 parse_header34,
120 );
121
122 test_various_parser_impl_eq!(
123 #[test_resources("../tests/various/rect/*/")] test_parse_rect<swf_types::Rect>,
124 crate::streaming::basic_data_types::parse_rect,
125 );
126
127 test_various_parser_impl_eq!(
128 #[test_resources("../tests/various/swf-signature/*/")] test_parse_swf_signature<swf_types::SwfSignature>,
129 crate::streaming::movie::parse_swf_signature,
130 );
131
132 test_various_parser_impl_eq!(
133 #[test_resources("../tests/various/uint32-leb128/*/")] test_parse_leb128_u32<u32>,
134 crate::streaming::basic_data_types::parse_leb128_u32,
135 );
136}