8 #ifndef swcdb_core_Serialization_h
9 #define swcdb_core_Serialization_h
24 namespace Serialization {
27 #define SWC_THROW_OVERRUN(_s_) \
28 SWC_THROWF(Error::SERIALIZATION_INPUT_OVERRUN, "Error decoding %s", _s_)
30 #define SWC_THROW_UNPOSSIBLE(_s_) \
31 SWC_THROWF(Error::UNPOSSIBLE, "%s", _s_)
35 constexpr
const uint64_t
MAX_V2B = 0x3fff;
36 constexpr
const uint64_t
MAX_V3B = 0x1fffff;
37 constexpr
const uint64_t
MAX_V4B = 0x0fffffff;
38 constexpr
const uint64_t
MAX_V5B = 0x07ffffffff;
39 constexpr
const uint64_t
MAX_V6B = 0x03ffffffffff;
40 constexpr
const uint64_t
MAX_V7B = 0x01ffffffffffff;
41 constexpr
const uint64_t
MAX_V8B = 0x00ffffffffffffff;
42 constexpr
const uint64_t
MAX_V9B = 0x7fffffffffffffff;
50 void memcopy(
void* dest,
const uint8_t** bufp,
size_t len) noexcept {
51 memcpy(dest, *bufp, len);
59 void memcopy(uint8_t** bufp,
const void* src,
size_t len) noexcept {
60 memcpy(*bufp, src, len);
71 "Zero byte remain need 1 byte");
85 void encode_i8(uint8_t** bufp, uint8_t val) noexcept {
91 uint8_t
decode_i8(
const uint8_t** bufp,
size_t* remainp) {
163 #define SWC_ENCODE_VI_0(_p_, _v_) \
164 if(_v_ <= MAX_V1B) { **_p_ = _v_; ++*_p_; return; }
166 #define SWC_ENCODE_VI_1(_p_, _v_) \
167 **_p_ = _v_ | 0x80; ++*_p_; _v_ >>= 7; \
168 SWC_ENCODE_VI_0(_p_, _v_);
170 #define SWC_ENCODE_VI(_p_, _v_, _shifts_) \
171 SWC_ENCODE_VI_0(_p_, _v_); \
172 for(uint8_t n=0; n < _shifts_; ++n) { SWC_ENCODE_VI_1(_p_, _v_); } \
173 SWC_THROW_UNPOSSIBLE("breached encoding length");
176 #define SWC_DECODE_NEED_BYTE(_r_) \
177 if(!*_r_) SWC_THROW(Error::SERIALIZATION_INPUT_OVERRUN, \
178 "Zero byte remain need 1 byte"); \
181 #define SWC_DECODE_VI_(_v_, _tmp_, _p_, _shift_) \
182 _v_ |= (_tmp_ = **_p_ & 0x7f) <<= _shift_; \
183 _tmp_ = **_p_ & 0x80; ++*_p_; if(!tmp) return _v_;
185 #define SWC_DECODE_VI_1(_v_, _tmp_, _p_, _r_, _shift_) \
186 SWC_DECODE_NEED_BYTE(_r_); \
187 SWC_DECODE_VI_(_v_, _tmp_, _p_, _shift_);
189 #define SWC_DECODE_VI(_t_, _p_, _r_, _bits_, _name_) \
190 _t_ n = 0; _t_ tmp; for(uint8_t shift=0; *_r_ ; shift+=7) { \
191 --*_r_; SWC_DECODE_VI_(n, tmp, _p_, shift); \
192 if(shift == _bits_) SWC_THROW_OVERRUN(_name_); \
194 SWC_THROW(Error::SERIALIZATION_INPUT_OVERRUN, \
195 "Zero byte remain need 1 byte");
256 uint32_t n = 0; uint32_t tmp = 0;
282 (val <=
MAX_V9B ? 9 : 10)))))))));
304 uint64_t n = 0; uint64_t tmp = 0;
325 template<u
int8_t BITS, u
int8_t SZ,
typename T>
330 memcpy(++*bufp,
reinterpret_cast<const uint8_t*
>(&_val), SZ);
334 template<u
int8_t BITS, u
int8_t SZ,
typename T>
338 memcpy(
reinterpret_cast<uint8_t*
>(&val), *bufp, SZ);
360 return encode_fixed_vi<8, 1, uint32_t>(bufp, val);
362 return encode_fixed_vi<16, 2, uint32_t>(bufp, val);
363 return encode_fixed_vi<24, 3, uint32_t>(bufp, val);
374 return decode_fixed_vi<8, 1, uint32_t>(bufp);
376 return decode_fixed_vi<16, 2, uint32_t>(bufp);
378 return decode_fixed_vi<24, 3, uint32_t>(bufp);
407 return encode_fixed_vi<8, 1, uint32_t>(bufp, val);
409 return encode_fixed_vi<16, 2, uint32_t>(bufp, val);
411 return encode_fixed_vi<24, 3, uint32_t>(bufp, val);
412 return encode_fixed_vi<32, 4, uint32_t>(bufp, val);
423 return decode_fixed_vi<8, 1, uint32_t>(bufp);
425 return decode_fixed_vi<16, 2, uint32_t>(bufp);
427 return decode_fixed_vi<24, 3, uint32_t>(bufp);
429 return decode_fixed_vi<32, 4, uint32_t>(bufp);
450 if(val <= 0xFFFFFFFF)
452 if(val <= 0xFFFFFFFFFF)
454 if(val <= 0xFFFFFFFFFFFF)
456 if(val <= 0xFFFFFFFFFFFFFF)
466 return encode_fixed_vi<8, 1, uint64_t>(bufp, val);
468 return encode_fixed_vi<16, 2, uint64_t>(bufp, val);
470 return encode_fixed_vi<24, 3, uint64_t>(bufp, val);
471 if(val <= 0xFFFFFFFF)
472 return encode_fixed_vi<32, 4, uint64_t>(bufp, val);
473 if(val < 0xFFFFFFFFFF)
474 return encode_fixed_vi<40, 5, uint64_t>(bufp, val);
475 if(val < 0xFFFFFFFFFFFF)
476 return encode_fixed_vi<48, 6, uint64_t>(bufp, val);
477 if(val < 0xFFFFFFFFFFFFFF)
478 return encode_fixed_vi<56, 7, uint64_t>(bufp, val);
479 return encode_fixed_vi<64, 8, uint64_t>(bufp, val);
490 return decode_fixed_vi<8, 1, uint64_t>(bufp);
492 return decode_fixed_vi<16, 2, uint64_t>(bufp);
494 return decode_fixed_vi<24, 3, uint64_t>(bufp);
496 return decode_fixed_vi<32, 4, uint64_t>(bufp);
498 return decode_fixed_vi<40, 5, uint64_t>(bufp);
500 return decode_fixed_vi<48, 6, uint64_t>(bufp);
502 return decode_fixed_vi<56, 7, uint64_t>(bufp);
504 return decode_fixed_vi<64, 8, uint64_t>(bufp);
518 return sizeof(
long double);
552 const uint8_t* out = *bufp;
560 const char* s =
reinterpret_cast<const char*
>(
562 return std::string(s, len);
569 uint32_t len) noexcept {
577 const uint8_t* out = *bufp;
586 #endif // swcdb_core_Serialization_h