1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| //Serialization of unsigned integers of 16, 32 or 64 bits
//16 bits (can't store more than 15 bits):
// range [ 0, 127] -> one byte
// range [ 128, 32767] -> two bytes
//
//|c|xxxxxxx| |xxxxxxxx|
// ^ 7 8
// 0 <--7-->
// 1 <-------15------->
//32 bits (can't store more than 30 bits):
// range [ 0, 63] -> one byte
// range [ 64, 16383] -> two bytes
// range [ 16384, 4194303] -> three bytes
// range [ 4194304, 1073741823] -> four bytes
//
//|cc|xxxxxx| |xxxxxxxx| |xxxxxxxx| |xxxxxxxx|
// ^^ 6 8 8 8
// 00 <--6->
// 01 <-------14------>
// 10 <------------22------------>
// 11 <------------------30----------------->
//64 bits (can't store more than 61 bits):
// range [ 0, 31] -> one byte
// range [ 32, 8191] -> two bytes
// range [ 8192, 2097151] -> three bytes
// range [ 2097152, 536870911] -> four bytes
// range [ 536870912, 137438953471] -> five bytes (up to 128 "Gigas")
// range [ 137438953472, 35184372088831] -> six bytes (up to 32768 "Gigas")
// range [ 35184372088831, ...] -> seven bytes (up to 8388608 "Gigas")
// ...
//
//|ccc|xxxxx| |xxxxxxxx| |xxxxxxxx| |xxxxxxxx| |xxxxxxxx| |xxxxxxxx|
// ^^^ 5 8 8 8 8 8
// 000 <-5->
// 001 <------13------>
// 010 <-----------21------------>
// 011 <-----------------29----------------->
// 100 <-----------------------37---------------------->
// 101 <----------------------------45---------------------------->
// 110 ... etc
template<typename U>
inline unsigned count_serial_unsigned(U u)
{
//U must be unsigned 16, 32 or 64 bits
unsigned const shift_bits=serializable_type<U>::log2;
unsigned const bytes=serializable_type<U>::bytes;
//loose the log2(sizeof(U)) most significant bits
u<<=shift_bits;
//count bytes
unsigned count_bytes=bytes-1;
while (count_bytes && (u & U(0xFF)<<((bytes-1)*8))==0)
{
u<<=8;
--count_bytes;
}
return count_bytes+1;
}
template<typename IT, typename U>
inline IT serialize_unsigned(IT it_out, U u)
{
//IT is meant to be a byte string iterator
//U must be unsigned 16, 32 or 64 bits
unsigned const shift_bits=serializable_type<U>::log2;
unsigned const bits=serializable_type<U>::bits;
unsigned const bytes=serializable_type<U>::bytes;
//loose the log2(sizeof(U)) most significant bits
u<<=shift_bits;
//count bytes
unsigned count_bytes=bytes-1;
while (count_bytes && (u & U(0xFF)<<((bytes-1)*8))==0)
{
u<<=8;
--count_bytes;
}
//reconstruct shifted value, add count bytes in front
u=(U(count_bytes)<<(bits-shift_bits)) | (u>>shift_bits);
//serialize most significant byte first
u=byte_swap(u);
do
{
*it_out=(unsigned char)u;//explicit truncation
++it_out;
u>>=8;
}
while (count_bytes--);
return it_out;
}
template<typename IT, typename U>
inline IT unserialize_unsigned(IT cit_in, U & ru)
{
//IT is meant to be a byte string iterator
//U must be unsigned 16, 32 or 64 bits
unsigned const shift_bits=serializable_type<U>::log2;
U u=(unsigned char)*cit_in;
++cit_in;
unsigned count_bytes=unsigned(u>>(8-shift_bits));
u&=(0xFF>>shift_bits);
while (count_bytes)
{
u<<=8;
u|=(unsigned char)*cit_in;
++cit_in;
--count_bytes;
}
ru=u;
return cit_in;
}
template<typename IT, typename U>
inline IT serialize_unsigned_inverted(IT it_out, U u)
{
//IT is meant to be a byte string iterator
//U must be unsigned 16, 32 or 64 bits
unsigned const shift_bits=serializable_type<U>::log2;
unsigned const bits=serializable_type<U>::bits;
unsigned const bytes=serializable_type<U>::bytes;
//loose the log2(sizeof(U)) most significant bits
u<<=shift_bits;
//count bytes
unsigned count_bytes=bytes-1;
while (count_bytes && (u & U(0xFF)<<((bytes-1)*8))==0)
{
u<<=8;
--count_bytes;
}
//reconstruct shifted value, add count bytes in front
u=(U(count_bytes)<<(bits-shift_bits)) | (u>>shift_bits);
//serialize most significant byte first
u=byte_swap(u);
do
{
*it_out=(unsigned char)u ^ 0xFF;//explicit truncation, inversion
++it_out;
u>>=8;
}
while (count_bytes--);
return it_out;
}
template<typename IT, typename U>
inline IT unserialize_unsigned_inverted(IT cit_in, U & ru)
{
//IT is meant to be a byte string iterator
//U must be unsigned 16, 32 or 64 bits
unsigned const shift_bits=serializable_type<U>::log2;
U u=(unsigned char)*cit_in ^ 0xFF;//restore inversion
++cit_in;
unsigned count_bytes=unsigned(u>>(8-shift_bits));
u&=(0xFF>>shift_bits);
while (count_bytes)
{
u<<=8;
u|=(unsigned char)*cit_in ^ 0xFF;//restore inversion
++cit_in;
--count_bytes;
}
ru=u;
return cit_in;
} |
Partager