118 lines
3.8 KiB
C
118 lines
3.8 KiB
C
/* *****************************************************************************
|
|
* decode/cas/cell.c -- decode a CAS matrix/list/variable.
|
|
* 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/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Internal function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* decode_cell:
|
|
* Read a cell.
|
|
*
|
|
* @arg buffer the buffer to read from.
|
|
* @arg cell the cell to fill.
|
|
* @arg x the abscissa of the cell.
|
|
* @arg y the ordinate of the cell.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int decode_cell(g1m_buffer_t *buffer, g1m_mcscell_t *cell,
|
|
unsigned int *x, unsigned int *y)
|
|
{
|
|
uint8_t csum = 0;
|
|
cell->g1m_mcscell_flags = g1m_mcscellflag_used;
|
|
|
|
/* read position */
|
|
uint16_t fx, fy;
|
|
READ(&fx, sizeof(uint16_t)) *x = be16toh(fx) - 1;
|
|
READ(&fy, sizeof(uint16_t)) *y = be16toh(fy) - 1;
|
|
csum += g1m_checksum8(&fx, sizeof(uint16_t));
|
|
csum += g1m_checksum8(&fy, sizeof(uint16_t));
|
|
|
|
/* read the parts */
|
|
DREAD(wkg, g1m_casbcd_s)
|
|
csum += g1m_checksum8(&wkg, sizeof(g1m_casbcd_t));
|
|
if (g1m_bcd_fromcas(&wkg, &cell->g1m_mcscell_real)) {
|
|
READ(&wkg, sizeof(g1m_casbcd_t))
|
|
csum += g1m_checksum8(&wkg, sizeof(g1m_casbcd_t));
|
|
g1m_bcd_fromcas(&wkg, &cell->g1m_mcscell_imgn);
|
|
}
|
|
|
|
/* read and check the checksum */
|
|
uint8_t checksum; READ(&checksum, sizeof(uint8_t))
|
|
if (~csum + 1 != checksum)
|
|
return (g1m_error_checksum);
|
|
|
|
/* no prob'! */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Cell-reading CAS part decoding functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_caspart_matrix:
|
|
* Decode a CAS matrix part.
|
|
*
|
|
* @arg handle the handle to contribute to.
|
|
* @arg buffer the buffer to read from.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_caspart_matrix(g1m_mcsfile_t *handle, g1m_buffer_t *buffer)
|
|
{
|
|
/* read the cell */
|
|
g1m_mcscell_t cell; unsigned int y, x;
|
|
int err = decode_cell(buffer, &cell, &x, &y);
|
|
if (err) return (err);
|
|
|
|
/* save the cell (FIXME: secure) */
|
|
handle->g1m_mcsfile_cells[y][x] = cell;
|
|
|
|
/* check if its the last cell */
|
|
if (y == handle->g1m_mcsfile_head.g1m_mcshead_height - 1
|
|
&& x == handle->g1m_mcsfile_head.g1m_mcshead_width - 1)
|
|
handle->g1m_mcsfile_head.g1m_mcshead_flags &= ~g1m_mcsflag_unfinished;
|
|
|
|
/* no error! */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* g1m_decode_caspart_var:
|
|
* Decode a CAS variable part.
|
|
*
|
|
* @arg handle the handle to contribute to.
|
|
* @arg buffer the buffer to read from.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_caspart_var(g1m_mcsfile_t *handle, g1m_buffer_t *buffer)
|
|
{
|
|
/* read and save the cell */
|
|
unsigned int x, y;
|
|
int err = decode_cell(buffer, &handle->g1m_mcsfile_var, &x, &y);
|
|
if (err) return (err);
|
|
if (x != 1 || y != 1) return (g1m_error_magic);
|
|
|
|
/* no error! */
|
|
handle->g1m_mcsfile_head.g1m_mcshead_flags &= ~g1m_mcsflag_unfinished;
|
|
return (0);
|
|
}
|