nghttp2_hd: Refactor a bit

This commit is contained in:
Tatsuhiro Tsujikawa 2014-11-21 01:59:20 +09:00
parent ee65dea8af
commit 4ee89e62fc
1 changed files with 18 additions and 28 deletions

View File

@ -436,51 +436,41 @@ static size_t count_encoded_length(size_t n, size_t prefix)
{ {
size_t k = (1 << prefix) - 1; size_t k = (1 << prefix) - 1;
size_t len = 0; size_t len = 0;
if(n >= k) {
n -= k; if(n < k) {
++len;
} else {
return 1; return 1;
} }
do {
n -= k;
++len; ++len;
if(n >= 128) {
n >>= 7; for(; n >= 128; n >>= 7, ++len);
} else {
break; return len + 1;
}
} while(n);
return len;
} }
static size_t encode_length(uint8_t *buf, size_t n, size_t prefix) static size_t encode_length(uint8_t *buf, size_t n, size_t prefix)
{ {
size_t k = (1 << prefix) - 1; size_t k = (1 << prefix) - 1;
size_t len = 0; uint8_t *begin = buf;
*buf &= ~k; *buf &= ~k;
if(n < k) { if(n < k) {
*buf++ |= n; *buf |= n;
return 1; return 1;
} }
*buf++ |= k; *buf++ |= k;
n -= k; n -= k;
++len;
do { for(; n >= 128; n >>= 7) {
++len;
if(n >= 128) {
*buf++ = (1 << 7) | (n & 0x7f); *buf++ = (1 << 7) | (n & 0x7f);
n >>= 7;
} else {
*buf++ = (uint8_t)n;
break;
} }
} while(n);
return len; *buf++ = (uint8_t)n;
return (size_t)(buf - begin);
} }
/* /*