Widevine MediaCas client code that works with Android R
This commit is contained in:
32
wvutil/Android.bp
Normal file
32
wvutil/Android.bp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Builds libcasutil.a
|
||||
cc_library_static {
|
||||
|
||||
name: "libcasutil",
|
||||
|
||||
proprietary: true,
|
||||
|
||||
local_include_dirs: [
|
||||
"include",
|
||||
],
|
||||
|
||||
// Filestore support is needed for building unit tests.
|
||||
|
||||
srcs: [
|
||||
"src/clock.cpp",
|
||||
"src/log.cpp",
|
||||
"src/file_store.cpp",
|
||||
"src/file_utils.cpp",
|
||||
"src/rw_lock.cpp",
|
||||
"src/string_conversions.cpp",
|
||||
"src/android_properties.cpp",
|
||||
"src/timer.cpp",
|
||||
],
|
||||
|
||||
shared_libs: [
|
||||
"liblog",
|
||||
"libutils",
|
||||
"libcrypto",
|
||||
],
|
||||
|
||||
export_include_dirs: ["include"],
|
||||
}
|
||||
46
wvutil/include/advance_iv_ctr.h
Normal file
46
wvutil/include/advance_iv_ctr.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#ifndef WV_UTIL_ADVANCE_IV_CTR_H_
|
||||
#define WV_UTIL_ADVANCE_IV_CTR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
// Advance an IV according to ISO-CENC's CTR modes. The lower half of the IV is
|
||||
// split off and treated as an unsigned 64-bit integer, then incremented by the
|
||||
// number of complete crypto blocks decrypted. The resulting value is then
|
||||
// copied back into the IV over the previous lower half.
|
||||
inline void AdvanceIvCtr(uint8_t (*subsample_iv)[16], size_t bytes) {
|
||||
constexpr size_t kAesBlockSize = 16;
|
||||
constexpr size_t kIvSize = kAesBlockSize;
|
||||
constexpr size_t kCounterIndex = kIvSize / 2;
|
||||
constexpr size_t kCounterSize = kIvSize / 2;
|
||||
|
||||
uint64_t counter;
|
||||
|
||||
static_assert(
|
||||
sizeof(*subsample_iv) == kIvSize,
|
||||
"The subsample_iv field is no longer the length of an AES-128 IV.");
|
||||
static_assert(sizeof(counter) == kCounterSize,
|
||||
"A uint64_t failed to be half the size of an AES-128 IV.");
|
||||
|
||||
// Defensive copy because the elements of the array may not be properly
|
||||
// aligned
|
||||
memcpy(&counter, &(*subsample_iv)[kCounterIndex], kCounterSize);
|
||||
|
||||
const size_t increment =
|
||||
bytes / kAesBlockSize; // The truncation here is intentional
|
||||
counter = htonll64(ntohll64(counter) + increment);
|
||||
|
||||
memcpy(&(*subsample_iv)[kCounterIndex], &counter, kCounterSize);
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WV_UTIL_ADVANCE_IV_CTR_H_
|
||||
20
wvutil/include/arraysize.h
Normal file
20
wvutil/include/arraysize.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#ifndef WVCDM_UTIL_ARRAYSIZE_H_
|
||||
#define WVCDM_UTIL_ARRAYSIZE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// Returns the size of a fixed-length array.
|
||||
template <typename T, size_t N>
|
||||
constexpr size_t ArraySize(const T (&)[N]) {
|
||||
return N;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_ARRAYSIZE_H_
|
||||
43
wvutil/include/cas_properties.h
Normal file
43
wvutil/include/cas_properties.h
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CAS_PROPERTIES_H
|
||||
#define CAS_PROPERTIES_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace wvcas {
|
||||
|
||||
// Properties methods must be implemented for a platform. The values returned
|
||||
// describe the capabilities and configuration of a device using Widevine CAS.
|
||||
class Properties {
|
||||
private:
|
||||
Properties(); // Not implemented
|
||||
~Properties(); // NotImplemented
|
||||
public:
|
||||
// Sets the |company_name| field value to be populated in and EMM license
|
||||
// request. Returns false if unable to set the value.
|
||||
static bool GetCompanyName(std::string* company_name);
|
||||
// Sets the |model_name| field value to be populated in and EMM license
|
||||
// request. Returns false if unable to set the value.
|
||||
static bool GetModelName(std::string* model_name);
|
||||
// Sets the |product_name| field value to be populated in and EMM license
|
||||
// request. Returns false if unable to set the value.
|
||||
static bool GetProductName(std::string* product_name);
|
||||
// Sets the |arch_name| field value to be populated in and EMM license
|
||||
// request. Returns false if unable to set the value.
|
||||
static bool GetArchitectureName(std::string* arch_name);
|
||||
// Sets the |device_name| field value to be populated in and EMM license
|
||||
// request. Returns false if unable to set the value.
|
||||
static bool GetDeviceName(std::string* device_name);
|
||||
// Returns a path to CAS oemcrypto library, either default,
|
||||
// or overridden through system property.
|
||||
// Returned path could be either absolute or relative.
|
||||
// Returns false if unable to set the value.
|
||||
static bool GetOEMCryptoPath(std::string* path);
|
||||
};
|
||||
|
||||
} // namespace wvcas
|
||||
|
||||
#endif // CAS_PROPERTIES_H
|
||||
117
wvutil/include/cdm_random.h
Normal file
117
wvutil/include/cdm_random.h
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
#ifndef WVCDM_CORE_CDM_RANDOM_H_
|
||||
#define WVCDM_CORE_CDM_RANDOM_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// CdmRandomGenerator is a thread safe, pseudo-random number generator.
|
||||
// It's purpose is to simplified interface for C++11's <random> library.
|
||||
// Some of the methods use a "device specific" random seed, if the
|
||||
// compiler/device does not support device specific randomizers, then the
|
||||
// actual value supplied may not be random. The generator is designed to
|
||||
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
|
||||
// be used with standard library functions / class which are designed to
|
||||
// work with the standard library generators.
|
||||
class CdmRandomGenerator {
|
||||
public:
|
||||
// Result type of operator().
|
||||
using result_type = unsigned int;
|
||||
// Inclusive boundaries of operator().
|
||||
static constexpr unsigned int min() { return 0; }
|
||||
static constexpr unsigned int max() { return RAND_MAX; }
|
||||
|
||||
// The maximum number of bytes that can be generated at once for
|
||||
// `RandomData()`.
|
||||
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
|
||||
|
||||
// Initializes the pseudo-random generator with a value from a device
|
||||
// specific random number generator.
|
||||
CdmRandomGenerator();
|
||||
|
||||
// Initializes the pseudo-random generator with the specified seed value.
|
||||
explicit CdmRandomGenerator(unsigned int seed) : generator_(seed) {}
|
||||
|
||||
// All of these methods are thread-safe.
|
||||
|
||||
// Seeds the pseudo-random generator with a value from a device specific
|
||||
// random number generator.
|
||||
void Seed();
|
||||
|
||||
// Seeds the pseudo-random generator with the specified seed value.
|
||||
// This is somewhat similar to `srand()` from the C standard library;
|
||||
// except that the sequence generated from successive calls to `Rand()`
|
||||
// will not necessarily be the same as they would be from the
|
||||
// standard library `rand()`. This is due to the underlying pseudo-random
|
||||
// generator that is used.
|
||||
void Seed(unsigned int seed);
|
||||
|
||||
// Returns a pseudo-random integer.
|
||||
// This is similar to `rand()` from the C standard library. The integer
|
||||
// returned is in the range of [min(), max()].
|
||||
unsigned int Rand();
|
||||
|
||||
// Allows for RNG to be callable.
|
||||
unsigned int operator()() { return Rand(); }
|
||||
|
||||
// Returns a pseudo-random integer within the provided inclusive range.
|
||||
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
|
||||
uint64_t RandomInRange(uint64_t upper) { return RandomInRange(0, upper); }
|
||||
|
||||
// Returns a byte string containing randomized bytes of the specified
|
||||
// length.
|
||||
// If |length| is greater than |CdmRandomGenerator::kMaxRandomDataLength|,
|
||||
// then an error is logged and an empty string is returned.
|
||||
std::string RandomData(size_t length);
|
||||
|
||||
// Random true/false using Bernoulli distribution of equal probability.
|
||||
bool RandomBool();
|
||||
|
||||
private:
|
||||
// Mutex is used to lock the object, and allowing it to be used
|
||||
// concurrently in different threads.
|
||||
std::mutex generator_lock_;
|
||||
|
||||
// The `default_random_engine` depends on the compiler used and
|
||||
// potentially its version. This is important to know if you need to
|
||||
// create reproducible tests between platforms.
|
||||
std::default_random_engine generator_;
|
||||
};
|
||||
|
||||
// Provides a static interface to a process-wide instance of
|
||||
// CdmRandomGenerator.
|
||||
class CdmRandom {
|
||||
public:
|
||||
static unsigned int Rand() { return GetInstance()->Rand(); }
|
||||
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
|
||||
return GetInstance()->RandomInRange(lower, upper);
|
||||
}
|
||||
static uint64_t RandomInRange(uint64_t upper) {
|
||||
return GetInstance()->RandomInRange(upper);
|
||||
}
|
||||
|
||||
static std::string RandomData(size_t length) {
|
||||
return GetInstance()->RandomData(length);
|
||||
}
|
||||
|
||||
static bool RandomBool() { return GetInstance()->RandomBool(); }
|
||||
|
||||
private:
|
||||
// These are intended to be used by tests if needed.
|
||||
static void Seed(unsigned int seed) { GetInstance()->Seed(seed); }
|
||||
static void Seed() { GetInstance()->Seed(); }
|
||||
|
||||
// Returns the process-wide instance of CdmRandomGenerator.
|
||||
// It the global instance has not yet been created, then a new instance
|
||||
// is created using a device-specific random seed.
|
||||
static CdmRandomGenerator* GetInstance();
|
||||
};
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_CORE_CDM_RANDOM_H_
|
||||
26
wvutil/include/clock.h
Normal file
26
wvutil/include/clock.h
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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.
|
||||
//
|
||||
// Clock - Platform independent interface for a time library
|
||||
//
|
||||
#ifndef WV_UTIL_CLOCK_H_
|
||||
#define WV_UTIL_CLOCK_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
// Provides time related information. The implementation is platform dependent.
|
||||
class Clock {
|
||||
public:
|
||||
Clock() {}
|
||||
virtual ~Clock() {}
|
||||
|
||||
// Provides the number of seconds since an epoch - 01/01/1970 00:00 UTC
|
||||
virtual int64_t GetCurrentTime();
|
||||
};
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WV_UTIL_CLOCK_H_
|
||||
17
wvutil/include/disallow_copy_and_assign.h
Normal file
17
wvutil/include/disallow_copy_and_assign.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
#ifndef WV_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
|
||||
#define WV_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
#define CORE_DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WVCAS_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
|
||||
77
wvutil/include/file_store.h
Normal file
77
wvutil/include/file_store.h
Normal file
@@ -0,0 +1,77 @@
|
||||
// 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.
|
||||
//
|
||||
// File - Platform independent interface for a File class
|
||||
//
|
||||
#ifndef WV_UTIL_FILE_STORE_H_
|
||||
#define WV_UTIL_FILE_STORE_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "disallow_copy_and_assign.h"
|
||||
#include "platform.h"
|
||||
#include "util_common.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
// File class. The implementation is platform dependent.
|
||||
class CORE_UTIL_EXPORT File {
|
||||
public:
|
||||
File() {}
|
||||
virtual ~File() {}
|
||||
virtual ssize_t Read(char* buffer, size_t bytes) = 0;
|
||||
virtual ssize_t Write(const char* buffer, size_t bytes) = 0;
|
||||
|
||||
friend class FileSystem;
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(File);
|
||||
};
|
||||
|
||||
class CORE_UTIL_EXPORT FileSystem {
|
||||
public:
|
||||
FileSystem();
|
||||
FileSystem(const std::string& origin, void* extra_data);
|
||||
virtual ~FileSystem();
|
||||
|
||||
class Impl;
|
||||
|
||||
// defines as bit flag
|
||||
enum OpenFlags {
|
||||
kNoFlags = 0,
|
||||
kCreate = 1,
|
||||
kReadOnly = 2, // defaults to read and write access
|
||||
kTruncate = 4
|
||||
};
|
||||
|
||||
virtual std::unique_ptr<File> Open(const std::string& file_path, int flags);
|
||||
|
||||
virtual bool Exists(const std::string& file_path);
|
||||
virtual bool Remove(const std::string& file_path);
|
||||
virtual ssize_t FileSize(const std::string& file_path);
|
||||
|
||||
// Return the filenames stored at dir_path.
|
||||
// dir_path will be stripped from the returned names.
|
||||
virtual bool List(const std::string& dir_path,
|
||||
std::vector<std::string>* names);
|
||||
|
||||
const std::string& origin() const { return origin_; }
|
||||
void set_origin(const std::string& origin);
|
||||
|
||||
const std::string& identifier() const { return identifier_; }
|
||||
void set_identifier(const std::string& identifier);
|
||||
bool IsGlobal() const { return identifier_.empty(); }
|
||||
|
||||
private:
|
||||
std::unique_ptr<FileSystem::Impl> impl_;
|
||||
std::string origin_;
|
||||
std::string identifier_;
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(FileSystem);
|
||||
};
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WV_UTIL_FILE_STORE_H_
|
||||
28
wvutil/include/file_utils.h
Normal file
28
wvutil/include/file_utils.h
Normal file
@@ -0,0 +1,28 @@
|
||||
// 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 <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
const char kCurrentDirectory[] = ".";
|
||||
const char kParentDirectory[] = "..";
|
||||
const char kDirectoryDelimiter = '/';
|
||||
const char kWildcard[] = "*";
|
||||
bool IsCurrentOrParentDirectory(char* dir);
|
||||
|
||||
class FileUtils {
|
||||
public:
|
||||
static bool Exists(const std::string& src);
|
||||
// The caller may only specifying a single wildcard
|
||||
static bool Remove(const std::string& src);
|
||||
static bool Copy(const std::string& src, const std::string& dest);
|
||||
static bool List(const std::string& path, std::vector<std::string>* files);
|
||||
static bool IsRegularFile(const std::string& path);
|
||||
static bool IsDirectory(const std::string& path);
|
||||
static bool CreateDirectory(const std::string& path);
|
||||
};
|
||||
|
||||
} // namespace wvutil
|
||||
54
wvutil/include/log.h
Normal file
54
wvutil/include/log.h
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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.
|
||||
//
|
||||
// Log - Platform independent interface for a Logging class
|
||||
//
|
||||
#ifndef WV_UTIL_LOG_H_
|
||||
#define WV_UTIL_LOG_H_
|
||||
|
||||
#include "util_common.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
// Simple logging class. The implementation is platform dependent.
|
||||
|
||||
typedef enum {
|
||||
// This log level should only be used for |g_cutoff|, in order to silence all
|
||||
// logging. It should never be passed to |Log()| as a log level.
|
||||
LOG_SILENT = -1,
|
||||
|
||||
LOG_ERROR = 0,
|
||||
LOG_WARN = 1,
|
||||
LOG_INFO = 2,
|
||||
LOG_DEBUG = 3,
|
||||
LOG_VERBOSE = 4,
|
||||
} LogPriority;
|
||||
|
||||
extern LogPriority g_cutoff;
|
||||
|
||||
// Enable/disable verbose logging (LOGV).
|
||||
// This function is supplied for cases where the system layer does not
|
||||
// initialize logging. This is also needed to initialize logging in
|
||||
// unit tests.
|
||||
CORE_UTIL_EXPORT void InitLogging();
|
||||
|
||||
CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line,
|
||||
LogPriority level, const char* fmt, ...);
|
||||
|
||||
// Log APIs
|
||||
#ifndef LOGE
|
||||
#define LOGE(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvutil::LOG_ERROR, __VA_ARGS__)
|
||||
#define LOGW(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvutil::LOG_WARN, __VA_ARGS__)
|
||||
#define LOGI(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvutil::LOG_INFO, __VA_ARGS__)
|
||||
#define LOGD(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvutil::LOG_DEBUG, __VA_ARGS__)
|
||||
#define LOGV(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvutil::LOG_VERBOSE, __VA_ARGS__)
|
||||
#endif
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WV_UTIL_LOG_H_
|
||||
31
wvutil/include/platform.h
Normal file
31
wvutil/include/platform.h
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
//
|
||||
// Platform - Abstracts some utilities between platforms.
|
||||
//
|
||||
#ifndef WV_UTIL_PLATFORM_H_
|
||||
#define WV_UTIL_PLATFORM_H_
|
||||
|
||||
#include "util_common.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <BaseTsd.h>
|
||||
# include <winsock2.h> // For htonl and ntohl.
|
||||
# include <wtypes.h>
|
||||
# define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
# undef NO_ERROR
|
||||
# undef GetCurrentTime
|
||||
# undef DeleteFile
|
||||
|
||||
using ssize_t = SSIZE_T;
|
||||
|
||||
inline void sleep(int seconds) { Sleep(seconds * 1000); }
|
||||
CORE_UTIL_EXPORT int setenv(const char* key, const char* value, int overwrite);
|
||||
#else
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#endif // WV_UTIL_PLATFORM_H_
|
||||
65
wvutil/include/rw_lock.h
Normal file
65
wvutil/include/rw_lock.h
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#ifndef WVCAS_UTIL_RW_LOCK_H_
|
||||
#define WVCAS_UTIL_RW_LOCK_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
#include "disallow_copy_and_assign.h"
|
||||
#include "util_common.h"
|
||||
|
||||
namespace wvcas {
|
||||
|
||||
// A simple reader-writer mutex implementation that mimics the one from C++17
|
||||
class CORE_UTIL_EXPORT shared_mutex {
|
||||
public:
|
||||
shared_mutex() : reader_count_(0), has_writer_(false) {}
|
||||
~shared_mutex();
|
||||
|
||||
// These methods take the mutex as a reader. They do not fulfill the
|
||||
// SharedMutex requirement from the C++14 STL, but they fulfill enough of it
|
||||
// to be used with |shared_lock| below.
|
||||
void lock_shared();
|
||||
void unlock_shared();
|
||||
|
||||
// These methods take the mutex as a writer. They fulfill the Mutex
|
||||
// requirement from the C++11 STL so that this mutex can be used with
|
||||
// |std::unique_lock|.
|
||||
void lock() { lock_implementation(false); }
|
||||
bool try_lock() { return lock_implementation(true); }
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
bool lock_implementation(bool abort_if_unavailable);
|
||||
|
||||
uint32_t reader_count_;
|
||||
bool has_writer_;
|
||||
|
||||
std::mutex mutex_;
|
||||
std::condition_variable condition_variable_;
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(shared_mutex);
|
||||
};
|
||||
|
||||
// A simple reader lock implementation that mimics the one from C++14
|
||||
template <typename Mutex>
|
||||
class shared_lock {
|
||||
public:
|
||||
explicit shared_lock(Mutex& lock) : lock_(&lock) { lock_->lock_shared(); }
|
||||
explicit shared_lock(Mutex* lock) : lock_(lock) { lock_->lock_shared(); }
|
||||
~shared_lock() { lock_->unlock_shared(); }
|
||||
|
||||
private:
|
||||
Mutex* lock_;
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(shared_lock);
|
||||
};
|
||||
|
||||
} // namespace wvcas
|
||||
|
||||
#endif // WVCAS_UTIL_RW_LOCK_H_
|
||||
44
wvutil/include/string_conversions.h
Normal file
44
wvutil/include/string_conversions.h
Normal file
@@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
|
||||
#ifndef WVCAS_UTIL_STRING_CONVERSIONS_H_
|
||||
#define WVCAS_UTIL_STRING_CONVERSIONS_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "util_common.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
CORE_UTIL_EXPORT std::vector<uint8_t> a2b_hex(const std::string& b);
|
||||
CORE_UTIL_EXPORT std::vector<uint8_t> a2b_hex(const std::string& label,
|
||||
const std::string& b);
|
||||
CORE_UTIL_EXPORT std::string a2bs_hex(const std::string& b);
|
||||
CORE_UTIL_EXPORT std::string b2a_hex(const std::vector<uint8_t>& b);
|
||||
CORE_UTIL_EXPORT std::string b2a_hex(const std::string& b);
|
||||
CORE_UTIL_EXPORT std::string Base64Encode(
|
||||
const std::vector<uint8_t>& bin_input);
|
||||
CORE_UTIL_EXPORT std::vector<uint8_t> Base64Decode(
|
||||
const std::string& bin_input);
|
||||
CORE_UTIL_EXPORT std::string Base64SafeEncode(
|
||||
const std::vector<uint8_t>& bin_input);
|
||||
CORE_UTIL_EXPORT std::string Base64SafeEncodeNoPad(
|
||||
const std::vector<uint8_t>& bin_input);
|
||||
CORE_UTIL_EXPORT std::vector<uint8_t> Base64SafeDecode(
|
||||
const std::string& bin_input);
|
||||
CORE_UTIL_EXPORT std::string HexEncode(const uint8_t* bytes, unsigned size);
|
||||
CORE_UTIL_EXPORT std::string IntToString(int value);
|
||||
CORE_UTIL_EXPORT int64_t htonll64(int64_t x);
|
||||
CORE_UTIL_EXPORT inline int64_t ntohll64(int64_t x) { return htonll64(x); }
|
||||
CORE_UTIL_EXPORT std::string BytesToString(const uint8_t* bytes, unsigned size);
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
CORE_UTIL_EXPORT std::string EncodeUint32(unsigned int u);
|
||||
|
||||
} // namespace wvutil
|
||||
|
||||
#endif // WVCAS_UTIL_STRING_CONVERSIONS_H_
|
||||
55
wvutil/include/timer.h
Normal file
55
wvutil/include/timer.h
Normal file
@@ -0,0 +1,55 @@
|
||||
// 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 - Platform independent interface for a Timer class
|
||||
//
|
||||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "disallow_copy_and_assign.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
// Timer Handler class.
|
||||
//
|
||||
// Derive from this class if you wish to receive events when the timer
|
||||
// expires. Provide the handler when setting up a new Timer.
|
||||
|
||||
class TimerHandler {
|
||||
public:
|
||||
TimerHandler() {};
|
||||
virtual ~TimerHandler() {};
|
||||
|
||||
virtual void OnTimerEvent() = 0;
|
||||
};
|
||||
|
||||
// Timer class. The implementation is platform dependent.
|
||||
//
|
||||
// This class provides a simple recurring timer API. The class receiving
|
||||
// timer expiry events should derive from TimerHandler.
|
||||
// Specify the receiver class and the periodicty of timer events when
|
||||
// the timer is initiated by calling Start.
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
class Impl;
|
||||
|
||||
Timer();
|
||||
~Timer();
|
||||
|
||||
bool Start(TimerHandler *handler, uint32_t time_in_secs);
|
||||
void Stop();
|
||||
bool IsRunning();
|
||||
|
||||
private:
|
||||
Impl *impl_;
|
||||
|
||||
CORE_DISALLOW_COPY_AND_ASSIGN(Timer);
|
||||
};
|
||||
|
||||
} // namespace wvcas
|
||||
|
||||
#endif // TIMER_H_
|
||||
22
wvutil/include/util_common.h
Normal file
22
wvutil/include/util_common.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// 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.
|
||||
|
||||
#ifndef WV_UTIL_UTIL_COMMON_H_
|
||||
#define WV_UTIL_UTIL_COMMON_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __declspec(dllexport)
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // WV_UTIL_UTIL_COMMON_H_
|
||||
85
wvutil/src/android_properties.cpp
Normal file
85
wvutil/src/android_properties.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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 "cas_properties.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <cutils/properties.h>
|
||||
|
||||
namespace {
|
||||
|
||||
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;
|
||||
}
|
||||
char value[PROPERTY_VALUE_MAX];
|
||||
constexpr char key[] = "cas.widevine.oemcrypto.path";
|
||||
if (property_get(key, value, "libcasoemcrypto.so") <= 0) {
|
||||
return false;
|
||||
}
|
||||
*path = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace wvcas
|
||||
107
wvutil/src/cdm_random.cpp
Normal file
107
wvutil/src/cdm_random.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine Master
|
||||
// License Agreement.
|
||||
|
||||
#include "cdm_random.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
// This type alias is for convenience.
|
||||
using CdmRandomLock = std::unique_lock<std::mutex>;
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
namespace {
|
||||
// More information about C++11's random number generators can be found
|
||||
// from the introductory paper https://isocpp.org/files/papers/n3551.pdf
|
||||
|
||||
// Attemps to get random data in a device specific manner. If the device
|
||||
// does not support true random data, then a pseudo-random sequence might
|
||||
// be used instead. The exact behaviour depends on the compiler and
|
||||
// platform combination.
|
||||
unsigned int GetDeviceRandomSeed() {
|
||||
static std::random_device rdev;
|
||||
static std::mutex rdev_mutex;
|
||||
CdmRandomLock rdev_lock(rdev_mutex);
|
||||
return rdev();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// CdmRandomGenerator.
|
||||
|
||||
CdmRandomGenerator::CdmRandomGenerator() : generator_(GetDeviceRandomSeed()) {}
|
||||
|
||||
void CdmRandomGenerator::Seed() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
generator_.seed(GetDeviceRandomSeed());
|
||||
}
|
||||
|
||||
void CdmRandomGenerator::Seed(unsigned int s) {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
generator_.seed(s);
|
||||
}
|
||||
|
||||
unsigned int CdmRandomGenerator::Rand() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::uniform_int_distribution<unsigned int> dist(0, RAND_MAX);
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
uint64_t CdmRandomGenerator::RandomInRange(uint64_t lower, uint64_t upper) {
|
||||
if (lower == upper) {
|
||||
return lower;
|
||||
}
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
if (lower > upper) {
|
||||
LOGW(
|
||||
"Lower bound is larger than upper bound, swapping bounds: "
|
||||
"lower = %llu, upper = %llu",
|
||||
// Casting to insure this will work on 32-bit systems.
|
||||
static_cast<unsigned long long>(lower),
|
||||
static_cast<unsigned long long>(upper));
|
||||
std::swap(lower, upper);
|
||||
}
|
||||
std::uniform_int_distribution<uint64_t> dist(lower, upper);
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
std::string CdmRandomGenerator::RandomData(size_t length) {
|
||||
if (length > kMaxRandomDataLength) {
|
||||
LOGE("Maximum random data length exceeded: length = %zu, max_length = %zu",
|
||||
length, kMaxRandomDataLength);
|
||||
return std::string();
|
||||
}
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::uniform_int_distribution<uint8_t> dist; // Range of [0, 255].
|
||||
std::string random_data(length, '\0');
|
||||
std::generate(random_data.begin(), random_data.end(),
|
||||
[&]() { return dist(generator_); });
|
||||
return random_data;
|
||||
}
|
||||
|
||||
bool CdmRandomGenerator::RandomBool() {
|
||||
CdmRandomLock lock(generator_lock_);
|
||||
std::bernoulli_distribution dist; // 50/50.
|
||||
return dist(generator_);
|
||||
}
|
||||
|
||||
// CdmRandom.
|
||||
|
||||
// static
|
||||
CdmRandomGenerator* CdmRandom::GetInstance() {
|
||||
static std::mutex g_instance_lock;
|
||||
static CdmRandomGenerator* g_instance = nullptr;
|
||||
CdmRandomLock lock(g_instance_lock);
|
||||
if (g_instance == nullptr) {
|
||||
LOGV("Initalizing CDM random number generator");
|
||||
g_instance = new CdmRandomGenerator(GetDeviceRandomSeed());
|
||||
}
|
||||
return g_instance;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
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 Master
|
||||
// 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
|
||||
11
wvutil/src/dllmain.cpp
Normal file
11
wvutil/src/dllmain.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
// 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.
|
||||
//
|
||||
// dllmain - A dummy DllMain method for Windows DLLs.
|
||||
//
|
||||
#include <windows.h>
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
|
||||
return TRUE;
|
||||
}
|
||||
191
wvutil/src/file_store.cpp
Normal file
191
wvutil/src/file_store.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
// 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.
|
||||
//
|
||||
// File class - provides a simple android specific file implementation
|
||||
|
||||
#include "file_store.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 <memory>
|
||||
|
||||
#include "file_utils.h"
|
||||
#include "log.h"
|
||||
#include "string_conversions.h"
|
||||
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
namespace {
|
||||
const char kCertificateFileNamePrefix[] = "cert";
|
||||
const char kCertificateFileNameExt[] = ".bin";
|
||||
const char kCertificateFileName[] = "cert.bin";
|
||||
|
||||
std::string GetFileNameSafeHash(const std::string& input) {
|
||||
std::vector<uint8_t> hash(MD5_DIGEST_LENGTH);
|
||||
const unsigned char* input_ptr =
|
||||
reinterpret_cast<const unsigned char*>(input.data());
|
||||
MD5(input_ptr, input.size(), &hash[0]);
|
||||
return 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;
|
||||
}
|
||||
|
||||
if (dir_path.empty())
|
||||
return file_name;
|
||||
else
|
||||
return dir_path + kDirectoryDelimiter + file_name;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class FileImpl : public File {
|
||||
public:
|
||||
FileImpl(FILE* file, const std::string& file_path)
|
||||
: file_(file), file_path_(file_path) {}
|
||||
|
||||
void FlushFile() {
|
||||
fflush(file_);
|
||||
fsync(fileno(file_));
|
||||
}
|
||||
|
||||
~FileImpl() {
|
||||
if (file_) {
|
||||
FlushFile();
|
||||
fclose(file_);
|
||||
file_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t Read(char* buffer, size_t bytes) override {
|
||||
if (!buffer) {
|
||||
LOGW("File::Read: buffer is empty");
|
||||
return -1;
|
||||
}
|
||||
if (!file_) {
|
||||
LOGW("File::Read: file not open");
|
||||
return -1;
|
||||
}
|
||||
size_t len = fread(buffer, sizeof(char), bytes, file_);
|
||||
if (len != bytes) {
|
||||
LOGW("File::Read: fread failed: %d, %s", errno, strerror(errno));
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
ssize_t Write(const char* buffer, size_t bytes) override {
|
||||
if (!buffer) {
|
||||
LOGW("File::Write: buffer is empty");
|
||||
return -1;
|
||||
}
|
||||
if (!file_) {
|
||||
LOGW("File::Write: file not open");
|
||||
return -1;
|
||||
}
|
||||
size_t len = fwrite(buffer, sizeof(char), bytes, file_);
|
||||
if (len != bytes) {
|
||||
LOGW("File::Write: fwrite failed: %d, %s", errno, strerror(errno));
|
||||
}
|
||||
FlushFile();
|
||||
return len;
|
||||
}
|
||||
|
||||
FILE* file_;
|
||||
std::string file_path_;
|
||||
};
|
||||
|
||||
class FileSystem::Impl {};
|
||||
|
||||
FileSystem::FileSystem() : FileSystem("", nullptr) {}
|
||||
FileSystem::FileSystem(const std::string& origin, void* /* extra_data */)
|
||||
: origin_(origin) {}
|
||||
|
||||
FileSystem::~FileSystem() {}
|
||||
|
||||
std::unique_ptr<File> FileSystem::Open(const std::string& in_name, int flags) {
|
||||
std::string open_flags;
|
||||
|
||||
std::string name = GetFileNameForIdentifier(in_name, identifier_);
|
||||
|
||||
// create the enclosing directory if it does not exist
|
||||
size_t delimiter_pos = name.rfind(kDirectoryDelimiter);
|
||||
if (delimiter_pos != std::string::npos) {
|
||||
std::string dir_path = name.substr(0, delimiter_pos);
|
||||
if ((flags & FileSystem::kCreate) && !Exists(dir_path))
|
||||
FileUtils::CreateDirectory(dir_path);
|
||||
}
|
||||
|
||||
// ensure only owners has access
|
||||
mode_t old_mask = umask(077);
|
||||
if (((flags & FileSystem::kTruncate) && Exists(name)) ||
|
||||
((flags & FileSystem::kCreate) && !Exists(name))) {
|
||||
FILE* fp = fopen(name.c_str(), "w+");
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
open_flags = (flags & FileSystem::kReadOnly) ? "rb" : "rb+";
|
||||
|
||||
FILE* file = fopen(name.c_str(), open_flags.c_str());
|
||||
umask(old_mask);
|
||||
if (!file) {
|
||||
LOGW("File::Open: fopen failed: %d, %s", errno, strerror(errno));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::unique_ptr<File>(new FileImpl(file, name));
|
||||
}
|
||||
|
||||
bool FileSystem::Exists(const std::string& path) {
|
||||
return FileUtils::Exists(GetFileNameForIdentifier(path, identifier_));
|
||||
}
|
||||
|
||||
bool FileSystem::Remove(const std::string& path) {
|
||||
return FileUtils::Remove(GetFileNameForIdentifier(path, identifier_));
|
||||
}
|
||||
|
||||
ssize_t FileSystem::FileSize(const std::string& in_path) {
|
||||
std::string path = GetFileNameForIdentifier(in_path, identifier_);
|
||||
struct stat buf;
|
||||
if (stat(path.c_str(), &buf) == 0)
|
||||
return buf.st_size;
|
||||
else
|
||||
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
|
||||
228
wvutil/src/file_utils.cpp
Normal file
228
wvutil/src/file_utils.cpp
Normal file
@@ -0,0 +1,228 @@
|
||||
// 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 "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 "file_store.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
bool IsCurrentOrParentDirectory(char* dir) {
|
||||
return strcmp(dir, kCurrentDirectory) == 0 ||
|
||||
strcmp(dir, kParentDirectory) == 0;
|
||||
}
|
||||
|
||||
bool FileUtils::Exists(const std::string& path) {
|
||||
struct stat buf;
|
||||
int res = stat(path.c_str(), &buf) == 0;
|
||||
if (!res) {
|
||||
LOGV("File::Exists: stat failed: %d, %s", errno, strerror(errno));
|
||||
}
|
||||
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);
|
||||
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
|
||||
80
wvutil/src/log.cpp
Normal file
80
wvutil/src/log.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
// 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.
|
||||
//
|
||||
// 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 1024
|
||||
|
||||
#include "log.h"
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
/*
|
||||
* 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 {
|
||||
|
||||
LogPriority g_cutoff = LOG_INFO;
|
||||
|
||||
void InitLogging() {}
|
||||
|
||||
void Log(const char* file, const char* function, int line, LogPriority level,
|
||||
const char* format, ...) {
|
||||
if (level > g_cutoff) return;
|
||||
|
||||
const char* filename = strrchr(file, '/');
|
||||
filename = filename == nullptr ? file : filename + 1;
|
||||
|
||||
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 LOG_ERROR: prio = ANDROID_LOG_ERROR; break;
|
||||
case LOG_WARN: prio = ANDROID_LOG_WARN; break;
|
||||
case LOG_INFO: prio = ANDROID_LOG_INFO; break;
|
||||
case LOG_DEBUG: prio = ANDROID_LOG_DEBUG; break;
|
||||
#if LOG_NDEBUG
|
||||
case LOG_VERBOSE: return;
|
||||
#else
|
||||
case LOG_VERBOSE: prio = ANDROID_LOG_VERBOSE; break;
|
||||
#endif
|
||||
}
|
||||
|
||||
__android_log_write(prio, LOG_TAG, buf);
|
||||
}
|
||||
|
||||
} // namespace wvutil
|
||||
21
wvutil/src/platform.cpp
Normal file
21
wvutil/src/platform.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
// 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 "platform.h"
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
int setenv(const char* key, const char* value, int overwrite) {
|
||||
if (!overwrite) {
|
||||
size_t size;
|
||||
errno_t err = getenv_s(&size, nullptr, 0, key);
|
||||
if (err != 0 || size != 0)
|
||||
return err; // Return 0 if it exists, but don't change.
|
||||
}
|
||||
return _putenv_s(key, value);
|
||||
}
|
||||
|
||||
#endif
|
||||
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 Master
|
||||
// License Agreement.
|
||||
|
||||
#include "rw_lock.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
namespace wvcas {
|
||||
|
||||
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 wvcas
|
||||
313
wvutil/src/string_conversions.cpp
Normal file
313
wvutil/src/string_conversions.cpp
Normal file
@@ -0,0 +1,313 @@
|
||||
// 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 "string_conversions.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "log.h"
|
||||
#include "platform.h"
|
||||
|
||||
namespace wvutil {
|
||||
|
||||
static const char kBase64Codes[] =
|
||||
"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)
|
||||
|
||||
int DecodeBase64Char(char c) {
|
||||
const char* it = strchr(kBase64Codes, c);
|
||||
if (it == nullptr) return -1;
|
||||
return it - kBase64Codes;
|
||||
}
|
||||
|
||||
bool DecodeHexChar(char ch, unsigned char* digit) {
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
*digit = ch - '0';
|
||||
} else {
|
||||
ch = tolower(ch);
|
||||
if ((ch >= 'a') && (ch <= 'f')) {
|
||||
*digit = ch - 'a' + 10;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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;
|
||||
unsigned int count = byte.size();
|
||||
if (count == 0 || (count % 2) != 0) {
|
||||
LOGE("Invalid input size %u for string %s", count, byte.c_str());
|
||||
return array;
|
||||
}
|
||||
|
||||
for (unsigned int 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 %d", 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 b2a_hex(const std::string& byte) {
|
||||
if (byte.empty()) return "";
|
||||
return HexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
|
||||
byte.length());
|
||||
}
|
||||
|
||||
// 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 -----------------> |
|
||||
std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
|
||||
if (bin_input.empty()) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
// |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(bin_input.size(), 3) * 4;
|
||||
std::string result(out_size, '\0');
|
||||
for (size_t i = 0; i < bin_input.size(); i++) {
|
||||
// "insert" 8-bits of data
|
||||
temp |= (bin_input[i] << ((2 - (i % 3)) * 8));
|
||||
|
||||
if (i % 3 == 2) {
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 0, 6)];
|
||||
temp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (bin_input.size() % 3 == 1) {
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = '=';
|
||||
result[out_index++] = '=';
|
||||
} else if (bin_input.size() % 3 == 2) {
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 18, 24)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 12, 18)];
|
||||
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
|
||||
result[out_index++] = '=';
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Filename-friendly base64 encoding (RFC4648), commonly referred to
|
||||
// as Base64WebSafeEncode.
|
||||
//
|
||||
// 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();
|
||||
}
|
||||
|
||||
std::string ret = Base64Encode(bin_input);
|
||||
for (size_t i = 0; i < ret.size(); i++) {
|
||||
if (ret[i] == '+')
|
||||
ret[i] = '-';
|
||||
else if (ret[i] == '/')
|
||||
ret[i] = '_';
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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>();
|
||||
}
|
||||
|
||||
const size_t out_size_max = CEIL_DIVIDE(b64_input.size() * 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 < b64_input.size(); i++) {
|
||||
if (b64_input[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 < b64_input.size(); j++) {
|
||||
if (b64_input[j] != '=') {
|
||||
LOGE("base64Decode failed");
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
const int decoded = DecodeBase64Char(b64_input[i]);
|
||||
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;
|
||||
}
|
||||
|
||||
// Decode for Filename-friendly base64 encoding (RFC4648), commonly referred
|
||||
// as Base64WebSafeDecode. Add padding if needed.
|
||||
std::vector<uint8_t> Base64SafeDecode(const std::string& b64_input) {
|
||||
if (b64_input.empty()) {
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
|
||||
// Make a copy so we can modify it to replace the web-safe special characters
|
||||
// with the normal ones.
|
||||
std::string input_copy = b64_input;
|
||||
for (size_t i = 0; i < input_copy.size(); i++) {
|
||||
if (input_copy[i] == '-')
|
||||
input_copy[i] = '+';
|
||||
else if (input_copy[i] == '_')
|
||||
input_copy[i] = '/';
|
||||
}
|
||||
return Base64Decode(input_copy);
|
||||
}
|
||||
|
||||
std::string HexEncode(const uint8_t* in_buffer, unsigned int 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;
|
||||
}
|
||||
|
||||
std::string IntToString(int value) {
|
||||
// log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
|
||||
// So round up to allocate 3 output characters per byte, plus 1 for '-'.
|
||||
const int kOutputBufSize = 3 * sizeof(int) + 1;
|
||||
char buffer[kOutputBufSize];
|
||||
memset(buffer, 0, kOutputBufSize);
|
||||
snprintf(buffer, kOutputBufSize, "%d", value);
|
||||
|
||||
std::string out_string(buffer);
|
||||
return out_string;
|
||||
}
|
||||
|
||||
int64_t htonll64(int64_t x) { // Convert to big endian (network-byte-order)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BytesToString(const uint8_t* bytes, unsigned size) {
|
||||
if (!bytes || !size) return "";
|
||||
const char* char_bytes = reinterpret_cast<const char*>(bytes);
|
||||
return std::string(char_bytes, char_bytes + size);
|
||||
}
|
||||
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
std::string EncodeUint32(unsigned int u) {
|
||||
std::string s;
|
||||
s.append(1, (u >> 24) & 0xFF);
|
||||
s.append(1, (u >> 16) & 0xFF);
|
||||
s.append(1, (u >> 8) & 0xFF);
|
||||
s.append(1, (u >> 0) & 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