128 lines
4.0 KiB
C
128 lines
4.0 KiB
C
/* *****************************************************************************
|
|
* decode/main.c -- decode a file.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Check using extensions */
|
|
/* ************************************************************************** */
|
|
/* Decode function */
|
|
typedef int (*decode_func)(g1m_t**, g1m_buffer_t*);
|
|
|
|
/* Correspondance type */
|
|
struct corresp {
|
|
/* extension */
|
|
const char *ext;
|
|
|
|
/* expected types */
|
|
g1m_type_t type;
|
|
|
|
/* function */
|
|
decode_func decode;
|
|
};
|
|
|
|
/* Correspondances */
|
|
static struct corresp correspondances[] = {
|
|
// {"g1s", g1m_type_storage, g1m_decode_storage},
|
|
{"grc", g1m_type_mcs, g1m_decode_grc},
|
|
|
|
/* terminating entry */
|
|
{NULL, 0, NULL}
|
|
};
|
|
|
|
/**
|
|
* lookup_extension:
|
|
* Get the function using the type.
|
|
*
|
|
* @arg path the path.
|
|
* @arg types the expected types.
|
|
* @arg decode the decode function to set.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int lookup_extension(const char *path, g1m_type_t types,
|
|
decode_func *func)
|
|
{
|
|
/* match the extension */
|
|
char ext[5];
|
|
if (!g1m_getext(path, ext, 5)) return (g1m_error_magic);
|
|
struct corresp *c = correspondances - 1;
|
|
while ((++c)->ext) if (!strcmp(c->ext, ext)) break;
|
|
|
|
/* check if correspondance is valid and expected */
|
|
if (!c->ext) return (g1m_error_magic);
|
|
if (types && !(types & c->type)) return (g1m_error_wrong_type);
|
|
|
|
/* return the decode function */
|
|
*func = c->decode;
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Main decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode:
|
|
* Decode a 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_types the expected types.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode(g1m_t **handle, const char *path, g1m_buffer_t *buffer,
|
|
g1m_type_t expected_types)
|
|
{
|
|
/* initialize the handle */
|
|
memset(handle, 0, sizeof(g1m_t));
|
|
|
|
/* match using extension */
|
|
decode_func decode;
|
|
int err = lookup_extension(path, expected_types, &decode);
|
|
if (err == g1m_error_wrong_type) return (g1m_error_wrong_type);
|
|
else if (!err) return ((*decode)(handle, buffer));
|
|
|
|
/* identify a CAS file */
|
|
unsigned char buf[0x20]; READ(buf, 1)
|
|
if (buf[0] == ':') {
|
|
if (expected_types && !(expected_types & g1m_type_mcs))
|
|
return (g1m_error_wrong_type);
|
|
return (g1m_decode_cas(handle, buffer));
|
|
}
|
|
|
|
/* identify a Casemul file */
|
|
READ(&buf[1], 3)
|
|
int casemul_be = !memcmp(buf, "CAFS", 4);
|
|
if (casemul_be || !memcmp(buf, "ACFS", 4)) {
|
|
if (expected_types && !(expected_types & g1m_type_mcs))
|
|
return (g1m_error_wrong_type);
|
|
return (g1m_decode_casemul(handle, buffer, casemul_be));
|
|
}
|
|
|
|
/* identify a standard header (send a _copy_) */
|
|
READ(&buf[4], 0x1C)
|
|
uint8_t altbuf[0x20]; memcpy(altbuf, buf, 0x20);
|
|
return (g1m_decode_std(handle, path, buffer,
|
|
(struct standard_header*)altbuf, expected_types));
|
|
}
|