278 lines
7.9 KiB
C
278 lines
7.9 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>
|
|
#define FUNC(NAME) g1m_decode_caspart_##NAME
|
|
|
|
/* ************************************************************************** */
|
|
/* Type correspondance list */
|
|
/* ************************************************************************** */
|
|
/* Part parsing function type */
|
|
typedef int (*cas_decode_function)(g1m_mcsfile_t*, g1m_buffer_t*);
|
|
|
|
/* Correspondance type */
|
|
struct cas_corresp {
|
|
unsigned int type;
|
|
cas_decode_function decode;
|
|
};
|
|
|
|
/* All correspondances */
|
|
#define TTERM {0, NULL}
|
|
static struct cas_corresp cas_types[] = {
|
|
{g1m_mcstype_var, FUNC(var)},
|
|
{g1m_mcstype_program, FUNC(program)},
|
|
TTERM
|
|
};
|
|
|
|
/**
|
|
* lookup_cas_decode:
|
|
* Lookup for a parsing function for the CAS part.
|
|
*
|
|
* @arg type the libg1m MCS file type.
|
|
* @return the function (NULL if not found).
|
|
*/
|
|
|
|
static cas_decode_function lookup_cas_decode(g1m_mcstype_t type)
|
|
{
|
|
/* lookup for the type */
|
|
struct cas_corresp *c = cas_types;
|
|
while (c->decode) {
|
|
if (type == c->type)
|
|
break;
|
|
c++;
|
|
}
|
|
|
|
/* return the function */
|
|
return (c->decode);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Head decoding functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* decode_caspro_head:
|
|
* Decode a CASIOLINK Protocol header.
|
|
*
|
|
* @arg head the head to fill.
|
|
* @arg hd the header.
|
|
* @return if there was an error, or not.
|
|
*/
|
|
|
|
static int decode_caspro_head(g1m_mcshead_t *head, struct caspro_header *hd)
|
|
{
|
|
/* log the raw header */
|
|
log_info("Raw CASPRO header:");
|
|
logm_info(hd, sizeof(struct caspro_header));
|
|
|
|
/* check the checksum */
|
|
uint8_t csum = ~g1m_checksum8(hd, sizeof(struct caspro_header) - 1) + 1;
|
|
if (csum != hd->checksum)
|
|
return (g1m_error_checksum);
|
|
|
|
/* copy the basic information */
|
|
head->size = be16toh(hd->length) - 2 /* checksum, colon */;
|
|
char *end = memchr(hd->name, 0xFF, 8);
|
|
size_t len = end ? (size_t)(end - (char*)hd->name) : 8;
|
|
memcpy(head->name, hd->name, len); head->name[len] = 0;
|
|
|
|
/* read specific data */
|
|
switch (head->type) {
|
|
case g1m_mcstype_program:;
|
|
/* copy password */
|
|
head->flags |= g1m_mcsflag_unfinished;
|
|
end = memchr(hd->aux, 0xFF, 8);
|
|
len = end ? (size_t)(end - (char*)hd->aux) : 8;
|
|
memcpy(head->password, hd->aux, len); head->password[len] = 0;
|
|
break;
|
|
case g1m_mcstype_variable:
|
|
if (hd->used) head->flags |= g1m_mcsflag_unfinished;
|
|
/* TODO: id */
|
|
head->count = 1;
|
|
break;
|
|
}
|
|
|
|
/* TODO */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* g1m_decode_casfile_head:
|
|
* Decode the CAS file head.
|
|
*
|
|
* @arg head the head to decode.
|
|
* @arg buffer the buffer to read the header from.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_casfile_head(g1m_mcshead_t *head, g1m_buffer_t *buffer)
|
|
{
|
|
/* check that the head exists */
|
|
if (!head) return (-1);
|
|
|
|
/* read beginning of the header, try to guess a newer header */
|
|
uint8_t buf[49]; READ(buf, 7)
|
|
struct caspro_header *phd = (void*)buf;
|
|
if (!g1m_maketype_caspro(head, (char*)phd->type, (char*)phd->data)) {
|
|
READ(&buf[7], 42)
|
|
return (decode_caspro_head(head, phd));
|
|
}
|
|
|
|
/* check the data type */
|
|
READ(&buf[7], 32)
|
|
struct cas_header *hd = (void*)buf;
|
|
log_info("Raw CAS header:");
|
|
logm_info(hd, sizeof(struct cas_header));
|
|
if (g1m_maketype_cas(head, (char*)hd->data))
|
|
return (g1m_error_unrecognized);
|
|
|
|
/* fill the handle */
|
|
memcpy(head->name, hd->filename, 12); head->name[12] = 0;
|
|
|
|
/* type specific */
|
|
if (head->type == g1m_mcstype_program) {
|
|
struct cas_spe_program *spe = (void*)hd->misc;
|
|
head->size = be16toh(spe->length);
|
|
/* TODO: store flags? */
|
|
}
|
|
|
|
/* no error! */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Part decoding functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_casfile_part:
|
|
* Decode a CASIOLINK Protocol content part.
|
|
*
|
|
* @arg file the file to contribute to.
|
|
* @arg buffer the buffer to read from.
|
|
* @return if there was an error, or not.
|
|
*/
|
|
|
|
int g1m_decode_casfile_part(g1m_mcsfile_t *file, g1m_buffer_t *buffer)
|
|
{
|
|
/* checks */
|
|
if (!file) return (g1m_error_op);
|
|
|
|
/* look for the decoding function */
|
|
cas_decode_function decode = lookup_cas_decode(file->head.type);
|
|
if (!decode) {
|
|
log_error("No dedicated decoding function was found for this type!");
|
|
return (g1m_error_unknown);
|
|
}
|
|
|
|
/* decode the part */
|
|
return ((*decode)(file, buffer));
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* File decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_cas:
|
|
* Decode a CAS file. TODO.
|
|
*
|
|
* The colon ':' (0x3A) is already read at this point.
|
|
*
|
|
* @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;
|
|
|
|
/* prepare the handle */
|
|
handle->type = g1m_type_mcs;
|
|
handle->files = NULL;
|
|
handle->_size = 0;
|
|
|
|
/* read each */
|
|
for (handle->count = 0;;) {
|
|
/* read the head */
|
|
log_info("Reading file head #%d", handle->count);
|
|
g1m_mcshead_t head;
|
|
if ((err = g1m_decode_casfile_head(&head, buffer))) goto fail;
|
|
if (head.type == g1m_mcstype_end) break;
|
|
|
|
/* prepare space in the index */
|
|
if (handle->count == handle->_size) {
|
|
int newsize = handle->_size + 4;
|
|
log_info("index not big enough, allocating from %d to %d slots",
|
|
handle->_size, newsize);
|
|
|
|
/* allocate new index */
|
|
err = g1m_error_alloc;
|
|
g1m_mcsfile_t **index = calloc(sizeof(g1m_mcsfile_t*), newsize);
|
|
if (!index) goto fail;
|
|
|
|
/* copy old data */
|
|
memcpy(index, handle->files,
|
|
sizeof(g1m_mcsfile_t*) * handle->_size);
|
|
memset(&index[handle->_size], 0,
|
|
sizeof(g1m_mcsfile_t*) * (newsize - handle->_size));
|
|
handle->files = index;
|
|
handle->_size = newsize;
|
|
}
|
|
|
|
/* prepare the file */
|
|
log_info("Preparing the file.");
|
|
err = g1m_make_mcsfile(&handle->files[handle->count], &head);
|
|
if (err) goto fail;
|
|
|
|
/* read each part */
|
|
g1m_mcsfile_t *file = handle->files[handle->count]; handle->count++;
|
|
#if LOGLEVEL <= ll_info
|
|
for (int j = 1; file->head.flags & g1m_mcsflag_unfinished; j++) {
|
|
#else
|
|
while (file->head.flags & g1m_mcsflag_unfinished) {
|
|
#endif
|
|
log_info("Reading part #%d", j);
|
|
/* read the colon */
|
|
uint8_t colon; GREAD(&colon, 1)
|
|
err = g1m_error_magic;
|
|
if (colon != ':') goto fail;
|
|
|
|
/* read the part */
|
|
err = g1m_decode_casfile_part(file, buffer);
|
|
if (err) goto fail;
|
|
}
|
|
|
|
/* read first colon of the next part */
|
|
err = g1m_error_magic;
|
|
uint8_t nextcolon; GREAD(&nextcolon, 1)
|
|
if (nextcolon != ':') goto fail;
|
|
}
|
|
|
|
/* everything went well :) */
|
|
return (0);
|
|
|
|
fail:
|
|
/* END OF DA WORLD */
|
|
g1m_free_mcs(handle);
|
|
return (err);
|
|
}
|