Added membuffers and mcsfile mem decoding utility
This commit is contained in:
parent
3f000c4f30
commit
e558ab19d4
|
@ -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,
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 */
|
||||
/* ************************************************************************** */
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
/* *****************************************************************************
|
||||
* utils/membuffer.c -- memory to libg1m buffer utilities.
|
||||
* 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 <string.h>
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
Reference in New Issue