-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparse_declarations.rs
More file actions
78 lines (70 loc) · 2.17 KB
/
Copy pathparse_declarations.rs
File metadata and controls
78 lines (70 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2024-2026 Elias Bachaalany
// SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
//
// This file is licensed under the Human-Origin Source License v1.0.
// See LICENSE.
//
// parse_declarations: Import C type declarations, verify, clean up.
//
// Usage: parse_declarations [host_url]
use libghidra as ghidra;
fn main() {
let args: Vec<String> = std::env::args().collect();
let url = args.get(1).map_or("http://127.0.0.1:18080", |s| s.as_str());
let client = ghidra::connect(url);
// 1. Parse a block of C declarations
let decls = r#"
typedef enum ExampleOpcode {
OP_NONE = 0,
OP_INIT = 1,
OP_PROCESS = 2,
OP_SHUTDOWN = 3
} ExampleOpcode;
typedef struct ExampleHeader {
int magic;
int version;
int flags;
} ExampleHeader;
typedef struct ExamplePacket {
ExampleHeader header;
ExampleOpcode opcode;
int payload_size;
} ExamplePacket;
"#;
println!("--- Parsing C declarations ---");
let result = client.parse_declarations(decls).unwrap_or_else(|e| {
eprintln!("parse_declarations failed: {e}");
std::process::exit(1);
});
println!("Types created: {}", result.types_created);
for name in &result.type_names {
println!(" + {name}");
}
if !result.errors.is_empty() {
println!("Errors:");
for err in &result.errors {
println!(" ! {err}");
}
}
// 2. Verify the types exist in the type system
println!("\n--- Verifying types ---");
let check_names = ["/ExampleOpcode", "/ExampleHeader", "/ExamplePacket"];
for name in &check_names {
match client.get_type(name) {
Ok(resp) => {
if let Some(t) = &resp.r#type {
println!(" {name}: kind={} length={}", t.kind, t.length);
} else {
println!(" {name}: NOT FOUND");
}
}
Err(_) => println!(" {name}: NOT FOUND"),
}
}
// 3. Clean up: delete the types we created
println!("\n--- Cleanup ---");
for name in check_names.iter().rev() {
let _ = client.delete_type(name);
}
println!(" Deleted all example types");
}