Skip to content

Commit f6bb4ca

Browse files
committed
Intial BinRead commit
0 parents  commit f6bb4ca

19 files changed

+1326
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "binread"
3+
version = "0.0.0"
4+
authors = ["jam1garner <[email protected]>"]
5+
edition = "2018"
6+
repository = "https://github.com/jam1garner/binread"
7+
license = "MIT"
8+
description = "A Rust crate for helping read structs from binary data using ✨macro magic✨"
9+
readme = "README.md"
10+
documentation = "https://docs.rs/binread"
11+
12+
[dependencies]
13+
binread_derive = { git = "https://github.com/jam1garner/binread_derive" }
14+
lazy_static = { version = "1.4" , optional=true }
15+
#rustversion = "1.0"
16+
17+
[build-dependencies]
18+
rustc_version = "0.2"
19+
20+
[features]
21+
default = ["std"]
22+
std = []
23+
debug_template = ["std", "lazy_static", "binread_derive/debug_template"]

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 jam1garner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# binread
2+
3+
A Rust crate for helping parse structs from binary data using ✨macro magic✨
4+
5+
6+
## Usage
7+
8+
BinRead uses a derive macro for declaratively defining binary parsing methods for structs.
9+

build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use rustc_version::{version_meta, Channel};
2+
3+
fn main() {
4+
// Set cfg flags depending on release channel
5+
match version_meta().unwrap().channel {
6+
Channel::Nightly => {
7+
println!("cargo:rustc-cfg=nightly");
8+
}
9+
_ => {}
10+
}
11+
}

