From 49abdbd0092134ab984bddac65a2d6bc4b5f07fd Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Jun 2007 09:38:51 +0200 Subject: [PATCH] fix Append and optimize GetUleb128Size --- src/common/packet-history.cc | 59 ++++++++++++++++++++++++------------ src/common/packet-history.h | 2 +- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/common/packet-history.cc b/src/common/packet-history.cc index 283c1e433..16dd5c7d0 100644 --- a/src/common/packet-history.cc +++ b/src/common/packet-history.cc @@ -310,17 +310,6 @@ PacketHistory::PrintStats (void) g_three = 0; g_four = 0; g_five = 0; -#if 0 - PacketHistory h (0, 0); - for (uint32_t i = 0; i < 0xffffff; i++) - { - if (h.GetUleb128Size (i) == 4) - { - std::cout << i << std::endl; - break; - } - } -#endif } void @@ -375,13 +364,23 @@ PacketHistory::Reserve (uint32_t size) uint32_t PacketHistory::GetUleb128Size (uint32_t value) const { - uint32_t n = 0; - uint32_t tmp = value; - do { - tmp >>= 7; - n++; - } while (tmp != 0); - return n; + if (value < 0x80) + { + return 1; + } + if (value < 0x4000) + { + return 2; + } + if (value < 0x200000) + { + return 3; + } + if (value < 0x10000000) + { + return 4; + } + return 5; } uint32_t PacketHistory::ReadUleb128 (const uint8_t **pBuffer) const @@ -484,25 +483,47 @@ PacketHistory::TryToAppend (uint32_t value, uint8_t **pBuffer, uint8_t *end) uint8_t byte = value & (~0x80); start[0] = 0x80 | byte; value >>= 7; + byte = value & (~0x80); start[1] = 0x80 | byte; value >>= 7; + byte = value & (~0x80); start[2] = value; *pBuffer = start + 3; return true; } - if (start + 3 < end) + if (value < 0x10000000 && start + 3 < end) { uint8_t byte = value & (~0x80); start[0] = 0x80 | byte; value >>= 7; + byte = value & (~0x80); start[1] = 0x80 | byte; value >>= 7; + byte = value & (~0x80); start[2] = 0x80 | byte; value >>= 7; start[3] = value; *pBuffer = start + 4; return true; } + if (start + 4 < end) + { + uint8_t byte = value & (~0x80); + start[0] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + start[1] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + start[2] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + start[3] = 0x80 | byte; + value >>= 7; + start[4] = value; + *pBuffer = start + 5; + return true; + } return false; } diff --git a/src/common/packet-history.h b/src/common/packet-history.h index 93e2d8099..2db574aed 100644 --- a/src/common/packet-history.h +++ b/src/common/packet-history.h @@ -107,7 +107,7 @@ private: uint32_t available); inline void UpdateHead (uint16_t written); inline void UpdateTail (uint16_t written); - uint32_t GetUleb128Size (uint32_t value) const; + inline uint32_t GetUleb128Size (uint32_t value) const; uint32_t ReadUleb128 (const uint8_t **pBuffer) const; inline void Append16 (uint16_t value, uint8_t *buffer); inline bool TryToAppend (uint32_t value, uint8_t **pBuffer, uint8_t *end);