149 lines
4.3 KiB
C
149 lines
4.3 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* decode/fkey.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2017/02/19 23:01:18 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Utilities */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* fkeydup:
|
|
* "Duplicate" function key.
|
|
*
|
|
* @arg fkey fkey to duplicate.
|
|
* @return the "duplicated" function key.
|
|
*/
|
|
|
|
static uint32_t **fkeydup(uint8_t *fkey)
|
|
{
|
|
uint32_t **pixels = alloc_pixels(FKEY_WIDTH, FKEY_HEIGHT);
|
|
if (!pixels) return (NULL);
|
|
prepare_pixels(pixels, FKEY_WIDTH, FKEY_HEIGHT)
|
|
|
|
g1m_pixels_from_packed1bit(pixels, fkey, FKEY_WIDTH, FKEY_HEIGHT);
|
|
return (pixels);
|
|
}
|
|
|
|
/**
|
|
* fkeydup3:
|
|
* "Duplicate" function key for fx-CG.
|
|
*
|
|
* @arg fkey fkey to duplicate.
|
|
* @return the "duplicated" function key.
|
|
*/
|
|
|
|
static uint32_t **fkeydup3(uint8_t *fkey)
|
|
{
|
|
uint32_t **pixels = alloc_pixels(FKEY3_WIDTH, FKEY3_HEIGHT);
|
|
if (!pixels) return (NULL);
|
|
prepare_pixels(pixels, FKEY3_WIDTH, FKEY3_HEIGHT)
|
|
|
|
g1m_pixels_from_packed1bit(pixels, fkey, FKEY3_WIDTH, FKEY3_HEIGHT);
|
|
return (pixels);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* fx function keys file parsing utilities */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_fkey:
|
|
* Decode fx function keys files.
|
|
*
|
|
* @arg handle the libg1m handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg std pointer to the standard header.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_fkey(g1m_t *handle, g1m_buffer_t *buffer,
|
|
struct standard_header *std)
|
|
{
|
|
(void)std;
|
|
/* set handle type */
|
|
handle->type = g1m_type_fkey;
|
|
|
|
/* read the subheader */
|
|
DREAD(hd, g1n_subheader)
|
|
uint_fast16_t num = be16toh(hd.fkey_count) + 1;
|
|
|
|
/* beat the best! read the rest! */
|
|
size_t data_size = std->filesize - sizeof(struct standard_header)
|
|
- sizeof(struct g1n_subheader);
|
|
uint8_t data[data_size];
|
|
READ(data, data_size)
|
|
|
|
/* allocate tab */
|
|
handle->fkeys = malloc(sizeof(uint32_t**) * num);
|
|
handle->count = 0;
|
|
handle->_size = num;
|
|
if (!handle->fkeys) return (g1m_error_alloc);
|
|
|
|
/* get the offset table */
|
|
uint16_t *offsets = (uint16_t*)data;
|
|
uint8_t *fkeys = (uint8_t*)(offsets + num);
|
|
|
|
/* read the function keys name */
|
|
handle->title[16] = 0;
|
|
strncpy(handle->title, (char*)fkeys, 16);
|
|
|
|
/* read all */
|
|
for (uint_fast32_t i = 1; i < num; i++) {
|
|
handle->fkeys[i] = NULL;
|
|
if (offsets[i] == (uint16_t)-1) {
|
|
handle->count++;
|
|
continue ;
|
|
}
|
|
|
|
/* correct offset */
|
|
offsets[i] = be16toh(offsets[i]);
|
|
|
|
/* store */
|
|
handle->fkeys[i] = fkeydup(&fkeys[i]);
|
|
if (!handle->fkeys[i]) goto fail;
|
|
handle->count++;
|
|
}
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* omg fail! */
|
|
fail:
|
|
for (int i = 0; i < handle->count; i++)
|
|
free(handle->fkeys[i]);
|
|
free(handle->messages);
|
|
handle->messages = NULL;
|
|
return (g1m_error_alloc);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* cg function keys file parsing utilities */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_fkey_cg_content:
|
|
* Decode fx-CG key files.
|
|
*
|
|
* In fact, the main parsing function is `g1m_decode_lang_cg` (they really
|
|
* have the same subheader). TODO
|
|
*
|
|
* @arg handle the libg1m handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_fkey_cg_content(g1m_t *handle, g1m_buffer_t *buffer,
|
|
uint_fast32_t zonesize, uint32_t *pchecksum)
|
|
{
|
|
handle->type = 0x00;
|
|
//handle->type = g1m_type_fkey;
|
|
(void)buffer;
|
|
(void)zonesize;
|
|
(void)pchecksum;
|
|
return (g1m_error_magic);
|
|
}
|