147 lines
5.5 KiB
C
147 lines
5.5 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* libg1m/internals.h |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/12/01 13:33:58 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#ifndef LIBG1M_INTERNALS_H
|
|
# define LIBG1M_INTERNALS_H
|
|
# include <libg1m.h>
|
|
# include <libg1m/formatutils.h>
|
|
# include <libg1m/internals/log.h>
|
|
# include <string.h>
|
|
# include <libg1m/internals/endian.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Macros and platform-specific functions */
|
|
/* ************************************************************************** */
|
|
/* MS-Windows <3 (srsly) */
|
|
# if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) \
|
|
&& !defined(__WINDOWS__)
|
|
# define __WINDOWS__
|
|
# endif
|
|
|
|
/* Macros */
|
|
# ifndef min
|
|
# define min(A, B) ((A) < (B) ? (A) : (B))
|
|
# endif
|
|
# ifndef max
|
|
# define max(A, B) ((A) > (B) ? (A) : (B))
|
|
# endif
|
|
|
|
/* Platform-specific functions */
|
|
# ifdef __WINDOWS__
|
|
# define bzero(B, LEN) ((void)memset((B), 0, (LEN)))
|
|
# endif
|
|
|
|
/* ************************************************************************** */
|
|
/* Macros and functions for parsing */
|
|
/* ************************************************************************** */
|
|
/* read with EOF check */
|
|
#define READ(TO, SZ) { \
|
|
size_t READ_size = fread(TO, 1, SZ, stream); \
|
|
if (!READ_size) { \
|
|
log_error("READING failed: read %" PRIuSIZE "/%" PRIuSIZE \
|
|
" bytes, %" PRIuSIZE " missing.", READ_size, SZ, \
|
|
(SZ) - READ_size); \
|
|
return (g1m_error_eof); \
|
|
}}
|
|
#define GREAD(TO, SZ) { \
|
|
size_t READ_size = fread(TO, 1, SZ, stream); \
|
|
if (!READ_size) { \
|
|
log_error("READING failed: read %" PRIuSIZE "/%" PRIuSIZE \
|
|
" bytes, %" PRIuSIZE " missing.", READ_size, SZ, \
|
|
(SZ) - READ_size); \
|
|
err = g1m_error_eof; \
|
|
goto fail; \
|
|
}}
|
|
/* read with EOF check, declare var before */
|
|
#define DREAD(NAM, STRUCT) \
|
|
struct STRUCT NAM; \
|
|
READ(&NAM, sizeof(struct STRUCT))
|
|
#define GDREAD(NAM, STRUCT) \
|
|
struct STRUCT NAM; \
|
|
GREAD(&NAM, sizeof(struct STRUCT))
|
|
/* skip */
|
|
#define SKIP(SZ) \
|
|
if (g1m_parse_skip(stream, SZ, NULL)) return (g1m_error_eof);
|
|
|
|
/* ************************************************************************** */
|
|
/* Specific parsing functions */
|
|
/* ************************************************************************** */
|
|
/* types */
|
|
int g1m_parse_g3p(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_c2p(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_mcs(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_eact(g1m_t * handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_addin(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_addin_cg(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_lang(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_lang_cg(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_fkey(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
int g1m_parse_storage(g1m_t *handle, FILE *stream,
|
|
struct standard_header *std);
|
|
|
|
/* others */
|
|
int g1m_parse_fkey_cg_content(g1m_t *handle, FILE *stream,
|
|
uint_fast32_t zonesize, uint32_t *pchecksum);
|
|
|
|
/* ************************************************************************** */
|
|
/* Picture utilities */
|
|
/* ************************************************************************** */
|
|
# define alloc_pixels(W, H) \
|
|
malloc(sizeof(uint32_t*) * (H) + sizeof(uint32_t) * (W) * (H))
|
|
# define prepare_pixels(I, W, H) { \
|
|
uint32_t *PIXPREP_line = (uint32_t*)&(I)[(H)]; \
|
|
for (int PIXPREP_y = 0; PIXPREP_y < (H); PIXPREP_y++) { \
|
|
(I)[PIXPREP_y] = PIXPREP_line; \
|
|
PIXPREP_line += (W); \
|
|
}}
|
|
|
|
/* just wanted to do this macro for fun. */
|
|
# define G1M_PROTOTYPE_PIX(NATURE) \
|
|
void g1m_pixels_from_##NATURE(uint32_t **pixels, unsigned char *raw, \
|
|
int width, int height); \
|
|
void g1m_pixels_to_##NATURE(unsigned char *dest, uint32_t **pixels, \
|
|
int width, int height);
|
|
|
|
G1M_PROTOTYPE_PIX(packed1bit)
|
|
G1M_PROTOTYPE_PIX(packed4bits)
|
|
G1M_PROTOTYPE_PIX(16bits)
|
|
|
|
/* ************************************************************************** */
|
|
/* Utilities */
|
|
/* ************************************************************************** */
|
|
/* Parsing */
|
|
int g1m_parse(g1m_t *handle, const char *path, FILE *stream,
|
|
g1m_type_t expected_type);
|
|
|
|
/* Making */
|
|
int g1m_make_mcs(g1m_t **h);
|
|
|
|
/* Free-ing */
|
|
void g1m_free_content(g1m_t *handle);
|
|
void g1m_free_mcsfile_content(g1m_mcsfile_t *handle);
|
|
void g1m_free_mcs(g1m_t *handle);
|
|
void g1m_free_line_content(g1m_line_t *line);
|
|
|
|
/* Skipping */
|
|
int g1m_parse_skip(FILE *stream, size_t size, uint_fast32_t *checksum);
|
|
|
|
/* Checksum-ing */
|
|
uint32_t g1m_checksum32(void *mem, size_t size, uint32_t checksum);
|
|
|
|
#endif /* LIBG1M_INTERNALS_H */
|