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/fkey.c

158 lines
4.6 KiB
C

/* *****************************************************************************
* decode/fkey.c -- decode a function keys 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>
/* ************************************************************************** */
/* 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);
}