299 lines
8.5 KiB
C
299 lines
8.5 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*, 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, 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(alphamem)},
|
|
{g1m_mcstype_variable, FUNC(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);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* 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 */
|
|
if (g1m_maketype_mcs((char*)groupname, (char*)filename, raw_type,
|
|
&head->type, &head->id))
|
|
head->type = g1m_mcstype_unknown;
|
|
log_info("libg1m file type is 0x%08" PRIXMCSTYPE, 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);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* 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);
|
|
|
|
/* 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;
|
|
}
|
|
|
|
/* read the buffer (safer) */
|
|
uint8_t *buf = malloc(head->size);
|
|
if (!buf) return (g1m_error_alloc);
|
|
READ(buf, head->size)
|
|
|
|
/* decode */
|
|
g1m_buffer_t membuf = MEMBUFFER(buf, head->size);
|
|
err = (*decode)(h, &membuf, head->size);
|
|
free(buf);
|
|
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)
|
|
{
|
|
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 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_std_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);
|
|
}
|