cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/decode/caspro.c

134 lines
4.1 KiB
C

/* *****************************************************************************
* decode/caspro.c -- decode legacy CASIO protocol '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_caspro_##NAME
/* ************************************************************************** */
/* Type correspondance list */
/* ************************************************************************** */
/* Part parsing function type */
typedef int (*caspro_decode_function)(g1m_mcsfile_t*, g1m_buffer_t*);
/* Correspondance type */
struct caspro_corresp {
unsigned int type;
caspro_decode_function decode;
};
/* All correspondances */
#define TTERM {0, NULL}
static struct caspro_corresp caspro_types[] = {
{g1m_mcstype_var, FUNC(var)},
TTERM
};
/**
* lookup_caspro_decode:
* Lookup for a parsing function for the Caspro part.
*
* @arg type the libg1m MCS file type.
* @return the function (NULL if not found).
*/
static caspro_decode_function lookup_caspro_decode(g1m_mcstype_t type)
{
/* lookup for the type */
struct caspro_corresp *c = caspro_types;
while (c->decode) {
if (type == c->type)
break;
c++;
}
/* return the function */
return (c->decode);
}
/* ************************************************************************** */
/* Head decoding function */
/* ************************************************************************** */
/**
* g1m_decode_caspro_head:
* Decode a CASIOLINK Protocol header.
*
* @arg head the head to fill.
* @arg buffer the buffer to read from.
* @return if there was an error, or not.
*/
int g1m_decode_caspro_head(g1m_mcshead_t *head, g1m_buffer_t *buffer)
{
/* read header, get type */
DREAD(hd, caspro_header)
hd.length = be16toh(hd.length);
if (g1m_maketype_caspro(head, (char*)hd.type, (char*)hd.data))
return (g1m_error_unknown);
/* read specific data */
switch (head->type) {
case g1m_mcstype_program:;
/* copy password */
char *endp = memchr(hd.aux, 0xFF, 8);
size_t plen = endp ? (size_t)(endp - (char*)hd.aux) : 8;
memcpy(head->password, hd.aux, plen);
head->password[plen] = 0;
break;
case g1m_mcstype_variable:
/* TODO: id */
head->count = 1;
if (hd.used)
head->flags |= g1m_mcsflag_unfinished;
break;
}
/* TODO */
return (0);
}
/* ************************************************************************** */
/* Part decoding function */
/* ************************************************************************** */
/**
* g1m_decode_caspro_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_caspro_part(g1m_mcsfile_t *file, g1m_buffer_t *buffer)
{
/* checks */
if (!file) return (g1m_error_op);
/* look for the decoding function */
caspro_decode_function decode = lookup_caspro_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));
}