597 lines
15 KiB
C
597 lines
15 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* parse/mcs.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/11/02 14:47:49 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
#include <stdlib.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Internal functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* get_image:
|
|
* Allocate space, and convert monochromic image to 0x0RGB.
|
|
*
|
|
* @arg stream the stream from which to read.
|
|
* @arg img the image to make.
|
|
* @arg width the image width.
|
|
* @arg height the image height.
|
|
*/
|
|
|
|
static int get_image(FILE *stream, 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 */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_parse_mcs_program:
|
|
* Parse a program.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_program(g1m_mcsfile_t *handle, FILE *stream,
|
|
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;
|
|
|
|
/* seek for what's left in the file */
|
|
SKIP(length - sizeof(struct mcs_programheader))
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs_spreadsheet:
|
|
* Parse a spreadsheet.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
|
|
uint_fast32_t length)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_spreadsheetheader)
|
|
|
|
/* correct endianess */
|
|
uint_fast32_t colcount = hd.column_count;
|
|
colcount = be32toh(colcount << 8);
|
|
hd.defs_size = be32toh(hd.defs_size);
|
|
|
|
/* log some info */
|
|
log_info("%ld columns to parse!", colcount);
|
|
|
|
/* get the column directory */
|
|
uint32_t column_directory[colcount];
|
|
READ(&column_directory, sizeof(uint32_t) * colcount)
|
|
|
|
/* 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;
|
|
|
|
/* 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 */
|
|
DREAD(cell, bcd)
|
|
|
|
/* store it */
|
|
cells[c * 1000 + i] = (g1m_mcs_cell_t){
|
|
.used = 1,
|
|
.real = cell,
|
|
.imgn = {}
|
|
};
|
|
|
|
/* check things (max row, max col, cells count) */
|
|
rows = max(rows, i);
|
|
cols = c;
|
|
cells_count++;
|
|
|
|
/* log it */
|
|
char *bcd_string = g1m_bcdtoa(&cell);
|
|
log_info("[%ld, %ld] contains '%s'.", c, i, bcd_string);
|
|
free(bcd_string);
|
|
}
|
|
|
|
/* 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);
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs_list:
|
|
* Parse an List.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg size the length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_list(g1m_mcsfile_t *handle, FILE *stream,
|
|
uint_fast32_t size)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_listheader)
|
|
|
|
/* correct endianess */
|
|
uint16_t elcount = hd.element_count;
|
|
elcount = be16toh(elcount);
|
|
|
|
/* prepare browsing */
|
|
size -= sizeof(struct mcs_listheader);
|
|
|
|
/* make tabs */
|
|
size_t elsize = sizeof(struct bcd) * elcount;
|
|
struct bcd real[elcount];
|
|
struct bcd imgn[elcount];
|
|
|
|
/* browse real elements */
|
|
log_info("Browsing %d list elements", elcount);
|
|
READ(&real, elsize)
|
|
size -= elsize;
|
|
|
|
/* browse imaginary elements */
|
|
if (size) {
|
|
log_info("They have complex parts!");
|
|
size_t toread = min(size, elsize);
|
|
READ(&imgn, toread)
|
|
} else
|
|
bzero(imgn, elsize);
|
|
|
|
/* 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++) {
|
|
/* log */
|
|
#if LOGLEVEL <= ll_info
|
|
char *real_string = g1m_bcdtoa(&real[y]);
|
|
if (g1m_bcd_is_zero(&imgn[y]))
|
|
log_info("Cell #%ld is %s.", y, real_string);
|
|
else {
|
|
char *imgn_string = g1m_bcdtoa(&imgn[y]);
|
|
log_info("Cell #%ld is %s+%si.", y, real_string, imgn_string);
|
|
free(imgn_string);
|
|
}
|
|
free(real_string);
|
|
#endif
|
|
|
|
/* prepare index and store */
|
|
tab[y] = &rws[y];
|
|
tab[y][0] = (g1m_mcs_cell_t){
|
|
.used = 1,
|
|
.real = real[y],
|
|
.imgn = imgn[y]
|
|
};
|
|
}
|
|
|
|
/* don't forget your baguette! */
|
|
handle->cells = tab;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs_mat:
|
|
* Parse a Matrix.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_mat(g1m_mcsfile_t *handle, FILE *stream,
|
|
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 %ld*%ld", 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 */
|
|
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 */
|
|
GDREAD(cell, bcd)
|
|
|
|
/* store it */
|
|
tab[y][x] = (g1m_mcs_cell_t){
|
|
.real = cell,
|
|
.imgn = {},
|
|
.used = 1
|
|
};
|
|
}
|
|
}
|
|
|
|
/* your sandwich, monsieur. */
|
|
handle->cells = tab;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* in case of unexpected EOF */
|
|
fail:
|
|
free(tab);
|
|
free(rws);
|
|
return (err);
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs_capture:
|
|
* Parse a capture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_capture(g1m_mcsfile_t *handle, FILE *stream,
|
|
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 %ux%u sized", hd.width, hd.height);
|
|
|
|
/* get the image and return */
|
|
return (get_image(stream, &handle->image, handle->width, handle->height));
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs_picture:
|
|
* Parse a picture.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg length the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
static int g1m_parse_mcs_picture(g1m_mcsfile_t *handle, FILE *stream,
|
|
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(stream, &handle->image, 128, 64))) {
|
|
log_fatal("Failed to get the first image.");
|
|
return (err);
|
|
}
|
|
|
|
/* and the second */
|
|
if ((err = get_image(stream, &handle->second_image, 128, 64))) {
|
|
log_fatal("Failed to get the second image.");
|
|
return (err);
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Public API functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_parse_mcsfile:
|
|
* Parse 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.
|
|
* @arg stream the stream to parse from.
|
|
* @arg raw_type the raw file type.
|
|
* @arg filename the filename (up to 8 bytes are read).
|
|
* @arg dirname the directory name (up to 8 bytes are read).
|
|
* @arg filesize the data length.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_parse_mcsfile_content(g1m_mcsfile_t **handle, FILE *stream,
|
|
int raw_type, const char *filename, const char *dirname,
|
|
uint_fast32_t filesize)
|
|
{
|
|
/* create handle */
|
|
*handle = malloc(sizeof(g1m_mcsfile_t));
|
|
if (!handle) return (g1m_error_alloc);
|
|
|
|
/* save data */
|
|
g1m_mcsfile_t *h = *handle;
|
|
strncpy(h->name, filename, 8);
|
|
strncpy(h->dirname, dirname, 8);
|
|
|
|
/* read that much data */
|
|
int err = 0;
|
|
h->type = 0x00;
|
|
switch (raw_type) {
|
|
case mcs_ftype_program:
|
|
h->type = g1m_mcstype_program;
|
|
err = g1m_parse_mcs_program(h, stream, filesize);
|
|
break;
|
|
case mcs_ftype_list:
|
|
h->type = g1m_mcstype_list;
|
|
err = g1m_parse_mcs_list(h, stream, filesize);
|
|
break;
|
|
case mcs_ftype_mat:
|
|
h->type = g1m_mcstype_mat;
|
|
err = g1m_parse_mcs_mat(h, stream, filesize);
|
|
break;
|
|
case mcs_ftype_spreadsheet:
|
|
h->type = g1m_mcstype_spreadsheet;
|
|
err = g1m_parse_mcs_spreadsheet(h, stream, filesize);
|
|
break;
|
|
case mcs_ftype_picture:
|
|
h->type = g1m_mcstype_pict;
|
|
err = g1m_parse_mcs_picture(h, stream, filesize);
|
|
break;
|
|
case mcs_ftype_capture:
|
|
h->type = g1m_mcstype_capt;
|
|
err = g1m_parse_mcs_capture(h, stream, filesize);
|
|
break;
|
|
default:
|
|
SKIP(filesize)
|
|
}
|
|
|
|
/* free if error, return */
|
|
if (err) {
|
|
free(*handle);
|
|
*handle = NULL;
|
|
}
|
|
return (err);
|
|
}
|
|
|
|
/**
|
|
* g1m_parse_mcs:
|
|
* Parse an MCS file, after the Standard Header.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg stream the stream to parse from.
|
|
* @arg num number of sizes.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_parse_mcs(g1m_t *handle, FILE *stream, uint_fast16_t num)
|
|
{
|
|
int err = g1m_error_alloc;
|
|
|
|
/* No "global" header for MCS parts, because this "global header"
|
|
* was the standard header. */
|
|
|
|
/* allocate memory for the subparts */
|
|
handle->part_count = 0;
|
|
handle->_parts_size = num;
|
|
handle->parts = malloc(sizeof(g1m_mcs_t*) * num);
|
|
if (!handle->parts) return (g1m_error_alloc);
|
|
bzero(handle->parts, sizeof(g1m_mcs_t*) * num);
|
|
|
|
/* proxy */
|
|
g1m_mcs_t **pmcs = handle->parts;
|
|
|
|
/* read all of the parts */
|
|
log_info("%ld parts to browse", num);
|
|
for (uint_fast16_t i = 0; i < num; i++) {
|
|
/* get the subheader */
|
|
GDREAD(hd, mcs_subheader)
|
|
|
|
/* correct endianess */
|
|
hd.subcount = be32toh(hd.subcount);
|
|
|
|
/* log info about part */
|
|
log_info("[%ld] Internal name is '%.16s'", i, hd.intname);
|
|
|
|
/* allocate tab and proxy */
|
|
err = g1m_error_alloc;
|
|
pmcs[i] = malloc(sizeof(g1m_mcs_t));
|
|
if (!pmcs[i]) goto fail;
|
|
g1m_mcs_t *mcs = pmcs[i];
|
|
|
|
/* increment parts count, and remove to num the "extra" subparts */
|
|
handle->part_count++;
|
|
num -= hd.subcount - 1;
|
|
|
|
/* prepare it */
|
|
mcs->file_count = hd.subcount;
|
|
mcs->_files_size = hd.subcount;
|
|
if (!hd.subcount) continue;
|
|
mcs->files = malloc(sizeof(g1m_mcsfile_t*) * hd.subcount);
|
|
if (!mcs->files) goto fail;
|
|
bzero(mcs->files, sizeof(g1m_mcsfile_t*) * hd.subcount);
|
|
|
|
/* foreach subpart */
|
|
log_info("[%ld] %d mcs files to browse", i, hd.subcount);
|
|
for (uint_fast32_t j = 0; j < hd.subcount; j++) {
|
|
/* get the part header */
|
|
GDREAD(fhd, mcs_fileheader)
|
|
|
|
/* correct endianess */
|
|
fhd.datalength = be32toh(fhd.datalength);
|
|
|
|
/* log info about the subpart */
|
|
log_info("[%ld,%ld] directory name is '%.8s'", i, j, fhd.dirname);
|
|
log_info("[%ld,%ld] filename is '%.8s'", i, j, fhd.filename);
|
|
log_info("[%ld,%ld] file type is '%s' (0x%02d)",
|
|
i, j, g1m_get_mcs_ftype_string(fhd.filetype), fhd.filetype);
|
|
log_info("[%ld,%ld] data length is %u", i, j, fhd.datalength);
|
|
|
|
/* decode */
|
|
int err = g1m_parse_mcsfile_content(&mcs->files[j], stream,
|
|
fhd.filetype, (char*)fhd.filename, (char*)fhd.dirname,
|
|
fhd.datalength);
|
|
if (err) return (err);
|
|
}
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* was error! */
|
|
fail:
|
|
g1m_free_mcs(handle);
|
|
return (err);
|
|
}
|