/* ***************************************************************************** * libg1m/internals.h -- the libg1m internals. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * * This is the root of the libg1m internal headers. All source files shall * include this one to include all the headers (so they don't have to do * it themselves). * ************************************************************************** */ #ifndef LIBG1M_INTERNALS_H # define LIBG1M_INTERNALS_H # include # include # include # include # include # include # define FILEBUFFER(F) g1m_filebuffer(F) # define MEMBUFFER(P, SZ) g1m_membuffer(P, SZ) # define LIMBUFFER(BUFFER, SZ) g1m_limbuffer(BUFFER, SZ) /* ************************************************************************** */ /* 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 decoding */ /* ************************************************************************** */ /* read from buffer */ # define READ(TO, SZ) /* normal read */ { \ int READ_err = (*buffer->read)(buffer->cookie, (void*)(TO), (SZ)); \ buffer->_offset += SZ; \ if (READ_err) return (READ_err); } # define FREAD(TO, SZ) /* fail-less read */ \ err = (*buffer->read)(buffer->cookie, (void*)(TO), (SZ)); \ buffer->_offset += SZ; # define GREAD(TO, SZ) /* read with goto fail */ { \ if ((err = (*buffer->read)(buffer->cookie, (void*)(TO), (SZ)))) \ goto fail; \ buffer->_offset += SZ; } /* read from buffer, declare var before */ # define DREAD(NAM, STRUCT) /* D read (I don't remember where D comes from) */ \ struct STRUCT NAM; \ READ(&NAM, sizeof(struct STRUCT)) # define GDREAD(NAM, STRUCT) /* D read with goto fail */ \ struct STRUCT NAM; \ GREAD(&NAM, sizeof(struct STRUCT)) /* read from buffer, declare tab before */ # define TREAD(NAM, TYPE, NUM) /* Tableau (list) read */ \ TYPE NAM[NUM]; \ READ(NAM, sizeof(TYPE)) /* skip */ # define SKIP(SZ) { \ int SKIP_err = g1m_skip(buffer, SZ, NULL); \ if (SKIP_err) return (SKIP_err); } # define GSKIP(SZ) \ err = g1m_skip(buffer, SZ, NULL); \ if (err) goto fail; /* write */ # define WRITE(BUF, SZ) { \ int WRITE_err = (*buffer->write)(buffer->cookie, \ (unsigned char*)(BUF), (SZ)); \ if (WRITE_err) return (WRITE_err); } # define DWRITE(S) WRITE(&(S), sizeof(S)) /* ************************************************************************** */ /* Decoding functions */ /* ************************************************************************** */ /* with expected types */ extern int g1m_decode_std(g1m_t **handle, const char *path, g1m_buffer_t *buffer, struct standard_header*, g1m_type_t expected_types); /* w/o expected types */ int g1m_decode_casemul(g1m_t **handle, g1m_buffer_t *buffer, int big_endian); int g1m_decode_storage(g1m_t **handle, g1m_buffer_t *buffer); int g1m_decode_cas(g1m_t **handle, g1m_buffer_t *buffer); int g1m_decode_grc(g1m_t **handle, g1m_buffer_t *buffer); /* ************************************************************************** */ /* "Std"-specific decoding functions */ /* ************************************************************************** */ # define G1M_STDFUNC(NAME) \ extern int g1m_decode_std_##NAME(g1m_t **handle, g1m_buffer_t *buffer, \ struct standard_header *std); # define G1M_PRIZMFUNC(NAME) \ extern int g1m_decode_std_cg_##NAME(g1m_t **handle, g1m_buffer_t *buffer, \ struct standard_header *std, struct standard_subheader *sub, \ struct _prizm_subheader *pzm, uint32_t *check); # define G1M_CPFUNC(NAME) \ extern int g1m_decode_std_cp_##NAME(g1m_t **handle, g1m_buffer_t *buffer, \ struct standard_header *std, struct standard_subheader *sub, \ struct _classpad_subheader *cp, uint32_t *check); # define G1M_PICFUNC(NAME) \ extern int g1m_decode_std_##NAME(g1m_t **handle, g1m_buffer_t *buffer, \ struct standard_header *std, struct standard_picheader *pic); /* standard funcs */ G1M_STDFUNC(mcs) G1M_STDFUNC(eact) G1M_STDFUNC(addin) G1M_STDFUNC(lang) G1M_STDFUNC(fkey) /* specific funcs */ G1M_CPFUNC(addin) G1M_PRIZMFUNC(addin) G1M_PRIZMFUNC(lang) G1M_PRIZMFUNC(fkey) G1M_PICFUNC(g3p) G1M_PICFUNC(c2p) /* ************************************************************************** */ /* MCS-specific decoding functions */ /* ************************************************************************** */ # define G1M_MCSFUNC(NAME) \ extern int g1m_decode_mcs_##NAME(g1m_mcsfile_t **handle, g1m_buffer_t *buffer, \ g1m_mcshead_t *head); \ extern int g1m_announce_mcs_##NAME(const g1m_mcsfile_t *handle, size_t *sz); \ extern int g1m_encode_mcs_##NAME(const g1m_mcsfile_t *handle, \ g1m_buffer_t *buffer); G1M_MCSFUNC(var) G1M_MCSFUNC(list) G1M_MCSFUNC(matrix) G1M_MCSFUNC(picture) G1M_MCSFUNC(capture) G1M_MCSFUNC(program) G1M_MCSFUNC(setup) G1M_MCSFUNC(spreadsheet) G1M_MCSFUNC(string) /* ************************************************************************** */ /* CASPRO-specific decoding functions */ /* ************************************************************************** */ # define G1M_CASFUNC(NAME) \ extern int g1m_decode_caspart_##NAME(g1m_mcsfile_t *handle, \ g1m_buffer_t *buffer); # define G1M_CASHFUNC(NAME) \ extern int g1m_decode_cashpart_##NAME(g1m_mcshead_t *head, \ g1m_mcshead_t *heads, g1m_buffer_t *buffer); G1M_CASFUNC(var) G1M_CASFUNC(program) G1M_CASHFUNC(program) G1M_CASFUNC(matrix) G1M_CASFUNC(capture) /* ************************************************************************** */ /* Handle encoding functions */ /* ************************************************************************** */ # define G1M_ENCFUNC(NAME) \ extern int g1m_announce_##NAME(g1m_t *handle, size_t *size); \ extern int g1m_encode_##NAME(g1m_t *handle, g1m_buffer_t *buffer); G1M_ENCFUNC(addin) /* ************************************************************************** */ /* 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 (unsigned int PIXPREP_y = 0; PIXPREP_y < (H); PIXPREP_y++) { \ (I)[PIXPREP_y] = PIXPREP_line; \ PIXPREP_line += (W); \ }} /* ************************************************************************** */ /* Utilities */ /* ************************************************************************** */ /* Free-ing */ extern void g1m_free_line_content(g1m_line_t *line); /* Skipping */ extern int g1m_skip(g1m_buffer_t *buffer, size_t size, uint32_t *checksum); /* Checksum-ing */ extern uint8_t g1m_checksum8(void *mem, size_t size); extern uint32_t g1m_checksum32(void *mem, size_t size, uint32_t checksum); /* Get extension */ extern int g1m_getext(const char *path, char *buf, size_t n); #endif /* LIBG1M_INTERNALS_H */