summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/compression.cpp23
-rw-r--r--core/io/compression.h2
-rw-r--r--core/io/stream_peer.cpp143
-rw-r--r--core/io/stream_peer.h36
4 files changed, 189 insertions, 15 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp
index a17e358cbb..ca44d24911 100644
--- a/core/io/compression.cpp
+++ b/core/io/compression.cpp
@@ -60,6 +60,10 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,M
strm.avail_in=p_src_size;
int aout = deflateBound(&strm,p_src_size);;
+ /*if (aout>p_src_size) {
+ deflateEnd(&strm);
+ return -1;
+ }*/
strm.avail_out=aout;
strm.next_in=(Bytef*)p_src;
strm.next_out=p_dst;
@@ -107,19 +111,21 @@ int Compression::get_max_compressed_buffer_size(int p_src_size,Mode p_mode){
-void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){
+int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode){
switch(p_mode) {
case MODE_FASTLZ: {
+ int ret_size=0;
+
if (p_dst_max_size<16) {
uint8_t dst[16];
- fastlz_decompress(p_src,p_src_size,dst,16);
+ ret_size = fastlz_decompress(p_src,p_src_size,dst,16);
copymem(p_dst,dst,p_dst_max_size);
} else {
- fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size);
+ ret_size = fastlz_decompress(p_src,p_src_size,p_dst,p_dst_max_size);
}
- return;
+ return ret_size;
} break;
case MODE_DEFLATE: {
@@ -130,7 +136,7 @@ void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *
strm.avail_in= 0;
strm.next_in=Z_NULL;
int err = inflateInit(&strm);
- ERR_FAIL_COND(err!=Z_OK);
+ ERR_FAIL_COND_V(err!=Z_OK,-1);
strm.avail_in=p_src_size;
strm.avail_out=p_dst_max_size;
@@ -138,11 +144,12 @@ void Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *
strm.next_out=p_dst;
err = inflate(&strm,Z_FINISH);
+ int total = strm.total_out;
inflateEnd(&strm);
- ERR_FAIL_COND(err!=Z_STREAM_END);
- return;
+ ERR_FAIL_COND_V(err!=Z_STREAM_END,-1);
+ return total;
} break;
}
- ERR_FAIL();
+ ERR_FAIL_V(-1);
}
diff --git a/core/io/compression.h b/core/io/compression.h
index 07a293c940..e0a4d31a51 100644
--- a/core/io/compression.h
+++ b/core/io/compression.h
@@ -43,7 +43,7 @@ public:
static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ);
static int get_max_compressed_buffer_size(int p_src_size,Mode p_mode=MODE_FASTLZ);
- static void decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ);
+ static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size,Mode p_mode=MODE_FASTLZ);
Compression();
};
diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp
index 306e7d8c9d..176d55ffdd 100644
--- a/core/io/stream_peer.cpp
+++ b/core/io/stream_peer.cpp
@@ -222,6 +222,7 @@ void StreamPeer::put_double(double p_val){
void StreamPeer::put_utf8_string(const String& p_string) {
CharString cs=p_string.utf8();
+ put_u32(p_string.length());
put_data((const uint8_t*)cs.get_data(),cs.length());
}
@@ -348,8 +349,10 @@ String StreamPeer::get_string(int p_bytes){
ERR_FAIL_COND_V(p_bytes<0,String());
Vector<char> buf;
- buf.resize(p_bytes+1);
- get_data((uint8_t*)&buf[0],p_bytes);
+ Error err = buf.resize(p_bytes+1);
+ ERR_FAIL_COND_V(err!=OK,String());
+ err = get_data((uint8_t*)&buf[0],p_bytes);
+ ERR_FAIL_COND_V(err!=OK,String());
buf[p_bytes]=0;
return buf.ptr();
@@ -359,8 +362,10 @@ String StreamPeer::get_utf8_string(int p_bytes){
ERR_FAIL_COND_V(p_bytes<0,String());
Vector<uint8_t> buf;
- buf.resize(p_bytes);
- get_data(buf.ptr(),p_bytes);
+ Error err = buf.resize(p_bytes);
+ ERR_FAIL_COND_V(err!=OK,String());
+ err = get_data(buf.ptr(),p_bytes);
+ ERR_FAIL_COND_V(err!=OK,String());
String ret;
ret.parse_utf8((const char*)buf.ptr(),buf.size());
@@ -371,8 +376,10 @@ Variant StreamPeer::get_var(){
int len = get_32();
Vector<uint8_t> var;
- var.resize(len);
- get_data(var.ptr(),len);
+ Error err = var.resize(len);
+ ERR_FAIL_COND_V(err!=OK,Variant());
+ err = get_data(var.ptr(),len);
+ ERR_FAIL_COND_V(err!=OK,Variant());
Variant ret;
decode_variant(ret,var.ptr(),len);
@@ -420,3 +427,127 @@ void StreamPeer::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_utf8_string","bytes"),&StreamPeer::get_utf8_string);
ObjectTypeDB::bind_method(_MD("get_var:Variant"),&StreamPeer::get_var);
}
+////////////////////////////////
+
+
+void StreamPeerBuffer::_bind_methods() {
+
+ ObjectTypeDB::bind_method(_MD("seek","pos"),&StreamPeerBuffer::seek);
+ ObjectTypeDB::bind_method(_MD("get_size"),&StreamPeerBuffer::get_size);
+ ObjectTypeDB::bind_method(_MD("get_pos"),&StreamPeerBuffer::get_pos);
+ ObjectTypeDB::bind_method(_MD("resize","size"),&StreamPeerBuffer::resize);
+ ObjectTypeDB::bind_method(_MD("set_data_array","data"),&StreamPeerBuffer::set_data_array);
+ ObjectTypeDB::bind_method(_MD("get_data_array"),&StreamPeerBuffer::get_data_array);
+ ObjectTypeDB::bind_method(_MD("clear"),&StreamPeerBuffer::clear);
+ ObjectTypeDB::bind_method(_MD("duplicate:StreamPeerBuffer"),&StreamPeerBuffer::duplicate);
+
+}
+
+
+Error StreamPeerBuffer::put_data(const uint8_t* p_data,int p_bytes) {
+
+ if (p_bytes<=0)
+ return OK;
+
+ if (pointer+p_bytes > data.size()) {
+ data.resize(pointer+p_bytes);
+
+ }
+
+ DVector<uint8_t>::Write w = data.write();
+ copymem(&w[pointer],p_data,p_bytes);
+
+ pointer+=p_bytes;
+ return OK;
+}
+
+Error StreamPeerBuffer::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent){
+
+ r_sent=p_bytes;
+ return put_data(p_data,p_bytes);
+}
+
+Error StreamPeerBuffer::get_data(uint8_t* p_buffer, int p_bytes){
+
+ int recv;
+ get_partial_data(p_buffer,p_bytes,recv);
+ if (recv!=p_bytes)
+ return ERR_INVALID_PARAMETER;
+
+ return OK;
+
+}
+Error StreamPeerBuffer::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received){
+
+
+ if (pointer+p_bytes > data.size()) {
+ r_received=data.size()-pointer;
+ if (r_received<=0) {
+ r_received=0;
+ return OK; //you got 0
+ }
+ } else {
+ r_received=p_bytes;
+ }
+
+ DVector<uint8_t>::Read r = data.read();
+ copymem(p_buffer,r.ptr(),r_received);
+}
+
+int StreamPeerBuffer::get_available_bytes() const {
+
+ return data.size()-pointer;
+}
+
+void StreamPeerBuffer::seek(int p_pos){
+
+ ERR_FAIL_COND(p_pos < 0);
+ ERR_FAIL_COND(p_pos > data.size());
+ pointer=p_pos;
+}
+int StreamPeerBuffer::get_size() const{
+
+ return data.size();
+}
+
+int StreamPeerBuffer::get_pos() const {
+
+ return pointer;
+}
+
+void StreamPeerBuffer::resize(int p_size){
+
+ data.resize(p_size);
+}
+
+void StreamPeerBuffer::set_data_array(const DVector<uint8_t> & p_data){
+
+ data=p_data;
+ pointer=0;
+}
+
+DVector<uint8_t> StreamPeerBuffer::get_data_array() const{
+
+ return data;
+}
+
+
+void StreamPeerBuffer::clear() {
+
+ data.resize(0);
+ pointer=0;
+}
+
+
+Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const {
+
+ Ref<StreamPeerBuffer> spb;
+ spb.instance();
+ spb->data=data;
+}
+
+
+StreamPeerBuffer::StreamPeerBuffer() {
+
+ pointer=0;
+}
diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h
index 970e6695a5..f28e6f594d 100644
--- a/core/io/stream_peer.h
+++ b/core/io/stream_peer.h
@@ -91,4 +91,40 @@ public:
StreamPeer() { big_endian=false; }
};
+
+class StreamPeerBuffer : public StreamPeer {
+
+ OBJ_TYPE(StreamPeerBuffer,StreamPeer);
+
+ DVector<uint8_t> data;
+ int pointer;
+protected:
+
+ static void _bind_methods();
+public:
+ Error put_data(const uint8_t* p_data,int p_bytes);
+ Error put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent);
+
+ Error get_data(uint8_t* p_buffer, int p_bytes);
+ Error get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received);
+
+ virtual int get_available_bytes() const;
+
+ void seek(int p_pos);
+ int get_size() const;
+ int get_pos() const;
+ void resize(int p_size);
+
+
+ void set_data_array(const DVector<uint8_t> & p_data);
+ DVector<uint8_t> get_data_array() const;
+
+ void clear();
+
+ Ref<StreamPeerBuffer> duplicate() const;
+
+ StreamPeerBuffer();
+};
+
+
#endif // STREAM_PEER_H