ODK and Shared Libraries

In this code drop we introduce the ODK dependency. The reference
implementation has been updated to make use of the ODK and the related
tests have been included.

In addition, we have included an example of how a shared libraries can
be created. This will allow make it easier to test and verify different
implementations of the API.

Most other changes introduce by this code drop were made to clean-up the
reference implementation and limit dependencies.
This commit is contained in:
Aaron Vaage
2020-07-23 16:13:28 -07:00
parent 5d90e8d89b
commit 789377fed2
37 changed files with 1160 additions and 1127 deletions

View File

@@ -1,63 +0,0 @@
// Copyright 2020 Google LLC. All Rights Reserved.
#include "base/base64.h"
#include <stddef.h>
#include <stdint.h>
namespace base {
namespace {
static const char kBase64Codes[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// Calculates a/b using round-up division (only works for numbers
// greater than 0).
constexpr size_t CeilDivide(size_t a, size_t b) {
return ((a - 1) / b) + 1;
}
} // namespace
void Base64Encode(const std::string& input, std::string* output) {
if (input.empty()) {
output->clear();
return;
}
// |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 = CeilDivide(input.size(), 3) * 4;
std::string result(out_size, '\0');
for (size_t i = 0; i < input.size(); i++) {
// "insert" 8-bits of data
temp = (temp << 8) | (input[i] & 0x0FF);
if (i % 3 == 2) {
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
result[out_index++] = kBase64Codes[(temp >> 6) & 0x3f];
result[out_index++] = kBase64Codes[temp & 0x3f];
temp = 0;
}
}
if (input.size() % 3 == 1) {
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
result[out_index++] = '=';
result[out_index++] = '=';
} else if (input.size() % 3 == 2) {
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
result[out_index++] = kBase64Codes[(temp >> 6) & 0x3f];
result[out_index++] = '=';
}
output->swap(result);
}
} // namespace base

View File

@@ -1,18 +0,0 @@
// Copyright 2020 Google LLC. All Rights Reserved.
#ifndef BASE_BASE64_H_
#define BASE_BASE64_H_
#include <string>
namespace base {
// Encodes the input string in base64.
// Note that the Chromium code uses base::StringPiece for |input|, but to
// avoid dragging in too much code, use std::string instead (which is
// convertable to base::StringPiece automatically).
void Base64Encode(const std::string& input, std::string* output);
} // namespace base
#endif // BASE_BASE64_H_

View File

@@ -1,14 +0,0 @@
// Copyright 2020 Google LLC. All Rights Reserved.
#include "base/strings/string_number_conversions.h"
#include "absl/strings/escaping.h"
namespace base {
std::string HexEncode(const void* bytes, size_t size) {
return absl::BytesToHexString(
std::string(reinterpret_cast<const char*>(bytes), size));
}
} // namespace base

View File

@@ -1,17 +0,0 @@
// Copyright 2020 Google LLC. All Rights Reserved.
#ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
#define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
namespace base {
// Returns a hex string representation of a binary buffer.
std::string HexEncode(const void* bytes, size_t size);
} // namespace base
#endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_