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/decode/main.c

175 lines
5.0 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* decode/main.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/02/19 23:01:18 |___/ */
/* */
/* ************************************************************************** */
#include <libg1m/internals.h>
/* ************************************************************************** */
/* Getting the parsing function */
/* ************************************************************************** */
/* Correspondance type */
struct corresp {
unsigned int platform;
unsigned int type;
/* result */
g1m_decode_function *decode;
};
/* The correspondances */
static struct corresp parsing_functions[] = {
/* add-ins */
{g1m_platform_fx, g1m_type_addin,
g1m_decode_addin},
{g1m_platform_cg, g1m_type_addin,
g1m_decode_addin_cg},
/* mcs */
{g1m_platform_fx, g1m_type_mcs,
g1m_decode_mcs},
{g1m_platform_cg, g1m_type_mcs,
g1m_decode_mcs},
/* language files */
{g1m_platform_fx, g1m_type_lang,
g1m_decode_lang},
{g1m_platform_cg, g1m_type_lang,
g1m_decode_lang_cg},
/* function keys file */
{g1m_platform_fx, g1m_type_fkey,
g1m_decode_fkey},
{g1m_platform_cg, g1m_type_fkey,
g1m_decode_lang_cg},
/* e-activities */
{g1m_platform_fx, g1m_type_eact,
g1m_decode_eact},
/* pictures */
{g1m_platform_cg, g1m_type_pict,
g1m_decode_g3p},
{g1m_platform_cp, g1m_type_pict,
g1m_decode_c2p},
/* storage */
/*{g1m_platform_none, g1m_type_storage,
g1m_decode_storage},*/
{0, 0, NULL}
};
/**
* find_decode_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 decode function.
* @return the error code (0 if ok).
*/
static int find_decode_function(g1m_t *handle, const char *path,
struct standard_header *std, g1m_decode_function **rd)
{
/* 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)->decode) {
if (c->type != type)
continue;
if (c->platform != platform)
continue;
break;
}
if (!c->decode) {
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->decode;
return (0);
}
/* ************************************************************************** */
/* Main parsing function */
/* ************************************************************************** */
/**
* g1m_decode:
* Decode 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 buffer the buffer to read from.
* @arg expected_type the expected type.
* @return the error code (0 if ok).
*/
int g1m_decode(g1m_t *handle, const char *path, g1m_buffer_t *buffer,
g1m_type_t expected_type)
{
/* 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 */
g1m_decode_function *read_func; int err;
if ((err = find_decode_function(handle, path, &hd, &read_func)))
return (err);
/* check if the type is alright */
if (expected_type && handle->type != expected_type)
return (g1m_error_wrong_type);
/* log some data */
log_info("Standard Header filesize is %" PRIu32 "o.", hd.filesize);
log_info("Standard Header num is %" PRIu16 ".", hd.number);
/* subdecode. */
return ((*read_func)(handle, buffer, &hd));
}