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

158 lines
4.5 KiB
C

/* *****************************************************************************
* decode/std/picture.c -- decode a picture 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 <zlib.h>
/**
* g3p_deobfuscate:
* De-obfuscate the image data.
*
* @arg buf the buffer.
* @arg n the buffer size.
*/
static void g3p_deobfuscate(uint8_t *buf, size_t n)
{
while (n--) {
int byte = *buf;
*buf++ = (byte >> 3) | ((byte & 0x7) << 5);
}
}
/**
* g1m_decode_std_g3p:
* Decode a G3P file.
*
* @arg handle the handle.
* @arg buffer the buffer to read from.
* @arg std the standard header.
* @return the error code (0 if ok).
*/
int g1m_decode_std_g3p(g1m_t **h, g1m_buffer_t *buffer,
struct standard_header *std)
{
/* get the G3P global header */
DREAD(hd, g3p_subheader)
hd.g3p_size = be32toh(hd.g3p_size);
/* check magic */
if (memcmp(hd.magic, "CP", 2)) {
log_fatal("could not read 'CP'.");
return (g1m_error_magic);
}
/* read the image header */
DREAD(ihd, g3p_imageheader)
ihd.df_size = be32toh(ihd.df_size);
ihd.width = be16toh(ihd.width);
ihd.height = be16toh(ihd.height);
ihd.color_depth = be16toh(ihd.color_depth);
ihd.data_size = be32toh(ihd.data_size);
/* check some little things */
int is_obfuscated =
((std->obfuscated + 8) & 0xff) != ((std->filesize & 0xff00) >> 8);
int has_footer = 1; /* might change? */
size_t deflated_image_size = ihd.data_size - has_footer * 0x4;
/* log info */
log_info("Width: %" PRIu16 "px, height: %" PRIu16 "px",
ihd.width, ihd.height);
log_info("Pixel depth: %s",
(ihd.color_depth == g3p_color_4bit) ? "4-bit" : "16-bit");
log_info("Deflated image length: %" PRIuSIZE "o", deflated_image_size);
log_info("Is obfuscated: %s", is_obfuscated ? "yes" : "no");
/* read image */
uint8_t deflated_image[deflated_image_size];
READ(deflated_image, deflated_image_size)
/* read footer and check sum */
if (has_footer) {
DREAD(ft, g3p_imagefooter);
ft.checksum = be32toh(ft.checksum);
uLong adl = adler32(0, deflated_image, deflated_image_size);
if (adl != ft.checksum) {
log_fatal("Incorrect Adler32 checksum!");
log_fatal("Expected %" PRIu32 ", got %lu",
ft.checksum, adl);
return (g1m_error_magic);
}
}
/* unobfuscate if required */
if (is_obfuscated)
g3p_deobfuscate(deflated_image, deflated_image_size);
/* find out length of inflated data */
uLongf inflated_size = 0L; int z_err;
if ((z_err = uncompress(Z_NULL, &inflated_size, deflated_image,
deflated_image_size)) != Z_MEM_ERROR) {
log_fatal("Zlib error: error #%d", z_err);
return (g1m_error_magic);
}
log_info("Inflated image size is %lu", inflated_size);
/* allocate space for it, and uncompress */
uint8_t inflated_image[inflated_size];
if ((z_err = uncompress(inflated_image, &inflated_size, deflated_image,
deflated_image_size))) {
log_fatal("Zlib error: error #%d", z_err);
return (g1m_error_magic);
}
/* allocate picture in handle */
int err = g1m_make_picture(h, ihd.width, ihd.height);
if (err) return (err);
g1m_t *handle = *h;
/* then store it */
g1m_decode_picture(handle->pixels, ihd.color_depth == g3p_color_4bit
? g1m_pictureformat_4bit_code : g1m_pictureformat_16bit,
inflated_image, handle->width, handle->height);
/* no error */
return (0);
}
/**
* g1m_decode_std_c2p:
* Decode Classpad images.
*
* @arg handle the handle.
* @arg buffer the buffer to read from.
* @return the error code (0 if ok).
*/
int g1m_decode_std_c2p(g1m_t **h, g1m_buffer_t *buffer,
struct standard_header *std)
{
(void)std;
(void)h;
(void)buffer;
/* TODO */
log_info("C2P files are not managed yet.");
log_info("Size is %" PRIuSIZE "B", sizeof(struct c2p_subheader));
/* no error */
return (g1m_error_magic);
}