/* ***************************************************************************** * 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 /* ************************************************************************** */ /* 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) { \ int READ_err = (*buffer->read)(buffer->cookie, (void*)(TO), (SZ)); \ if (READ_err) return (READ_err); \ } #define GREAD(TO, SZ) \ if ((err = (*buffer->read)(buffer->cookie, (void*)(TO), (SZ)))) \ 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) { \ int SKIP_err = g1m_skip(buffer, SZ, NULL); \ if (SKIP_err) return (SKIP_err); \ } /* ************************************************************************** */ /* Specific parsing functions */ /* ************************************************************************** */ typedef int (g1m_decode_function)(g1m_t*, g1m_buffer_t*, struct standard_header*); /* types */ g1m_decode_function g1m_decode_g3p; g1m_decode_function g1m_decode_c2p; g1m_decode_function g1m_decode_mcs; g1m_decode_function g1m_decode_eact; g1m_decode_function g1m_decode_addin; g1m_decode_function g1m_decode_addin_cg; g1m_decode_function g1m_decode_lang; g1m_decode_function g1m_decode_lang_cg; g1m_decode_function g1m_decode_fkey; g1m_decode_function g1m_decode_storage; /* others */ int g1m_decode_fkey_cg_content(g1m_t *handle, g1m_buffer_t *buffer, 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_decode(g1m_t *handle, const char *path, g1m_buffer_t *buffer, 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_skip(g1m_buffer_t *buffer, size_t size, uint_fast32_t *checksum); /* Checksum-ing */ uint32_t g1m_checksum32(void *mem, size_t size, uint32_t checksum); /* File buffer */ int g1m_filebuffer_read(void *vcookie, unsigned char *buf, size_t size); #endif /* LIBG1M_INTERNALS_H */