From 499631c1e4ad2672a333898e652d905c372793a1 Mon Sep 17 00:00:00 2001 From: "sprang@webrtc.org" Date: Tue, 3 Dec 2013 13:22:48 +0000 Subject: [PATCH] Utility class for reading/writing network-byte-ordered integers. BUG= R=holmer@google.com, mflodman@webrtc.org, stefan@webrtc.org Review URL: https://webrtc-codereview.appspot.com/2151008 git-svn-id: http://webrtc.googlecode.com/svn/trunk@5203 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/modules/modules.gyp | 1 + webrtc/modules/rtp_rtcp/source/byte_io.h | 238 ++++++++++++++++++ .../rtp_rtcp/source/byte_io_unittest.cc | 210 ++++++++++++++++ webrtc/modules/rtp_rtcp/source/rtp_rtcp.gypi | 1 + 4 files changed, 450 insertions(+) create mode 100644 webrtc/modules/rtp_rtcp/source/byte_io.h create mode 100644 webrtc/modules/rtp_rtcp/source/byte_io_unittest.cc diff --git a/webrtc/modules/modules.gyp b/webrtc/modules/modules.gyp index 5c2f77f99d..579ae149fb 100644 --- a/webrtc/modules/modules.gyp +++ b/webrtc/modules/modules.gyp @@ -187,6 +187,7 @@ 'remote_bitrate_estimator/test/bwe_test.cc', 'remote_bitrate_estimator/test/bwe_test.h', 'rtp_rtcp/source/mock/mock_rtp_payload_strategy.h', + 'rtp_rtcp/source/byte_io_unittest.cc', 'rtp_rtcp/source/fec_receiver_unittest.cc', 'rtp_rtcp/source/fec_test_helper.cc', 'rtp_rtcp/source/fec_test_helper.h', diff --git a/webrtc/modules/rtp_rtcp/source/byte_io.h b/webrtc/modules/rtp_rtcp/source/byte_io.h new file mode 100644 index 0000000000..646f1eb55d --- /dev/null +++ b/webrtc/modules/rtp_rtcp/source/byte_io.h @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_ +#define WEBRTC_MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_ + + +// This file contains classes for reading and writing integer types from/to +// byte array representations. Signed/unsigned, partial (whole byte) sizes, +// and big/little endian byte order is all supported. +// +// Usage examples: +// +// uint8_t* buffer = ...; +// +// // Read an unsigned 4 byte integer in big endian format +// uint32_t val = ByteReader::ReadBigEndian(buffer); +// +// // Read a signed 24-bit (3 byte) integer in little endian format +// int32_t val = ByteReader::ReadLittle(buffer); +// +// // Write an unsigned 8 byte integer in little endian format +// ByteWriter::WriteLittleEndian(buffer, val); +// +// Write an unsigned 40-bit (5 byte) integer in big endian format +// ByteWriter::WriteBigEndian(buffer, val); +// +// These classes are implemented as recursive templetizations, inteded to make +// it easy for the compiler to completely inline the reading/writing. + + +#include + +#include "webrtc/typedefs.h" + +namespace webrtc { + +// Class for reading integers from a sequence of bytes. +// T = type of integer, B = bytes to read, is_signed = true if signed integer +// If is_signed is true and B < sizeof(T), sign extension might be needed +template::is_signed> +class ByteReader { + public: + static T ReadBigEndian(uint8_t* data) { + if (is_signed && B < sizeof(T)) { + return SignExtend(InternalReadBigEndian(data)); + } + return InternalReadBigEndian(data); + } + + static T ReadLittleEndian(uint8_t* data) { + if (is_signed && B < sizeof(T)) { + return SignExtend(InternalReadLittleEndian(data)); + } + return InternalReadLittleEndian(data); + } + + private: + static T InternalReadBigEndian(uint8_t* data) { + T val(0); + for (unsigned int i = 0; i < B; ++i) { + val |= static_cast(data[i]) << ((B - 1 - i) * 8); + } + return val; + } + + static T InternalReadLittleEndian(uint8_t* data) { + T val(0); + for (unsigned int i = 0; i < B; ++i) { + val |= static_cast(data[i]) << (i * 8); + } + return val; + } + + // If number of bytes is less than native data type (eg 24 bit, in int32_t), + // and the most significant bit of the actual data is set, we must sign + // extend the remaining byte(s) with ones so that the correct negative + // number is retained. + // Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B + static T SignExtend(T val) { + uint8_t msb = static_cast(val >> ((B - 1) * 8)); + if (msb & 0x80) { + // Sign extension is -1 (all ones) shifted left B bytes. + // The "B % sizeof(T)"-part is there to avoid compiler warning for + // shifting the whole size of the data type. + T sign_extend = (sizeof(T) == B ? 0 : + (static_cast(-1L) << ((B % sizeof(T)) * 8))); + + return val | sign_extend; + } + return val; + } +}; + +// Class for writing integers to a sequence of bytes +// T = type of integer, B = bytes to write +template +class ByteWriter { + public: + static void WriteBigEndian(uint8_t* data, T val) { + for (unsigned int i = 0; i < B; ++i) { + data[i] = val >> ((B - 1 - i) * 8); + } + } + + static void WriteLittleEndian(uint8_t* data, T val) { + for (unsigned int i = 0; i < B; ++i) { + data[i] = val >> (i * 8); + } + } +}; + + +// -------- Below follows specializations for B in { 2, 4, 8 } -------- + + +// Specializations for two byte words +template +class ByteReader { + public: + static T ReadBigEndian(uint8_t* data) { + return (data[0] << 8) | data[1]; + } + + static T ReadLittleEndian(uint8_t* data) { + return data[0] | (data[1] << 8); + } +}; + +template +class ByteWriter { + public: + static void WriteBigEndian(uint8_t* data, T val) { + data[0] = val >> 8; + data[1] = val; + } + + static void WriteLittleEndian(uint8_t* data, T val) { + data[0] = val; + data[1] = val >> 8; + } +}; + +// Specializations for four byte words. +template +class ByteReader { + public: + static T ReadBigEndian(uint8_t* data) { + return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + } + + static T ReadLittleEndian(uint8_t* data) { + return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); + } +}; + +// Specializations for four byte words. +template +class ByteWriter { + public: + static void WriteBigEndian(uint8_t* data, T val) { + data[0] = val >> 24; + data[1] = val >> 16; + data[2] = val >> 8; + data[3] = val; + } + + static void WriteLittleEndian(uint8_t* data, T val) { + data[0] = val; + data[1] = val >> 8; + data[2] = val >> 16; + data[3] = val >> 24; + } +}; + +// Specializations for eight byte words. +template +class ByteReader { + public: + static T ReadBigEndian(uint8_t* data) { + return + (Get(data, 0) << 56) | (Get(data, 1) << 48) | + (Get(data, 2) << 40) | (Get(data, 3) << 32) | + (Get(data, 4) << 24) | (Get(data, 5) << 16) | + (Get(data, 6) << 8) | Get(data, 7); + } + + static T ReadLittleEndian(uint8_t* data) { + return + Get(data, 0) | (Get(data, 1) << 8) | + (Get(data, 2) << 16) | (Get(data, 3) << 24) | + (Get(data, 4) << 32) | (Get(data, 5) << 40) | + (Get(data, 6) << 48) | (Get(data, 7) << 56); + } + + private: + inline static T Get(uint8_t* data, unsigned int index) { + return static_cast(data[index]); + } +}; + +template +class ByteWriter { + public: + static void WriteBigEndian(uint8_t* data, T val) { + data[0] = val >> 56; + data[1] = val >> 48; + data[2] = val >> 40; + data[3] = val >> 32; + data[4] = val >> 24; + data[5] = val >> 16; + data[6] = val >> 8; + data[7] = val; + } + + static void WriteLittleEndian(uint8_t* data, T val) { + data[0] = val; + data[1] = val >> 8; + data[2] = val >> 16; + data[3] = val >> 24; + data[4] = val >> 32; + data[5] = val >> 40; + data[6] = val >> 48; + data[7] = val >> 56; + } +}; + +} // namespace webrtc + +#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_ diff --git a/webrtc/modules/rtp_rtcp/source/byte_io_unittest.cc b/webrtc/modules/rtp_rtcp/source/byte_io_unittest.cc new file mode 100644 index 0000000000..5b7010994e --- /dev/null +++ b/webrtc/modules/rtp_rtcp/source/byte_io_unittest.cc @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include + +#include "testing/gtest/include/gtest/gtest.h" +#include "webrtc/modules/rtp_rtcp/source/byte_io.h" + +namespace webrtc { +namespace { + +class ByteIoTest : public ::testing::Test { + protected: + ByteIoTest() {} + virtual ~ByteIoTest() {} + + enum { kAlignments = sizeof(uint64_t) - 1 }; + + // Method to create a test value that is not the same when byte reversed. + template + T CreateTestValue(bool negative, uint8_t num_bytes) { + T val = 0; + for (uint8_t i = 0; i != num_bytes; ++i) { + val = (val << 8) + (negative ? (0xFF - i) : (i + 1)); + } + if (negative && std::numeric_limits::is_signed) { + val |= static_cast(-1) << (8 * num_bytes); + } + return val; + } + + // Populate byte buffer with value, in big endian format. + template + void PopulateTestData(uint8_t* data, T value, int num_bytes, bool bigendian) { + if (bigendian) { + for (int i = 0; i < num_bytes; ++i) { + data[i] = (value >> ((num_bytes - i - 1) * 8)) & 0xFF; + } + } else { + for (int i = 0; i < num_bytes; ++i) { + data[i] = (value >> (i * 8)) & 0xFF; + } + } + } + + // Test reading big endian numbers. + // Template arguments: Type T, read method RM(buffer), B bytes of data. + template + void TestRead(bool big_endian) { + // Test both for values that are positive and negative (if signed) + for (int neg = 0; neg < 2; ++neg) { + bool negative = neg > 0; + + // Write test value to byte buffer, in big endian format. + T test_value = CreateTestValue(negative, B); + uint8_t bytes[B + kAlignments]; + + // Make one test for each alignment. + for (int i = 0; i < kAlignments; ++i) { + PopulateTestData(bytes + i, test_value, B, big_endian); + + // Check that test value is retrieved from buffer when used read method. + EXPECT_EQ(test_value, RM(bytes + i)); + } + } + } + + // Test writing big endian numbers. + // Template arguments: Type T, write method WM(buffer, value), B bytes of data + template + void TestWrite(bool big_endian) { + // Test both for values that are positive and negative (if signed). + for (int neg = 0; neg < 2; ++neg) { + bool negative = neg > 0; + + // Write test value to byte buffer, in big endian format. + T test_value = CreateTestValue(negative, B); + uint8_t expected_bytes[B + kAlignments]; + uint8_t bytes[B + kAlignments]; + + // Make one test for each alignment. + for (int i = 0; i < kAlignments; ++i) { + PopulateTestData(expected_bytes + i, test_value, B, big_endian); + + // Zero initialize buffer and let WM populate it. + memset(bytes, 0, B + kAlignments); + WM(bytes + i, test_value); + + // Check that data produced by WM is big endian as expected. + for (int j = 0; j < B; ++j) { + EXPECT_EQ(expected_bytes[i + j], bytes[i + j]); + } + } + } + } +}; + +TEST_F(ByteIoTest, Test16UBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(uint16_t)>(true); + TestWrite::WriteBigEndian, + sizeof(uint16_t)>(true); +} + +TEST_F(ByteIoTest, Test24UBitBigEndian) { + TestRead::ReadBigEndian, 3>(true); + TestWrite::WriteBigEndian, 3>(true); +} + +TEST_F(ByteIoTest, Test32UBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(uint32_t)>(true); + TestWrite::WriteBigEndian, + sizeof(uint32_t)>(true); +} + +TEST_F(ByteIoTest, Test64UBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(uint64_t)>(true); + TestWrite::WriteBigEndian, + sizeof(uint64_t)>(true); +} + +TEST_F(ByteIoTest, Test16SBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(int16_t)>(true); + TestWrite::WriteBigEndian, + sizeof(int16_t)>(true); +} + +TEST_F(ByteIoTest, Test24SBitBigEndian) { + TestRead::ReadBigEndian, 3>(true); + TestWrite::WriteBigEndian, 3>(true); +} + +TEST_F(ByteIoTest, Test32SBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(int32_t)>(true); + TestWrite::WriteBigEndian, + sizeof(int32_t)>(true); +} + +TEST_F(ByteIoTest, Test64SBitBigEndian) { + TestRead::ReadBigEndian, + sizeof(int64_t)>(true); + TestWrite::WriteBigEndian, + sizeof(int64_t)>(true); +} + +TEST_F(ByteIoTest, Test16UBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(uint16_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(uint16_t)>(false); +} + +TEST_F(ByteIoTest, Test24UBitLittleEndian) { + TestRead::ReadLittleEndian, 3>(false); + TestWrite::WriteLittleEndian, 3>(false); +} + +TEST_F(ByteIoTest, Test32UBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(uint32_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(uint32_t)>(false); +} + +TEST_F(ByteIoTest, Test64UBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(uint64_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(uint64_t)>(false); +} + +TEST_F(ByteIoTest, Test16SBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(int16_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(int16_t)>(false); +} + +TEST_F(ByteIoTest, Test24SBitLittleEndian) { + TestRead::ReadLittleEndian, 3>(false); + TestWrite::WriteLittleEndian, 3>(false); +} + +TEST_F(ByteIoTest, Test32SBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(int32_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(int32_t)>(false); +} + +TEST_F(ByteIoTest, Test64SBitLittleEndian) { + TestRead::ReadLittleEndian, + sizeof(int64_t)>(false); + TestWrite::WriteLittleEndian, + sizeof(int64_t)>(false); +} + +} // namespace +} // namespace webrtc diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp.gypi b/webrtc/modules/rtp_rtcp/source/rtp_rtcp.gypi index bd6886cd2a..070845bc79 100644 --- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp.gypi +++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp.gypi @@ -27,6 +27,7 @@ '../interface/rtp_rtcp_defines.h', 'bitrate.cc', 'bitrate.h', + 'byte_io.h', 'fec_receiver_impl.cc', 'fec_receiver_impl.h', 'receive_statistics_impl.cc',