diff --git a/src/xenia/base/bit_stream.cc b/src/xenia/base/bit_stream.cc index ce74937dd..98a53af15 100644 --- a/src/xenia/base/bit_stream.cc +++ b/src/xenia/base/bit_stream.cc @@ -2,7 +2,7 @@ ****************************************************************************** * Xenia : Xbox 360 Emulator Research Project * ****************************************************************************** - * Copyright 2015 Ben Vanik. All rights reserved. * + * Copyright 2021 Ben Vanik. All rights reserved. * * Released under the BSD license - see LICENSE in the root for more details. * ****************************************************************************** */ @@ -111,6 +111,8 @@ size_t BitStream::Copy(uint8_t* dest_buffer, size_t num_bits) { // First: Copy the first few bits up to a byte boundary. if (rel_offset_bits) { uint64_t bits = Peek(8 - rel_offset_bits); + uint8_t clear_mask = ~((uint8_t(1) << rel_offset_bits) - 1); + dest_buffer[out_offset_bytes] &= clear_mask; dest_buffer[out_offset_bytes] |= (uint8_t)bits; bits_left -= 8 - rel_offset_bits; @@ -132,6 +134,8 @@ size_t BitStream::Copy(uint8_t* dest_buffer, size_t num_bits) { uint64_t bits = Peek(bits_left); bits <<= 8 - bits_left; + uint8_t clear_mask = ((uint8_t(1) << bits_left) - 1); + dest_buffer[out_offset_bytes] &= clear_mask; dest_buffer[out_offset_bytes] |= (uint8_t)bits; Advance(bits_left); }