Make sure ByteReader<T>::Read* is properly constified.

Also, start using it in real code...

BUG=
R=holmer@google.com, pbos@webrtc.org, stefan@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/37809004

Cr-Commit-Position: refs/heads/master@{#8181}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8181 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
sprang@webrtc.org
2015-01-28 12:37:36 +00:00
parent 7aef80c6d1
commit 2a6558c2a5
3 changed files with 15 additions and 15 deletions

View File

@ -49,14 +49,14 @@ template<typename T, unsigned int B = sizeof(T),
bool is_signed = std::numeric_limits<T>::is_signed> bool is_signed = std::numeric_limits<T>::is_signed>
class ByteReader { class ByteReader {
public: public:
static T ReadBigEndian(uint8_t* data) { static T ReadBigEndian(const uint8_t* data) {
if (is_signed && B < sizeof(T)) { if (is_signed && B < sizeof(T)) {
return SignExtend(InternalReadBigEndian(data)); return SignExtend(InternalReadBigEndian(data));
} }
return InternalReadBigEndian(data); return InternalReadBigEndian(data);
} }
static T ReadLittleEndian(uint8_t* data) { static T ReadLittleEndian(const uint8_t* data) {
if (is_signed && B < sizeof(T)) { if (is_signed && B < sizeof(T)) {
return SignExtend(InternalReadLittleEndian(data)); return SignExtend(InternalReadLittleEndian(data));
} }
@ -64,7 +64,7 @@ class ByteReader {
} }
private: private:
static T InternalReadBigEndian(uint8_t* data) { static T InternalReadBigEndian(const uint8_t* data) {
T val(0); T val(0);
for (unsigned int i = 0; i < B; ++i) { for (unsigned int i = 0; i < B; ++i) {
val |= static_cast<T>(data[i]) << ((B - 1 - i) * 8); val |= static_cast<T>(data[i]) << ((B - 1 - i) * 8);
@ -72,7 +72,7 @@ class ByteReader {
return val; return val;
} }
static T InternalReadLittleEndian(uint8_t* data) { static T InternalReadLittleEndian(const uint8_t* data) {
T val(0); T val(0);
for (unsigned int i = 0; i < B; ++i) { for (unsigned int i = 0; i < B; ++i) {
val |= static_cast<T>(data[i]) << (i * 8); val |= static_cast<T>(data[i]) << (i * 8);
@ -85,7 +85,7 @@ class ByteReader {
// extend the remaining byte(s) with ones so that the correct negative // extend the remaining byte(s) with ones so that the correct negative
// number is retained. // number is retained.
// Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B // Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B
static T SignExtend(T val) { static T SignExtend(const T val) {
uint8_t msb = static_cast<uint8_t>(val >> ((B - 1) * 8)); uint8_t msb = static_cast<uint8_t>(val >> ((B - 1) * 8));
if (msb & 0x80) { if (msb & 0x80) {
// Sign extension is -1 (all ones) shifted left B bytes. // Sign extension is -1 (all ones) shifted left B bytes.
@ -126,11 +126,11 @@ class ByteWriter {
template<typename T, bool is_signed> template<typename T, bool is_signed>
class ByteReader<T, 2, is_signed> { class ByteReader<T, 2, is_signed> {
public: public:
static T ReadBigEndian(uint8_t* data) { static T ReadBigEndian(const uint8_t* data) {
return (data[0] << 8) | data[1]; return (data[0] << 8) | data[1];
} }
static T ReadLittleEndian(uint8_t* data) { static T ReadLittleEndian(const uint8_t* data) {
return data[0] | (data[1] << 8); return data[0] | (data[1] << 8);
} }
}; };
@ -153,11 +153,11 @@ class ByteWriter<T, 2> {
template<typename T, bool is_signed> template<typename T, bool is_signed>
class ByteReader<T, 4, is_signed> { class ByteReader<T, 4, is_signed> {
public: public:
static T ReadBigEndian(uint8_t* data) { static T ReadBigEndian(const uint8_t* data) {
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
} }
static T ReadLittleEndian(uint8_t* data) { static T ReadLittleEndian(const uint8_t* data) {
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
} }
}; };
@ -185,7 +185,7 @@ class ByteWriter<T, 4> {
template<typename T, bool is_signed> template<typename T, bool is_signed>
class ByteReader<T, 8, is_signed> { class ByteReader<T, 8, is_signed> {
public: public:
static T ReadBigEndian(uint8_t* data) { static T ReadBigEndian(const uint8_t* data) {
return return
(Get(data, 0) << 56) | (Get(data, 1) << 48) | (Get(data, 0) << 56) | (Get(data, 1) << 48) |
(Get(data, 2) << 40) | (Get(data, 3) << 32) | (Get(data, 2) << 40) | (Get(data, 3) << 32) |
@ -193,7 +193,7 @@ class ByteReader<T, 8, is_signed> {
(Get(data, 6) << 8) | Get(data, 7); (Get(data, 6) << 8) | Get(data, 7);
} }
static T ReadLittleEndian(uint8_t* data) { static T ReadLittleEndian(const uint8_t* data) {
return return
Get(data, 0) | (Get(data, 1) << 8) | Get(data, 0) | (Get(data, 1) << 8) |
(Get(data, 2) << 16) | (Get(data, 3) << 24) | (Get(data, 2) << 16) | (Get(data, 3) << 24) |
@ -202,7 +202,7 @@ class ByteReader<T, 8, is_signed> {
} }
private: private:
inline static T Get(uint8_t* data, unsigned int index) { inline static T Get(const uint8_t* data, unsigned int index) {
return static_cast<T>(data[index]); return static_cast<T>(data[index]);
} }
}; };

View File

@ -52,7 +52,7 @@ class ByteIoTest : public ::testing::Test {
// Test reading big endian numbers. // Test reading big endian numbers.
// Template arguments: Type T, read method RM(buffer), B bytes of data. // Template arguments: Type T, read method RM(buffer), B bytes of data.
template <typename T, T (*RM)(uint8_t*), int B> template <typename T, T (*RM)(const uint8_t*), int B>
void TestRead(bool big_endian) { void TestRead(bool big_endian) {
// Test both for values that are positive and negative (if signed) // Test both for values that are positive and negative (if signed)
for (int neg = 0; neg < 2; ++neg) { for (int neg = 0; neg < 2; ++neg) {

View File

@ -19,6 +19,7 @@
#include "webrtc/common.h" #include "webrtc/common.h"
#include "webrtc/config.h" #include "webrtc/config.h"
#include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h"
#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
#include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h" #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h" #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
@ -439,8 +440,7 @@ PacketReceiver::DeliveryStatus Call::DeliverRtp(const uint8_t* packet,
if (length < 12) if (length < 12)
return DELIVERY_PACKET_ERROR; return DELIVERY_PACKET_ERROR;
const uint8_t* ptr = &packet[8]; uint32_t ssrc = ByteReader<uint32_t>::ReadBigEndian(&packet[8]);
uint32_t ssrc = ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
ReadLockScoped read_lock(*receive_crit_); ReadLockScoped read_lock(*receive_crit_);
std::map<uint32_t, VideoReceiveStream*>::iterator it = std::map<uint32_t, VideoReceiveStream*>::iterator it =