2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_stream.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
|
|
|
|
2009-08-17 12:41:51 +02: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
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
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.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
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 __GBX_STREAM_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_common_swap.h"
|
|
|
|
#include "gb_common_check.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_string.h"
|
2008-10-30 01:54:18 +01:00
|
|
|
#include "gbx_watch.h"
|
2010-09-27 21:34:27 +02:00
|
|
|
#include "gbx_c_array.h"
|
|
|
|
#include "gbx_c_collection.h"
|
2010-11-26 21:30:58 +01:00
|
|
|
#include "gbx_struct.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_stream.h"
|
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
static STREAM _temp_stream = { 0 };
|
|
|
|
static STREAM *_temp_save = NULL;
|
|
|
|
static int _temp_level;
|
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
static unsigned char _tag = 0;
|
|
|
|
static int _nopen = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void STREAM_exit(void)
|
|
|
|
{
|
2010-11-27 01:06:43 +01:00
|
|
|
#if DEBUG_STREAM
|
2008-12-31 16:55:40 +01:00
|
|
|
if (_nopen)
|
2010-08-28 14:33:05 +02:00
|
|
|
ERROR_warning("%d streams yet opened", _nopen);
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif
|
2010-11-27 01:06:43 +01:00
|
|
|
STREAM_close(&_temp_stream);
|
|
|
|
}
|
2008-12-31 16:55:40 +01:00
|
|
|
|
2015-10-31 02:49:40 +01:00
|
|
|
static void wait_for_fd_ready_to_read(int fd)
|
2013-05-01 15:02:38 +02:00
|
|
|
{
|
|
|
|
if (fd >= 0)
|
|
|
|
WATCH_process(fd, -1, 0);
|
|
|
|
}
|
2010-04-07 20:39:45 +02:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool STREAM_in_archive(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE *arch;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
ARCHIVE_get_current(&arch);
|
|
|
|
if (arch->name || EXEC_arch) // || !FILE_exist_real(path)) - Why that test ?
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-08-25 17:13:39 +02:00
|
|
|
int STREAM_get_readable(STREAM *stream, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-03-11 14:29:47 +01:00
|
|
|
int fd;
|
2008-09-22 01:22:07 +02:00
|
|
|
off_t off;
|
|
|
|
off_t end;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
fd = STREAM_handle(stream);
|
2013-01-06 17:13:31 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
//_IOCTL:
|
|
|
|
|
2008-03-21 01:55:43 +01:00
|
|
|
#ifdef FIONREAD
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (!stream->common.no_fionread)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-03-06 04:17:47 +01:00
|
|
|
if (ioctl(fd, FIONREAD, len) >= 0)
|
2008-03-11 14:29:47 +01:00
|
|
|
return 0;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
stream->common.no_fionread = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-21 01:55:43 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//_LSEEK:
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (!stream->common.no_lseek)
|
|
|
|
{
|
|
|
|
off = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (off >= 0)
|
|
|
|
{
|
|
|
|
end = lseek(fd, 0, SEEK_END);
|
|
|
|
if (end >= 0)
|
|
|
|
{
|
|
|
|
*len = (int)(end - off);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
off = lseek(fd, off, SEEK_SET);
|
|
|
|
if (off >= 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
stream->common.no_lseek = TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "STREAM_get_readable: lseek: %d\n", *len);
|
2008-03-11 14:29:47 +01:00
|
|
|
return (-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 23:57:30 +02:00
|
|
|
bool STREAM_default_eof(STREAM *stream)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int fd;
|
|
|
|
int ilen;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
2010-05-10 13:20:47 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return TRUE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-05-14 23:17:04 +02:00
|
|
|
if (STREAM_is_blocking(stream) && !stream->common.available_now)
|
2010-05-08 22:17:29 +02:00
|
|
|
wait_for_fd_ready_to_read(STREAM_handle(stream));
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-05-10 13:20:47 +02:00
|
|
|
if (STREAM_get_readable(stream, &ilen))
|
2008-09-22 01:22:07 +02:00
|
|
|
return TRUE;
|
2008-01-27 15:00:04 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return (ilen == 0);
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-02-05 15:53:16 +01:00
|
|
|
// STREAM_open *MUST* initialize completely the stream structure
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_open(STREAM *stream, const char *path, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-09-18 17:36:30 +02:00
|
|
|
STREAM_CLASS *sclass;
|
2014-11-23 19:19:57 +01:00
|
|
|
int fd;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-18 17:36:30 +02:00
|
|
|
stream->type = NULL;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
if (mode & STO_PIPE)
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_pipe;
|
2018-08-30 13:33:13 +02:00
|
|
|
else if (mode & STO_MEMORY)
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_memory;
|
2018-08-30 13:33:13 +02:00
|
|
|
else if (mode & STO_STRING)
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_string;
|
2018-08-30 13:33:13 +02:00
|
|
|
else if (mode & STO_LOCK)
|
2017-12-18 13:02:03 +01:00
|
|
|
sclass = &STREAM_lock;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
2011-09-18 17:21:11 +02:00
|
|
|
// ".99" is used for opening a file descriptor in direct mode
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
if (FILE_is_relative(path) && !((mode & STO_DIRECT) && path[0] == '.' && isdigit(path[1])))
|
2009-09-17 22:58:27 +02:00
|
|
|
{
|
|
|
|
ARCHIVE *arch = NULL;
|
2015-09-13 01:21:44 +02:00
|
|
|
const char *tpath = path;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
/*ARCHIVE *arch = NULL;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2009-09-17 22:58:27 +02:00
|
|
|
if (strncmp(path, "../", 3))
|
|
|
|
ARCHIVE_get_current(&arch);
|
|
|
|
else if (!EXEC_arch)
|
|
|
|
path += 3;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2009-09-17 22:58:27 +02:00
|
|
|
if ((arch && arch->name) || EXEC_arch) // || !FILE_exist_real(path)) - Why that test ?
|
|
|
|
{
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_arch;
|
2009-09-17 22:58:27 +02:00
|
|
|
goto _OPEN;
|
2015-09-13 01:21:44 +02:00
|
|
|
}*/
|
2009-09-17 22:58:27 +02:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
if ((mode & STO_ACCESS) != STO_READ || mode & STO_PIPE)
|
2009-09-17 22:58:27 +02:00
|
|
|
THROW(E_ACCESS);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
if (!ARCHIVE_find_from_path(&arch, &tpath))
|
|
|
|
{
|
|
|
|
sclass = &STREAM_arch;
|
|
|
|
goto __OPEN;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
path = tpath;
|
2009-09-17 22:58:27 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
if (mode & STO_DIRECT)
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_direct;
|
2008-09-22 01:22:07 +02:00
|
|
|
else
|
2011-09-18 17:36:30 +02:00
|
|
|
sclass = &STREAM_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
__OPEN:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->common.mode = mode;
|
|
|
|
stream->common.swap = FALSE;
|
|
|
|
stream->common.eol = 0;
|
2010-03-31 01:50:34 +02:00
|
|
|
stream->common.eof = FALSE;
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->common.buffer = NULL;
|
|
|
|
stream->common.buffer_pos = 0;
|
|
|
|
stream->common.buffer_len = 0;
|
|
|
|
stream->common.no_fionread = FALSE;
|
|
|
|
stream->common.no_lseek = FALSE;
|
2009-02-05 15:53:16 +01:00
|
|
|
stream->common.standard = FALSE;
|
2010-03-31 01:50:34 +02:00
|
|
|
stream->common.blocking = TRUE;
|
2010-05-08 22:17:29 +02:00
|
|
|
stream->common.available_now = FALSE;
|
2011-04-04 02:40:32 +02:00
|
|
|
stream->common.redirected = FALSE;
|
|
|
|
stream->common.redirect = NULL;
|
2018-02-16 02:39:30 +01:00
|
|
|
stream->common.no_read_ahead = FALSE;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2011-09-18 17:36:30 +02:00
|
|
|
if ((*(sclass->open))(stream, path, mode, NULL))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW_SYSTEM(errno, path);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-09-18 17:36:30 +02:00
|
|
|
stream->type = sclass;
|
2014-11-23 19:19:57 +01:00
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
if (fd >= 0)
|
|
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | O_CLOEXEC);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
#if DEBUG_STREAM
|
2009-02-04 23:51:20 +01:00
|
|
|
_tag++;
|
2008-12-31 16:55:40 +01:00
|
|
|
stream->common.tag = _tag;
|
|
|
|
_nopen++;
|
2009-02-04 23:51:20 +01:00
|
|
|
fprintf(stderr, "Open %p [%d] (%d)\n", stream, _tag, _nopen);
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:39:38 +02:00
|
|
|
static void release_buffer(STREAM *stream)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer)
|
|
|
|
{
|
2009-02-04 23:51:20 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
fprintf(stderr, "Stream %p [%d]: Free buffer\n", stream, stream->common.tag);
|
|
|
|
#endif
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&stream->common.buffer);
|
2008-09-22 01:22:07 +02:00
|
|
|
stream->common.buffer_pos = 0;
|
|
|
|
stream->common.buffer_len = 0;
|
|
|
|
}
|
2015-07-09 16:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void STREAM_release(STREAM *stream)
|
|
|
|
{
|
|
|
|
release_buffer(stream);
|
2011-04-04 02:40:32 +02:00
|
|
|
STREAM_cancel(stream);
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2013-08-19 23:45:13 +02:00
|
|
|
static void stop_watching(STREAM *stream, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-08-19 23:45:13 +02:00
|
|
|
int fd = STREAM_handle(stream);
|
|
|
|
if (fd >= 0)
|
|
|
|
GB_Watch(fd, mode, NULL, 0);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-19 23:45:13 +02:00
|
|
|
void STREAM_close(STREAM *stream)
|
|
|
|
{
|
2009-02-04 23:51:20 +01:00
|
|
|
STREAM_release(stream);
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-19 23:45:13 +02:00
|
|
|
stop_watching(stream, GB_WATCH_NONE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-03-08 21:04:55 +01:00
|
|
|
if (!stream->common.standard)
|
|
|
|
{
|
|
|
|
if ((*(stream->type->close))(stream))
|
2016-03-08 21:24:24 +01:00
|
|
|
{
|
2016-03-23 03:58:13 +01:00
|
|
|
if (errno != EBADF && errno != EINPROGRESS && errno != EAGAIN)
|
2016-03-08 21:24:24 +01:00
|
|
|
THROW_SYSTEM(errno, "");
|
|
|
|
}
|
2016-03-08 21:04:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->type = NULL;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
_nopen--;
|
2009-02-04 23:51:20 +01:00
|
|
|
fprintf(stderr, "Close %p [%d] (%d)\n", stream, stream->common.tag, _nopen);
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_flush(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-07-06 21:00:41 +02:00
|
|
|
STREAM_end(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
(*(stream->type->flush))(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
static int read_buffer(STREAM *stream, void *addr, int len)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
|
|
|
int l = stream->common.buffer_len - stream->common.buffer_pos;
|
2017-09-27 02:48:00 +02:00
|
|
|
if (l > len)
|
|
|
|
l = len;
|
2008-01-27 15:00:04 +01:00
|
|
|
if (l > 0)
|
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
memcpy(addr, stream->common.buffer + stream->common.buffer_pos, l);
|
2008-01-27 15:00:04 +01:00
|
|
|
stream->common.buffer_pos += l;
|
2017-09-27 02:48:00 +02:00
|
|
|
return l;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
int STREAM_read(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
int eff = 0;
|
|
|
|
int n;
|
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (len <= 0)
|
2017-09-27 02:48:00 +02:00
|
|
|
return 0;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer)
|
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
eff = read_buffer(stream, addr, len);
|
|
|
|
addr += eff;
|
|
|
|
len -= eff;
|
|
|
|
}
|
2013-08-19 23:45:13 +02:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
n = (*(stream->type->read))(stream, addr, len);
|
|
|
|
|
|
|
|
if ((n <= 0) && errno != EINTR)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
stop_watching(stream, GB_WATCH_READ);
|
|
|
|
|
|
|
|
if (n == 0)
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_EOF);
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
|
|
|
THROW(E_EOF);
|
|
|
|
case EIO:
|
|
|
|
THROW(E_READ);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
eff += n;
|
|
|
|
addr += n;
|
|
|
|
len -= n;
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
return eff;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 16:29:10 +01:00
|
|
|
#if 0
|
2008-01-17 22:39:26 +01:00
|
|
|
char STREAM_getchar(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
char c = 0;
|
|
|
|
bool ret;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len)
|
|
|
|
return stream->common.buffer[stream->common.buffer_pos++];
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (stream->type->getchar)
|
|
|
|
ret = (*(stream->type->getchar))(stream, &c);
|
|
|
|
else
|
|
|
|
ret = (*(stream->type->read))(stream, &c, 1);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
if (ret == 1)
|
2008-11-24 13:09:40 +01:00
|
|
|
break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2013-08-19 23:45:13 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
stop_watching(stream, GB_WATCH_READ);
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case EAGAIN:
|
|
|
|
THROW(E_EOF);
|
|
|
|
case EIO:
|
|
|
|
THROW(E_READ);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return c;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2014-11-29 16:29:10 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2010-04-05 19:02:16 +02:00
|
|
|
int STREAM_read_max(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
int eff = 0;
|
|
|
|
int n;
|
2008-09-22 01:22:07 +02:00
|
|
|
int flags, handle;
|
2010-04-06 19:00:25 +02:00
|
|
|
int save_errno;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if (!stream->type)
|
|
|
|
THROW(E_CLOSED);
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return 0;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.buffer)
|
2017-09-27 02:48:00 +02:00
|
|
|
{
|
|
|
|
eff += read_buffer(stream, addr, len);
|
|
|
|
addr += eff;
|
|
|
|
len -= eff;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
while (len > 0)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
if (stream->common.available_now)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
n = (*(stream->type->read))(stream, addr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handle = STREAM_handle(stream);
|
|
|
|
flags = fcntl(handle, F_GETFL);
|
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
2008-11-24 13:09:40 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
wait_for_fd_ready_to_read(handle);
|
|
|
|
fcntl(handle, F_SETFL, flags | O_NONBLOCK);
|
2008-11-24 13:09:40 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
errno = 0;
|
|
|
|
n = (*(stream->type->read))(stream, addr, len);
|
|
|
|
save_errno = errno;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
fcntl(handle, F_SETFL, flags);
|
2013-08-19 23:45:13 +02:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
errno = save_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
2017-09-27 14:15:23 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
eff += n;
|
2017-09-27 14:15:23 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-08-19 23:45:13 +02:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
if (n <= 0 && errno != EINTR)
|
|
|
|
{
|
|
|
|
if (n == 0 || errno == 0)
|
|
|
|
{
|
2013-08-29 22:21:20 +02:00
|
|
|
stop_watching(stream, GB_WATCH_READ);
|
2017-09-27 02:48:00 +02:00
|
|
|
return eff;
|
|
|
|
}
|
|
|
|
|
2008-10-21 15:03:03 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
2017-10-10 18:49:15 +02:00
|
|
|
case EIO:
|
2017-09-27 02:48:00 +02:00
|
|
|
return eff;
|
2008-10-21 15:03:03 +02:00
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
addr += n;
|
|
|
|
len -= n;
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
return eff;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write(STREAM *stream, void *addr, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
int n;
|
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed_for_writing(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
if (len <= 0)
|
|
|
|
return;
|
2018-02-20 23:00:17 +01:00
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
if (stream->common.redirected)
|
|
|
|
stream = stream->common.redirect;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2018-02-23 04:01:56 +01:00
|
|
|
do
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
n = ((*(stream->type->write))(stream, addr, len));
|
|
|
|
|
|
|
|
if (n <= 0 && errno != EINTR)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case EIO:
|
|
|
|
THROW(E_WRITE);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
addr += n;
|
|
|
|
len -= n;
|
2018-02-23 04:01:56 +01:00
|
|
|
|
|
|
|
if (STREAM_is_closed_for_writing(stream))
|
|
|
|
THROW(E_CLOSED);
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2018-02-23 04:01:56 +01:00
|
|
|
while (len > 0);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
void STREAM_write_zeros(STREAM *stream, int len)
|
|
|
|
{
|
|
|
|
static const char buffer[32] = { 0 };
|
|
|
|
int lenw;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
lenw = Min(len, sizeof(buffer));
|
|
|
|
STREAM_write(stream, (void *)buffer, lenw);
|
|
|
|
len -= lenw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write_eol(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed_for_writing(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
switch(stream->common.eol)
|
|
|
|
{
|
|
|
|
case ST_EOL_UNIX: STREAM_write(stream, "\n", 1); break;
|
|
|
|
case ST_EOL_WINDOWS: STREAM_write(stream, "\r\n", 2); break;
|
|
|
|
case ST_EOL_MAC: STREAM_write(stream, "\r", 1); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int64_t STREAM_tell(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int64_t pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->type->tell(stream, &pos))
|
|
|
|
/*THROW(E_SEEK, ERROR_get());*/
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_seek(STREAM *stream, int64_t pos, int whence)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->type->seek(stream, pos, whence))
|
|
|
|
{
|
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
THROW(E_ARG);
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2015-07-09 16:39:38 +02:00
|
|
|
release_buffer(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
static int fill_buffer(STREAM *stream, char *addr, bool do_not_wait_ready)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
int n;
|
2008-10-29 19:26:56 +01:00
|
|
|
int flags, fd;
|
2017-09-27 02:48:00 +02:00
|
|
|
int len;
|
|
|
|
int eff = 0;
|
2008-10-29 19:26:56 +01:00
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
2017-09-27 02:48:00 +02:00
|
|
|
len = STREAM_BUFFER_SIZE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
while (len > 0)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2008-11-24 13:09:40 +01:00
|
|
|
if (stream->common.available_now)
|
2017-09-27 02:48:00 +02:00
|
|
|
n = (*(stream->type->read))(stream, addr, len);
|
2008-11-24 13:09:40 +01:00
|
|
|
else
|
|
|
|
{
|
2015-03-19 14:50:35 +01:00
|
|
|
if (!do_not_wait_ready)
|
2017-09-27 14:15:23 +02:00
|
|
|
{
|
2015-03-19 14:50:35 +01:00
|
|
|
wait_for_fd_ready_to_read(fd);
|
2017-09-27 14:15:23 +02:00
|
|
|
do_not_wait_ready = TRUE;
|
|
|
|
}
|
2010-04-07 20:39:45 +02:00
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
n = (*(stream->type->read))(stream, addr, len);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-11-24 13:09:40 +01:00
|
|
|
if ((flags & O_NONBLOCK) == 0)
|
2013-08-30 09:12:36 +02:00
|
|
|
{
|
|
|
|
int save_errno = errno;
|
2008-11-24 13:09:40 +01:00
|
|
|
fcntl(fd, F_SETFL, flags);
|
2013-08-30 09:12:36 +02:00
|
|
|
errno = save_errno;
|
|
|
|
}
|
2008-11-24 13:09:40 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
if (n == 0)
|
|
|
|
return eff;
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
eff += n;
|
|
|
|
|
|
|
|
if (n < 0 && errno != EINTR)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
case EINPROGRESS:
|
|
|
|
case EIO:
|
|
|
|
return eff;
|
|
|
|
default:
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
}
|
2008-10-29 19:26:56 +01:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
addr += n;
|
|
|
|
len -= n;
|
2008-10-29 19:26:56 +01:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
|
|
|
|
return eff;
|
2008-10-29 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
bool STREAM_read_ahead(STREAM *stream)
|
|
|
|
{
|
2017-09-27 02:48:00 +02:00
|
|
|
int eff;
|
|
|
|
|
2015-03-20 00:23:47 +01:00
|
|
|
if (stream->common.no_read_ahead)
|
|
|
|
return FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2014-11-30 23:41:41 +01:00
|
|
|
if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!stream->common.buffer)
|
|
|
|
ALLOC(&stream->common.buffer, STREAM_BUFFER_SIZE);
|
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
eff = fill_buffer(stream, stream->common.buffer, TRUE);
|
2014-11-30 23:41:41 +01:00
|
|
|
|
|
|
|
stream->common.buffer_pos = 0;
|
2017-09-27 02:48:00 +02:00
|
|
|
stream->common.buffer_len = eff;
|
2014-12-12 00:41:48 +01:00
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
if (eff == 0)
|
2014-11-30 23:41:41 +01:00
|
|
|
{
|
|
|
|
stream->common.eof = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
2015-01-02 03:22:22 +01:00
|
|
|
{
|
|
|
|
stream->common.eof = FALSE;
|
2014-11-30 23:41:41 +01:00
|
|
|
return FALSE;
|
2015-01-02 03:22:22 +01:00
|
|
|
}
|
2014-11-30 23:41:41 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
static char *input(STREAM *stream, bool line, char *escape)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
unsigned char mode;
|
2008-09-09 13:03:47 +02:00
|
|
|
int len = 0;
|
|
|
|
int start;
|
2015-11-02 04:24:44 +01:00
|
|
|
unsigned char c = 0, lc = 0;
|
|
|
|
void *test;
|
2008-09-09 13:03:47 +02:00
|
|
|
char *buffer;
|
2010-06-09 01:08:04 +02:00
|
|
|
int buffer_len;
|
|
|
|
int buffer_pos;
|
|
|
|
char *addr;
|
2015-11-02 04:24:44 +01:00
|
|
|
char ec;
|
|
|
|
bool inside_escape = FALSE;
|
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
addr = NULL;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
mode = 2;
|
|
|
|
ec = escape ? *escape : 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!line)
|
2018-08-30 13:33:13 +02:00
|
|
|
test = &&__TESTO_INPUT;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(stream->common.eol)
|
|
|
|
{
|
2018-08-30 13:33:13 +02:00
|
|
|
case GB_EOL_WINDOWS: test = &&__TESTO_WINDOWS; break;
|
|
|
|
case GB_EOL_MAC: test = &&__TESTO_MAC; break;
|
2015-11-02 04:24:44 +01:00
|
|
|
default: test = NULL; mode = ec ? 1 : 0; break;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
stream->common.eof = FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (!STREAM_is_blocking(stream) && STREAM_eof(stream))
|
2008-01-27 15:00:04 +01:00
|
|
|
THROW(E_EOF);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
buffer = stream->common.buffer;
|
|
|
|
buffer_len = stream->common.buffer_len;
|
|
|
|
buffer_pos = stream->common.buffer_pos;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
if (!buffer)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2009-02-04 23:51:20 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
fprintf(stderr, "Stream %p [%d]: Alloc buffer\n", stream, stream->common.tag);
|
|
|
|
#endif
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&buffer, STREAM_BUFFER_SIZE);
|
2008-03-11 14:29:47 +01:00
|
|
|
buffer_pos = 0;
|
|
|
|
buffer_len = 0;
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2008-03-11 14:29:47 +01:00
|
|
|
start = buffer_pos;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
if (mode == 0)
|
2008-09-09 13:03:47 +02:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
while (buffer_pos < buffer_len)
|
2008-01-27 15:00:04 +01:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
c = buffer[buffer_pos++]; //STREAM_getchar(stream);
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
len = buffer_pos - start - 1;
|
|
|
|
if (buffer_pos)
|
|
|
|
lc = buffer[buffer_pos - 1];
|
|
|
|
if (lc == '\r')
|
|
|
|
len--;
|
|
|
|
goto __FINISH;
|
|
|
|
}
|
2008-01-27 15:00:04 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
len = buffer_len - start;
|
|
|
|
}
|
|
|
|
else if (mode == 1)
|
|
|
|
{
|
|
|
|
while (buffer_pos < buffer_len)
|
2008-09-09 13:03:47 +02:00
|
|
|
{
|
2015-11-02 04:24:44 +01:00
|
|
|
c = buffer[buffer_pos++]; //STREAM_getchar(stream);
|
|
|
|
|
|
|
|
if (c == ec)
|
|
|
|
inside_escape = !inside_escape;
|
|
|
|
else if (!inside_escape)
|
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
len = buffer_pos - start - 1;
|
|
|
|
if (buffer_pos)
|
|
|
|
lc = buffer[buffer_pos - 1];
|
|
|
|
if (lc == '\r')
|
|
|
|
len--;
|
|
|
|
goto __FINISH;
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 13:03:47 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
len = buffer_pos - start;
|
2011-02-16 23:01:35 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (buffer_pos < buffer_len)
|
|
|
|
{
|
|
|
|
c = buffer[buffer_pos++]; //STREAM_getchar(stream);
|
2011-02-16 23:01:35 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
if (ec && c == ec)
|
|
|
|
{
|
|
|
|
inside_escape = !inside_escape;
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
if (inside_escape)
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
goto *test;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
__TESTO_INPUT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
if (c <= ' ')
|
|
|
|
{
|
|
|
|
len = buffer_pos - start - 1;
|
|
|
|
goto __FINISH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
__TESTO_MAC:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
if (c == '\r')
|
|
|
|
{
|
|
|
|
len = buffer_pos - start - 1;
|
|
|
|
goto __FINISH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
__TESTO_WINDOWS:
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if ((lc == '\r') && (c == '\n'))
|
|
|
|
{
|
|
|
|
len = buffer_pos - start - 2;
|
|
|
|
goto __FINISH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lc = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
len = buffer_pos - start;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
lc = c;
|
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
if (!addr)
|
|
|
|
addr = STRING_new(buffer + start, len);
|
|
|
|
else
|
|
|
|
addr = STRING_add(addr, buffer + start, len);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->common.buffer = buffer;
|
|
|
|
stream->common.buffer_pos = buffer_pos;
|
|
|
|
stream->common.buffer_len = buffer_len;
|
|
|
|
|
|
|
|
buffer_pos = 0;
|
2017-09-27 02:48:00 +02:00
|
|
|
buffer_len = fill_buffer(stream, buffer, FALSE);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
|
|
|
if (!buffer_len)
|
|
|
|
{
|
|
|
|
stream->common.eof = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
2015-11-02 04:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
start = 0;
|
2008-09-09 13:03:47 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-02 04:24:44 +01:00
|
|
|
__FINISH:
|
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
if (len > 0)
|
2015-11-02 04:24:44 +01:00
|
|
|
{
|
|
|
|
if (!addr)
|
|
|
|
addr = STRING_new(buffer + start, len);
|
|
|
|
else
|
|
|
|
addr = STRING_add(addr, buffer + start, len);
|
|
|
|
}
|
2014-07-27 23:04:29 +02:00
|
|
|
else if (len < 0)
|
|
|
|
addr = STRING_extend(addr, STRING_length(addr) + len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-09 13:03:47 +02:00
|
|
|
stream->common.buffer = buffer;
|
|
|
|
stream->common.buffer_pos = buffer_pos;
|
|
|
|
stream->common.buffer_len = buffer_len;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
return addr;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
char *STREAM_line_input(STREAM *stream, char *escape)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-02-16 23:01:35 +01:00
|
|
|
return input(stream, TRUE, escape);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-09 01:08:04 +02:00
|
|
|
char *STREAM_input(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-02-16 23:01:35 +01:00
|
|
|
return input(stream, FALSE, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
static int read_length(STREAM *stream)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _data[4];
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
}
|
|
|
|
buffer;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
STREAM_read(stream, buffer._data, 1);
|
|
|
|
|
|
|
|
switch (buffer._data[0] >> 6)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
len = buffer._data[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
STREAM_read(stream, &buffer._data[1], 1);
|
|
|
|
buffer._data[0] &= 0x3F;
|
|
|
|
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
|
|
|
|
len = buffer._short;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
STREAM_read(stream, &buffer._data[1], 3);
|
|
|
|
buffer._data[0] &= 0x3F;
|
|
|
|
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
|
|
|
|
len = buffer._int;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
static STREAM *enter_temp_stream(STREAM *stream)
|
|
|
|
{
|
|
|
|
if (stream != &_temp_stream)
|
|
|
|
{
|
|
|
|
_temp_save = stream;
|
|
|
|
_temp_level = 0;
|
2018-06-25 19:17:44 +02:00
|
|
|
if (_temp_stream.type)
|
2010-11-27 01:06:43 +01:00
|
|
|
STREAM_close(&_temp_stream);
|
2018-08-30 13:33:13 +02:00
|
|
|
STREAM_open(&_temp_stream, NULL, STO_STRING | STO_WRITE);
|
2010-11-27 01:06:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_temp_level++;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
return &_temp_stream;
|
|
|
|
}
|
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
static STREAM *leave_temp_stream(void)
|
2010-11-27 01:06:43 +01:00
|
|
|
{
|
|
|
|
_temp_level--;
|
|
|
|
if (_temp_level > 0)
|
|
|
|
return &_temp_stream;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
STREAM_write(_temp_save, _temp_stream.string.buffer, STRING_length(_temp_stream.string.buffer));
|
|
|
|
STREAM_close(&_temp_stream);
|
|
|
|
return _temp_save;
|
|
|
|
}
|
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
static void read_structure(STREAM *stream, CLASS *class, char *base);
|
|
|
|
|
|
|
|
static void read_value_ctype(STREAM *stream, CLASS *class, CTYPE ctype, void *addr)
|
|
|
|
{
|
|
|
|
TYPE type;
|
|
|
|
VALUE temp;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
type = (TYPE)ctype.id;
|
|
|
|
if (type == T_OBJECT && ctype.value >= 0)
|
|
|
|
{
|
|
|
|
class = class->load->class_ref[ctype.value];
|
|
|
|
if (CLASS_is_struct(class))
|
|
|
|
{
|
|
|
|
CSTRUCT *structure = (CSTRUCT *)OBJECT_create(class, NULL, NULL, 0);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(structure);
|
2010-11-26 21:30:58 +01:00
|
|
|
read_structure(stream, class, (char *)structure + sizeof(CSTRUCT));
|
|
|
|
*((void **)addr) = structure;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == TC_STRUCT)
|
|
|
|
{
|
|
|
|
class = class->load->class_ref[ctype.value];
|
|
|
|
read_structure(stream, class, addr);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_read_type(stream, type, &temp);
|
2010-11-26 21:30:58 +01:00
|
|
|
VALUE_class_write(class, &temp, addr, ctype);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_structure(STREAM *stream, CLASS *class, char *base)
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
CLASS_DESC *desc;
|
|
|
|
char *addr;
|
|
|
|
CTYPE ctype;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
for (n = 0; n < class->n_desc; n++)
|
|
|
|
{
|
|
|
|
desc = class->table[n].desc;
|
|
|
|
ctype = desc->variable.ctype;
|
|
|
|
addr = base + desc->variable.offset;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (ctype.id == TC_STRUCT)
|
|
|
|
{
|
|
|
|
read_structure(stream, class->load->class_ref[ctype.value], addr);
|
|
|
|
}
|
|
|
|
else if (ctype.id == TC_ARRAY)
|
|
|
|
{
|
|
|
|
CLASS_ARRAY *adesc = class->load->array[ctype.value];
|
|
|
|
int size = CLASS_sizeof_ctype(class, adesc->ctype);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
for (i = 0; i < CARRAY_get_static_count(adesc); i++)
|
|
|
|
{
|
|
|
|
read_value_ctype(stream, desc->variable.class, adesc->ctype, addr);
|
|
|
|
addr += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read_value_ctype(stream, desc->variable.class, ctype, addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
void STREAM_read_type(STREAM *stream, TYPE type, VALUE *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-09-27 21:34:27 +02:00
|
|
|
bool variant;
|
2010-12-28 22:58:42 +01:00
|
|
|
int len;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _byte;
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
float _single;
|
|
|
|
unsigned char _data[4];
|
|
|
|
}
|
|
|
|
buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
variant = (type == T_VARIANT);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (variant || TYPE_is_object(type))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-11-26 21:30:58 +01:00
|
|
|
if (TYPE_is_pure_object(type) && CLASS_is_struct((CLASS *)type))
|
|
|
|
{
|
|
|
|
CLASS *class = (CLASS *)type;
|
|
|
|
CSTRUCT *structure = (CSTRUCT *)OBJECT_create(class, NULL, NULL, 0);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
read_structure(stream, class, (char *)structure + sizeof(CSTRUCT));
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
value->_object.class = class;
|
|
|
|
value->_object.object = structure;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-07-24 11:45:44 +02:00
|
|
|
if (buffer._byte == 0)
|
|
|
|
{
|
|
|
|
value->type = T_VARIANT;
|
|
|
|
value->_variant.vtype = T_NULL;
|
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
if (buffer._byte == 'A')
|
|
|
|
{
|
|
|
|
CARRAY *array;
|
|
|
|
int size, i;
|
|
|
|
VALUE temp;
|
|
|
|
void *data;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
type = (TYPE)buffer._byte;
|
2017-03-18 13:14:01 +01:00
|
|
|
if (type > T_OBJECT)
|
|
|
|
THROW(E_SERIAL);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
size = read_length(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
GB_ArrayNew((GB_ARRAY *)&array, type, size);
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
data = CARRAY_get_data(array, i);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_read_type(stream, type, &temp);
|
2010-09-27 21:34:27 +02:00
|
|
|
VALUE_write(&temp, data, type);
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
value->type = T_VARIANT;
|
|
|
|
value->_variant.vtype = (TYPE)OBJECT_class(array);
|
|
|
|
value->_variant.value._object = array;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
if (buffer._byte == 'c' || buffer._byte == 'C')
|
|
|
|
{
|
|
|
|
GB_COLLECTION col;
|
|
|
|
int size, i;
|
|
|
|
VALUE temp;
|
|
|
|
char *key;
|
|
|
|
char tkey[32];
|
|
|
|
int len;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
size = read_length(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
GB_CollectionNew(&col, buffer._byte == 'c');
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
len = read_length(stream);
|
|
|
|
if (len < sizeof(tkey))
|
|
|
|
key = tkey;
|
|
|
|
else
|
|
|
|
key = STRING_new(NULL, len);
|
|
|
|
STREAM_read(stream, key, len);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_read_type(stream, T_VARIANT, &temp);
|
2010-09-27 21:34:27 +02:00
|
|
|
GB_CollectionSet(col, key, len, (GB_VARIANT *)&temp);
|
|
|
|
if (len >= 32)
|
|
|
|
STRING_free_real(key);
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
value->type = T_VARIANT;
|
|
|
|
value->_variant.vtype = (TYPE)OBJECT_class(col);
|
|
|
|
value->_variant.value._object = col;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-28 19:05:10 +02:00
|
|
|
if (variant)
|
|
|
|
type = buffer._byte;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
value->type = type;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
value->_integer.value = (buffer._byte != 0) ? (-1) : 0;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_BYTE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._byte, 1);
|
|
|
|
value->_integer.value = buffer._byte;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_SHORT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._short, sizeof(short));
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
value->_integer.value = buffer._short;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_INTEGER:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_integer.value, sizeof(int));
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_int(&value->_integer.value);
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_LONG:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_long.value, sizeof(int64_t));
|
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int64(&value->_long.value);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-07-29 12:19:10 +02:00
|
|
|
case T_POINTER:
|
|
|
|
|
|
|
|
STREAM_read(stream, &value->_pointer.value, sizeof(void *));
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_pointer(&value->_pointer.value);
|
|
|
|
break;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_SINGLE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
STREAM_read(stream, &buffer._single, sizeof(float));
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_float(&buffer._single);
|
2010-11-16 02:49:18 +01:00
|
|
|
value->_single.value = buffer._single;
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_FLOAT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_float.value, sizeof(double));
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_double(&value->_float.value);
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_DATE:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(stream, &value->_date.date, sizeof(int));
|
|
|
|
STREAM_read(stream, &value->_date.time, sizeof(int));
|
|
|
|
if (stream->common.swap)
|
|
|
|
{
|
|
|
|
SWAP_int(&value->_date.date);
|
|
|
|
SWAP_int(&value->_date.time);
|
|
|
|
}
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_CSTRING:
|
2007-12-30 17:41:49 +01:00
|
|
|
value->type = T_STRING;
|
|
|
|
// continue
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_STRING:
|
|
|
|
|
2013-11-04 16:17:01 +01:00
|
|
|
if (stream->type == &STREAM_memory)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2013-01-06 17:13:31 +01:00
|
|
|
ssize_t slen;
|
2010-12-28 22:58:42 +01:00
|
|
|
if (CHECK_strlen(stream->memory.addr + stream->memory.pos, &slen))
|
|
|
|
THROW(E_READ);
|
|
|
|
len = (int)slen;
|
|
|
|
}
|
|
|
|
else
|
2013-11-04 16:17:01 +01:00
|
|
|
len = read_length(stream);
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(value, NULL, labs(len));
|
|
|
|
STREAM_read(stream, value->_string.addr, len);
|
|
|
|
}
|
2011-01-15 02:04:32 +01:00
|
|
|
else
|
2012-06-17 01:45:57 +02:00
|
|
|
{
|
|
|
|
STRING_void_value(value);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
default:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_TYPE, "Standard type", TYPE_get_name(type));
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
if (variant)
|
|
|
|
VALUE_convert_variant(value);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
static void write_length(STREAM *stream, int len)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _byte;
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
}
|
|
|
|
buffer;
|
|
|
|
|
|
|
|
if (len < 0x80)
|
|
|
|
{
|
|
|
|
buffer._byte = (unsigned char)len;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
|
|
|
}
|
|
|
|
else if (len < 0x4000)
|
|
|
|
{
|
|
|
|
buffer._short = (short)len | 0x8000;
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
STREAM_write(stream, &buffer._short, sizeof(short));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer._int = len | 0xC0000000;
|
|
|
|
if (!EXEC_big_endian)
|
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-12-28 22:58:42 +01:00
|
|
|
void STREAM_write_type(STREAM *stream, TYPE type, VALUE *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-07 18:14:32 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char _byte;
|
|
|
|
short _short;
|
|
|
|
int _int;
|
|
|
|
int64_t _long;
|
|
|
|
float _single;
|
|
|
|
double _float;
|
|
|
|
unsigned char _data[8];
|
2010-07-29 12:19:10 +02:00
|
|
|
void *_pointer;
|
2009-12-07 18:14:32 +01:00
|
|
|
}
|
|
|
|
buffer;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-07-24 11:45:44 +02:00
|
|
|
if (VALUE_is_null(value))
|
|
|
|
{
|
|
|
|
buffer._byte = 0;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (type == T_VARIANT)
|
|
|
|
{
|
|
|
|
VARIANT_undo(value);
|
|
|
|
type = value->type;
|
2010-09-27 21:34:27 +02:00
|
|
|
if (!TYPE_is_object(type))
|
|
|
|
{
|
|
|
|
buffer._byte = (unsigned char)type;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
if (TYPE_is_object(type))
|
|
|
|
type = T_OBJECT;
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = value->_integer.value ? 0xFF : 0;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BYTE:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._byte = (unsigned char)value->_integer.value;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._short = (short)value->_integer.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_short(&buffer._short);
|
|
|
|
STREAM_write(stream, &buffer._short, sizeof(short));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_INTEGER:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_integer.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._long = value->_long.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int64(&buffer._long);
|
|
|
|
STREAM_write(stream, &buffer._long, sizeof(int64_t));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-29 12:19:10 +02:00
|
|
|
case T_POINTER:
|
|
|
|
|
|
|
|
buffer._pointer = value->_pointer.value;
|
|
|
|
if (stream->common.swap)
|
|
|
|
SWAP_pointer(&buffer._pointer);
|
|
|
|
STREAM_write(stream, &buffer._pointer, sizeof(void *));
|
|
|
|
break;
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
case T_SINGLE:
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
buffer._single = value->_single.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_float(&buffer._single);
|
|
|
|
STREAM_write(stream, &buffer._single, sizeof(float));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._float = value->_float.value;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_double(&buffer._float);
|
|
|
|
STREAM_write(stream, &buffer._float, sizeof(double));
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DATE:
|
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_date.date;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
|
2009-12-07 18:14:32 +01:00
|
|
|
buffer._int = value->_date.time;
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->common.swap)
|
2009-12-07 18:14:32 +01:00
|
|
|
SWAP_int(&buffer._int);
|
|
|
|
STREAM_write(stream, &buffer._int, sizeof(int));
|
2008-09-22 01:22:07 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
case T_CSTRING:
|
|
|
|
|
2013-11-04 19:16:26 +01:00
|
|
|
if (stream->type == &STREAM_memory)
|
|
|
|
{
|
|
|
|
STREAM_write(stream, value->_string.addr + value->_string.start, value->_string.len);
|
|
|
|
buffer._byte = 0;
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
write_length(stream, value->_string.len);
|
|
|
|
STREAM_write(stream, value->_string.addr + value->_string.start, value->_string.len);
|
|
|
|
}
|
2008-09-22 01:22:07 +02:00
|
|
|
break;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
case T_OBJECT:
|
|
|
|
{
|
|
|
|
CLASS *class = OBJECT_class(value->_object.object);
|
2010-11-26 21:30:58 +01:00
|
|
|
void *structure;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (class->quick_array == CQA_ARRAY || class->is_array_of_struct)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-09-27 21:34:27 +02:00
|
|
|
CARRAY *array = (CARRAY *)value->_object.object;
|
|
|
|
VALUE temp;
|
|
|
|
void *data;
|
|
|
|
int i;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-10-06 13:29:31 +02:00
|
|
|
if (OBJECT_is_locked((OBJECT *)array))
|
|
|
|
THROW(E_SERIAL);
|
|
|
|
OBJECT_lock((OBJECT *)array, TRUE);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (!array->ref)
|
|
|
|
{
|
|
|
|
buffer._byte = 'A';
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2017-03-18 13:14:01 +01:00
|
|
|
|
|
|
|
if (TYPE_is_object(array->type))
|
|
|
|
buffer._byte = T_OBJECT;
|
|
|
|
else
|
|
|
|
buffer._byte = (unsigned char)array->type;
|
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
write_length(stream, array->count);
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (class->is_array_of_struct)
|
2010-09-27 21:34:27 +02:00
|
|
|
{
|
2010-11-26 21:30:58 +01:00
|
|
|
for (i = 0; i < array->count; i++)
|
|
|
|
{
|
|
|
|
data = CARRAY_get_data(array, i);
|
|
|
|
structure = CSTRUCT_create_static(array, (CLASS *)array->type, data);
|
|
|
|
temp._object.class = OBJECT_class(structure);
|
|
|
|
temp._object.object = structure;
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(structure);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_write_type(stream, T_OBJECT, &temp);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(structure);
|
2010-11-26 21:30:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < array->count; i++)
|
|
|
|
{
|
|
|
|
data = CARRAY_get_data(array, i);
|
|
|
|
VALUE_read(&temp, data, array->type);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_write_type(stream, array->type, &temp);
|
2010-11-26 21:30:58 +01:00
|
|
|
}
|
2010-09-27 21:34:27 +02:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-10-06 13:29:31 +02:00
|
|
|
OBJECT_lock((OBJECT *)array, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-09-27 21:34:27 +02:00
|
|
|
else if (class->quick_array == CQA_COLLECTION)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-09-27 21:34:27 +02:00
|
|
|
CCOLLECTION *col = (CCOLLECTION *)value->_object.object;
|
2010-10-06 14:03:27 +02:00
|
|
|
GB_COLLECTION_ITER iter;
|
2010-09-27 21:34:27 +02:00
|
|
|
char *key = NULL;
|
|
|
|
int len;
|
|
|
|
VALUE temp;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-10-06 13:29:31 +02:00
|
|
|
if (OBJECT_is_locked((OBJECT *)col))
|
|
|
|
THROW(E_SERIAL);
|
|
|
|
OBJECT_lock((OBJECT *)col, TRUE);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
buffer._byte = col->mode ? 'c' : 'C';
|
|
|
|
STREAM_write(stream, &buffer._byte, 1);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
write_length(stream, CCOLLECTION_get_count(col));
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-10-06 14:03:27 +02:00
|
|
|
GB_CollectionEnum(col, &iter, (GB_VARIANT *)&temp, NULL, NULL);
|
|
|
|
while (!GB_CollectionEnum(col, &iter, (GB_VARIANT *)&temp, &key, &len))
|
2010-09-27 21:34:27 +02:00
|
|
|
{
|
|
|
|
write_length(stream, len);
|
|
|
|
STREAM_write(stream, key, len);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_write_type(stream, T_VARIANT, &temp);
|
2010-09-27 21:34:27 +02:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-10-06 13:29:31 +02:00
|
|
|
OBJECT_lock((OBJECT *)col, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
break;
|
2010-09-27 21:34:27 +02:00
|
|
|
}
|
2010-11-26 21:30:58 +01:00
|
|
|
else if (class->is_struct)
|
|
|
|
{
|
|
|
|
CSTRUCT *structure = (CSTRUCT *)value->_object.object;
|
|
|
|
int i;
|
|
|
|
CLASS_DESC *desc;
|
|
|
|
VALUE temp;
|
|
|
|
char *addr;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
stream = enter_temp_stream(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
if (OBJECT_is_locked((OBJECT *)structure))
|
|
|
|
THROW(E_SERIAL);
|
|
|
|
OBJECT_lock((OBJECT *)structure, TRUE);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
for (i = 0; i < class->n_desc; i++)
|
|
|
|
{
|
|
|
|
desc = class->table[i].desc;
|
|
|
|
|
|
|
|
if (structure->ref)
|
|
|
|
addr = (char *)((CSTATICSTRUCT *)structure)->addr + desc->variable.offset;
|
|
|
|
else
|
|
|
|
addr = (char *)structure + sizeof(CSTRUCT) + desc->variable.offset;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
VALUE_class_read(desc->variable.class, &temp, (void *)addr, desc->variable.ctype, (void *)structure);
|
|
|
|
BORROW(&temp);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_write_type(stream, temp.type, &temp);
|
2010-11-26 21:30:58 +01:00
|
|
|
RELEASE(&temp);
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
stream = leave_temp_stream();
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-11-26 21:30:58 +01:00
|
|
|
OBJECT_lock((OBJECT *)structure, FALSE);
|
|
|
|
break;
|
|
|
|
}
|
2010-09-27 21:34:27 +02:00
|
|
|
// continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
default:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_TYPE, "Standard type", TYPE_get_name(type));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_load(const char *path, char **buffer, int *rlen)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM stream;
|
|
|
|
int64_t len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
STREAM_open(&stream, path, STO_READ);
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_lof(&stream, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (len >> 31)
|
|
|
|
THROW(E_MEMORY);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
*rlen = len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(buffer, *rlen);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM_read(&stream, *buffer, *rlen);
|
|
|
|
STREAM_close(&stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
bool STREAM_map(const char *path, char **paddr, int *plen)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
STREAM stream;
|
2008-12-28 23:41:55 +01:00
|
|
|
int fd;
|
|
|
|
struct stat info;
|
|
|
|
void *addr;
|
|
|
|
size_t len;
|
2008-12-31 16:55:40 +01:00
|
|
|
bool ret = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-08-30 13:33:13 +02:00
|
|
|
STREAM_open(&stream, path, STO_READ + STO_DIRECT);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
if (stream.type == &STREAM_arch)
|
2008-09-22 01:22:07 +02:00
|
|
|
{
|
2008-12-28 23:41:55 +01:00
|
|
|
*paddr = (char *)stream.arch.arch->arch->addr + stream.arch.start;
|
|
|
|
*plen = stream.arch.size;
|
2008-12-31 16:55:40 +01:00
|
|
|
ret = FALSE;
|
|
|
|
goto __RETURN;
|
2008-09-22 01:22:07 +02:00
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
fd = STREAM_handle(&stream);
|
|
|
|
if (fd < 0)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
if (fstat(fd, &info) < 0)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
len = info.st_size;
|
|
|
|
addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (addr == MAP_FAILED)
|
2008-12-31 16:55:40 +01:00
|
|
|
goto __RETURN;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
*paddr = addr;
|
|
|
|
*plen = len;
|
2008-12-31 16:55:40 +01:00
|
|
|
ret = FALSE;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
__RETURN:
|
|
|
|
|
|
|
|
STREAM_close(&stream);
|
|
|
|
return ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-12-28 23:41:55 +01:00
|
|
|
void STREAM_unmap(char *addr, int len)
|
|
|
|
{
|
|
|
|
if (addr && len > 0 && ARCHIVE_check_addr(addr))
|
2008-12-31 16:55:40 +01:00
|
|
|
{
|
2008-12-28 23:41:55 +01:00
|
|
|
munmap(addr, len);
|
2008-12-31 16:55:40 +01:00
|
|
|
}
|
2008-12-28 23:41:55 +01:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_handle(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (stream->type->handle)
|
|
|
|
return (*stream->type->handle)(stream);
|
|
|
|
else
|
|
|
|
return (-1);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-18 13:02:03 +01:00
|
|
|
bool STREAM_lock_all(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int64_t pos;
|
|
|
|
int fd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
fd = STREAM_handle(stream);
|
|
|
|
if (fd < 0)
|
2014-03-02 21:18:06 +01:00
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (pos < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (lseek(fd, 0, SEEK_SET) < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-03-21 02:04:18 +01:00
|
|
|
#ifdef F_TLOCK
|
|
|
|
|
|
|
|
if (lockf(fd, F_TLOCK, 0))
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
2014-03-02 21:18:06 +01:00
|
|
|
return TRUE;
|
2008-03-21 02:04:18 +01:00
|
|
|
else
|
|
|
|
goto __ERROR;
|
|
|
|
}
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-21 02:04:18 +01:00
|
|
|
#else
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
ERROR_warning("locking is not implemented");
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-03-21 02:04:18 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (lseek(fd, pos, SEEK_SET) < 0)
|
|
|
|
goto __ERROR;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
__ERROR:
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW_SYSTEM(errno, NULL);
|
2014-03-02 21:18:06 +01:00
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_lof(STREAM *stream, int64_t *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
int fd;
|
|
|
|
int ilen;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2014-08-02 23:37:49 +02:00
|
|
|
|
|
|
|
*len = 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (stream->type->lof)
|
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
if (!(*(stream->type->lof))(stream, len))
|
2017-10-05 23:18:19 +02:00
|
|
|
goto ADD_BUFFER;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = STREAM_handle(stream);
|
2008-03-11 14:29:47 +01:00
|
|
|
if ((fd >= 0) && (STREAM_get_readable(stream, &ilen) == 0))
|
2007-12-30 17:41:49 +01:00
|
|
|
*len = ilen;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2017-10-05 23:18:19 +02:00
|
|
|
ADD_BUFFER:
|
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
if (stream->common.buffer)
|
|
|
|
*len += stream->common.buffer_len - stream->common.buffer_pos;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool STREAM_eof(STREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-07-06 21:00:41 +02:00
|
|
|
if (STREAM_is_closed(stream))
|
2008-09-22 01:22:07 +02:00
|
|
|
THROW(E_CLOSED);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
if (stream->common.buffer && stream->common.buffer_pos < stream->common.buffer_len)
|
|
|
|
return FALSE;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (stream->type->eof)
|
2008-09-22 01:22:07 +02:00
|
|
|
return ((*(stream->type->eof))(stream));
|
|
|
|
else
|
2018-08-27 23:57:30 +02:00
|
|
|
return STREAM_default_eof(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
#if 0
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_read_direct(int fd, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
ssize_t eff_read;
|
|
|
|
ssize_t len_read;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
len_read = Min(len, MAX_IO);
|
|
|
|
eff_read = read(fd, buffer, len_read);
|
|
|
|
|
|
|
|
if (eff_read > 0)
|
|
|
|
{
|
|
|
|
STREAM_eff_read += eff_read;
|
|
|
|
len -= eff_read;
|
|
|
|
buffer += eff_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eff_read < len_read)
|
|
|
|
{
|
|
|
|
if (eff_read == 0)
|
|
|
|
errno = 0;
|
|
|
|
if (eff_read <= 0 && errno != EINTR)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int STREAM_write_direct(int fd, char *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
ssize_t eff_write;
|
|
|
|
ssize_t len_write;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
len_write = Min(len, MAX_IO);
|
|
|
|
eff_write = write(fd, buffer, len_write);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
if (eff_write < len_write)
|
|
|
|
{
|
|
|
|
if (eff_write <= 0 && errno != EINTR)
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
len -= eff_write;
|
|
|
|
buffer += eff_write;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2017-09-27 02:48:00 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
void STREAM_blocking(STREAM *stream, bool block)
|
|
|
|
{
|
|
|
|
int fd = STREAM_handle(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-02-27 23:08:04 +01:00
|
|
|
stream->common.blocking = block;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
if (block)
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
|
|
|
else
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-07 20:39:45 +02:00
|
|
|
void STREAM_check_blocking(STREAM *stream)
|
2008-10-29 19:26:56 +01:00
|
|
|
{
|
2010-04-07 20:39:45 +02:00
|
|
|
int fd = STREAM_handle(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2010-04-07 20:39:45 +02:00
|
|
|
stream->common.blocking = (fd < 0) ? TRUE : ((fcntl(fd, F_GETFL) & O_NONBLOCK) == 0);
|
2008-10-29 19:26:56 +01:00
|
|
|
}
|
2011-04-04 02:40:32 +02:00
|
|
|
|
|
|
|
void STREAM_cancel(STREAM *stream)
|
|
|
|
{
|
|
|
|
if (!stream->common.redirect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
STREAM_close(stream->common.redirect);
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&stream->common.redirect);
|
2011-04-04 02:40:32 +02:00
|
|
|
stream->common.redirected = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void STREAM_begin(STREAM *stream)
|
|
|
|
{
|
|
|
|
STREAM_cancel(stream);
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
if (!stream->common.redirect)
|
|
|
|
{
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC_ZERO(&stream->common.redirect, sizeof(STREAM));
|
2018-08-30 13:33:13 +02:00
|
|
|
STREAM_open(stream->common.redirect, NULL, STO_STRING | STO_WRITE);
|
2011-04-04 02:40:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->common.redirected = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void STREAM_end(STREAM *stream)
|
|
|
|
{
|
|
|
|
if (!stream->common.redirect)
|
|
|
|
return;
|
2015-11-02 04:24:44 +01:00
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
stream->common.redirected = FALSE;
|
|
|
|
STREAM_write(stream, stream->common.redirect->string.buffer, STRING_length(stream->common.redirect->string.buffer));
|
2018-02-17 23:16:28 +01:00
|
|
|
stream->common.redirected = TRUE;
|
2011-04-04 02:40:32 +02:00
|
|
|
STREAM_cancel(stream);
|
|
|
|
}
|
|
|
|
|