/* ***************************************************************************** * decode/main.c -- decode a file. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************** */ #include /* ************************************************************************** */ /* Check using extensions */ /* ************************************************************************** */ /* Decode function */ typedef int (*decode_func)(g1m_handle_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}, /* 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_handle_t **handle, const char *path, g1m_buffer_t *buffer, g1m_type_t expected_types) { /* match using extension */ decode_func decode; int err = lookup_extension(path, expected_types, &decode); if (err == g1m_error_wrong_type) return (err); 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 GraphCard file */ READ(&buf[1], 2) if (!buf[0] && buf[1] == 0x32 && buf[2] == ':') { if (expected_types && !(expected_types & g1m_type_mcs)) return (g1m_error_wrong_type); return (g1m_decode_grc(handle, buffer)); } /* identify a Casemul file */ READ(&buf[3], 9) int casemul_be = !memcmp(buf, "CAFS", 4); if (casemul_be || !memcmp(buf, "ACFS", 4)) { g1m_casemul_intheader_t *ch = (void*)buf; uint32_t ver = casemul_be ? be32toh(ch->g1m_casemul_intheader_version) : le32toh(ch->g1m_casemul_intheader_version); uint32_t siz = casemul_be ? le32toh(ch->g1m_casemul_intheader_size) : le32toh(ch->g1m_casemul_intheader_size); if (ver != 0x100 || siz != sizeof(g1m_casemul_header_t)) return (g1m_error_magic); 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[12], 0x14) uint8_t altbuf[0x20]; memcpy(altbuf, buf, 0x20); return (g1m_decode_std(handle, path, buffer, (g1m_standard_header_t*)altbuf, expected_types)); }