KEXview/kexview.c

99 lines
3.0 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/ksys.h>
#define SIGNATURE_SIZE 8
typedef struct {
char signature[SIGNATURE_SIZE];
uint32_t version;
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;
void show_menuet_xx(const char *fpath) {
ksys_ufile_t file = _ksys_load_file(fpath);
if (file.data == NULL) {
printf("Error: Unable to open file.");
return;
}
menuet_header_t header;
if (file.size < sizeof(menuet_header_t)) {
printf("Error: Unable to read header information.");
return;
} else {
memcpy(&header, file.data, sizeof(menuet_header_t));
}
if (strncmp(header.signature, "MENUET01", SIGNATURE_SIZE) != 0 && strncmp(header.signature, "MENUET02", SIGNATURE_SIZE) != 0) {
printf("Error: Invalid signature.\n");
return;
}
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);
printf("Parameter Address: 0x%x\n", header.param_address);
printf("File Path Address: 0x%x\n", header.file_path_address);
if (strncmp(header.signature, "MENUET02", SIGNATURE_SIZE) == 0) {
printf("TLS Data Address: 0x%x\n", header.tls_data_address);
}
}
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");
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);
}
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]);
}
return 0;
}