// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine Master // License Agreement. #include "buffer_reader.h" #include "log.h" #include "platform.h" namespace wvcdm { bool BufferReader::Read1(uint8_t* v) { if (v == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null"); return false; } if (!HasBytes(1)) { LOGV("Parse failure: No bytes available"); return false; } *v = buf_[pos_++]; return true; } // Internal implementation of multi-byte reads template bool BufferReader::Read(T* v) { if (v == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null (%s)", __PRETTY_FUNCTION__); return false; } if (!HasBytes(sizeof(T))) { LOGV("Parse failure: Not enough bytes (%zu)", sizeof(T)); return false; } T tmp = 0; for (size_t i = 0; i < sizeof(T); i++) { tmp <<= 8; tmp += buf_[pos_++]; } *v = tmp; return true; } bool BufferReader::Read2(uint16_t* v) { return Read(v); } bool BufferReader::Read2s(int16_t* v) { return Read(v); } bool BufferReader::Read4(uint32_t* v) { return Read(v); } bool BufferReader::Read4s(int32_t* v) { return Read(v); } bool BufferReader::Read8(uint64_t* v) { return Read(v); } bool BufferReader::Read8s(int64_t* v) { return Read(v); } bool BufferReader::ReadString(std::string* str, size_t count) { if (str == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null"); return false; } if (!HasBytes(count)) { LOGV("Parse failure: Not enough bytes (%zu)", count); return false; } str->assign(buf_ + pos_, buf_ + pos_ + count); pos_ += count; return true; } bool BufferReader::ReadVec(std::vector* vec, size_t count) { if (vec == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null"); return false; } if (!HasBytes(count)) { LOGV("Parse failure: Not enough bytes (%zu)", count); return false; } vec->clear(); vec->insert(vec->end(), buf_ + pos_, buf_ + pos_ + count); pos_ += count; return true; } bool BufferReader::SkipBytes(size_t bytes) { if (!HasBytes(bytes)) { LOGV("Parse failure: Not enough bytes (%zu)", bytes); return false; } pos_ += bytes; return true; } bool BufferReader::Read4Into8(uint64_t* v) { if (v == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null"); return false; } uint32_t tmp; if (!Read4(&tmp)) { return false; } *v = tmp; return true; } bool BufferReader::Read4sInto8s(int64_t* v) { if (v == nullptr) { LOGE("Parse failure: Null output parameter when expecting non-null"); return false; } // Beware of the need for sign extension. int32_t tmp; if (!Read4s(&tmp)) { return false; } *v = tmp; return true; } } // namespace wvcdm