Skip to content

Commit

Permalink
special treatment for the last byte
Browse files Browse the repository at this point in the history
we need that to reach full 64 bits, otherwise
we are short exactly one bit.

and in the 9th bit we already know we're going
to stop reading anyway.
  • Loading branch information
timo committed Jan 26, 2014
1 parent eb8dc90 commit 6bb4d26
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/6model/serialization.c
Expand Up @@ -212,11 +212,14 @@ static size_t write_varint128(char *buffer, size_t offset, int64_t value) {
size_t position;
size_t needed_bytes = varintsize(value);

for (position = 0; position < needed_bytes; position++) {
for (position = 0; position < needed_bytes && position != 8; position++) {
buffer[offset + position] = value & 0x7f;
if (position != needed_bytes - 1) buffer[offset + position] = buffer[offset + position] | 0x80;
value = value >> 7;
}
if (position == 8) {
buffer[offset + position] = value;
}
return needed_bytes;
}

Expand Down Expand Up @@ -1197,7 +1200,7 @@ static size_t read_varint128(char *buffer, size_t offset, int64_t *value) {
int64_t negation_mask = 0;
*value = 0;
int read_on = !!(buffer[offset] & 0x80) + 1;
while (read_on) {
while (read_on && inner_offset != 8) {
*value = *value | ((buffer[offset + inner_offset] & 0x7f) << shift_amount);
negation_mask = negation_mask | (0b1111111 << shift_amount);
if (read_on == 1 && buffer[offset + inner_offset] & 0x80) {
Expand All @@ -1207,6 +1210,12 @@ static size_t read_varint128(char *buffer, size_t offset, int64_t *value) {
inner_offset++;
shift_amount += 7;
}
// our last byte will be a full byte, so that we reach the full 64 bits
if (inner_offset == 8) {
shift_amount += 1;
*value = *value | (buffer[offset + inner_offset] << shift_amount);
negation_mask = negation_mask | (0b11111111 << shift_amount);
}
negation_mask = negation_mask >> 1;
// do we have a negative number so far?
if (*value & ~negation_mask) {
Expand Down

0 comments on commit 6bb4d26

Please sign in to comment.