Initial v16 ODK Library

This commit has the initial ODK library.  Partners may use this code
to begin integrating the ODK library into their platform.  The
functionality is not complete, but this should help partners get an
early start playing with build files.
This commit is contained in:
Fred Gylys-Colwell
2019-10-04 14:10:55 -07:00
parent ded4417dd4
commit 4de11d11e8
65 changed files with 4371 additions and 2004 deletions

107
util/src/cdm_random.cpp Normal file
View 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);
}
int CdmRandomGenerator::Rand() {
CdmRandomLock lock(generator_lock_);
std::uniform_int_distribution<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

View File

@@ -6,7 +6,6 @@
//
#include <windows.h>
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved) {
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
return TRUE;
}

View File

@@ -8,6 +8,7 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
@@ -24,12 +25,11 @@ static const char kBase64Codes[] =
// 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)
#define CEIL_DIVIDE(a, b) ((((a)-1) / (b)) + 1)
int DecodeBase64Char(char c) {
const char* it = strchr(kBase64Codes, c);
if (it == NULL)
return -1;
if (it == nullptr) return -1;
return it - kBase64Codes;
}
@@ -121,8 +121,8 @@ std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
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)];
result[out_index++] = kBase64Codes[GET_BITS(temp, 6, 12)];
result[out_index++] = kBase64Codes[GET_BITS(temp, 0, 6)];
temp = 0;
}
}
@@ -135,7 +135,7 @@ std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
} 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++] = kBase64Codes[GET_BITS(temp, 6, 12)];
result[out_index++] = '=';
}
@@ -208,8 +208,8 @@ std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
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);
result[out_index++] = GET_BITS(temp, 8, 16);
result[out_index++] = GET_BITS(temp, 0, 8);
temp = 0;
}
}
@@ -223,7 +223,7 @@ std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
break;
case 3:
result[out_index++] = GET_BITS(temp, 16, 24);
result[out_index++] = GET_BITS(temp, 8, 16);
result[out_index++] = GET_BITS(temp, 8, 16);
break;
}
result.resize(out_index);
@@ -298,4 +298,14 @@ std::string BytesToString(const uint8_t* bytes, unsigned size) {
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 wvcdm