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/mcs/list.c

116 lines
3.2 KiB
C

/* *****************************************************************************
* decode/mcs/list.c -- decode an MCS list.
* 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_list:
* Decode an List.
*
* @arg handle the handle.
* @arg buffer the buffer to read from.
* @arg size the length.
* @return the error code (0 if ok).
*/
int g1m_decode_mcs_list(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
uint_fast32_t size)
{
/* read header */
DREAD(hd, mcs_listheader)
/* correct endianess */
uint_fast16_t elcount = be16toh(hd.element_count);
/* log */
log_info("Title is '%.8s'", hd.title);
/* prepare browsing */
size -= sizeof(struct mcs_listheader);
/* make tabs */
g1m_bcd_t real[elcount], imgn[elcount];
if (elcount) {
int one_imgn = 0;
mcs_bcd_t tab[elcount];
/* read the real parts */
size_t elsize = sizeof(mcs_bcd_t) * elcount;
size -= elsize; {
/* read reals */
READ(tab, elsize)
/* convert them */
for (uint_fast16_t i = 0; i < elcount; i++)
one_imgn |= g1m_bcd_frommcs(&tab[i], &real[i]);
}
/* read the imaginary parts */
if (one_imgn) {
READ(tab, elsize)
/* convert */
for (uint_fast16_t i = 0; i < elcount; i++)
g1m_bcd_frommcs(&tab[i], &imgn[i]);
}
}
/* fill final tab */
handle->head.height = elcount;
handle->head.width = 1;
handle->cells = NULL;
if (elcount) {
#if LOGLEVEL >= ll_info
char rbuf[G1M_BCD_GOODBUFSIZE], ibuf[G1M_BCD_GOODBUFSIZE];
#endif
/* allocate final tab */
g1m_mcs_cell_t **tab = malloc(sizeof(g1m_mcs_cell_t*) * elcount);
if (!tab) return (g1m_error_alloc);
g1m_mcs_cell_t *rws = malloc(sizeof(g1m_mcs_cell_t) * elcount);
if (!tab) { free(tab); return (g1m_error_alloc); }
/* main copying loop */
for (uint_fast32_t y = 0; y < elcount; y++) {
int has_complex = g1m_bcd_has_special(&real[y]);
/* prepare index and store */
tab[y] = &rws[y];
tab[y][0] = (g1m_mcs_cell_t){
.used = 1,
.real = real[y],
.imgn = has_complex ? imgn[y] : (g1m_bcd_t){}
};
#if LOGLEVEL >= ll_info
g1m_bcdtoa(&tab[y][0].real, rbuf, G1M_BCD_GOODBUFSIZE);
if (has_complex) {
g1m_bcdtoa(&tab[y][0].imgn, ibuf, G1M_BCD_GOODBUFSIZE);
log_info("[%" PRIuFAST32 "] %si + %s", y, ibuf, rbuf);
} else
log_info("[%" PRIuFAST32 "] %s", y, rbuf);
#endif
}
/* don't forget your baguette! */
handle->cells = tab;
}
/* no error */
return (0);
}