src/binary_template.rs

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#![allow(unused_must_use, dead_code)]
2+
use std::io::prelude::*;
3+
use std::sync::{Mutex, atomic::{AtomicUsize, Ordering}};
4+
use super::Endian;
5+
use std::path::Path;
6+
7+
use lazy_static::lazy_static;
8+
9+
lazy_static! {
10+
static ref FILE: Mutex<Option<Box<dyn Write + Send>>> = Mutex::new({
11+
std::env::var("DEBUG_TEMPLATE")
12+
.ok()
13+
.map(|path|{
14+
let mut file = std::fs::File::create(path).ok()?;
15+
write!(file, "{}", SETUP).ok()?;
16+
Some(file)
17+
})
18+
.flatten()
19+
.map(|file| Box::new(file) as _)
20+
});
21+
}
22+
23+
static CURRENT_STRUCT_NUM: AtomicUsize = AtomicUsize::new(0);
24+
static CURRENT_VAR_NUM: AtomicUsize = AtomicUsize::new(0);
25+
static CURRENT_COLOR: AtomicUsize = AtomicUsize::new(0);
26+
27+
const SETUP: &str =
28+
"// Generated by BinRead macro by jam1garner (https://github.com/jam1garner/binread)
29+
typedef char i8;
30+
typedef uchar u8;
31+
typedef int16 i16;
32+
typedef uint16 u16;
33+
typedef int32 i32;
34+
typedef uint32 u32;
35+
typedef int64 i64;
36+
typedef uint64 u64;
37+
typedef float f32;
38+
typedef double f64;
39+
40+
";
41+
42+
const COLORS: &[&str] = &[
43+
"0xE85EBE", "0xFF6E41", "0x00FFC6", "0x788231", "0x00B917", "0x85A900", "0x0076FF", "0x006401",
44+
"0x009BFF", "0x00FF78", "0xDEFF74", "0xE56FFE", "0xBDD393", "0x7E2DD2", "0x90FB92", "0xFFDB66",
45+
"0xFFB167", "0xB500FF", "0x43002C", "0x004754", "0x263400", "0x7A4782", "0x774D00", "0xFFA6FE",
46+
"0xA5FFD2", "0x7544B1", "0xBB8800", "0x01D0FF", "0xBDC6FF", "0xFE8900", "0xFFEEE8", "0x01FFFE",
47+
"0xA75740", "0x98FF52", "0x968AE8", "0xFF74A3", "0x683D3B", "0xFF029D", "0xFF00F6", "0xFF0000",
48+
"0x5FAD4E", "0x008F9C", "0xBE9970", "0xC28C9F", "0x00AE7E", "0x6A826C", "0x007DB5", "0x0000FF",
49+
"0x6B6882", "0x620E00", "0x91D0CB", "0x001544", "0xA42400", "0xFF937E", "0x95003A", "0x00FF00",
50+
"0x005F39", "0xFFE502", "0x0E4CA1", "0x9E008E", "0xFF0056", "0xD5FF00", "0x010067", "0x000000"
51+
];
52+
53+
pub fn set_output_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
54+
Ok(set_output(std::fs::File::create(path.as_ref())?))
55+
}
56+
57+
pub fn set_output<W: Write + Send + 'static>(mut writer: W) {
58+
writeln!(writer, "{}", SETUP);
59+
*FILE.lock().unwrap() = Some(Box::new(writer));
60+
}
61+
62+
pub fn unset_output() {
63+
*FILE.lock().unwrap() = None;
64+
}
65+
66+
pub fn write_named(endian: Endian, pos: u64, type_name: &str, var_name: &str) {
67+
let mut lock = FILE.lock().unwrap();
68+
let file = match lock.as_mut() {
69+
Some(x) => x,
70+
None => return
71+
};
72+
73+
match endian {
74+
Endian::Big => writeln!(*file, "BigEndian();"),
75+
Endian::Little => writeln!(*file, "LittleEndian();"),
76+
_ => writeln!(*file, "")
77+
};
78+
79+
let color_index = CURRENT_VAR_NUM.fetch_add(1, Ordering::SeqCst) & 0x3f;
80+
81+
writeln!(*file, "FSeek(0x{:X});", pos);
82+
writeln!(*file, "{} {}<bgcolor={}>;\n", type_name, var_name, COLORS[color_index]);
83+
}
84+
85+
pub fn get_next_var_name() -> String {
86+
let var_num = CURRENT_VAR_NUM.fetch_add(1, Ordering::SeqCst);
87+
format!("var{}", var_num)
88+
}
89+
90+
pub fn get_next_color() -> &'static str {
91+
let color_index = CURRENT_VAR_NUM.fetch_add(1, Ordering::SeqCst) & 0x3f;
92+
COLORS[color_index]
93+
}
94+
95+
pub fn write_start_struct(name: &str) {
96+
let mut lock = FILE.lock().unwrap();
97+
let file = match lock.as_mut() {
98+
Some(x) => x,
99+
None => return
100+
};
101+
102+
writeln!(*file, "struct {}_{} {{", name, CURRENT_STRUCT_NUM.fetch_add(1, Ordering::SeqCst));
103+
}
104+
105+
pub fn write_comment(comment: &str) {
106+
let mut lock = FILE.lock().unwrap();
107+
let file = match lock.as_mut() {
108+
Some(x) => x,
109+
None => return
110+
};
111+
112+
writeln!(*file, "// {}", comment);
113+
}
114+
115+
pub fn write_end_struct(name: Option<&str>) {
116+
let mut lock = FILE.lock().unwrap();
117+
let file = match lock.as_mut() {
118+
Some(x) => x,
119+
None => return
120+
};
121+
122+
let var_name = name.unwrap_or("root");
123+
124+
writeln!(*file, "}} {};", var_name);
125+
}
126+
127+
pub fn write(endian: Endian, pos: u64, type_name: &str) {
128+
let var_name = get_next_var_name();
129+
130+
write_named(endian, pos, type_name, &var_name);
131+
}
132+
133+
pub fn write_vec_named(endian: Endian, pos: u64, type_name: &str, count: usize, name: &str) {
134+
let mut lock = FILE.lock().unwrap();
135+
let file = match lock.as_mut() {
136+
Some(x) => x,
137+
None => return
138+
};
139+
140+
match endian {
141+
Endian::Big => writeln!(*file, "BigEndian();"),
142+
Endian::Little => writeln!(*file, "LittleEndian();"),
143+
_ => writeln!(*file, "")
144+
};
145+
146+
let color_index = CURRENT_VAR_NUM.fetch_add(1, Ordering::SeqCst) & 0x3f;
147+
148+
writeln!(*file, "FSeek(0x{:X});", pos);
149+
writeln!(*file, "{} {}[{}]<bgcolor={}>;\n", type_name, name, count, COLORS[color_index]);
150+
}
151+
152+
pub fn write_vec(endian: Endian, pos: u64, type_name: &str, count: usize) {
153+
let var_num = CURRENT_VAR_NUM.fetch_add(1, Ordering::SeqCst);
154+
write_vec_named(endian, pos, type_name, count, &format!("var{}", var_num))
155+
}

0 commit comments

Comments
 (0)