diff --git a/connections/implementation/ble_advertisement.cc b/connections/implementation/ble_advertisement.cc index 4e2d93ef..4d9d2293 100644 --- a/connections/implementation/ble_advertisement.cc +++ b/connections/implementation/ble_advertisement.cc @@ -115,7 +115,7 @@ absl::StatusOr BleAdvertisement::CreateBleAdvertisement( } ByteArray advertisement_bytes{ble_advertisement_bytes}; - StreamReader stream_reader{advertisement_bytes}; + StreamReader stream_reader{&advertisement_bytes}; // The first 1 byte is supposed to be the version and pcp. auto version_and_pcp_byte = stream_reader.ReadUint8(); if (!version_and_pcp_byte.has_value()) { diff --git a/connections/implementation/bluetooth_device_name.cc b/connections/implementation/bluetooth_device_name.cc index 47becf48..daaed17b 100644 --- a/connections/implementation/bluetooth_device_name.cc +++ b/connections/implementation/bluetooth_device_name.cc @@ -75,7 +75,7 @@ BluetoothDeviceName::BluetoothDeviceName( return; } - StreamReader stream_reader{bluetooth_device_name_bytes}; + StreamReader stream_reader{&bluetooth_device_name_bytes}; // The first 1 byte is supposed to be the version and pcp. auto version_and_pcp_byte = stream_reader.ReadUint8(); if (!version_and_pcp_byte.has_value()) { diff --git a/connections/implementation/mediums/advertisements/advertisement_util.cc b/connections/implementation/mediums/advertisements/advertisement_util.cc index c85a07ca..1363aa04 100644 --- a/connections/implementation/mediums/advertisements/advertisement_util.cc +++ b/connections/implementation/mediums/advertisements/advertisement_util.cc @@ -32,7 +32,7 @@ constexpr absl::string_view kFakeEncryptionKey = // Should always match the protocol implementation to read device name. // LINT.IfChange std::optional ReadDeviceName(const ByteArray& endpoint_info) { - StreamReader reader(endpoint_info); + StreamReader reader(&endpoint_info); std::optional version = reader.ReadBits(3); if (!version.has_value() || *version > 1) { return std::nullopt; diff --git a/connections/implementation/mediums/advertisements/data_element.cc b/connections/implementation/mediums/advertisements/data_element.cc index 9aa9ac66..c18464a7 100644 --- a/connections/implementation/mediums/advertisements/data_element.cc +++ b/connections/implementation/mediums/advertisements/data_element.cc @@ -99,7 +99,7 @@ std::optional DataElement::FromStreamReader(StreamReader& reader) { std::optional DataElement::FromData(const std::string& data) { ByteArray bytes(data); - StreamReader reader(bytes); + StreamReader reader(&bytes); return FromStreamReader(reader); } diff --git a/connections/implementation/mediums/advertisements/data_element_test.cc b/connections/implementation/mediums/advertisements/data_element_test.cc index ca56d46c..eba31811 100644 --- a/connections/implementation/mediums/advertisements/data_element_test.cc +++ b/connections/implementation/mediums/advertisements/data_element_test.cc @@ -48,7 +48,7 @@ TEST(DataElementTest, FromInvalidLength) { TEST(DataElementTest, OneByteFromStreamReader) { ByteArray bytes("\x21\x01\x02"); - StreamReader reader(bytes); + StreamReader reader(&bytes); std::optional data_element = DataElement::FromStreamReader(reader); ASSERT_TRUE(data_element.has_value()); @@ -59,7 +59,7 @@ TEST(DataElementTest, OneByteFromStreamReader) { TEST(DataElementTest, TwoByteFromStreamReader) { ByteArray bytes("\x84\x01\x01\x02\x03\x04"); - StreamReader reader(bytes); + StreamReader reader(&bytes); std::optional data_element = DataElement::FromStreamReader(reader); ASSERT_TRUE(data_element.has_value()); diff --git a/connections/implementation/mediums/advertisements/dct_advertisement.cc b/connections/implementation/mediums/advertisements/dct_advertisement.cc index 77cbf0ed..59f527dc 100644 --- a/connections/implementation/mediums/advertisements/dct_advertisement.cc +++ b/connections/implementation/mediums/advertisements/dct_advertisement.cc @@ -79,7 +79,7 @@ std::optional DctAdvertisement::Create( std::optional DctAdvertisement::Parse( const std::string& advertisement) { ByteArray data(advertisement); - StreamReader reader(data); + StreamReader reader(&data); DctAdvertisement dct_advertisement; std::optional header = reader.ReadUint8(); @@ -118,7 +118,8 @@ std::optional DctAdvertisement::Parse( return std::nullopt; } - StreamReader psm_reader(ByteArray(psm->value())); + ByteArray psm_bytes(psm->value()); + StreamReader psm_reader(&psm_bytes); dct_advertisement.psm_ = psm_reader.ReadUint16().value_or(0); // Read device information diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement.cc b/connections/implementation/mediums/ble_v2/ble_advertisement.cc index c024411b..40a037b0 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement.cc +++ b/connections/implementation/mediums/ble_v2/ble_advertisement.cc @@ -100,7 +100,7 @@ absl::StatusOr BleAdvertisement::CreateBleAdvertisement( } ByteArray advertisement_bytes(ble_advertisement_bytes); - StreamReader stream_reader(advertisement_bytes); + StreamReader stream_reader(&advertisement_bytes); // The first 1 byte is supposed to be the version, socket version and the fast // advertisement flag. auto version_byte = stream_reader.ReadUint8(); @@ -308,7 +308,7 @@ BleAdvertisement::BleExtraFields::BleExtraFields( } ByteArray mutated_extra_fields_bytes = {ble_extra_fields_bytes}; - StreamReader stream_reader{mutated_extra_fields_bytes}; + StreamReader stream_reader{&mutated_extra_fields_bytes}; // The first 1 byte is field mask. auto mask_byte = stream_reader.ReadUint8().value_or(0); if (!mask_byte) { diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc index b8821635..be4fa4eb 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc +++ b/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc @@ -86,7 +86,7 @@ BleAdvertisementHeader::BleAdvertisementHeader( return; } - StreamReader stream_reader(advertisement_header_bytes); + StreamReader stream_reader(&advertisement_header_bytes); // The first 1 byte is supposed to be the version and number of slots. auto version_and_num_slots_byte = stream_reader.ReadUint8(); if (!version_and_num_slots_byte.has_value()) { diff --git a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc b/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc index 016ec257..4647ac22 100644 --- a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc +++ b/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc @@ -51,7 +51,7 @@ absl::StatusOr BleL2capPacket::CreateFromBytes( "Cannot deserialize BleL2capPacket: input bytes too short."); } - StreamReader stream_reader(bytes); + StreamReader stream_reader(&bytes); // The first 1 byte is the command. auto command_byte = stream_reader.ReadUint8(); if (!command_byte.has_value()) { diff --git a/connections/implementation/mediums/ble_v2/ble_packet.cc b/connections/implementation/mediums/ble_v2/ble_packet.cc index 244b2c8e..bef94669 100644 --- a/connections/implementation/mediums/ble_v2/ble_packet.cc +++ b/connections/implementation/mediums/ble_v2/ble_packet.cc @@ -137,7 +137,7 @@ BlePacket::BlePacket(const ByteArray& ble_packet_bytes) { } ByteArray packet_bytes(ble_packet_bytes); - StreamReader stream_reader{packet_bytes}; + StreamReader stream_reader{&packet_bytes}; // The first 3 bytes are supposed to be the service_id_hash. auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength); if (!service_id_hash_bytes.has_value()) { diff --git a/connections/implementation/wifi_lan_service_info.cc b/connections/implementation/wifi_lan_service_info.cc index 9938e01d..481bd0a1 100644 --- a/connections/implementation/wifi_lan_service_info.cc +++ b/connections/implementation/wifi_lan_service_info.cc @@ -96,7 +96,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) { return; } - StreamReader stream_reader{service_info_bytes}; + StreamReader stream_reader{&service_info_bytes}; // The first 1 byte is supposed to be the version and pcp. auto version_and_pcp_byte = stream_reader.ReadUint8(); if (!version_and_pcp_byte.has_value()) { diff --git a/internal/platform/byte_utils.cc b/internal/platform/byte_utils.cc index 3c261c24..d3bc4230 100644 --- a/internal/platform/byte_utils.cc +++ b/internal/platform/byte_utils.cc @@ -27,7 +27,7 @@ std::string ByteUtils::ToFourDigitString(ByteArray& bytes) { int multiplier = 1; int hashCode = 0; - StreamReader stream_reader{bytes}; + StreamReader stream_reader{&bytes}; while (stream_reader.IsAvailable(1)) { auto byte = stream_reader.ReadInt8().value_or(0); hashCode = (hashCode + byte * multiplier) % kHashBasePrime; diff --git a/internal/platform/stream_reader.cc b/internal/platform/stream_reader.cc index f6bf4741..b987fc65 100644 --- a/internal/platform/stream_reader.cc +++ b/internal/platform/stream_reader.cc @@ -31,7 +31,7 @@ std::optional StreamReader::ReadBits(int bits) { if (!IsAvailable(1)) { return std::nullopt; } - bits_buffer_ = (uint8_t)buffer_.data()[position_++]; + bits_buffer_ = (uint8_t)buffer_->data()[position_++]; bits_unused_ = 8; } if (bits_unused_ < bits) { @@ -177,7 +177,7 @@ ExceptionOr StreamReader::Read(std::int64_t size) { } ByteArray read_bytes{static_cast(size)}; - if (read_bytes.CopyAt(/*offset=*/0, buffer_, + if (read_bytes.CopyAt(/*offset=*/0, *buffer_, /*source_offset=*/position_)) { position_ += size; return ExceptionOr{read_bytes}; diff --git a/internal/platform/stream_reader.h b/internal/platform/stream_reader.h index ecd6b6f5..d2f215e6 100644 --- a/internal/platform/stream_reader.h +++ b/internal/platform/stream_reader.h @@ -27,7 +27,7 @@ namespace nearby { // A base {@link InputStream } for reading the contents of a byte array. class StreamReader { public: - explicit StreamReader(const ByteArray &buffer) : buffer_{buffer} {} + explicit StreamReader(const ByteArray *buffer) : buffer_{buffer} {} StreamReader(const StreamReader &) = delete; StreamReader &operator=(const StreamReader &) = delete; ~StreamReader() = default; @@ -46,7 +46,7 @@ class StreamReader { std::optional ReadBytes(int size); bool IsAvailable(int size) const { - return buffer_.size() - position_ >= size; + return buffer_ != nullptr && (buffer_->size() - position_ >= size); } private: @@ -54,7 +54,7 @@ class StreamReader { uint8_t bits_unused_{0}; uint8_t bits_buffer_{0}; - const ByteArray &buffer_; + const ByteArray *buffer_ = nullptr; size_t position_{0}; }; diff --git a/internal/platform/stream_reader_test.cc b/internal/platform/stream_reader_test.cc index ef3016c7..d06afeef 100644 --- a/internal/platform/stream_reader_test.cc +++ b/internal/platform/stream_reader_test.cc @@ -25,7 +25,7 @@ namespace { TEST(StreamReaderTest, ReadBits) { std::string data{static_cast(0b01011100)}; ByteArray byte_array(data); - StreamReader stream{byte_array}; + StreamReader stream{&byte_array}; EXPECT_EQ(stream.ReadBits(1), 0); EXPECT_EQ(stream.ReadBits(2), 2); EXPECT_EQ(stream.ReadBits(3), 7); @@ -37,7 +37,7 @@ TEST(StreamReaderTest, ReadBits) { TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) { std::string data = "ab"; ByteArray byte_array(data); - StreamReader stream{byte_array}; + StreamReader stream{&byte_array}; EXPECT_FALSE(stream.ReadBits(9).has_value()); EXPECT_EQ(stream.ReadBits(1), 0); EXPECT_FALSE(stream.ReadInt16().has_value()); @@ -46,7 +46,7 @@ TEST(StreamReaderTest, ReadBitsExceedsByteBoundary) { TEST(StreamReaderTest, ReadUintValues) { std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06"; ByteArray byte_array(data); - StreamReader stream{byte_array}; + StreamReader stream{&byte_array}; EXPECT_EQ(stream.ReadUint16(), 0xfff1); EXPECT_EQ(stream.ReadUint32(), 0x0f0e0102); EXPECT_EQ(stream.ReadUint64(), 0x0101010203040506); @@ -55,7 +55,7 @@ TEST(StreamReaderTest, ReadUintValues) { TEST(StreamReaderTest, ReadIntValues) { std::string data = "\xff\xf1\x0f\x0e\x01\x02\x01\x01\x01\x02\x03\x04\x05\x06"; ByteArray byte_array(data); - StreamReader stream{byte_array}; + StreamReader stream{&byte_array}; EXPECT_EQ(stream.ReadInt16(), static_cast(0xfff1)); EXPECT_EQ(stream.ReadInt32(), static_cast(0x0f0e0102)); EXPECT_EQ(stream.ReadInt64(), static_cast(0x0101010203040506));