diff --git a/src/apps/debugger/util/BitBuffer.cpp b/src/apps/debugger/util/BitBuffer.cpp index 15c8d1d699..5ba0b135c9 100644 --- a/src/apps/debugger/util/BitBuffer.cpp +++ b/src/apps/debugger/util/BitBuffer.cpp @@ -138,3 +138,38 @@ BitBuffer::AddBits(const void* _data, uint64 bitSize, uint32 bitOffset) return true; } + + +bool +BitBuffer::AddZeroBits(uint64 bitSize) +{ + if (bitSize == 0) + return true; + + // handle special case first: no more bits than missing + size_t oldSize = fBytes.Size(); + if (fMissingBits > 0 && bitSize <= fMissingBits) { + fMissingBits -= bitSize; + return true; + } + + // resize the buffer + if (!fBytes.AddUninitialized((bitSize - fMissingBits + 7) / 8)) + return false; + + // fill in missing bits + if (fMissingBits > 0) { + bitSize -= fMissingBits; + fMissingBits = 0; + } + + // zero the remaining bytes, including a potentially partial last byte + uint8* buffer = fBytes.Elements() + oldSize; + memset(buffer, 0, (bitSize + 7) / 8); + bitSize %= 8; + + if (bitSize > 0) + fMissingBits = 8 - bitSize; + + return true; +} diff --git a/src/apps/debugger/util/BitBuffer.h b/src/apps/debugger/util/BitBuffer.h index 59e9455df5..6c852c0a9a 100644 --- a/src/apps/debugger/util/BitBuffer.h +++ b/src/apps/debugger/util/BitBuffer.h @@ -20,6 +20,7 @@ public: bool AddBytes(const void* data, size_t size); bool AddBits(const void* data, uint64 bitSize, uint32 bitOffset = 0); + bool AddZeroBits(uint64 bitSize); uint8* Bytes() const { return fBytes.Elements(); } size_t Size() const { return fBytes.Size(); }