/* ***************************************************************************** * core/decode.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 /** * 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)); /* identify a CAS file */ unsigned char buf[0x20]; READ(buf, 1) if (buf[0] == ':') return (g1m_decode_cas(handle, buffer, expected_types)); /* identify a Casemul file */ READ(&buf[1], 3) if (!memcmp(buf, "CAFS", 4)) { handle->platform |= g1m_platflag_be; } else if (!memcmp(buf, "ACFS", 4)) return (g1m_decode_casemul(handle, buffer)); /* identify a standard header (send a _copy_) */ READ(&buf[1], 0x1C) uint8_t altbuf[0x20]; memcpy(altbuf, buf, 0x20); return (g1m_decode_std(handle, path, buffer, (struct standard_header*)altbuf, expected_types)); }