V18.4.0 CAS plugin
Note that this version does not have Widevine Provisioning 4.0 support. It is only suitable for device upgrades. A new patch with provisioning 4.0 support will be made later.
This commit is contained in:
88
wvutil/src/android_properties.cpp
Normal file
88
wvutil/src/android_properties.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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 <cutils/properties.h>
|
||||
|
||||
#include "cas_properties.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Version format: OEMCrypto_major.OEMCrypto_minor.Plugin_version
|
||||
constexpr char kWvCasPluginVersion[] = "18.4.0";
|
||||
|
||||
bool GetAndroidProperty(const char* key, std::string* value) {
|
||||
char val[PROPERTY_VALUE_MAX];
|
||||
if (!key) {
|
||||
LOGW("GetAndroidProperty: Invalid property key parameter");
|
||||
return false;
|
||||
}
|
||||
if (!value) {
|
||||
LOGW("GetAndroidProperty: Invalid property value parameter");
|
||||
return false;
|
||||
}
|
||||
if (property_get(key, val, "Unknown") <= 0) return false;
|
||||
*value = val;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace wvcas {
|
||||
|
||||
bool Properties::GetCompanyName(std::string* company_name) {
|
||||
if (!company_name) {
|
||||
LOGW("Properties::GetCompanyName: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
return GetAndroidProperty("ro.product.manufacturer", company_name);
|
||||
}
|
||||
|
||||
bool Properties::GetModelName(std::string* model_name) {
|
||||
if (!model_name) {
|
||||
LOGW("Properties::GetModelName: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
return GetAndroidProperty("ro.product.model", model_name);
|
||||
}
|
||||
|
||||
bool Properties::GetArchitectureName(std::string* arch_name) {
|
||||
if (!arch_name) {
|
||||
LOGW("Properties::GetArchitectureName: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
return GetAndroidProperty("ro.product.cpu.abi", arch_name);
|
||||
}
|
||||
|
||||
bool Properties::GetDeviceName(std::string* device_name) {
|
||||
if (!device_name) {
|
||||
LOGW("Properties::GetDeviceName: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
return GetAndroidProperty("ro.product.device", device_name);
|
||||
}
|
||||
|
||||
bool Properties::GetProductName(std::string* product_name) {
|
||||
if (!product_name) {
|
||||
LOGW("Properties::GetProductName: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
return GetAndroidProperty("ro.product.name", product_name);
|
||||
}
|
||||
|
||||
bool Properties::GetOEMCryptoPath(std::string* path) {
|
||||
if (path == nullptr) {
|
||||
LOGW("Properties::GetOEMCryptoPath: Invalid parameter");
|
||||
return false;
|
||||
}
|
||||
*path = "liboemcrypto.so";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Properties::GetWvCasPluginVersion(std::string& version) {
|
||||
version = kWvCasPluginVersion;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace wvcas
|
||||
20
wvutil/src/clock.cpp
Normal file
20
wvutil/src/clock.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
//
|
||||
// Clock - implemented using the standard linux time library
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
int64_t Clock::GetCurrentTime() {
|
||||
struct timeval tv;
|
||||
tv.tv_sec = tv.tv_usec = 0;
|
||||
gettimeofday(&tv, nullptr);
|
||||
return tv.tv_sec;
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
537
wvutil/src/file_store.cpp
Normal file
537
wvutil/src/file_store.cpp
Normal file
@@ -0,0 +1,537 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
//
|
||||
// File class - provides a simple android specific file implementation
|
||||
|
||||
#include "file_store.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <threads.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "file_utils.h"
|
||||
#include "log.h"
|
||||
#include "string_conversions.h"
|
||||
|
||||
#include <openssl/md5.h>
|
||||
|
||||
// Size of the thread-local error string buffer. Used for calls
|
||||
// to strerror_r(3).
|
||||
#define ERRORSTR_BUF_SIZE 1024
|
||||
|
||||
namespace wvutil {
|
||||
namespace {
|
||||
// Maximum number of attempts to read or write on a file.
|
||||
constexpr size_t kMaxIoAttempts = 5;
|
||||
|
||||
// Stand in constant for a closed file descriptor.
|
||||
constexpr int kClosedFd = -1;
|
||||
|
||||
// A reset value of |errno|. Although unlikely, it is possible that a
|
||||
// system call could fail and not set |errno| to a new value. This
|
||||
// would technically be a bug with glibc, but it could cause our error
|
||||
// handling code to enter a bad state.
|
||||
constexpr int kNoError = 0;
|
||||
|
||||
constexpr char kEmptyOrigin[] = "";
|
||||
|
||||
// Reads from file specified by |fd| into the provided |buffer| up to
|
||||
// the number of bytes specified by |count|.
|
||||
// This is an internal function and assumes that all parameters are
|
||||
// valid.
|
||||
//
|
||||
// Returns:
|
||||
// 0 to |count| - Number of bytes successfully read from file.
|
||||
// -1 - Error occurred, check |errno| for read(2).
|
||||
ssize_t SafeRead(int fd, char* buffer, size_t count) {
|
||||
size_t attempts = 0;
|
||||
size_t total_bytes_read = 0;
|
||||
while (total_bytes_read < count && attempts < kMaxIoAttempts) {
|
||||
const size_t to_read = count - total_bytes_read;
|
||||
errno = kNoError;
|
||||
const ssize_t res = read(fd, buffer, to_read);
|
||||
if (res > 0) {
|
||||
attempts = 0; // Clearing |attempts| on success.
|
||||
// It is possible that fewer bytes than |to_read| were read.
|
||||
// In this case, try reading again. Non-critical errors will
|
||||
// likely result in success or |errno| being set to EAGAIN on
|
||||
// the second call. Critical errors will result in a different
|
||||
// error that the caller will need to handle.
|
||||
total_bytes_read += static_cast<size_t>(res);
|
||||
continue;
|
||||
}
|
||||
if (res == 0) return total_bytes_read; // EOF.
|
||||
attempts++;
|
||||
if ((errno != EINTR && errno != EAGAIN) || attempts >= kMaxIoAttempts) {
|
||||
// Caller must handle all other errors, or if max attempts
|
||||
// have been reached.
|
||||
return -1;
|
||||
}
|
||||
// read() was interrupted by signal, safe to try again.
|
||||
}
|
||||
return total_bytes_read;
|
||||
}
|
||||
|
||||
// Writes to the file specified by |fd| from the provided |buffer|.
|
||||
// Function will attempt to write all bytes specified by |count|.
|
||||
// This is an internal function and assumes that all parameters are
|
||||
// valid.
|
||||
//
|
||||
// Returns:
|
||||
// |count| - Successfully wrote all bytes to file.
|
||||
// -1 - Error occurred, check |errno| for write(2).
|
||||
ssize_t SafeWrite(int fd, const char* buffer, size_t count) {
|
||||
size_t attempts = 0;
|
||||
size_t total_bytes_written = 0;
|
||||
while (total_bytes_written < count && attempts < kMaxIoAttempts) {
|
||||
const size_t to_write = count - total_bytes_written;
|
||||
errno = kNoError;
|
||||
const ssize_t res = write(fd, &buffer[total_bytes_written], to_write);
|
||||
if (res > 0) {
|
||||
attempts = 0; // Clearing |attempts| on success.
|
||||
// It is possible that fewer bytes than |to_write| were written.
|
||||
// In this case, try writing again. Non-critical errors will
|
||||
// likely result in success or |errno| being set to EAGAIN on
|
||||
// the second call. Critical errors will result in a different
|
||||
// error that the caller will need to handle.
|
||||
total_bytes_written += static_cast<size_t>(res);
|
||||
continue;
|
||||
}
|
||||
if (res == 0) return total_bytes_written; // Possible EOF.
|
||||
attempts++;
|
||||
if ((errno != EINTR && errno != EAGAIN) || attempts >= kMaxIoAttempts) {
|
||||
// Caller must handle all other errors, or if max attempts
|
||||
// have been reached.
|
||||
return -1;
|
||||
}
|
||||
// write() was interrupted by signal, safe to try again.
|
||||
}
|
||||
return total_bytes_written;
|
||||
}
|
||||
|
||||
// Converts the provided error number to its string representation.
|
||||
// Supports a subset of error numbers expected from the system calls
|
||||
// used in this module.
|
||||
// TODO(b/183653374): Replace this with strerrorname_np().
|
||||
const char* ErrnoToString(int num) {
|
||||
switch (num) {
|
||||
case kNoError:
|
||||
return "ZERO";
|
||||
case EACCES:
|
||||
return "EACCES";
|
||||
case EAGAIN:
|
||||
return "EAGAIN";
|
||||
case EBADF:
|
||||
return "EBADF";
|
||||
case EBUSY:
|
||||
return "EBUSY";
|
||||
case EDESTADDRREQ:
|
||||
return "EDESTADDRREQ";
|
||||
case EDQUOT:
|
||||
return "EDQUOT";
|
||||
case EEXIST:
|
||||
return "EEXIST";
|
||||
case EFAULT:
|
||||
return "EFAULT";
|
||||
case EFBIG:
|
||||
return "EFBIG";
|
||||
case EINTR:
|
||||
return "EINTR";
|
||||
case EINVAL:
|
||||
return "EINVAL";
|
||||
case EIO:
|
||||
return "EIO";
|
||||
case EISDIR:
|
||||
return "EISDIR";
|
||||
case ELOOP:
|
||||
return "ELOOP";
|
||||
case EMFILE:
|
||||
return "EMFILE";
|
||||
case ENAMETOOLONG:
|
||||
return "ENAMETOOLONG";
|
||||
case ENFILE:
|
||||
return "ENFILE";
|
||||
case ENODEV:
|
||||
return "ENODEV";
|
||||
case ENOENT:
|
||||
return "ENOENT";
|
||||
case ENOMEM:
|
||||
return "ENOMEM";
|
||||
case ENOSPC:
|
||||
return "ENOSPC";
|
||||
case ENOTDIR:
|
||||
return "ENOTDIR";
|
||||
case ENXIO:
|
||||
return "ENXIO";
|
||||
case EOPNOTSUPP:
|
||||
return "EOPNOTSUPP";
|
||||
case EOVERFLOW:
|
||||
return "EOVERFLOW";
|
||||
case EPERM:
|
||||
return "EPERM";
|
||||
case EPIPE:
|
||||
return "EPIPE";
|
||||
case EROFS:
|
||||
return "EROFS";
|
||||
case ETXTBSY:
|
||||
return "ETXTBSY";
|
||||
#if EWOULDBLOCK != EAGAIN
|
||||
case EWOULDBLOCK:
|
||||
return "EWOULDBLOCK";
|
||||
#endif
|
||||
}
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
// Safely converts the provided error number to its standard
|
||||
// description string as provided by strerror_r().
|
||||
// This function is guaranteed to return null-terminated string,
|
||||
// and is thread safe.
|
||||
const char* ErrnoToDescription(int num) {
|
||||
static thread_local char error_buf[ERRORSTR_BUF_SIZE];
|
||||
if (num == kNoError) {
|
||||
return "Unspecified error";
|
||||
}
|
||||
// Always ensure there is a null term.
|
||||
error_buf[sizeof(error_buf) - 1] = 0;
|
||||
// See strerror_l(3) manual page for details.
|
||||
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
|
||||
// Portable version:
|
||||
// int strerror_r(int num, char* buf, size_t buflen)
|
||||
// Returns:
|
||||
// 0 on success
|
||||
// -1 or a positive value on error (depends on glibc version)
|
||||
const int res = strerror_r(num, error_buf, sizeof(error_buf));
|
||||
return res != 0 ? "Unknown" : error_buf;
|
||||
#else
|
||||
// GNU specific version:
|
||||
// char* strerror_r(int num, char* buf, size_t buflen)
|
||||
// Returns:
|
||||
// Pointer to |buf| or internal string on success
|
||||
// Null on error
|
||||
const char* res = strerror_r(num, error_buf, sizeof(error_buf));
|
||||
return res == nullptr ? "Unknown" : res;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Converts the provided file |mode| to a "ls" style file mode.
|
||||
std::string StatModeToString(unsigned int mode) {
|
||||
std::string mode_rep;
|
||||
mode_rep.reserve(11); // 1 file type + 9 permissions + 1 term.
|
||||
// File type.
|
||||
if (S_ISREG(mode)) {
|
||||
mode_rep.append("-");
|
||||
} else if (S_ISDIR(mode)) {
|
||||
mode_rep.append("d");
|
||||
} else if (S_ISLNK(mode)) {
|
||||
mode_rep.append("l");
|
||||
} else if (S_ISCHR(mode)) {
|
||||
mode_rep.append("c");
|
||||
} else if (S_ISBLK(mode)) {
|
||||
mode_rep.append("b");
|
||||
} else if (S_ISSOCK(mode)) {
|
||||
mode_rep.append("s");
|
||||
} else {
|
||||
mode_rep.append("?");
|
||||
}
|
||||
// User owner permission.
|
||||
mode_rep.append((mode & S_IRUSR) ? "r" : "-");
|
||||
mode_rep.append((mode & S_IWUSR) ? "w" : "-");
|
||||
mode_rep.append((mode & S_IXUSR) ? "x" : "-");
|
||||
// Group owner permission.
|
||||
mode_rep.append((mode & S_IRGRP) ? "r" : "-");
|
||||
mode_rep.append((mode & S_IWGRP) ? "w" : "-");
|
||||
mode_rep.append((mode & S_IXGRP) ? "x" : "-");
|
||||
// Others permission.
|
||||
mode_rep.append((mode & S_IROTH) ? "r" : "-");
|
||||
mode_rep.append((mode & S_IWOTH) ? "w" : "-");
|
||||
mode_rep.append((mode & S_IXOTH) ? "x" : "-");
|
||||
return mode_rep;
|
||||
}
|
||||
|
||||
// Coverts the provided system time in seconds to a string representation
|
||||
// in the system's local time.
|
||||
// Time format is a modified ISO8601.
|
||||
// Example: 2021-03-01 12:32:53
|
||||
// If the time cannot be converted into this format, then the decimal
|
||||
// representation of seconds are returned.
|
||||
std::string PosixTimeToString(time_t seconds) {
|
||||
struct tm timestamp;
|
||||
if (localtime_r(&seconds, ×tamp) == nullptr) {
|
||||
// Only possible failure is an overflow.
|
||||
return std::to_string(seconds);
|
||||
}
|
||||
char buffer[32];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
const size_t length = strftime(buffer, sizeof(buffer), "%F %T", ×tamp);
|
||||
if (length == 0) {
|
||||
// Unexpected error. Just return seconds.
|
||||
return std::to_string(seconds);
|
||||
}
|
||||
return std::string(buffer, length);
|
||||
}
|
||||
|
||||
std::string GetFileNameSafeHash(const std::string& input) {
|
||||
std::vector<uint8_t> hash(MD5_DIGEST_LENGTH);
|
||||
MD5(reinterpret_cast<const uint8_t*>(input.data()), input.size(),
|
||||
hash.data());
|
||||
return wvutil::Base64SafeEncode(hash);
|
||||
}
|
||||
|
||||
std::string GetFileNameForIdentifier(const std::string path,
|
||||
const std::string identifier) {
|
||||
std::string file_name = path;
|
||||
std::string dir_path;
|
||||
const size_t delimiter_pos = path.rfind(kDirectoryDelimiter);
|
||||
if (delimiter_pos != std::string::npos) {
|
||||
dir_path = file_name.substr(0, delimiter_pos);
|
||||
file_name = path.substr(delimiter_pos + 1);
|
||||
}
|
||||
|
||||
if (file_name == kCertificateFileName && !identifier.empty()) {
|
||||
const std::string hash = GetFileNameSafeHash(identifier);
|
||||
file_name = kCertificateFileNamePrefix + hash + kCertificateFileNameExt;
|
||||
} else if (file_name == kLegacyCertificateFileName && !identifier.empty()) {
|
||||
const std::string hash = GetFileNameSafeHash(identifier);
|
||||
file_name =
|
||||
kLegacyCertificateFileNamePrefix + hash + kCertificateFileNameExt;
|
||||
}
|
||||
|
||||
if (dir_path.empty())
|
||||
return file_name;
|
||||
return dir_path + kDirectoryDelimiter + file_name;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class AndroidFile : public File {
|
||||
public:
|
||||
// Parameters:
|
||||
// |fd| - Open file descriptor for a regular file.
|
||||
// |flags| - Bit field of flags originally passed to FileSystem::Open()
|
||||
// |file_path| - Path used to open file.
|
||||
AndroidFile(int fd, int flags, const std::string& file_path)
|
||||
: fd_(fd), flags_(flags), file_path_(file_path) {}
|
||||
|
||||
~AndroidFile() { Close(); }
|
||||
|
||||
bool IsOpen() const { return fd_ != kClosedFd; }
|
||||
|
||||
bool CanWrite() const { return !(flags_ & FileSystem::kReadOnly); }
|
||||
|
||||
// Used for logging.
|
||||
const char* file_path() const { return file_path_.c_str(); }
|
||||
|
||||
ssize_t Read(char* buffer, size_t bytes) override {
|
||||
if (!buffer) {
|
||||
LOGE("Output |buffer| is null");
|
||||
return -1;
|
||||
}
|
||||
if (!IsOpen()) {
|
||||
LOGE("File not open: path = %s", file_path());
|
||||
return -1;
|
||||
}
|
||||
const ssize_t res = SafeRead(fd_, buffer, bytes);
|
||||
if (res < 0) {
|
||||
const int saved_errno = errno;
|
||||
LOGE("Read failed: errno = %s (%d), desc = %s",
|
||||
ErrnoToString(saved_errno), saved_errno,
|
||||
ErrnoToDescription(saved_errno));
|
||||
return -1;
|
||||
} else if (res < bytes) {
|
||||
LOGD("Read output truncated: expected = %zu, actual = %zd", bytes, res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
ssize_t Write(const char* buffer, size_t bytes) override {
|
||||
if (!buffer) {
|
||||
LOGE("Input |buffer| is null");
|
||||
return -1;
|
||||
}
|
||||
if (!IsOpen()) {
|
||||
LOGE("File not open: path = %s", file_path());
|
||||
return -1;
|
||||
}
|
||||
if (!CanWrite()) {
|
||||
LOGE("File is read only: path = %s", file_path());
|
||||
return -1;
|
||||
}
|
||||
const ssize_t res = SafeWrite(fd_, buffer, bytes);
|
||||
if (res < 0) {
|
||||
const int saved_errno = errno;
|
||||
LOGE("Write failed: errno = %s (%d), desc = %s",
|
||||
ErrnoToString(saved_errno), saved_errno,
|
||||
ErrnoToDescription(saved_errno));
|
||||
return -1;
|
||||
}
|
||||
FlushFile();
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
void FlushFile() { fsync(fd_); }
|
||||
|
||||
void Close() {
|
||||
if (IsOpen()) {
|
||||
FlushFile();
|
||||
close(fd_);
|
||||
fd_ = kClosedFd;
|
||||
}
|
||||
}
|
||||
|
||||
// Logs the contents of the file's stat info.
|
||||
void LogStat() const {
|
||||
if (!IsOpen()) {
|
||||
LOGD("No stat info available");
|
||||
return;
|
||||
}
|
||||
struct stat st;
|
||||
errno = kNoError;
|
||||
if (fstat(fd_, &st) != 0) {
|
||||
const int saved_errno = errno;
|
||||
if (errno != EBADF) {
|
||||
// No logs if an issue with FD, caller would have indicated the problem.
|
||||
LOGE("Stat failed: errno = %s (%d), desc = %s",
|
||||
ErrnoToString(saved_errno), saved_errno,
|
||||
ErrnoToDescription(saved_errno));
|
||||
}
|
||||
return;
|
||||
}
|
||||
LOGD(
|
||||
"Stat: path = %s, st_dev = %lu, st_ino = %lu, st_mode = 0%o (%s), "
|
||||
"st_uid = %u, st_gid = %u, st_size = %ld, st_atime = %s, "
|
||||
"st_mtime = %s, st_ctime = %s",
|
||||
file_path(), st.st_dev, st.st_ino, st.st_mode,
|
||||
StatModeToString(st.st_mode).c_str(), st.st_uid, st.st_gid, st.st_size,
|
||||
PosixTimeToString(st.st_atime).c_str(),
|
||||
PosixTimeToString(st.st_mtime).c_str(),
|
||||
PosixTimeToString(st.st_ctime).c_str());
|
||||
}
|
||||
|
||||
// File descriptor of the opened file. Set to -1 when closed.
|
||||
int fd_ = kClosedFd;
|
||||
// Bit field of OpenFlags.
|
||||
int flags_ = 0;
|
||||
// Path used to open the file descriptor.
|
||||
std::string file_path_;
|
||||
};
|
||||
|
||||
class FileSystem::Impl {};
|
||||
|
||||
FileSystem::FileSystem() : FileSystem(kEmptyOrigin, nullptr) {}
|
||||
FileSystem::FileSystem(const std::string& origin, void* /* extra_data */)
|
||||
: origin_(origin) {}
|
||||
|
||||
FileSystem::~FileSystem() {}
|
||||
|
||||
std::unique_ptr<File> FileSystem::Open(const std::string& file_name,
|
||||
int flags) {
|
||||
const std::string file_path =
|
||||
GetFileNameForIdentifier(file_name, identifier_);
|
||||
// Verify flags.
|
||||
if ((flags & kReadOnly) && (flags & kTruncate)) {
|
||||
LOGE(
|
||||
"Cannot be both truncated and be read-only: "
|
||||
"file = %s, identifier = %s",
|
||||
file_name.c_str(), identifier_.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Create the enclosing directory if it does not exist.
|
||||
const size_t delimiter_pos = file_path.rfind(kDirectoryDelimiter);
|
||||
if (delimiter_pos != std::string::npos) {
|
||||
const std::string dir_path = file_path.substr(0, delimiter_pos);
|
||||
if ((flags & FileSystem::kCreate) && !Exists(dir_path))
|
||||
FileUtils::CreateDirectory(dir_path);
|
||||
}
|
||||
|
||||
const bool exists = Exists(file_path);
|
||||
if (!(flags & kCreate) && !exists) {
|
||||
LOGD("File does not exist: file = %s, identifier = %s", file_name.c_str(),
|
||||
identifier_.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const int open_flags = ((flags & kCreate) ? O_CREAT : 0) |
|
||||
((flags & kReadOnly) ? O_RDONLY : O_RDWR) |
|
||||
((flags & kTruncate) ? O_TRUNC : 0) |
|
||||
O_CLOEXEC; // Never share on calls to exec().
|
||||
constexpr mode_t kDefaultMode = S_IRUSR | S_IWUSR;
|
||||
errno = kNoError;
|
||||
const int fd = open(file_path.c_str(), open_flags, kDefaultMode);
|
||||
if (fd < 0) {
|
||||
const int saved_errno = errno;
|
||||
LOGE("%s failed: errno = %s (%d), desc = %s, path = %s",
|
||||
exists ? "Open" : "Create", ErrnoToString(saved_errno), saved_errno,
|
||||
ErrnoToDescription(saved_errno), file_path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
// Check that the opened file is a regular file.
|
||||
struct stat st;
|
||||
errno = kNoError;
|
||||
if (fstat(fd, &st) != 0) {
|
||||
const int saved_errno = errno;
|
||||
LOGE("Stat failed: errno = %s (%d), desc = %s", ErrnoToString(saved_errno),
|
||||
saved_errno, ErrnoToDescription(saved_errno));
|
||||
close(fd);
|
||||
return nullptr;
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
LOGE("Not a file: path = %s, st_mode = 0%o (%s)", file_path.c_str(),
|
||||
st.st_mode, StatModeToString(st.st_mode).c_str());
|
||||
close(fd);
|
||||
return nullptr;
|
||||
}
|
||||
return std::unique_ptr<File>(new AndroidFile(fd, flags, file_path));
|
||||
}
|
||||
|
||||
bool FileSystem::Exists(const std::string& path) {
|
||||
return FileUtils::Exists(GetFileNameForIdentifier(path, identifier_));
|
||||
}
|
||||
|
||||
bool FileSystem::Exists(const std::string& path, int* errno_value) {
|
||||
return FileUtils::Exists(GetFileNameForIdentifier(path, identifier_),
|
||||
errno_value);
|
||||
}
|
||||
|
||||
bool FileSystem::Remove(const std::string& path) {
|
||||
return FileUtils::Remove(GetFileNameForIdentifier(path, identifier_));
|
||||
}
|
||||
|
||||
ssize_t FileSystem::FileSize(const std::string& file_name) {
|
||||
const std::string file_path =
|
||||
GetFileNameForIdentifier(file_name, identifier_);
|
||||
struct stat st;
|
||||
errno = kNoError;
|
||||
if (stat(file_path.c_str(), &st) == 0) {
|
||||
if (st.st_size == 0) {
|
||||
LOGW("File is empty: name = %s", file_name.c_str());
|
||||
}
|
||||
return st.st_size;
|
||||
}
|
||||
// Else, error occurred.
|
||||
const int saved_errno = errno;
|
||||
LOGE("Stat failed: errno = %s (%d), desc = %s", ErrnoToString(saved_errno),
|
||||
saved_errno, ErrnoToDescription(saved_errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool FileSystem::List(const std::string& path,
|
||||
std::vector<std::string>* filenames) {
|
||||
return FileUtils::List(GetFileNameForIdentifier(path, origin_), filenames);
|
||||
}
|
||||
|
||||
void FileSystem::set_origin(const std::string& origin) { origin_ = origin; }
|
||||
|
||||
void FileSystem::set_identifier(const std::string& identifier) {
|
||||
identifier_ = identifier;
|
||||
}
|
||||
} // namespace wvutil
|
||||
240
wvutil/src/file_utils.cpp
Normal file
240
wvutil/src/file_utils.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "file_utils.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
bool IsCurrentOrParentDirectory(const char* dir) {
|
||||
return strcmp(dir, kCurrentDirectory) == 0 ||
|
||||
strcmp(dir, kParentDirectory) == 0;
|
||||
}
|
||||
|
||||
bool FileUtils::Exists(const std::string& path) {
|
||||
return Exists(path, nullptr);
|
||||
}
|
||||
|
||||
bool FileUtils::Exists(const std::string& path, int* errno_value) {
|
||||
struct stat buf;
|
||||
int error = 0;
|
||||
int res = stat(path.c_str(), &buf) == 0;
|
||||
if (!res) {
|
||||
error = errno;
|
||||
if (error == ENOENT) {
|
||||
LOGI("stat failed: ENOENT");
|
||||
} else {
|
||||
LOGE("stat failed: %d, %s", error, strerror(error));
|
||||
}
|
||||
}
|
||||
if (errno_value != nullptr) *errno_value = error;
|
||||
return res;
|
||||
}
|
||||
|
||||
bool FileUtils::Remove(const std::string& path) {
|
||||
if (FileUtils::IsDirectory(path)) {
|
||||
// Handle directory deletion
|
||||
DIR* dir;
|
||||
if ((dir = opendir(path.c_str())) != nullptr) {
|
||||
// first remove files and dir within it
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
if (!IsCurrentOrParentDirectory(entry->d_name)) {
|
||||
std::string path_to_remove = path + kDirectoryDelimiter;
|
||||
path_to_remove += entry->d_name;
|
||||
if (!Remove(path_to_remove)) {
|
||||
closedir(dir);
|
||||
LOGW("Failed to remove directory entry: dir_path = %s, entry = %s",
|
||||
path.c_str(), entry->d_name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
if (rmdir(path.c_str())) {
|
||||
LOGW("File::Remove: rmdir failed: %d, %s", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
size_t wildcard_pos = path.find(kWildcard);
|
||||
if (wildcard_pos == std::string::npos) {
|
||||
// Handle file deletion
|
||||
if (unlink(path.c_str()) && (errno != ENOENT)) {
|
||||
LOGW("File::Remove: unlink failed: %d, %s", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Handle wildcard specified file deletion
|
||||
size_t delimiter_pos = path.rfind(kDirectoryDelimiter, wildcard_pos);
|
||||
if (delimiter_pos == std::string::npos) {
|
||||
LOGW("File::Remove: unable to find path delimiter before wildcard");
|
||||
return false;
|
||||
}
|
||||
|
||||
DIR* dir;
|
||||
std::string dir_path = path.substr(0, delimiter_pos);
|
||||
std::string prepend =
|
||||
path.substr(delimiter_pos + 1, wildcard_pos - delimiter_pos - 1);
|
||||
if ((dir = opendir(dir_path.c_str())) == nullptr) {
|
||||
LOGW("File::Remove: directory open failed for wildcard: %d, %s", errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
struct dirent* entry;
|
||||
std::string ext = path.substr(wildcard_pos + 1);
|
||||
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
size_t filename_len = strlen(entry->d_name);
|
||||
if (filename_len > ext.size()) {
|
||||
if (strcmp(entry->d_name + filename_len - ext.size(), ext.c_str()) ==
|
||||
0 &&
|
||||
!IsCurrentOrParentDirectory(entry->d_name) &&
|
||||
strncmp(entry->d_name, prepend.c_str(), prepend.size()) == 0) {
|
||||
std::string file_path_to_remove =
|
||||
dir_path + kDirectoryDelimiter + entry->d_name;
|
||||
if (!Remove(file_path_to_remove)) {
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileUtils::Copy(const std::string& src, const std::string& dest) {
|
||||
struct stat stat_buf;
|
||||
if (stat(src.c_str(), &stat_buf)) {
|
||||
LOGV("File::Copy: file %s stat error: %d, %s", src.c_str(), errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
int fd_src = open(src.c_str(), O_RDONLY);
|
||||
if (fd_src < 0) {
|
||||
LOGW("File::Copy: unable to open file %s: %d, %s", src.c_str(), errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
int fd_dest = open(dest.c_str(), O_WRONLY | O_CREAT, stat_buf.st_mode);
|
||||
if (fd_dest < 0) {
|
||||
LOGW("File::Copy: unable to open file %s: %d, %s", dest.c_str(), errno,
|
||||
strerror(errno));
|
||||
close(fd_src);
|
||||
return false;
|
||||
}
|
||||
|
||||
off_t offset = 0;
|
||||
bool status = true;
|
||||
if (sendfile(fd_dest, fd_src, &offset, stat_buf.st_size) < 0) {
|
||||
LOGV("File::Copy: unable to copy %s to %s: %d, %s", src.c_str(),
|
||||
dest.c_str(), errno, strerror(errno));
|
||||
status = false;
|
||||
}
|
||||
|
||||
close(fd_src);
|
||||
close(fd_dest);
|
||||
return status;
|
||||
}
|
||||
|
||||
bool FileUtils::List(const std::string& path, std::vector<std::string>* files) {
|
||||
if (nullptr == files) {
|
||||
LOGV("File::List: files destination not provided");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!FileUtils::Exists(path)) {
|
||||
LOGV("File::List: path %s does not exist: %d, %s", path.c_str(), errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
DIR* dir = opendir(path.c_str());
|
||||
if (dir == nullptr) {
|
||||
LOGW("File::List: unable to open directory %s: %d, %s", path.c_str(), errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
files->clear();
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
if (!IsCurrentOrParentDirectory(entry->d_name)) {
|
||||
files->push_back(entry->d_name);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileUtils::IsRegularFile(const std::string& path) {
|
||||
struct stat buf;
|
||||
if (stat(path.c_str(), &buf) == 0)
|
||||
return buf.st_mode & S_IFREG;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileUtils::IsDirectory(const std::string& path) {
|
||||
struct stat buf;
|
||||
if (stat(path.c_str(), &buf) == 0)
|
||||
return buf.st_mode & S_IFDIR;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileUtils::CreateDirectory(const std::string& path_in) {
|
||||
std::string path = path_in;
|
||||
size_t size = path.size();
|
||||
if ((size == 1) && (path[0] == kDirectoryDelimiter)) return true;
|
||||
|
||||
if (size <= 1) return false;
|
||||
|
||||
size_t pos = path.find(kDirectoryDelimiter, 1);
|
||||
while (pos < size) {
|
||||
path[pos] = '\0';
|
||||
if (mkdir(path.c_str(), 0700) != 0) {
|
||||
if (errno != EEXIST) {
|
||||
LOGW("File::CreateDirectory: mkdir failed: %d, %s\n", errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
path[pos] = kDirectoryDelimiter;
|
||||
pos = path.find(kDirectoryDelimiter, pos + 1);
|
||||
}
|
||||
|
||||
if (path[size - 1] != kDirectoryDelimiter) {
|
||||
if (mkdir(path.c_str(), 0700) != 0) {
|
||||
if (errno != EEXIST) {
|
||||
LOGW("File::CreateDirectory: mkdir failed: %d, %s\n", errno,
|
||||
strerror(errno));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
144
wvutil/src/log.cpp
Normal file
144
wvutil/src/log.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
//
|
||||
// Log - implemented using the standard Android logging mechanism
|
||||
|
||||
/*
|
||||
* Qutoing from system/core/include/log/log.h:
|
||||
* Normally we strip ALOGV (VERBOSE messages) from release builds.
|
||||
* You can modify this (for example with "#define LOG_NDEBUG 0"
|
||||
* at the top of your source file) to change that behavior.
|
||||
*/
|
||||
#ifndef LOG_NDEBUG
|
||||
# ifdef NDEBUG
|
||||
# define LOG_NDEBUG 1
|
||||
# else
|
||||
# define LOG_NDEBUG 0
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define LOG_TAG "WVCas"
|
||||
#define LOG_BUF_SIZE 5120
|
||||
|
||||
#include "log.h"
|
||||
#include <hwbinder/IPCThreadState.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
* Uncomment the line below if you want to have the LOGV messages to print
|
||||
* IMPORTANT : this will affect all of CDM
|
||||
*/
|
||||
|
||||
// #define LOG_NDEBUG 0
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
namespace {
|
||||
int64_t GetCurrentTimeMs() {
|
||||
struct timeval tv {};
|
||||
gettimeofday(&tv, NULL);
|
||||
auto msec1 = static_cast<int64_t>(tv.tv_sec) * 1000;
|
||||
auto msec2 = static_cast<int64_t>(tv.tv_usec) / 1000;
|
||||
return msec1 + msec2;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
LogPriority g_cutoff = CDM_LOG_INFO;
|
||||
|
||||
LogBuffer g_logbuf;
|
||||
|
||||
thread_local bool tl_logging_uid_set_ = false;
|
||||
|
||||
thread_local uint32_t tl_logging_uid_ = UNKNOWN_UID;
|
||||
|
||||
void SetLoggingUid(const uint32_t uid) {
|
||||
tl_logging_uid_set_ = true;
|
||||
tl_logging_uid_ = uid;
|
||||
}
|
||||
|
||||
void ClearLoggingUid() {
|
||||
tl_logging_uid_set_ = false;
|
||||
tl_logging_uid_ = UNKNOWN_UID;
|
||||
}
|
||||
|
||||
uint32_t GetLoggingUid() { return tl_logging_uid_; }
|
||||
|
||||
uint32_t GetIpcCallingUid() {
|
||||
const auto self = android::hardware::IPCThreadState::selfOrNull();
|
||||
return self ? self->getCallingUid() : UNKNOWN_UID;
|
||||
}
|
||||
|
||||
void InitLogging() {}
|
||||
|
||||
void Log(const char* file, const char* function, int line, LogPriority level,
|
||||
const char* format, ...) {
|
||||
const char* filename = strrchr(file, '/');
|
||||
filename = filename == nullptr ? file : filename + 1;
|
||||
|
||||
static thread_local char buf[LOG_BUF_SIZE];
|
||||
int len =
|
||||
snprintf(buf, LOG_BUF_SIZE, "[%s(%d):%s] ", filename, line, function);
|
||||
if (len < 0) len = 0;
|
||||
if (static_cast<unsigned int>(len) < sizeof(buf)) {
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsnprintf(buf + len, LOG_BUF_SIZE - len, format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
android_LogPriority prio = ANDROID_LOG_VERBOSE;
|
||||
|
||||
switch (level) {
|
||||
case CDM_LOG_SILENT:
|
||||
return; // It is nonsensical to pass LOG_SILENT.
|
||||
case CDM_LOG_ERROR:
|
||||
prio = ANDROID_LOG_ERROR;
|
||||
break;
|
||||
case CDM_LOG_WARN:
|
||||
prio = ANDROID_LOG_WARN;
|
||||
break;
|
||||
case CDM_LOG_INFO:
|
||||
prio = ANDROID_LOG_INFO;
|
||||
break;
|
||||
case CDM_LOG_DEBUG:
|
||||
prio = ANDROID_LOG_DEBUG;
|
||||
break;
|
||||
#if LOG_NDEBUG
|
||||
case CDM_LOG_VERBOSE:
|
||||
return;
|
||||
#else
|
||||
case CDM_LOG_VERBOSE:
|
||||
prio = ANDROID_LOG_VERBOSE;
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
__android_log_write(prio, LOG_TAG, buf);
|
||||
if (level <= CDM_LOG_INFO) {
|
||||
uint32_t uid = tl_logging_uid_set_ ? tl_logging_uid_ : GetIpcCallingUid();
|
||||
g_logbuf.addLog({uid, GetCurrentTimeMs(), level, buf});
|
||||
}
|
||||
}
|
||||
|
||||
void LogBuffer::addLog(const LogMessage& log) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
buffer_.push_back(log);
|
||||
while (buffer_.size() > MAX_CAPACITY) {
|
||||
buffer_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<LogMessage> LogBuffer::getLogs() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return {buffer_.begin(), buffer_.end()};
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
60
wvutil/src/rw_lock.cpp
Normal file
60
wvutil/src/rw_lock.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "rw_lock.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
shared_mutex::~shared_mutex() {
|
||||
if (reader_count_ > 0) {
|
||||
LOGE("shared_mutex destroyed with active readers!");
|
||||
}
|
||||
if (has_writer_) {
|
||||
LOGE("shared_mutex destroyed with an active writer!");
|
||||
}
|
||||
}
|
||||
|
||||
void shared_mutex::lock_shared() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
while (has_writer_) {
|
||||
condition_variable_.wait(lock);
|
||||
}
|
||||
|
||||
++reader_count_;
|
||||
}
|
||||
|
||||
void shared_mutex::unlock_shared() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
--reader_count_;
|
||||
|
||||
if (reader_count_ == 0) {
|
||||
condition_variable_.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
bool shared_mutex::lock_implementation(bool abort_if_unavailable) {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
while (reader_count_ > 0 || has_writer_) {
|
||||
if (abort_if_unavailable) return false;
|
||||
condition_variable_.wait(lock);
|
||||
}
|
||||
|
||||
has_writer_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void shared_mutex::unlock() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
|
||||
has_writer_ = false;
|
||||
|
||||
condition_variable_.notify_all();
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
343
wvutil/src/string_conversions.cpp
Normal file
343
wvutil/src/string_conversions.cpp
Normal file
@@ -0,0 +1,343 @@
|
||||
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
|
||||
#include "string_conversions.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "log.h"
|
||||
#include "platform.h"
|
||||
|
||||
namespace wvutil {
|
||||
namespace {
|
||||
// Base64 character set, indexed for their 6-bit mapping, plus '='.
|
||||
const char kBase64Codes[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
// URL safe Base64 character set.
|
||||
const char kBase64SafeCodes[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
|
||||
|
||||
// Gets the low |n| bits of |in|.
|
||||
#define GET_LOW_BITS(in, n) ((in) & ((1 << (n)) - 1))
|
||||
// Gets the given (zero-indexed) bits [a, b) of |in|.
|
||||
#define GET_BITS(in, a, b) GET_LOW_BITS((in) >> (a), (b) - (a))
|
||||
// Calculates a/b using round-up division (only works for positive numbers).
|
||||
#define CEIL_DIVIDE(a, b) ((((a)-1) / (b)) + 1)
|
||||
|
||||
// Decodes a single Base64 encoded character into its 6-bit value.
|
||||
// The provided |codes| must be a Base64 character map.
|
||||
int DecodeBase64Char(char c, const char* codes) {
|
||||
const char* c_in_codes = strchr(codes, c);
|
||||
if (c_in_codes == nullptr) return -1;
|
||||
const uintptr_t c_in_codes_int = reinterpret_cast<uintptr_t>(c_in_codes);
|
||||
const uintptr_t codes_int = reinterpret_cast<uintptr_t>(codes);
|
||||
return static_cast<int>(c_in_codes_int - codes_int);
|
||||
}
|
||||
|
||||
bool DecodeHexChar(char ch, uint8_t* digit) {
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
*digit = ch - '0';
|
||||
return true;
|
||||
}
|
||||
ch = tolower(ch);
|
||||
if ((ch >= 'a') && (ch <= 'f')) {
|
||||
*digit = ch - 'a' + 10;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Encode for standard base64 encoding (RFC4648).
|
||||
// https://en.wikipedia.org/wiki/Base64
|
||||
// Text | M | a | n |
|
||||
// ASCI | 77 (0x4d) | 97 (0x61) | 110 (0x6e) |
|
||||
// Bits | 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0 |
|
||||
// Index | 19 | 22 | 5 | 46 |
|
||||
// Base64 | T | W | F | u |
|
||||
// | <----------------- 24-bits -----------------> |
|
||||
|
||||
// The provided |codes| must be a Base64 character map.
|
||||
std::string Base64EncodeInternal(const uint8_t* data, size_t length,
|
||||
const char* codes) {
|
||||
// |temp| stores a 24-bit block that is treated as an array where insertions
|
||||
// occur from high to low.
|
||||
uint32_t temp = 0;
|
||||
size_t out_index = 0;
|
||||
const size_t out_size = CEIL_DIVIDE(length, 3) * 4;
|
||||
std::string result(out_size, '\0');
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
// "insert" 8-bits of data
|
||||
temp |= (data[i] << ((2 - (i % 3)) * 8));
|
||||
if (i % 3 == 2) {
|
||||
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 0, 6)];
|
||||
temp = 0;
|
||||
}
|
||||
}
|
||||
if (length % 3 == 1) {
|
||||
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = '=';
|
||||
result[out_index++] = '=';
|
||||
} else if (length % 3 == 2) {
|
||||
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = '=';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Base64DecodeInternal(const char* encoded, size_t length,
|
||||
const char* codes) {
|
||||
const size_t out_size_max = CEIL_DIVIDE(length * 3, 4);
|
||||
std::vector<uint8_t> result(out_size_max, '\0');
|
||||
// |temp| stores 24-bits of data that is treated as an array where insertions
|
||||
// occur from high to low.
|
||||
uint32_t temp = 0;
|
||||
size_t out_index = 0;
|
||||
size_t i;
|
||||
for (i = 0; i < length; i++) {
|
||||
if (encoded[i] == '=') {
|
||||
// Verify an '=' only appears at the end. We want i to remain at the
|
||||
// first '=', so we need an inner loop.
|
||||
for (size_t j = i; j < length; j++) {
|
||||
if (encoded[j] != '=') {
|
||||
LOGE("base64Decode failed");
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
}
|
||||
if (length % 4 != 0) {
|
||||
// If padded, then the length must be a multiple of 4.
|
||||
// Unpadded messages are OK.
|
||||
LOGE("base64Decode failed");
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
const int decoded = DecodeBase64Char(encoded[i], codes);
|
||||
if (decoded < 0) {
|
||||
LOGE("base64Decode failed");
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
// "insert" 6-bits of data
|
||||
temp |= (decoded << ((3 - (i % 4)) * 6));
|
||||
|
||||
if (i % 4 == 3) {
|
||||
result[out_index++] = GET_BITS(temp, 16, 24);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
result[out_index++] = GET_BITS(temp, 0, 8);
|
||||
temp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
switch (i % 4) {
|
||||
case 1:
|
||||
LOGE("base64Decode failed");
|
||||
return std::vector<uint8_t>();
|
||||
case 2:
|
||||
result[out_index++] = GET_BITS(temp, 16, 24);
|
||||
break;
|
||||
case 3:
|
||||
result[out_index++] = GET_BITS(temp, 16, 24);
|
||||
result[out_index++] = GET_BITS(temp, 8, 16);
|
||||
break;
|
||||
}
|
||||
result.resize(out_index);
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// converts an ascii hex string(2 bytes per digit) into a decimal byte string
|
||||
std::vector<uint8_t> a2b_hex(const std::string& byte) {
|
||||
std::vector<uint8_t> array;
|
||||
size_t count = byte.size();
|
||||
if (count == 0 || (count % 2) != 0) {
|
||||
LOGE("Invalid input size %zu for string %s", count, byte.c_str());
|
||||
return array;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count / 2; ++i) {
|
||||
unsigned char msb = 0; // most significant 4 bits
|
||||
unsigned char lsb = 0; // least significant 4 bits
|
||||
if (!DecodeHexChar(byte[i * 2], &msb) ||
|
||||
!DecodeHexChar(byte[i * 2 + 1], &lsb)) {
|
||||
LOGE("Invalid hex value %c%c at index %zu", byte[i * 2], byte[i * 2 + 1],
|
||||
i);
|
||||
return array;
|
||||
}
|
||||
array.push_back((msb << 4) | lsb);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// converts an ascii hex string(2 bytes per digit) into a decimal byte string
|
||||
// dump the string with the label.
|
||||
std::vector<uint8_t> a2b_hex(const std::string& label,
|
||||
const std::string& byte) {
|
||||
std::cout << std::endl
|
||||
<< "[[DUMP: " << label << " ]= \"" << byte << "\"]" << std::endl
|
||||
<< std::endl;
|
||||
|
||||
return a2b_hex(byte);
|
||||
}
|
||||
|
||||
std::string a2bs_hex(const std::string& byte) {
|
||||
std::vector<uint8_t> array = a2b_hex(byte);
|
||||
return std::string(array.begin(), array.end());
|
||||
}
|
||||
|
||||
std::string b2a_hex(const std::vector<uint8_t>& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return HexEncode(byte.data(), byte.size());
|
||||
}
|
||||
|
||||
std::string unlimited_b2a_hex(const std::vector<uint8_t>& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return UnlimitedHexEncode(byte.data(), byte.size());
|
||||
}
|
||||
|
||||
std::string b2a_hex(const std::string& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return HexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
|
||||
byte.length());
|
||||
}
|
||||
|
||||
std::string unlimited_b2a_hex(const std::string& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return UnlimitedHexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
|
||||
byte.length());
|
||||
}
|
||||
|
||||
std::string HexEncode(const uint8_t* in_buffer, size_t size) {
|
||||
constexpr unsigned int kMaxSafeSize = 2048;
|
||||
if (size > kMaxSafeSize) size = kMaxSafeSize;
|
||||
return UnlimitedHexEncode(in_buffer, size);
|
||||
}
|
||||
|
||||
std::string UnlimitedHexEncode(const uint8_t* in_buffer, size_t size) {
|
||||
static const char kHexChars[] = "0123456789ABCDEF";
|
||||
if (size == 0) return "";
|
||||
// Each input byte creates two output hex characters.
|
||||
std::string out_buffer(size * 2, '\0');
|
||||
for (unsigned int i = 0; i < size; ++i) {
|
||||
char byte = in_buffer[i];
|
||||
out_buffer[(i << 1)] = kHexChars[(byte >> 4) & 0xf];
|
||||
out_buffer[(i << 1) + 1] = kHexChars[byte & 0xf];
|
||||
}
|
||||
return out_buffer;
|
||||
}
|
||||
|
||||
// Standard Base64 encoding and decoding.
|
||||
|
||||
std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
|
||||
if (bin_input.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
return Base64EncodeInternal(bin_input.data(), bin_input.size(), kBase64Codes);
|
||||
}
|
||||
|
||||
std::string Base64Encode(const std::string& bin_input) {
|
||||
if (bin_input.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
return Base64EncodeInternal(
|
||||
reinterpret_cast<const uint8_t*>(bin_input.data()), bin_input.size(),
|
||||
kBase64Codes);
|
||||
}
|
||||
|
||||
// Decode for standard base64 encoding (RFC4648).
|
||||
std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
|
||||
if (b64_input.empty()) {
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
return Base64DecodeInternal(b64_input.data(), b64_input.size(), kBase64Codes);
|
||||
}
|
||||
|
||||
// URL/Filename Safe Base64 encoding and decoding.
|
||||
|
||||
// This is the encoding required to interface with the provisioning server, as
|
||||
// well as for certain license server transactions. It is also used for logging
|
||||
// certain strings. The difference between web safe encoding vs regular encoding
|
||||
// is that the web safe version replaces '+' with '-' and '/' with '_'.
|
||||
std::string Base64SafeEncode(const std::vector<uint8_t>& bin_input) {
|
||||
if (bin_input.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
return Base64EncodeInternal(bin_input.data(), bin_input.size(),
|
||||
kBase64SafeCodes);
|
||||
}
|
||||
|
||||
std::string Base64SafeEncode(const std::string& bin_input) {
|
||||
if (bin_input.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
return Base64EncodeInternal(
|
||||
reinterpret_cast<const uint8_t*>(bin_input.data()), bin_input.size(),
|
||||
kBase64SafeCodes);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Base64SafeDecode(const std::string& b64_input) {
|
||||
if (b64_input.empty()) {
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
return Base64DecodeInternal(b64_input.data(), b64_input.size(),
|
||||
kBase64SafeCodes);
|
||||
}
|
||||
|
||||
// URL/Filename Safe Base64 encoding without padding.
|
||||
|
||||
std::string Base64SafeEncodeNoPad(const std::vector<uint8_t>& bin_input) {
|
||||
std::string b64_output = Base64SafeEncode(bin_input);
|
||||
// Output size: ceiling [ bin_input.size() * 4 / 3 ].
|
||||
b64_output.resize((bin_input.size() * 4 + 2) / 3);
|
||||
return b64_output;
|
||||
}
|
||||
|
||||
std::string Base64SafeEncodeNoPad(const std::string& bin_input) {
|
||||
std::string b64_output = Base64SafeEncode(bin_input);
|
||||
// Output size: ceiling [ bin_input.size() * 4 / 3 ].
|
||||
b64_output.resize((bin_input.size() * 4 + 2) / 3);
|
||||
return b64_output;
|
||||
}
|
||||
|
||||
// Host to Network/Network to Host conversion.
|
||||
|
||||
// Convert to big endian (network-byte-order)
|
||||
int64_t htonll64(int64_t x) {
|
||||
union {
|
||||
uint32_t array[2];
|
||||
int64_t number;
|
||||
} mixed;
|
||||
mixed.number = 1;
|
||||
if (mixed.array[0] == 1) { // Little Endian.
|
||||
mixed.number = x;
|
||||
uint32_t temp = mixed.array[0];
|
||||
mixed.array[0] = htonl(mixed.array[1]);
|
||||
mixed.array[1] = htonl(temp);
|
||||
return mixed.number;
|
||||
} else { // Big Endian.
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
std::string EncodeUint32(unsigned int u) {
|
||||
std::string s;
|
||||
s.push_back((u >> 24) & 0xFF);
|
||||
s.push_back((u >> 16) & 0xFF);
|
||||
s.push_back((u >> 8) & 0xFF);
|
||||
s.push_back(u & 0xFF);
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
104
wvutil/src/timer.cpp
Normal file
104
wvutil/src/timer.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
// 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.
|
||||
//
|
||||
// Timer class - provides a simple Android specific timer implementation
|
||||
|
||||
#include "timer.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <utils/Mutex.h>
|
||||
#include <utils/RefBase.h>
|
||||
#include <utils/StrongPointer.h>
|
||||
#include <utils/Thread.h>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
class Timer::Impl : virtual public android::RefBase {
|
||||
private:
|
||||
class ImplThread : public android::Thread {
|
||||
public:
|
||||
ImplThread() : Thread(false), handler_(NULL), period_ns_(0) {}
|
||||
virtual ~ImplThread() {};
|
||||
|
||||
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
|
||||
handler_ = handler;
|
||||
period_ns_ = time_in_secs * 1000000000ll;
|
||||
return run("wvutil::Timer::Impl") == android::NO_ERROR;
|
||||
}
|
||||
|
||||
void Stop() {
|
||||
{
|
||||
android::Mutex::Autolock autoLock(lock_);
|
||||
stop_condition_.signal();
|
||||
}
|
||||
requestExitAndWait();
|
||||
}
|
||||
|
||||
private:
|
||||
virtual bool threadLoop() {
|
||||
android::Mutex::Autolock autoLock(lock_);
|
||||
stop_condition_.waitRelative(lock_, period_ns_);
|
||||
handler_->OnTimerEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
TimerHandler *handler_;
|
||||
uint64_t period_ns_;
|
||||
android::Mutex lock_;
|
||||
android::Condition stop_condition_;
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(ImplThread);
|
||||
};
|
||||
|
||||
android::sp<ImplThread> impl_thread_;
|
||||
|
||||
public:
|
||||
Impl() {}
|
||||
virtual ~Impl() {};
|
||||
|
||||
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
|
||||
impl_thread_ = new ImplThread();
|
||||
return impl_thread_->Start(handler, time_in_secs);
|
||||
}
|
||||
|
||||
void Stop() {
|
||||
impl_thread_->Stop();
|
||||
impl_thread_.clear();
|
||||
}
|
||||
|
||||
bool IsRunning() {
|
||||
return (impl_thread_ != NULL) && (impl_thread_->isRunning());
|
||||
}
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(Impl);
|
||||
};
|
||||
|
||||
Timer::Timer() : impl_(new Timer::Impl()) {
|
||||
}
|
||||
|
||||
Timer::~Timer() {
|
||||
if (IsRunning())
|
||||
Stop();
|
||||
|
||||
delete impl_;
|
||||
impl_ = NULL;
|
||||
}
|
||||
|
||||
bool Timer::Start(TimerHandler *handler, uint32_t time_in_secs) {
|
||||
if (!handler || time_in_secs == 0)
|
||||
return false;
|
||||
|
||||
return impl_->Start(handler, time_in_secs);
|
||||
}
|
||||
|
||||
void Timer::Stop() {
|
||||
impl_->Stop();
|
||||
}
|
||||
|
||||
bool Timer::IsRunning() {
|
||||
return impl_->IsRunning();
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
Reference in New Issue
Block a user