2020-03-09 01:50:30 -07:00
|
|
|
#include <utils.hpp>
|
|
|
|
#include <stream.hpp>
|
2019-11-20 21:48:49 -05:00
|
|
|
|
|
|
|
static int strm_read(void *v, char *buf, int len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto strm = reinterpret_cast<stream *>(v);
|
|
|
|
return strm->read(buf, len);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int strm_write(void *v, const char *buf, int len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto strm = reinterpret_cast<stream *>(v);
|
|
|
|
return strm->write(buf, len);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static fpos_t strm_seek(void *v, fpos_t off, int whence) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto strm = reinterpret_cast<stream *>(v);
|
|
|
|
return strm->seek(off, whence);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int strm_close(void *v) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto strm = reinterpret_cast<stream *>(v);
|
|
|
|
delete strm;
|
|
|
|
return 0;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
sFILE make_stream_fp(stream_ptr &&strm) {
|
2020-12-30 22:11:24 -08:00
|
|
|
auto fp = make_file(funopen(strm.release(), strm_read, strm_write, strm_seek, strm_close));
|
|
|
|
setbuf(fp.get(), nullptr);
|
|
|
|
return fp;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int stream::read(void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("This stream does not support read\n");
|
|
|
|
return -1;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int stream::write(const void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("This stream does not support write\n");
|
|
|
|
return -1;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t stream::seek(off_t off, int whence) {
|
2020-12-30 22:11:24 -08:00
|
|
|
LOGE("This stream does not support seek\n");
|
|
|
|
return -1;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
int fp_stream::read(void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return fread(buf, 1, len, fp.get());
|
2019-11-21 06:08:02 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
int fp_stream::write(const void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return fwrite(buf, 1, len, fp.get());
|
2019-11-21 06:08:02 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
off_t fp_stream::seek(off_t off, int whence) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return fseek(fp.get(), off, whence);
|
2019-11-21 06:08:02 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 00:37:06 -05:00
|
|
|
int filter_stream::read(void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return base->read(buf, len);
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int filter_stream::write(const void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return base->write(buf, len);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
byte_stream::byte_stream(uint8_t *&buf, size_t &len) : _buf(buf), _len(len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
buf = nullptr;
|
|
|
|
len = 0;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int byte_stream::read(void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
len = std::min(len, _len - _pos);
|
|
|
|
memcpy(buf, _buf + _pos, len);
|
|
|
|
return len;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int byte_stream::write(const void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
resize(_pos + len);
|
|
|
|
memcpy(_buf + _pos, buf, len);
|
|
|
|
_pos += len;
|
|
|
|
_len = std::max(_len, _pos);
|
|
|
|
return len;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t byte_stream::seek(off_t off, int whence) {
|
2020-12-30 22:11:24 -08:00
|
|
|
off_t np;
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
np = _pos + off;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
np = _len + off;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
np = off;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
resize(np, true);
|
|
|
|
_pos = np;
|
|
|
|
return np;
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void byte_stream::resize(size_t new_pos, bool zero) {
|
2020-12-30 22:11:24 -08:00
|
|
|
bool resize = false;
|
|
|
|
size_t old_cap = _cap;
|
|
|
|
while (new_pos > _cap) {
|
|
|
|
_cap = _cap ? (_cap << 1) - (_cap >> 1) : 1 << 12;
|
|
|
|
resize = true;
|
|
|
|
}
|
|
|
|
if (resize) {
|
|
|
|
_buf = (uint8_t *) xrealloc(_buf, _cap);
|
|
|
|
if (zero)
|
|
|
|
memset(_buf + old_cap, 0, _cap - old_cap);
|
|
|
|
}
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd_stream::read(void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return ::read(fd, buf, len);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int fd_stream::write(const void *buf, size_t len) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return ::write(fd, buf, len);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t fd_stream::seek(off_t off, int whence) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return lseek(fd, off, whence);
|
2019-11-20 21:48:49 -05:00
|
|
|
}
|