103 lines
3.5 KiB
C
103 lines
3.5 KiB
C
/* *****************************************************************************
|
|
* decode/cas.c -- decode a CASIOLINK 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/>.
|
|
*
|
|
* Based on the Casetta project documentation:
|
|
* https://casetta.tuxfamily.org/formats/cas
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Head decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_casfile_head:
|
|
* Decode the CAS file head.
|
|
*
|
|
* @arg head the head to decode.
|
|
* @arg datatype the raw data type (at least 2 bytes).
|
|
* @arg filename the file name (at least 12 bytes).
|
|
* @arg specific the specific bytes (at least 5 bytes).
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_casfile_head(g1m_mcshead_t *head, const char *datatype,
|
|
const char *filename, const char *specific)
|
|
{
|
|
/* check that the head exists */
|
|
if (!head) return (-1);
|
|
|
|
/* check the data type */
|
|
if (g1m_maketype_cas(datatype, &head->type))
|
|
return (g1m_error_unrecognized);
|
|
|
|
/* fill the handle */
|
|
memset(head->_dirname, 0, 9);
|
|
memset(head->_group, 0, 17);
|
|
memcpy(head->name, filename, 12); head->name[12] = 0;
|
|
|
|
/* type specific */
|
|
if (head->type == g1m_mcstype_program) {
|
|
struct cas_spe_program *spe = (void*)specific;
|
|
head->size = be16toh(spe->length);
|
|
/* TODO: store flags? */
|
|
}
|
|
|
|
/* no error! */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* File decoding functions */
|
|
/* ************************************************************************** */
|
|
/* TODO: g1m_decode_casfile */
|
|
/* TODO: g1m_decode_casfile_data */
|
|
|
|
/* ************************************************************************** */
|
|
/* File decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_cas:
|
|
* Decode a CAS file. TODO.
|
|
*
|
|
* @arg handle the handle to create.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg expected_types the expected types.
|
|
* @return the libg1m error.
|
|
*/
|
|
|
|
int g1m_decode_cas(g1m_t *handle, g1m_buffer_t *buffer,
|
|
g1m_type_t expected_types)
|
|
{
|
|
int err;
|
|
|
|
/* read the header */
|
|
DREAD(hd, cas_header)
|
|
if (g1m_checksum8(&hd, sizeof(struct cas_header) - 1) != hd.checksum)
|
|
return (g1m_error_checksum);
|
|
|
|
/* decrypt it */
|
|
g1m_mcshead_t head;
|
|
if ((err = g1m_decode_casfile_head(&head, (char*)hd.data,
|
|
(char*)hd.filename, (char*)hd.misc)))
|
|
return (err);
|
|
|
|
/* TODO: decode it */
|
|
/* everything went fine :) */
|
|
return (g1m_error_wrong_type);
|
|
}
|