-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparse_declarations.cpp
More file actions
98 lines (86 loc) · 2.73 KB
/
Copy pathparse_declarations.cpp
File metadata and controls
98 lines (86 loc) · 2.73 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
// Copyright (c) 2024-2026 Elias Bachaalany
// SPDX-License-Identifier: MPL-2.0
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// parse_declarations: Import C type declarations, verify they exist, clean up.
//
// Usage: parse_declarations [host_url]
#include <cstdio>
#include <cstdlib>
#include <string>
#include "libghidra/ghidra.hpp"
int main(int argc, char* argv[]) {
const std::string host_url =
(argc >= 2) ? argv[1] : "http://127.0.0.1:18080";
auto client = ghidra::connect(host_url);
// 1. Check host health
auto status = client->GetStatus();
if (!status.ok()) {
fprintf(stderr, "Cannot reach host at %s: %s\n", host_url.c_str(),
status.status.message.c_str());
return 1;
}
printf("Connected: %s v%s\n",
status.value->service_name.c_str(),
status.value->service_version.c_str());
// 2. Parse a block of C declarations
const char* 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;
)";
printf("\n--- Parsing C declarations ---\n");
auto result = client->ParseDeclarations(decls);
if (!result.ok()) {
fprintf(stderr, "ParseDeclarations failed: %s\n",
result.status.message.c_str());
return 1;
}
printf("Types created: %d\n", result.value->types_created);
for (const auto& name : result.value->type_names) {
printf(" + %s\n", name.c_str());
}
if (!result.value->errors.empty()) {
printf("Errors:\n");
for (const auto& err : result.value->errors) {
printf(" ! %s\n", err.c_str());
}
}
// 3. Verify the types exist in the type system
printf("\n--- Verifying types ---\n");
const char* check_names[] = {
"/ExampleOpcode", "/ExampleHeader", "/ExamplePacket"};
for (const auto& name : check_names) {
auto t = client->GetType(name);
if (t.ok() && t.value->type) {
printf(" %s: kind=%s length=%llu\n", name,
t.value->type->kind.c_str(),
(unsigned long long)t.value->type->length);
} else {
printf(" %s: NOT FOUND\n", name);
}
}
// 4. Clean up: delete the types we created
printf("\n--- Cleanup ---\n");
client->DeleteType("/ExamplePacket");
client->DeleteType("/ExampleHeader");
client->DeleteType("/ExampleOpcode");
printf(" Deleted all example types\n");
return 0;
}