2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_stream_memory.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __STREAM_IMPL_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_check.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_open(STREAM *stream, const char *path, int mode)
|
|
|
|
{
|
2009-09-17 22:58:27 +02:00
|
|
|
stream->memory.addr = (char *)path;
|
2010-07-14 03:50:18 +02:00
|
|
|
|
|
|
|
if (stream->memory.addr == NULL)
|
|
|
|
{
|
|
|
|
stream->type = NULL;
|
|
|
|
THROW(E_ARG);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
stream->memory.pos = 0;
|
|
|
|
|
2008-11-22 13:58:36 +01:00
|
|
|
stream->common.available_now = TRUE;
|
2019-12-18 06:29:33 +01:00
|
|
|
stream->common.null_terminated = TRUE;
|
2008-11-22 13:58:36 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_close(STREAM *stream)
|
|
|
|
{
|
|
|
|
/*if (munmap(stream->memory.addr, stream->memory.size) != 0)
|
|
|
|
return TRUE;*/
|
|
|
|
|
|
|
|
stream->memory.addr = NULL;
|
|
|
|
//stream->memory.size = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_read(STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2018-08-30 13:33:13 +02:00
|
|
|
/*if ((stream->common.mode & STO_READ) == 0)
|
2009-09-17 22:58:27 +02:00
|
|
|
THROW(E_ACCESS);*/
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
CHECK_enter();
|
|
|
|
|
2011-12-18 04:03:07 +01:00
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
memmove(buffer, stream->memory.addr + stream->memory.pos, len);
|
|
|
|
|
|
|
|
CHECK_leave();
|
|
|
|
|
|
|
|
if (CHECK_got_error())
|
|
|
|
{
|
|
|
|
errno = EIO;
|
2017-09-27 02:48:00 +02:00
|
|
|
return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream->memory.pos += len;
|
2017-09-27 02:48:00 +02:00
|
|
|
return len;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_write(STREAM *stream, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2018-08-30 13:33:13 +02:00
|
|
|
if ((stream->common.mode & STO_WRITE) == 0)
|
2009-09-17 22:58:27 +02:00
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
CHECK_enter();
|
|
|
|
|
2011-12-18 04:03:07 +01:00
|
|
|
if (sigsetjmp(CHECK_jump, TRUE) == 0)
|
2007-12-30 17:41:49 +01:00
|
|
|
memmove(stream->memory.addr + stream->memory.pos, buffer, len);
|
|
|
|
|
|
|
|
CHECK_leave();
|
|
|
|
|
|
|
|
if (CHECK_got_error())
|
|
|
|
{
|
|
|
|
errno = EIO;
|
2017-09-27 02:48:00 +02:00
|
|
|
return -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream->memory.pos += len;
|
2017-09-27 02:48:00 +02:00
|
|
|
return len;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_seek(STREAM *stream, int64_t pos, int whence)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
int64_t new_pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
switch(whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
2008-01-17 22:39:26 +01:00
|
|
|
new_pos = pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
2008-01-17 22:39:26 +01:00
|
|
|
new_pos = stream->memory.pos + pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pos < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
stream->memory.pos = new_pos;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_tell(STREAM *stream, int64_t *pos)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-09-17 22:58:27 +02:00
|
|
|
*pos = (int64_t)stream->memory.pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_flush(STREAM *stream)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_eof(STREAM *stream)
|
|
|
|
{
|
|
|
|
//return (stream->memory.pos >= stream->memory.size);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static int stream_lof(STREAM *stream, int64_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
//*len = stream->memory.size;
|
|
|
|
//return FALSE;
|
|
|
|
*len = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int stream_handle(STREAM *stream)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECLARE_STREAM(STREAM_memory);
|