diff --git a/include/libg1m.h b/include/libg1m.h index b080d0d..75228b6 100644 --- a/include/libg1m.h +++ b/include/libg1m.h @@ -92,6 +92,8 @@ int g1m_decode_mcsfile_head(g1m_mcshead_t *head, uint_fast32_t filesize); int g1m_decode_mcsfile(g1m_mcsfile_t **handle, const g1m_mcshead_t *head, g1m_buffer_t *buffer); +int g1m_decode_mcsfile_data(g1m_mcsfile_t **handle, + const g1m_mcshead_t *head, const unsigned char *data, size_t size); /* open CAS head/file */ int g1m_decode_casfile_head(g1m_mcshead_t *head, diff --git a/include/libg1m/internals.h b/include/libg1m/internals.h index 53f5e89..92a7664 100644 --- a/include/libg1m/internals.h +++ b/include/libg1m/internals.h @@ -145,4 +145,7 @@ 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); +/* Callbacks */ +int g1m_membuffer_read(void *cookie, unsigned char *dest, size_t size); + #endif /* LIBG1M_INTERNALS_H */ diff --git a/include/libg1m/mcs.h b/include/libg1m/mcs.h index dffe410..b430680 100644 --- a/include/libg1m/mcs.h +++ b/include/libg1m/mcs.h @@ -66,11 +66,15 @@ typedef struct g1m_mcs_cell_s { /* ************************************************************************** */ /* Main structures */ /* ************************************************************************** */ +/* mcs file head flags */ +# define g1m_mcshead_complex 0x0001 /* is a complex variable */ + /* mcs file head */ typedef struct g1m_mcshead_s { /* file main information */ g1m_mcstype_t type; char name[13]; int id; + unsigned int flags; /* content size */ uint_fast32_t size; diff --git a/src/decode/mcs.c b/src/decode/mcs.c index c4fc5a5..3ff806c 100644 --- a/src/decode/mcs.c +++ b/src/decode/mcs.c @@ -686,7 +686,9 @@ int g1m_decode_mcsfile_head(g1m_mcshead_t *head, /* save raw data */ head->_rawtype = raw_type; memcpy(head->_group, groupname, 16); head->_group[16] = 0; - memcpy(head->_dirname, dirname, 8); head->_dirname[8] = 0; + if (dirname) { + memcpy(head->_dirname, dirname, 8); head->_dirname[8] = 0; + } else memset(head->_dirname, 0, 9); /* everything went well! */ return (0); @@ -760,6 +762,29 @@ fail: return (err); } +/** + * g1m_decode_mcsfile_data: + * Decode a MCS file content from raw memory. + * + * @arg handle the file handle. + * @arg head the file head. + * @arg data the buffer data. + * @arg size the buffer size. + * @return the libg1m error. + */ + +int g1m_decode_mcsfile_data(g1m_mcsfile_t **handle, + const g1m_mcshead_t *head, const unsigned char *data, size_t size) +{ + unsigned char *p = data; + g1m_buffer_t buffer = { + .cookie = p, + .read = g1m_membuffer_read + }; + + return (g1m_decode_mcsfile(handle, head, &buffer)); +} + /* ************************************************************************** */ /* Main file function */ /* ************************************************************************** */ diff --git a/src/utils/membuffer.c b/src/utils/membuffer.c new file mode 100644 index 0000000..4a941d9 --- /dev/null +++ b/src/utils/membuffer.c @@ -0,0 +1,38 @@ +/* ***************************************************************************** + * utils/membuffer.c -- memory to libg1m buffer utilities. + * 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 . + * ************************************************************************** */ +#include +#include + +/** + * g1m_membuffer_read: + * Read from a memory buffer. + * + * @arg vcookie the cursor (uncasted) + * @arg dest the destination buffer. + * @arg size the size to read. + * @return the error, if any. + */ + +int g1m_membuffer_read(void *vcookie, unsigned char *dest, size_t size) +{ + const unsigned char **p = (void*)vcookie; + memcpy(dest, *p, size); + *p += size; + return (0); +}