This source file includes following definitions.
- ofs_
- ReadUInt8
- ReadUInt16
- ReadUInt32
- ReadUInt64
- ReadUInt31
- ReadUInt24
- ReadStringPiece16
- ReadStringPiece32
- ReadBytes
- Seek
- IsDoneReading
- CanRead
- OnFailure
#include <limits>
#include "base/sys_byteorder.h"
#include "net/spdy/spdy_frame_reader.h"
#include "net/spdy/spdy_protocol.h"
namespace net {
SpdyFrameReader::SpdyFrameReader(const char* data, const size_t len)
: data_(data),
len_(len),
ofs_(0) {
}
bool SpdyFrameReader::ReadUInt8(uint8* result) {
if (!CanRead(1)) {
OnFailure();
return false;
}
*result = *reinterpret_cast<const uint8*>(data_ + ofs_);
ofs_ += 1;
return true;
}
bool SpdyFrameReader::ReadUInt16(uint16* result) {
if (!CanRead(2)) {
OnFailure();
return false;
}
*result = ntohs(*(reinterpret_cast<const uint16*>(data_ + ofs_)));
ofs_ += 2;
return true;
}
bool SpdyFrameReader::ReadUInt32(uint32* result) {
if (!CanRead(4)) {
OnFailure();
return false;
}
*result = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_)));
ofs_ += 4;
return true;
}
bool SpdyFrameReader::ReadUInt64(uint64* result) {
if (!CanRead(8)) {
OnFailure();
return false;
}
uint64 upper = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_)));
uint64 lower = ntohl(*(reinterpret_cast<const uint32*>(data_ + ofs_ + 4)));
*result = (upper << 32) + lower;
ofs_ += 8;
return true;
}
bool SpdyFrameReader::ReadUInt31(uint32* result) {
bool success = ReadUInt32(result);
if (success) {
*result &= 0x7fffffff;
}
return success;
}
bool SpdyFrameReader::ReadUInt24(uint32* result) {
if (!CanRead(3)) {
OnFailure();
return false;
}
*result = 0;
memcpy(reinterpret_cast<char*>(result) + 1, data_ + ofs_, 3);
*result = ntohl(*result);
ofs_ += 3;
return true;
}
bool SpdyFrameReader::ReadStringPiece16(base::StringPiece* result) {
uint16 result_len;
if (!ReadUInt16(&result_len)) {
return false;
}
if (!CanRead(result_len)) {
OnFailure();
return false;
}
result->set(data_ + ofs_, result_len);
ofs_ += result_len;
return true;
}
bool SpdyFrameReader::ReadStringPiece32(base::StringPiece* result) {
uint32 result_len;
if (!ReadUInt32(&result_len)) {
return false;
}
if (!CanRead(result_len)) {
OnFailure();
return false;
}
result->set(data_ + ofs_, result_len);
ofs_ += result_len;
return true;
}
bool SpdyFrameReader::ReadBytes(void* result, size_t size) {
if (!CanRead(size)) {
OnFailure();
return false;
}
memcpy(result, data_ + ofs_, size);
ofs_ += size;
return true;
}
bool SpdyFrameReader::Seek(size_t size) {
if (!CanRead(size)) {
OnFailure();
return false;
}
ofs_ += size;
return true;
}
bool SpdyFrameReader::IsDoneReading() const {
return len_ == ofs_;
}
bool SpdyFrameReader::CanRead(size_t bytes) const {
return bytes <= (len_ - ofs_);
}
void SpdyFrameReader::OnFailure() {
ofs_ = len_;
}
}