cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/parse/main.c

170 lines
4.8 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* parse/main.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/12/01 13:33:45 |___/ */
/* */
/* ************************************************************************** */
#include <libg1m/internals.h>
/* ************************************************************************** */
/* Getting the parsing function */
/* ************************************************************************** */
/* Correspondance type */
struct corresp {
unsigned int platform;
unsigned int type;
/* result */
int (*read)(g1m_t*, FILE*, struct standard_header*);
};
/* The correspondances */
static struct corresp parsing_functions[] = {
/* add-ins */
{g1m_platform_fx, g1m_type_addin,
g1m_parse_addin},
{g1m_platform_cg, g1m_type_addin,
g1m_parse_addin_cg},
/* mcs */
{g1m_platform_fx, g1m_type_mcs,
g1m_parse_mcs},
{g1m_platform_cg, g1m_type_mcs,
g1m_parse_mcs},
/* language files */
{g1m_platform_fx, g1m_type_lang,
g1m_parse_lang},
{g1m_platform_cg, g1m_type_lang,
g1m_parse_lang_cg},
/* function keys file */
{g1m_platform_fx, g1m_type_fkey,
g1m_parse_fkey},
{g1m_platform_cg, g1m_type_fkey,
g1m_parse_lang_cg},
/* e-activities */
{g1m_platform_fx, g1m_type_eact,
g1m_parse_eact},
/* pictures */
{g1m_platform_cg, g1m_type_pict,
g1m_parse_g3p},
{g1m_platform_cp, g1m_type_pict,
g1m_parse_c2p},
/* storage */
{g1m_platform_none, g1m_type_storage,
g1m_parse_storage},
{0, 0, NULL}
};
/**
* find_parse_function:
* Find the parsing function.
*
* @arg handle the handle.
* @arg path the file path.
* @arg std pointer to the standard header.
* @arg rd pointer to the read function.
* @return the error code (0 if ok).
*/
static int find_parse_function(g1m_t *handle, const char *path,
struct standard_header *std,
int (**rd)(g1m_t*, FILE*, struct standard_header*))
{
/* get the type */
unsigned int platform, type;
const char *type_string;
int check_one, check_two;
if (g1m_get_type_info(path, std->main_id, std->subtype, &type_string,
&check_one, &check_two, &platform, &type))
return (g1m_error_magic);
log_info("Standard Header type is '%s'.", type_string);
/* check control bytes */
if (check_one && std->control != ((std->filesize + 0x41) & 0xff)) {
log_info("First control byte isn't right.");
return (g1m_error_magic);
} else if (check_two && std->control2 != ((std->filesize + 0xb8) & 0xff)) {
log_info("Second control byte isn't right.");
return (g1m_error_magic);
}
/* look for corresponding parsing function */
struct corresp *c = parsing_functions - 1;
while ((++c)->read) {
if (c->type != type)
continue;
if (c->platform != platform)
continue;
break;
}
if (!c->read) {
log_fatal("No parsing function was found for this type.");
return (g1m_error_magic);
}
/* set the vars */
handle->type = type;
handle->platform = platform;
*rd = c->read;
return (0);
}
/* ************************************************************************** */
/* Main parsing function */
/* ************************************************************************** */
/**
* g1m_parse:
* Parse a G1M file.
*
* Read the standard header, correct endianness, check magic numbers,
* then read subparts according to the G1M type.
*
* @arg handle the handle.
* @arg path the file path.
* @arg stream the stream to parse from.
* @return the error code (0 if ok).
*/
int g1m_parse(g1m_t *handle, const char *path, FILE *stream)
{
/* initialize the handle */
bzero(handle, sizeof(g1m_t));
/* get the standard header */
DREAD(hd, standard_header)
/* reverse */
uint8_t *u = (uint8_t*)&hd;
for (size_t i = 0; i < sizeof(struct standard_header); i++) u[i] = ~u[i];
/* print header */
log_info("Raw inverted standard header is:");
logm_info(&hd, sizeof(struct standard_header));
/* correct standard header endianess */
hd.filesize = be32toh(hd.filesize);
hd.number = be16toh(hd.number);
/* get type */
int (*read_func)(g1m_t*, FILE*, struct standard_header*); int err;
if ((err = find_parse_function(handle, path, &hd, &read_func)))
return (err);
/* log some data */
log_info("Standard Header filesize is %" PRIu32 "o.", hd.filesize);
log_info("Standard Header num is %" PRIu16 ".", hd.number);
/* subparse. */
return ((*read_func)(handle, stream, &hd));
}