|
| 1 | +/* |
| 2 | + SonyRX.c - Contains functions for handling Sony Relocatable eXecutable files (e.g. IRX and ERX files). |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <errno.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <malloc.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#include "ELF.h" |
| 11 | +#include "SonyRX.h" |
| 12 | + |
| 13 | +int IsSonyRXModule(const char *path) |
| 14 | +{ |
| 15 | + FILE *file; |
| 16 | + elf_header_t header; |
| 17 | + elf_shdr_t SectionHeader; |
| 18 | + int result; |
| 19 | + |
| 20 | + result = 0; |
| 21 | + if ((file = fopen(path, "rb")) != NULL) { |
| 22 | + fread(&header, 1, sizeof(elf_header_t), file); |
| 23 | + if (*(u32 *)header.ident == ELF_MAGIC && (header.type == ELF_TYPE_ERX2 || header.type == ELF_TYPE_IRX)) { |
| 24 | + unsigned int i; |
| 25 | + for (i = 0; i < header.shnum; i++) { |
| 26 | + fseek(file, header.shoff + i * header.shentsize, SEEK_SET); |
| 27 | + fread(&SectionHeader, 1, sizeof(elf_shdr_t), file); |
| 28 | + |
| 29 | + if ((SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) || (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB))) { |
| 30 | + result = 1; |
| 31 | + break; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fclose(file); |
| 37 | + } |
| 38 | + |
| 39 | + return result; |
| 40 | +} |
| 41 | + |
| 42 | +int GetSonyRXModInfo(const char *path, char *description, unsigned int MaxLength, unsigned short int *version) |
| 43 | +{ |
| 44 | + FILE *file; |
| 45 | + int result; |
| 46 | + elf_header_t header; |
| 47 | + elf_shdr_t SectionHeader; |
| 48 | + |
| 49 | + result = ENOENT; |
| 50 | + if ((file = fopen(path, "rb")) != NULL) { |
| 51 | + fread(&header, 1, sizeof(elf_header_t), file); |
| 52 | + if (*(u32 *)header.ident == ELF_MAGIC && (header.type == ELF_TYPE_ERX2 || header.type == ELF_TYPE_IRX)) { |
| 53 | + unsigned int i; |
| 54 | + for (i = 0; i < header.shnum; i++) { |
| 55 | + fseek(file, header.shoff + i * header.shentsize, SEEK_SET); |
| 56 | + fread(&SectionHeader, 1, sizeof(elf_shdr_t), file); |
| 57 | + |
| 58 | + if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB) || SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) { |
| 59 | + void *buffer; |
| 60 | + if ((buffer = malloc(SectionHeader.size)) != NULL) { |
| 61 | + fseek(file, SectionHeader.offset, SEEK_SET); |
| 62 | + fread(buffer, 1, SectionHeader.size, file); |
| 63 | + |
| 64 | + if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) { |
| 65 | + *version = ((iopmod_t *)buffer)->version; |
| 66 | + strncpy(description, ((iopmod_t *)buffer)->modname, MaxLength - 1); |
| 67 | + description[MaxLength - 1] = '\0'; |
| 68 | + } else if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) { |
| 69 | + *version = ((eemod_t *)buffer)->version; |
| 70 | + strncpy(description, ((eemod_t *)buffer)->modname, MaxLength - 1); |
| 71 | + description[MaxLength - 1] = '\0'; |
| 72 | + } |
| 73 | + |
| 74 | + result = 0; |
| 75 | + |
| 76 | + free(buffer); |
| 77 | + } else |
| 78 | + result = ENOMEM; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } else |
| 83 | + result = EINVAL; |
| 84 | + |
| 85 | + fclose(file); |
| 86 | + } else |
| 87 | + result = ENOENT; |
| 88 | + |
| 89 | + return result; |
| 90 | +} |
0 commit comments