Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
void
write_varint32(struct tbuf *b, u32 value)
{
tbuf_ensure(b, 5);
if (value >= (1 << 7)) {
if (value >= (1 << 14)) {
if (value >= (1 << 21)) {
if (value >= (1 << 28))
append_byte(b, (u8)(value >> 28) | 0x80);
append_byte(b, (u8)(value >> 21) | 0x80);
}
append_byte(b, (u8)((value >> 14) | 0x80));
}
append_byte(b, (u8)((value >> 7) | 0x80));
}
append_byte(b, (u8)((value) & 0x7F));
}
u32
read_varint32(struct tbuf *buf)
{
u8 *b = buf->data;
int size = buf->size;
if (size < 1) {
tnt_raise(IllegalParams, :"packet too short (expected 1 byte)");
}
if (!(b[0] & 0x80)) {
buf->data += 1;
buf->capacity -= 1;
buf->size -= 1;
return (b[0] & 0x7f);
}
if (size < 2)
tnt_raise(IllegalParams, :"packet too short (expected 2 bytes)");
if (!(b[1] & 0x80)) {
buf->data += 2;
buf->capacity -= 2;
buf->size -= 2;
return (b[0] & 0x7f) << 7 | (b[1] & 0x7f);
}
if (size < 3)
tnt_raise(IllegalParams, :"packet too short (expected 3 bytes)");
if (!(b[2] & 0x80)) {
buf->data += 3;
buf->capacity -= 3;
buf->size -= 3;
return (b[0] & 0x7f) << 14 | (b[1] & 0x7f) << 7 | (b[2] & 0x7f);
}
if (size < 4)
tnt_raise(IllegalParams, :"packet too short (expected 4 bytes)");
if (!(b[3] & 0x80)) {
buf->data += 4;
buf->capacity -= 4;
buf->size -= 4;
return (b[0] & 0x7f) << 21 | (b[1] & 0x7f) << 14 |
(b[2] & 0x7f) << 7 | (b[3] & 0x7f);
}
if (size < 5)
tnt_raise(IllegalParams, :"packet too short (expected 5 bytes)");
if (!(b[4] & 0x80)) {
buf->data += 5;
buf->capacity -= 5;
buf->size -= 5;
return (b[0] & 0x7f) << 28 | (b[1] & 0x7f) << 21 |
(b[2] & 0x7f) << 14 | (b[3] & 0x7f) << 7 | (b[4] & 0x7f);
}
tnt_raise(IllegalParams, :"incorrect BER format");
return 0;
}
It is ‘‘little endian’’ only in the sense that it avoids using space to represent the
‘‘big’’ end of an unsigned integer, when the big end is all zeroes or sign extension bits).
DW_FORM_udata (unsigned LEB128) numbers are encoded as follows: start at the low order
end of an unsigned integer and chop it into 7-bit chunks. Place each chunk into the low order 7
bits of a byte. Typically, several of the high order bytes will be zero; discard them. Emit the
remaining bytes in a stream, starting with the low order byte; set the high order bit on each byte
except the last emitted byte. The high bit of zero on the last byte indicates to the decoder that it
has encountered the last byte.
Tarantool Данные и Протокол