813 lines
20 KiB
C
813 lines
20 KiB
C
/* *****************************************************************************
|
|
* decode/mcs.c -- decode an MCS archive 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>
|
|
#include <stdlib.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Internal functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* get_image:
|
|
* Allocate space, and convert monochromic image to 0x0RGB.
|
|
*
|
|
* @arg buffer the buffer to read from.
|
|
* @arg img the image to make.
|
|
* @arg width the image width.
|
|
* @arg height the image height.
|
|
*/
|
|
|
|
static int get_image(g1m_buffer_t *buffer, uint32_t ***img,
|
|
unsigned int width, unsigned int height)
|
|
{
|
|
/* get raw pixels */
|
|
size_t linesize = width / 8 + !!(width % 8);
|
|
size_t bufsize = height * linesize;
|
|
uint8_t buf[bufsize];
|
|
READ(buf, bufsize)
|
|
|
|
/* alloc */
|
|
*img = malloc(height * (sizeof(uint32_t*))
|
|
+ width * height * sizeof(uint32_t));
|
|
if (!*img)
|
|
return (g1m_error_alloc);
|
|
|
|
/* fill */
|
|
uint32_t **image = *img;
|
|
for (uint_fast16_t y = 0; y < height; y++) {
|
|
/* setup index */
|
|
image[y] = (uint32_t*)&image[height] + y * width;
|
|
|
|
/* init vars for monochrome with fill bits image browsing */
|
|
uint8_t *b = &buf[y * linesize];
|
|
int bit = 1 << 7;
|
|
|
|
/* browse and save pixels! (monochrome to 0x0RGB) */
|
|
for (uint_fast16_t x = 0; x < width; x++) {
|
|
/* set pixel */
|
|
image[y][x] = *b & bit ? 0xffffff : 0x000000;
|
|
|
|
/* go further */
|
|
b += bit & 1;
|
|
bit = (bit >> 1) | ((bit & 1) << 7);
|
|
}
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* MCS file parsing */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* mcs_decode_program:
|
|
* Decode a program.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_program(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_programheader)
|
|
|
|
/* print header data */
|
|
log_info("Program password is '%.8s'.", hd.password);
|
|
|
|
/* store info */
|
|
strncpy(handle->password, (char*)hd.password, 8);
|
|
handle->password[8] = 0;
|
|
|
|
/* get content */
|
|
size_t content_size = length - sizeof(struct mcs_programheader);
|
|
log_info("Getting program content (%" PRIuSIZE "o)", content_size);
|
|
handle->content = malloc(content_size + 1);
|
|
handle->head.size = content_size + 1;
|
|
if (!handle->content) return (g1m_error_alloc);
|
|
READ(handle->content, content_size);
|
|
handle->content[content_size] = 0;
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_spreadsheet:
|
|
* Decode a spreadsheet.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_spreadsheet(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_spreadsheetheader)
|
|
|
|
/* check if the spreadsheet type is known */
|
|
if (hd.has_subheader != 0x01)
|
|
return (0);
|
|
|
|
/* read subheader */
|
|
DREAD(shd, mcs_spreadsheet_subheader)
|
|
|
|
/* correct endianness */
|
|
uint_fast32_t colcount = hd.column_count;
|
|
colcount = be32toh(colcount << 8);
|
|
shd.defs_size = be32toh(shd.defs_size);
|
|
|
|
/* prepare */
|
|
g1m_mcs_cell_t cells[1000 * colcount];
|
|
bzero(cells, sizeof(g1m_mcs_cell_t) * 1000 * colcount);
|
|
int cells_count = 0;
|
|
uint_fast32_t rows = 0;
|
|
uint_fast32_t cols = 0;
|
|
|
|
/* log some info */
|
|
log_info("%" PRIuFAST32 " columns to read!", colcount);
|
|
|
|
if (colcount) {
|
|
/* get the column directory */
|
|
uint32_t column_directory[colcount];
|
|
READ(&column_directory, sizeof(uint32_t) * colcount)
|
|
|
|
/* browse columns */
|
|
for (uint_fast32_t c = 0; c < colcount; c++) {
|
|
/* check if column is empty */
|
|
if (!column_directory[c])
|
|
continue;
|
|
|
|
/* get the row directory */
|
|
uint8_t row_directory[0x80];
|
|
READ(&row_directory, (size_t)0x80)
|
|
|
|
/* initialize loop values */
|
|
uint8_t *rd = row_directory;
|
|
int bit = 1 << 7;
|
|
|
|
/* explore each cell */
|
|
for (uint_fast32_t i = 0; i < 1000; i++) {
|
|
/* check if used */
|
|
if (*rd & bit) {
|
|
/* get cell */
|
|
g1m_bcd_t cell;
|
|
DREAD(rawcell, bcd)
|
|
g1m_bcd_frommcs(&rawcell, &cell);
|
|
|
|
/* store it */
|
|
cells[c * 1000 + i] = (g1m_mcs_cell_t){
|
|
.used = 1,
|
|
.real = cell,
|
|
.imgn = (g1m_bcd_t){}
|
|
};
|
|
|
|
/* check things (max row, max col, cells count) */
|
|
rows = max(rows, i);
|
|
cols = c;
|
|
cells_count++;
|
|
}
|
|
|
|
/* iterate bit and rd */
|
|
rd += (bit & 1);
|
|
bit = (bit >> 1) | ((bit & 1) << 7);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* we have max rows and columns, increment to have sizes */
|
|
rows++, cols++;
|
|
|
|
/* create final tab */
|
|
handle->cells = NULL;
|
|
handle->columns = 0;
|
|
handle->rows = 0;
|
|
if (cells_count) {
|
|
handle->columns = cols;
|
|
handle->rows = rows;
|
|
|
|
/* alloc */
|
|
g1m_mcs_cell_t **tab = malloc(sizeof(g1m_mcs_cell_t*) * cols);
|
|
if (!tab) return (g1m_error_alloc);
|
|
g1m_mcs_cell_t *rws = malloc(sizeof(g1m_mcs_cell_t) * cols * rows);
|
|
if (!rws) { free(tab); return (g1m_error_alloc); }
|
|
|
|
/* main copying loop */
|
|
for (uint_fast32_t y = 0; y < cols; y++) {
|
|
/* prepare index */
|
|
tab[y] = &rws[y * rows];
|
|
|
|
/* copy each cell */
|
|
for (uint_fast32_t x = 0; x < rows; x++)
|
|
tab[y][x] = cells[x * 1000 + y];
|
|
}
|
|
|
|
/* i used to be a cow. */
|
|
handle->cells = tab;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_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).
|
|
*/
|
|
|
|
static int mcs_decode_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 < elsize; i++)
|
|
one_imgn |= g1m_bcd_frommcs(&tab[elcount], &real[elcount]);
|
|
}
|
|
|
|
/* read the imaginary parts */
|
|
if (one_imgn) {
|
|
READ(tab, elsize)
|
|
|
|
/* convert */
|
|
for (uint_fast16_t i = 0; i < elsize; i++)
|
|
g1m_bcd_frommcs(&tab[elcount], &imgn[elcount]);
|
|
}
|
|
}
|
|
|
|
/* fill final tab */
|
|
handle->rows = elcount;
|
|
handle->columns = 1;
|
|
handle->cells = NULL;
|
|
if (elcount) {
|
|
/* 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){}
|
|
};
|
|
}
|
|
|
|
/* don't forget your baguette! */
|
|
handle->cells = tab;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_matrix:
|
|
* Decode a matrix.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_matrix(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
g1m_mcs_cell_t **tab, *rws;
|
|
int err = g1m_error_alloc;
|
|
|
|
/* read header */
|
|
DREAD(hd, mcs_matheader)
|
|
|
|
/* correct endianess */
|
|
hd.width = be16toh(hd.width);
|
|
hd.height = be16toh(hd.height);
|
|
|
|
/* log info */
|
|
uint_fast32_t w = hd.width, h = hd.height;
|
|
log_info("Matrix size is %" PRIuFAST32 "*%" PRIuFAST32, w, h);
|
|
|
|
/* store dimensions */
|
|
handle->rows = w;
|
|
handle->columns = h;
|
|
handle->cells = NULL;
|
|
|
|
if (w && h) {
|
|
/* alloc real matrix */
|
|
tab = malloc(sizeof(g1m_mcs_cell_t*) * h);
|
|
if (!tab) return (g1m_error_alloc);
|
|
rws = malloc(sizeof(g1m_mcs_cell_t) * h * w);
|
|
if (!rws) { free(tab); return (g1m_error_alloc); }
|
|
|
|
/* copy */
|
|
int one_imgn = 0;
|
|
for (uint_fast32_t y = 0; y < h; y++) {
|
|
/* prepare index */
|
|
tab[y] = &rws[y * w];
|
|
|
|
/* read squares */
|
|
for (uint_fast32_t x = 0; x < w; x++) {
|
|
/* read the cell */
|
|
g1m_bcd_t cell;
|
|
GDREAD(rawcell, bcd)
|
|
one_imgn |= g1m_bcd_frommcs(&rawcell, &cell);
|
|
|
|
/* store it */
|
|
tab[y][x] = (g1m_mcs_cell_t){
|
|
.real = cell,
|
|
.imgn = {},
|
|
.used = 1
|
|
};
|
|
}
|
|
}
|
|
|
|
/* check imaginary parts */
|
|
if (one_imgn) {
|
|
for (uint_fast32_t y = 0; y < h; y++)
|
|
for (uint_fast32_t x = 0; x < w; x++) {
|
|
GDREAD(rawcell, bcd)
|
|
if (tab[y][x].real.spe) {
|
|
g1m_bcd_t cell;
|
|
g1m_bcd_frommcs(&rawcell, &cell);
|
|
tab[y][x].imgn = cell;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* your sandwich, monsieur. */
|
|
handle->cells = tab;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* in case of unexpected EOF */
|
|
fail:
|
|
free(tab);
|
|
free(rws);
|
|
return (err);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_capture:
|
|
* Decode a capture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_capture(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_captureheader)
|
|
|
|
/* correct endianess */
|
|
hd.width = be16toh(hd.width);
|
|
hd.height = be16toh(hd.height);
|
|
|
|
/* store */
|
|
handle->width = hd.width;
|
|
handle->height = hd.height;
|
|
|
|
/* print info */
|
|
log_info("capture is %dx%d sized", handle->width, handle->height);
|
|
|
|
/* get the image and return */
|
|
return (get_image(buffer, &handle->image, handle->width, handle->height));
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_picture:
|
|
* Decode a picture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_picture(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
int err;
|
|
|
|
/* store some dimensions, in case. */
|
|
handle->width = 128;
|
|
handle->height = 64;
|
|
|
|
/* get the first image */
|
|
if ((err = get_image(buffer, &handle->image, 128, 64))) {
|
|
log_fatal("Failed to get the first image.");
|
|
return (err);
|
|
}
|
|
|
|
/* and the second */
|
|
if ((err = get_image(buffer, &handle->second_image, 128, 64))) {
|
|
log_fatal("Failed to get the second image.");
|
|
return (err);
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_string:
|
|
* Decode an MCS string.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_string(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
(void)handle;
|
|
log_info("String MCS file is not managed yet. Content:");
|
|
uint8_t str[length];
|
|
READ(str, length);
|
|
logm_info(str, length);
|
|
/* TODO */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_setup:
|
|
* Decode settings.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_setup(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
(void)handle;
|
|
log_info("Settings MCS file is not managed yet.");
|
|
/* TODO */
|
|
SKIP(length)
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* mcs_decode_alphamem:
|
|
* Decode alpha memory (variables).
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int mcs_decode_alphamem(g1m_mcsfile_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read the data */
|
|
uint8_t buf[length];
|
|
READ(buf, length)
|
|
|
|
/* allocate the vars table */
|
|
handle->vars = malloc(29 * sizeof(g1m_mcs_cell_t));
|
|
if (!handle->vars) return (g1m_error_alloc);
|
|
bzero(handle->vars, 29 * sizeof(g1m_mcs_cell_t));
|
|
|
|
/* copy */
|
|
size_t tocopy = length - length % sizeof(g1m_mcs_cell_t);
|
|
memcpy(handle->vars, buf, tocopy);
|
|
|
|
/* no problem, woop woop */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Type correspondance list */
|
|
/* ************************************************************************** */
|
|
/* MCS file parsing function type */
|
|
typedef int (*mcs_decode_func_t)(g1m_mcsfile_t*, g1m_buffer_t*, uint_fast32_t);
|
|
|
|
/* Correspondance type */
|
|
struct mcs_corresp {
|
|
unsigned int type;
|
|
mcs_decode_func_t decode;
|
|
};
|
|
|
|
/* All correspondances */
|
|
static struct mcs_corresp mcs_types[] = {
|
|
{g1m_mcstype_program, mcs_decode_program},
|
|
{g1m_mcstype_list, mcs_decode_list},
|
|
{g1m_mcstype_mat, mcs_decode_matrix},
|
|
{g1m_mcstype_vct, mcs_decode_matrix},
|
|
{g1m_mcstype_spreadsheet, mcs_decode_spreadsheet},
|
|
{g1m_mcstype_pict, mcs_decode_picture},
|
|
{g1m_mcstype_capt, mcs_decode_capture},
|
|
{g1m_mcstype_string, mcs_decode_string},
|
|
{g1m_mcstype_setup, mcs_decode_setup},
|
|
{g1m_mcstype_alphamem, mcs_decode_alphamem},
|
|
{}
|
|
};
|
|
|
|
/**
|
|
* lookup_mcsfile_decode:
|
|
* Lookup for a parsing function for an MCS file type.
|
|
*
|
|
* @arg type the libg1m MCS file type.
|
|
* @return the function (NULL if not found).
|
|
*/
|
|
|
|
static mcs_decode_func_t lookup_mcsfile_decode(unsigned int type)
|
|
{
|
|
/* lookup for the type */
|
|
struct mcs_corresp *c = mcs_types;
|
|
while (c->decode) {
|
|
if (type == c->type)
|
|
break;
|
|
c++;
|
|
}
|
|
|
|
/* return the function */
|
|
return (c->decode);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Public file functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_mcsfile_head:
|
|
* Decode MCS file head.
|
|
*
|
|
* @arg head the head to fill.
|
|
* @arg raw_type the raw file type.
|
|
* @arg groupname the groupname (up to 16 bytes).
|
|
* @arg dirname the directory name (up to 8 bytes).
|
|
* @arg filename the filename (up to 8 bytes).
|
|
* @arg filesize the data length.
|
|
* @return 0 if the head was filled with success, -1 otherwise.
|
|
*/
|
|
|
|
int g1m_decode_mcsfile_head(g1m_mcshead_t *head,
|
|
int raw_type, const unsigned char *groupname,
|
|
const unsigned char *dirname, const unsigned char *filename,
|
|
uint_fast32_t filesize)
|
|
{
|
|
/* check that we have a head, lol */
|
|
if (!head) return (-1);
|
|
|
|
/* look for the raw type */
|
|
if (g1m_mcstype_get((char*)groupname, (char*)filename, raw_type,
|
|
&head->type, &head->id))
|
|
head->type = g1m_mcstype_unknown;
|
|
log_info("libg1m file type is 0x%04X", head->type);
|
|
#if LOGLEVEL <= ll_info
|
|
if (g1m_mcstype_uses_id(head->type)) {
|
|
log_info("libg1m file id is (%d, %d)", g1m_get_id_major(head->id),
|
|
g1m_get_id_minor(head->id));
|
|
}
|
|
#endif
|
|
|
|
/* copy the name and size */
|
|
memcpy(head->name, filename, 8); head->name[8] = 0;
|
|
head->size = filesize;
|
|
|
|
/* save raw data */
|
|
head->_rawtype = raw_type;
|
|
memcpy(head->_group, groupname, 16); head->_group[16] = 0;
|
|
if (dirname) {
|
|
memcpy(head->_dirname, dirname, 8); head->_dirname[8] = 0;
|
|
} else memset(head->_dirname, 0, 9);
|
|
|
|
/* everything went well! */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* g1m_decode_mcsfile:
|
|
* Decode MCS file content.
|
|
*
|
|
* Part of the public API because of the Protocol 7, where file is sent
|
|
* without its header (only with specific subheaders).
|
|
*
|
|
* @arg handle the handle to make.
|
|
* @arg head the head to use.
|
|
* @arg buffer the buffer to read from.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcsfile(g1m_mcsfile_t **handle, const g1m_mcshead_t *head,
|
|
g1m_buffer_t *buffer)
|
|
{
|
|
int err = 0;
|
|
/* check that the head is there */
|
|
if (!head) return (g1m_error_op);
|
|
|
|
/* create handle */
|
|
*handle = malloc(sizeof(g1m_mcsfile_t));
|
|
if (!handle) return (g1m_error_alloc);
|
|
g1m_mcsfile_t *h = *handle;
|
|
memset(h, 0, sizeof(g1m_mcsfile_t));
|
|
|
|
/* copy the head */
|
|
memcpy(&h->head, head, sizeof(g1m_mcshead_t));
|
|
|
|
/* look for the parsing function */
|
|
mcs_decode_func_t decode = lookup_mcsfile_decode(head->type);
|
|
if (!decode) {
|
|
log_error("No dedicated parsing function for this type was found!");
|
|
goto notparsing;
|
|
}
|
|
|
|
/* decode */
|
|
err = (*decode)(h, buffer, head->size);
|
|
if (err) goto fail;
|
|
|
|
/* no error :D */
|
|
return (0);
|
|
|
|
notparsing:
|
|
/* allocate enough space */
|
|
h->content = malloc(head->size);
|
|
err = g1m_error_alloc;
|
|
if (!h->content) goto fail;
|
|
|
|
/* read the content */
|
|
GREAD(h->content, head->size)
|
|
|
|
/* log */
|
|
log_info("File content:");
|
|
logm_info(h->content, head->size);
|
|
|
|
/* saved normally */
|
|
return (0);
|
|
|
|
fail:
|
|
if (h) {
|
|
if (h->content) free(h->content);
|
|
free(h);
|
|
}
|
|
*handle = NULL;
|
|
return (err);
|
|
}
|
|
|
|
/**
|
|
* g1m_decode_mcsfile_data:
|
|
* Decode a MCS file content from raw memory.
|
|
*
|
|
* @arg handle the file handle.
|
|
* @arg head the file head.
|
|
* @arg data the buffer data.
|
|
* @arg size the buffer size.
|
|
* @return the libg1m error.
|
|
*/
|
|
|
|
int g1m_decode_mcsfile_data(g1m_mcsfile_t **handle,
|
|
const g1m_mcshead_t *head, const unsigned char *data, size_t size)
|
|
{
|
|
const unsigned char *p = data;
|
|
g1m_buffer_t buffer = {
|
|
.cookie = p,
|
|
.read = g1m_membuffer_read
|
|
};
|
|
|
|
return (g1m_decode_mcsfile(handle, head, &buffer));
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Main file function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_mcs:
|
|
* Decode an MCS file, after the Standard Header.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg num number of sizes.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcs(g1m_t *handle, g1m_buffer_t *buffer,
|
|
struct standard_header *std)
|
|
{
|
|
int err = g1m_error_alloc;
|
|
/* get number of subparts from the standard header */
|
|
uint_fast16_t num = std->number;
|
|
|
|
/* allocate memory for the files index */
|
|
handle->count = 0;
|
|
handle->_size = num;
|
|
if (num) {
|
|
handle->files = malloc(sizeof(g1m_mcsfile_t*) * num);
|
|
if (!handle->files) return (g1m_error_alloc);
|
|
}
|
|
|
|
/* read all of the parts */
|
|
log_info("%" PRIuFAST16 " total mcs files to browse", num);
|
|
while (handle->count < (int)num) {
|
|
/* get the subheader */
|
|
GDREAD(hd, mcs_subheader)
|
|
|
|
/* correct endianess */
|
|
hd.subcount = be32toh(hd.subcount);
|
|
|
|
/* log info about part */
|
|
log_info("New group! Group name is '%.16s'.", hd.intname);
|
|
log_info("%d mcs files to browse in that group", hd.subcount);
|
|
|
|
/* foreach subpart */
|
|
for (uint_fast32_t i = 0; i < hd.subcount; i++) {
|
|
/* get the part header */
|
|
GDREAD(fhd, mcs_fileheader)
|
|
|
|
/* correct endianess */
|
|
fhd.datalength = be32toh(fhd.datalength);
|
|
|
|
/* log info about the subpart */
|
|
log_info("[%" PRIuFAST32 "] directory name is '%.8s'",
|
|
i, fhd.dirname);
|
|
log_info("[%" PRIuFAST32 "] filename is '%.8s'", i, fhd.filename);
|
|
log_info("[%" PRIuFAST32 "] data length is %" PRIu32,
|
|
i, fhd.datalength);
|
|
|
|
/* decode the head */
|
|
g1m_mcshead_t head;
|
|
g1m_decode_mcsfile_head(&head, fhd.filetype, hd.intname,
|
|
fhd.dirname, fhd.filename, fhd.datalength);
|
|
|
|
/* decode */
|
|
handle->files[handle->count] = NULL;
|
|
err = g1m_decode_mcsfile(&handle->files[handle->count],
|
|
&head, buffer);
|
|
if (err) goto fail;
|
|
handle->count++;
|
|
}
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* was error! */
|
|
fail:
|
|
g1m_free_mcs(handle);
|
|
return (err);
|
|
}
|