-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtype_system.rs
More file actions
160 lines (142 loc) · 4.99 KB
/
Copy pathtype_system.rs
File metadata and controls
160 lines (142 loc) · 4.99 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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.
//
// type_system: Browse types, create structs, aliases, enums, and clean up.
//
// Usage: type_system [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. List existing types (first page)
let types = match client.list_types("", 15, 0) {
Ok(t) => t,
Err(e) => {
eprintln!("ListTypes failed (is a program open?): {e}");
std::process::exit(1);
}
};
println!("Types ({} shown):", types.types.len());
for t in &types.types {
println!(
" {:<30} kind={:<10} len={:<4} path={}",
t.name, t.kind, t.length, t.path_name
);
}
// 2. Search for a specific built-in type
println!("\nSearching for 'int' types...");
let int_types = client.list_types("int", 10, 0).unwrap();
for t in &int_types.types {
println!(" {:<20} kind={}", t.name, t.kind);
}
// 3. Get details for a specific type
if let Some(first) = types.types.first() {
let detail = client.get_type(&first.path_name).unwrap_or_else(|e| {
eprintln!("GetType failed: {e}");
std::process::exit(1);
});
if let Some(t) = &detail.r#type {
println!("\nType detail for '{}':", t.name);
println!(" ID: {}", t.type_id);
println!(" Path: {}", t.path_name);
println!(" Category: {}", t.category_path);
println!(" Display: {}", t.display_name);
println!(" Kind: {}", t.kind);
println!(" Length: {}", t.length);
}
}
// 4. Create a new struct type
let struct_name = "RustExampleStruct";
println!("\nCreating struct '{}'...", struct_name);
let create = client
.create_type(struct_name, "struct", 16)
.unwrap_or_else(|e| {
eprintln!("CreateType failed: {e}");
std::process::exit(1);
});
println!(" updated={}", create.updated);
// 5. Verify the struct exists
let search = client.list_types(struct_name, 5, 0).unwrap();
println!(
" Found {} type(s) matching '{}'",
search.types.len(),
struct_name
);
// 6. Create a type alias pointing to our struct
let alias_name = "RustExampleAlias";
println!("\nCreating alias '{}' -> '{}'...", alias_name, struct_name);
let alias = client
.create_type_alias(alias_name, struct_name)
.unwrap_or_else(|e| {
eprintln!("CreateTypeAlias failed: {e}");
std::process::exit(1);
});
println!(" updated={}", alias.updated);
// 7. List aliases to verify
let aliases = client.list_type_aliases(alias_name, 5, 0).unwrap();
println!(
" Aliases matching '{}': {}",
alias_name,
aliases.aliases.len()
);
// 8. Create an enum type
let enum_name = "RustExampleEnum";
println!("\nCreating enum '{}' (width=4, signed=false)...", enum_name);
let enu = client
.create_type_enum(enum_name, 4, false)
.unwrap_or_else(|e| {
eprintln!("CreateTypeEnum failed: {e}");
std::process::exit(1);
});
println!(" updated={}", enu.updated);
// 9. Rename the struct
let new_struct_name = "RustExampleStructRenamed";
println!("\nRenaming '{}' -> '{}'...", struct_name, new_struct_name);
let rn = client
.rename_type(struct_name, new_struct_name)
.unwrap_or_else(|e| {
eprintln!("RenameType failed: {e}");
std::process::exit(1);
});
println!(" updated={}, name=\"{}\"", rn.updated, rn.name);
// 10. Clean up: delete everything we created
println!("\nCleaning up...");
let del_alias = client.delete_type_alias(alias_name);
println!(
" Delete alias '{}': {}",
alias_name,
match &del_alias {
Ok(d) => format!("deleted={}", d.deleted),
Err(e) => format!("error={e}"),
}
);
let del_enum = client.delete_type_enum(enum_name);
println!(
" Delete enum '{}': {}",
enum_name,
match &del_enum {
Ok(d) => format!("deleted={}", d.deleted),
Err(e) => format!("error={e}"),
}
);
let del_struct = client.delete_type(new_struct_name);
println!(
" Delete struct '{}': {}",
new_struct_name,
match &del_struct {
Ok(d) => format!("deleted={}", d.deleted),
Err(e) => format!("error={e}"),
}
);
// 11. Confirm cleanup
let final_check = client.list_types("RustExample", 10, 0).unwrap();
println!(
"\nTypes matching 'RustExample' after cleanup: {}",
final_check.types.len()
);
println!("\nDone.");
}