KEXview/kexview.c

99 lines
3.0 KiB
C
Raw Normal View History

2024-03-19 18:02:55 +01:00
#include <stdio.h>
#include <string.h>
#include <stdint.h>
2024-03-20 15:29:28 +01:00
#include <stdlib.h>
2024-03-19 18:02:55 +01:00
#include <sys/ksys.h>
#define SIGNATURE_SIZE 8
typedef struct {
char signature[SIGNATURE_SIZE];
uint32_t version;
2024-03-20 15:29:28 +01:00
uint32_t start_address;
uint32_t file_size;
uint32_t memory_required;
uint32_t esp_value;
uint32_t param_address;
uint32_t file_path_address;
uint32_t tls_data_address; // This field is only for MENUET02 signature
} menuet_header_t;
2024-03-19 18:02:55 +01:00
2024-03-20 15:29:28 +01:00
void show_menuet_xx(const char *fpath) {
ksys_ufile_t file = _ksys_load_file(fpath);
2024-03-19 18:02:55 +01:00
if (file.data == NULL) {
2024-03-20 15:29:28 +01:00
printf("Error: Unable to open file.");
return;
2024-03-19 18:02:55 +01:00
}
2024-03-20 15:29:28 +01:00
menuet_header_t header;
if (file.size < sizeof(menuet_header_t)) {
printf("Error: Unable to read header information.");
return;
2024-03-19 18:02:55 +01:00
} else {
2024-03-20 15:29:28 +01:00
memcpy(&header, file.data, sizeof(menuet_header_t));
2024-03-19 18:02:55 +01:00
}
if (strncmp(header.signature, "MENUET01", SIGNATURE_SIZE) != 0 && strncmp(header.signature, "MENUET02", SIGNATURE_SIZE) != 0) {
printf("Error: Invalid signature.\n");
2024-03-20 15:29:28 +01:00
return;
2024-03-19 18:02:55 +01:00
}
printf("Signature: %.*s\n", SIGNATURE_SIZE, header.signature);
printf("Version: 0x%x\n", header.version);
printf("Start Address: 0x%x\n", header.start_address);
printf("File Size: %u bytes\n", header.file_size);
printf("Memory Required: %u bytes\n", header.memory_required);
printf("ESP Value: 0x%x\n", header.esp_value);
2024-03-20 15:29:28 +01:00
printf("Parameter Address: 0x%x\n", header.param_address);
printf("File Path Address: 0x%x\n", header.file_path_address);
2024-03-19 18:02:55 +01:00
if (strncmp(header.signature, "MENUET02", SIGNATURE_SIZE) == 0) {
printf("TLS Data Address: 0x%x\n", header.tls_data_address);
2024-03-20 15:29:28 +01:00
}
}
void show_library(const char *fpath) {
ksys_dll_t *table = _ksys_dlopen(fpath);
if (table == NULL) {
printf("Failed to load library.");
return;
}
printf("Export table:\n\nFunction Address\n-------------------------------------\n");
2024-03-20 15:29:28 +01:00
size_t i = 0;
for (;;) {
char *func_name = (table + i)->func_name;
void *func_ptr = (table + i)->func_ptr;
if (func_name == NULL) {
if (i == 0) {
printf("No functions in export table.");
}
return;
} else {
printf("%s", func_name);
int x = strlen(func_name);
while (x < 25) {
putc(' ');
x++;
}
putc(' ');
printf(" 0x%x\n", func_ptr);
2024-03-20 15:29:28 +01:00
}
i++;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage:\nkexview <kex_file> - view info about kex program\nkexview -l <obj_file> - view info about obj library");
return;
}
if (strcmp(argv[1], "-l") == 0) {
if (argc < 3) {
printf("Error: library expected after -l");
return;
}
show_library(argv[2]);
} else {
show_menuet_xx(argv[1]);
2024-03-19 18:02:55 +01:00
}
return 0;
}