Files
android/libwvdrmengine/cdm/core/src/buffer_reader.cpp
Alex Dale cbc1fba713 Log cleanup and reformatting for core/ (part 1)
Merge from Widevine repo of http://go/wvgerrit/81265

Types of cleanup:
  - Removed function / class prefixes from the logs.
  - Fixed log string format options to match the types passed
  - Added static_cast conversion on enumerations
  - _Tried_ to make the log format more consistent (open to feedback)
  - Corrected small spelling mistakes

This set of changes is very large, splitting change across several
submissions.  This change:
  - core/src/buffer_reader.cpp
  - core/src/cdm_engine.cpp
  - core/src/cdm_session.cpp

Test: WV linux unittests
Bug: 134460638
Change-Id: I16c3297b8e94a99c2b8650b129d0f9e8d96b177f
2019-06-21 17:23:58 -07:00

129 lines
2.9 KiB
C++

// 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 == NULL) {
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 <typename T>
bool BufferReader::Read(T* v) {
if (v == NULL) {
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 == NULL) {
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<uint8_t>* vec, size_t count) {
if (vec == NULL) {
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 == NULL) {
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 == NULL) {
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