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

270 lines
7.8 KiB
C

/* *****************************************************************************
* decode/std/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>
#define FUNC(NAME) g1m_decode_mcs_##NAME
/* ************************************************************************** */
/* Type correspondance list */
/* ************************************************************************** */
/* MCS file parsing function type */
typedef int (*mcs_decode_func_t)(g1m_mcsfile_t**, g1m_buffer_t*,
g1m_mcshead_t*);
/* Correspondance type */
struct mcs_corresp {
unsigned int type;
mcs_decode_func_t decode;
};
/* All correspondances */
#define TTERM {0, NULL}
static struct mcs_corresp mcs_types[] = {
{g1m_mcstype_program, FUNC(program)},
{g1m_mcstype_list, FUNC(list)},
{g1m_mcstype_mat, FUNC(matrix)},
{g1m_mcstype_vct, FUNC(matrix)},
{g1m_mcstype_spreadsheet, FUNC(spreadsheet)},
{g1m_mcstype_pict, FUNC(picture)},
{g1m_mcstype_capt, FUNC(capture)},
{g1m_mcstype_string, FUNC(string)},
{g1m_mcstype_setup, FUNC(setup)},
{g1m_mcstype_alphamem, FUNC(var)},
{g1m_mcstype_variable, FUNC(var)},
TTERM
};
/**
* 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(g1m_mcstype_t 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);
}
/* ************************************************************************** */
/* Head decoding function */
/* ************************************************************************** */
/**
* 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 */
g1m_maketype_mcs(head, (char*)groupname, (char*)dirname,
(char*)filename, raw_type);
head->size = filesize;
log_info("libg1m file type is 0x%08" PRIXMCSTYPE, head->type);
#if LOGLEVEL <= ll_info
if (g1m_mcshead_uses_id(head)) {
log_info("libg1m file id is (%d, %d)", g1m_get_id_major(head->id),
g1m_get_id_minor(head->id));
}
#endif
/* everything went well! */
return (0);
}
/* ************************************************************************** */
/* File decoding functions */
/* ************************************************************************** */
/**
* 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);
g1m_mcshead_t h = *head;
/* look for the decoding function */
mcs_decode_func_t decode = lookup_mcsfile_decode(head->type);
if (!decode) {
log_error("No dedicated decoding function for this type was found!");
goto notparsing;
}
/* decode */
if (!head->size) err = (*decode)(handle, buffer, &h);
else {
g1m_buffer_t lbuf = LIMBUFFER(buffer, head->size);
err = (*decode)(handle, &lbuf, &h);
if (lbuf._offset < head->size) SKIP(head->size - lbuf._offset)
}
/* oh yeah, and go away. */
if (err) goto fail;
return (0);
notparsing:
/* allocate enough space */
if ((err = g1m_make_mcsfile(handle, &h)))
return (err);
/* read the content */
GREAD((*handle)->content, h.size)
/* log */
log_info("File content:");
logm_info((*handle)->content, h.size);
/* saved normally */
return (0);
fail:
g1m_free_mcsfile(*handle);
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)
{
g1m_buffer_t membuf = MEMBUFFER(data, size);
return (g1m_decode_mcsfile(handle, head, &membuf));
}
/* ************************************************************************** */
/* Main file function */
/* ************************************************************************** */
/**
* g1m_decode_std_mcs:
* Decode an MCS file, after the Standard Header.
*
* @arg h the handle to make.
* @arg buffer the buffer to read from.
* @arg num number of sizes.
* @return the error code (0 if ok).
*/
int g1m_decode_std_mcs(g1m_t **h, 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;
/* make the handle */
err = g1m_make_mcs(h, num);
if (err) return (err);
g1m_t *handle = *h;
/* read all of the parts */
log_info("%" PRIuFAST16 " total mcs files to browse", num);
for (handle->count = 0; 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)
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(*h); *h = NULL;
return (err);
}