PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
SonyRX.c
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 <stdlib.h>
8#include <string.h>
9
10#include "ELF.h"
11#include "SonyRX.h"
12
13int 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 if (fread(&header, 1, sizeof(elf_header_t), file) != 0) {
23 if (*(uint32_t *)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 if (fread(&SectionHeader, 1, sizeof(elf_shdr_t), file) != 0) {
28 if ((SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) || (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB))) {
29 result = 1;
30 break;
31 }
32 }
33 }
34 }
35 }
36
37 fclose(file);
38 }
39
40 return result;
41}
42
43int GetSonyRXModInfo(const char *path, char *description, unsigned int MaxLength, unsigned short int *version)
44{
45 FILE *file;
46 int result;
47 elf_header_t header;
48 elf_shdr_t SectionHeader;
49
50 result = ENOENT;
51 if ((file = fopen(path, "rb")) != NULL) {
52 if (fread(&header, 1, sizeof(elf_header_t), file) != 0) {
53 if (*(uint32_t *)header.ident == ELF_MAGIC && (header.type == ELF_TYPE_ERX2 || header.type == ELF_TYPE_IRX)) {
54 unsigned int i;
55 for (i = 0; i < header.shnum; i++) {
56 fseek(file, header.shoff + i * header.shentsize, SEEK_SET);
57 if (fread(&SectionHeader, 1, sizeof(elf_shdr_t), file) != 0) {
58
59 if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB) || SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) {
60 void *buffer;
61 if ((buffer = malloc(SectionHeader.size)) != NULL) {
62 fseek(file, SectionHeader.offset, SEEK_SET);
63 if (fread(buffer, 1, SectionHeader.size, file) != 0) {
64
65 if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_IOPMOD_TAB)) {
66 *version = ((iopmod_t *)buffer)->version;
67 strncpy(description, ((iopmod_t *)buffer)->modname, MaxLength - 1);
68 description[MaxLength - 1] = '\0';
69 } else if (SectionHeader.type == (SHT_LOPROC | SHT_LOPROC_EEMOD_TAB)) {
70 *version = ((eemod_t *)buffer)->version;
71 strncpy(description, ((eemod_t *)buffer)->modname, MaxLength - 1);
72 description[MaxLength - 1] = '\0';
73 }
74 }
75
76 result = 0;
77
78 free(buffer);
79 } else
80 result = ENOMEM;
81 break;
82 }
83 }
84 }
85 } else
86 result = EINVAL;
87 }
88
89 fclose(file);
90 } else
91 result = ENOENT;
92
93 return result;
94}
#define ENOENT
Definition errno.h:23
#define EINVAL
Definition errno.h:63
#define ENOMEM
Definition errno.h:43
unsigned int version
Definition fileXio.h:3