95 lines
2.8 KiB
C
95 lines
2.8 KiB
C
/* *****************************************************************************
|
|
* decode/mcs/var.c -- decode an MCS variable/alpha memory 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/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/**
|
|
* g1m_decode_mcs_var:
|
|
* Decode alpha memory (variables).
|
|
*
|
|
* @arg handle the handle to make.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg head the pre-filled head to complete and use.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcs_var(g1m_mcsfile_t **handle, g1m_buffer_t *buffer,
|
|
g1m_mcshead_t *head)
|
|
{
|
|
/* read the data */
|
|
uint_fast32_t length = head->size;
|
|
uint8_t buf[length];
|
|
READ(buf, length)
|
|
|
|
/* complete header */
|
|
head->count = length / (2 * sizeof(mcs_bcd_t));
|
|
int err = g1m_make_mcsfile(handle, head);
|
|
if (err) return (err);
|
|
g1m_mcsfile_t *h = *handle;
|
|
|
|
/* copy */
|
|
const mcs_bcd_t *b = (void*)buf;
|
|
for (int i = 0; i < head->count; i++) {
|
|
g1m_bcd_frommcs(b++, &h->vars[i].real);
|
|
g1m_bcd_frommcs(b++, &h->vars[i].imgn);
|
|
h->vars[i].flags = g1m_mcscellflag_used;
|
|
}
|
|
|
|
/* no problem, woop woop */
|
|
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 the magic and real part */
|
|
uint8_t magic[4]; READ(magic, 4)
|
|
DREAD(rawreal, cas_bcd)
|
|
uint8_t csum = g1m_checksum8(magic, 4)
|
|
+ g1m_checksum8(&rawreal, sizeof(cas_bcd_t));
|
|
|
|
/* read the imaginary part */
|
|
g1m_bcd_t real, imgn = {};
|
|
if (g1m_bcd_fromcas(&rawreal, &real)) {
|
|
DREAD(rawimgn, cas_bcd)
|
|
csum += g1m_checksum8(&rawimgn, sizeof(cas_bcd_t));
|
|
g1m_bcd_fromcas(&rawimgn, &imgn);
|
|
}
|
|
|
|
/* check the checksum */
|
|
uint8_t checksum; READ(&checksum, 1)
|
|
if (~csum + 1 != checksum)
|
|
return (g1m_error_checksum);
|
|
if (memcmp(magic, "\0\1\0\1", 4))
|
|
return (g1m_error_magic);
|
|
|
|
/* set the values and return */
|
|
handle->var.real = real;
|
|
handle->var.imgn = imgn;
|
|
handle->head.flags &= ~g1m_mcsflag_unfinished;
|
|
return (0);
|
|
}
|