More tweaks to base-writer
This commit is contained in:
@@ -197,12 +197,12 @@ build/$(OS)/eris/%.obj: ext/eris-master/src/%.c build/$(OS)/DIRECTORY
|
||||
$(COMPILE) $@ $(LUA_FLAGS) $<
|
||||
|
||||
build/$(OS)/core/%.obj: cpp/core/%.cpp build/$(OS)/DIRECTORY
|
||||
$(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $<
|
||||
$(COMPILE) $@ -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core $<
|
||||
$(MAKEDEPS) $@d -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -I./ext $<
|
||||
$(COMPILE) $@ -I./ext/eris-master/src -I./cpp/wrap -I./cpp/core -I./ext $<
|
||||
|
||||
build/$(OS)/drv/%.obj: cpp/drv/%.cpp build/$(OS)/DIRECTORY
|
||||
$(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv $<
|
||||
$(COMPILE) $@ $(OPENSSL_INCLUDE) -I./src/drv $<
|
||||
$(MAKEDEPS) $@d $(OPENSSL_INCLUDE) -I./src/drv -I./ext $<
|
||||
$(COMPILE) $@ $(OPENSSL_INCLUDE) -I./src/drv -I./ext $<
|
||||
|
||||
clean:
|
||||
rm -f luprex* luprex*.* *.pdb
|
||||
|
||||
@@ -169,7 +169,6 @@ void StreamBuffer::read_bytes_into(char *data, int64_t size) {
|
||||
read_cursor_ += size;
|
||||
}
|
||||
|
||||
|
||||
void StreamBuffer::write_xyz(const util::XYZ &xyz) {
|
||||
make_space(12);
|
||||
memcpy(write_cursor_, &xyz.x, 4);
|
||||
|
||||
@@ -241,8 +241,10 @@ public:
|
||||
virtual char const *what() const { return "Stream contained invalid data"; }
|
||||
};
|
||||
|
||||
class StreamBuffer : public eng::nevernew, public BaseReader<StreamBuffer, eng::string>, public BaseWriter<StreamBuffer> {
|
||||
class StreamBuffer : public eng::nevernew, public BaseReader<StreamBuffer>, public BaseWriter<StreamBuffer> {
|
||||
public:
|
||||
using read_string_type = eng::string;
|
||||
|
||||
// Construct an empty buffer.
|
||||
StreamBuffer();
|
||||
|
||||
@@ -290,10 +292,6 @@ public:
|
||||
void write_bytes(const char *bytes, int64_t len);
|
||||
void write_bytes(std::string_view s);
|
||||
|
||||
// Copy bytes from the StreamBuffer into an external buffer.
|
||||
//
|
||||
void read_bytes_into(char *target, int64_t len);
|
||||
|
||||
// Read a block of bytes from the buffer.
|
||||
//
|
||||
// Caution: the pointer returned is a pointer to the stream's buffer. It is
|
||||
@@ -302,6 +300,10 @@ public:
|
||||
//
|
||||
const char *read_bytes(int64_t bytes);
|
||||
|
||||
// Copy bytes from the StreamBuffer into an external buffer.
|
||||
//
|
||||
void read_bytes_into(char *target, int64_t len);
|
||||
|
||||
// Read and write larger types.
|
||||
//
|
||||
// Throws StreamEof if the specified number of bytes aren't present.
|
||||
|
||||
@@ -28,8 +28,8 @@
|
||||
// void write_char(char c)
|
||||
// void write_float(float data)
|
||||
// void write_double(double data)
|
||||
// void write_bytes(std::string_view bytes)
|
||||
// void write_string(std::string_view bytes)
|
||||
// void write_length(size_t data)
|
||||
// void write_string(std::string_view data)
|
||||
//
|
||||
// In order to derive from BaseWriter, you must use the CRTP pattern:
|
||||
//
|
||||
@@ -74,16 +74,19 @@ public:
|
||||
void write_float(float arg) { write_value_core(arg); }
|
||||
void write_double(double arg) { write_value_core(arg); }
|
||||
|
||||
void write_string(std::string_view s) {
|
||||
if (s.size() >= 255) {
|
||||
void write_length(size_t len) {
|
||||
if (len >= 255) {
|
||||
write_uint8(0xFF);
|
||||
write_uint64(s.size());
|
||||
static_cast<Derived*>(this)->write_bytes(s.data(), s.size());
|
||||
write_uint64(len);
|
||||
} else {
|
||||
write_uint8(s.size());
|
||||
static_cast<Derived*>(this)->write_bytes(s.data(), s.size());
|
||||
write_uint8(len);
|
||||
}
|
||||
}
|
||||
|
||||
void write_string(std::string_view s) {
|
||||
write_length(s.size());
|
||||
static_cast<Derived*>(this)->write_bytes(s.data(), s.size());
|
||||
}
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////
|
||||
@@ -104,8 +107,9 @@ public:
|
||||
// char read_char();
|
||||
// float read_float();
|
||||
// double read_double();
|
||||
// string read_string_limit(int64_t size);
|
||||
// string read_string();
|
||||
// size_t read_length();
|
||||
// String read_string_limit(uint64_t size);
|
||||
// String read_string();
|
||||
//
|
||||
// In order to derive from BaseReader, you must use the CRTP pattern:
|
||||
//
|
||||
@@ -113,18 +117,15 @@ public:
|
||||
//
|
||||
// The derived class must provide:
|
||||
//
|
||||
// using string = std::string (or similar)
|
||||
//
|
||||
// using read_string_type = std::string; // or similar
|
||||
// void read_bytes_into(char *n, size_t size)
|
||||
//
|
||||
// void raise_string_too_long();
|
||||
//
|
||||
///////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Derived, class String>
|
||||
template<class Derived>
|
||||
class BaseReader {
|
||||
protected:
|
||||
|
||||
template<class T>
|
||||
T read_value_core() {
|
||||
T result;
|
||||
@@ -148,22 +149,22 @@ public:
|
||||
float read_float() { return read_value_core<float>(); }
|
||||
double read_double() { return read_value_core<double>(); }
|
||||
|
||||
String read_string_limit(int64_t limit) {
|
||||
uint64_t size = read_uint8();
|
||||
if (size == 255) {
|
||||
size = read_uint64();
|
||||
size_t read_length() {
|
||||
uint64_t len = read_uint8();
|
||||
if (len == 255) {
|
||||
len = read_uint64();
|
||||
}
|
||||
if ((limit < 0)||(size > uint64_t(limit))) {
|
||||
static_cast<Derived*>(this)->raise_string_too_long();
|
||||
}
|
||||
String result;
|
||||
result.resize(size);
|
||||
static_cast<Derived*>(this)->read_bytes_into(&result[0], size);
|
||||
return len;
|
||||
}
|
||||
|
||||
auto read_string_limit(uint64_t limit) {
|
||||
size_t len = read_length();
|
||||
if (len > limit) static_cast<Derived*>(this)->raise_string_too_long();
|
||||
typename Derived::read_string_type result(len, ' ');
|
||||
static_cast<Derived*>(this)->read_bytes_into(&(result[0]), len);
|
||||
return result;
|
||||
}
|
||||
|
||||
String read_string() {
|
||||
return read_string_limit(0xFFFFFFF);
|
||||
}
|
||||
auto read_string() { return read_string_limit(0x1000000); } // 16MB limit default
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user