Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1fb11386d2a1
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 47b3d5d1bcf9
Choose a head ref
  • 7 commits
  • 12 files changed
  • 2 contributors

Commits on Apr 30, 2018

  1. Copy the full SHA
    f81ccf7 View commit details
  2. Clients of Encode.

    pleroy committed Apr 30, 2018
    Copy the full SHA
    4dd80db View commit details
  3. Client changes for Decode.

    pleroy committed Apr 30, 2018
    Copy the full SHA
    0f7cb17 View commit details
  4. Fix tests.

    pleroy committed Apr 30, 2018
    Copy the full SHA
    262f1f6 View commit details
  5. Fix ownership.

    pleroy committed Apr 30, 2018
    Copy the full SHA
    994c144 View commit details
  6. Lint.

    pleroy committed Apr 30, 2018
    Copy the full SHA
    01f6c44 View commit details
  7. Merge pull request #1804 from pleroy/Hexa

    Change the API for hex encoding/decoding
    eggrobin authored Apr 30, 2018
    Copy the full SHA
    47b3d5d View commit details
11 changes: 11 additions & 0 deletions base/hexadecimal.hpp
Original file line number Diff line number Diff line change
@@ -17,6 +17,12 @@ namespace base {
inline void HexadecimalEncode(Array<std::uint8_t const> input,
Array<std::uint8_t> output);

// Same as above but the storage is allocated by the callee. If
// |null_terminated| is true a null byte is appended to the encoded form.
inline UniqueArray<std::uint8_t> HexadecimalEncode(
Array<std::uint8_t const> input,
bool null_terminated);

// Invalid digits are read as 0. If |input.size| is odd, the last character of
// the input is ignored. Ignores case. Either |output.data <= &input.data[1]|
// or |&input.data[input.size & ~1] <= output.data| must hold, in particular,
@@ -26,6 +32,11 @@ inline void HexadecimalEncode(Array<std::uint8_t const> input,
inline void HexadecimalDecode(Array<std::uint8_t const> input,
Array<std::uint8_t> output);

// Same as above but the storage is allocated by the callee. The input may or
// may not be null-terminated.
inline UniqueArray<std::uint8_t> HexadecimalDecode(
Array<std::uint8_t const> input);

} // namespace base
} // namespace principia

20 changes: 20 additions & 0 deletions base/hexadecimal_body.hpp
Original file line number Diff line number Diff line change
@@ -58,6 +58,18 @@ void HexadecimalEncode(Array<std::uint8_t const> input,
std::memcpy(output.data, &byte_to_hexadecimal_digits[*input.data << 1], 2);
}
}
UniqueArray<std::uint8_t> HexadecimalEncode(Array<std::uint8_t const> input,
bool const null_terminated) {
base::UniqueArray<std::uint8_t> output((input.size << 1) +
(null_terminated ? 1 : 0));
if (output.size > 0) {
base::HexadecimalEncode(input, output.get());
}
if (null_terminated) {
output.data[output.size - 1] = 0;
}
return output;
}

void HexadecimalDecode(Array<std::uint8_t const> input,
Array<std::uint8_t> output) {
@@ -80,5 +92,13 @@ void HexadecimalDecode(Array<std::uint8_t const> input,
}
}

UniqueArray<std::uint8_t> HexadecimalDecode(Array<std::uint8_t const> input) {
UniqueArray<std::uint8_t> output(input.size >> 1);
if (output.size > 0) {
HexadecimalDecode({ input.data, input.size & ~1 }, output.get());
}
return output;
}

} // namespace base
} // namespace principia
8 changes: 8 additions & 0 deletions base/hexadecimal_test.cpp
Original file line number Diff line number Diff line change
@@ -52,6 +52,14 @@ TEST_F(HexadecimalTest, EncodeAndDecode) {
EXPECT_EQ(bytes_, bytes);
}

TEST_F(HexadecimalTest, UniqueEncodeAndDecode) {
auto const digits =
HexadecimalEncode(bytes_.get(), /*null_terminated=*/false);
EXPECT_EQ(uppercase_digits_, digits);
auto const bytes = HexadecimalDecode(digits.get());
EXPECT_EQ(bytes_, bytes);
}

TEST_F(HexadecimalTest, InPlace) {
auto buffer = std::make_unique<std::uint8_t[]>(digit_count);
std::memcpy(&buffer[1], bytes_.data.get(), byte_count);
3 changes: 3 additions & 0 deletions base/push_deserializer.hpp
Original file line number Diff line number Diff line change
@@ -81,6 +81,9 @@ class PushDeserializer final {
// associated with |bytes| in |done|.
void Push(Bytes bytes, std::function<void()> done);

// Same as above but ownership is taken.
void Push(UniqueBytes bytes);

private:
// Obtains the next chunk of data from the internal queue. Blocks if no data
// is available. Used as a callback for the underlying
7 changes: 7 additions & 0 deletions base/push_deserializer_body.hpp
Original file line number Diff line number Diff line change
@@ -169,6 +169,13 @@ inline void PushDeserializer::Push(Bytes const bytes,
} while (!is_last);
}

inline void PushDeserializer::Push(UniqueBytes bytes) {
Bytes const unowned_bytes = bytes.get();
bytes.data.release();
Push(unowned_bytes,
/*done=*/[b = unowned_bytes.data]() { delete[] b; });
}

inline Bytes PushDeserializer::Pull() {
Bytes result;
{
4 changes: 1 addition & 3 deletions journal/player.cpp
Original file line number Diff line number Diff line change
@@ -73,9 +73,7 @@ std::unique_ptr<serialization::Method> Player::Read() {
std::uint8_t const* const hexadecimal =
reinterpret_cast<std::uint8_t const*>(line.c_str());
int const hexadecimal_size = strlen(line.c_str());
UniqueBytes bytes(hexadecimal_size >> 1);
HexadecimalDecode({hexadecimal, hexadecimal_size},
{bytes.data.get(), bytes.size});
auto const bytes = HexadecimalDecode({hexadecimal, hexadecimal_size});
auto method = std::make_unique<serialization::Method>();
CHECK(method->ParseFromArray(bytes.data.get(),
static_cast<int>(bytes.size)));
9 changes: 3 additions & 6 deletions journal/recorder.cpp
Original file line number Diff line number Diff line change
@@ -28,12 +28,9 @@ void Recorder::Write(serialization::Method const& method) {
UniqueBytes bytes(method.ByteSize());
method.SerializeToArray(bytes.data.get(), static_cast<int>(bytes.size));

std::int64_t const hexadecimal_size = (bytes.size << 1) + 2;
UniqueBytes hexadecimal(hexadecimal_size);
HexadecimalEncode({bytes.data.get(), bytes.size}, hexadecimal.get());
hexadecimal.data.get()[hexadecimal_size - 2] = '\n';
hexadecimal.data.get()[hexadecimal_size - 1] = '\0';
stream_ << hexadecimal.data.get();
auto const hexadecimal = HexadecimalEncode(bytes.get(),
/*null_terminated=*/true);
stream_ << hexadecimal.data.get() << "\n";
stream_.flush();
}

19 changes: 5 additions & 14 deletions ksp_plugin/interface.cpp
Original file line number Diff line number Diff line change
@@ -359,19 +359,13 @@ void principia__DeserializePlugin(char const* const serialization,
std::uint8_t const* const hexadecimal =
reinterpret_cast<std::uint8_t const*>(serialization);
int const hexadecimal_size = serialization_size;
int const byte_size = hexadecimal_size >> 1;
// Ownership of the following pointer is transfered to the deserializer using
// the callback to |Push|.
std::uint8_t* bytes = new std::uint8_t[byte_size];
HexadecimalDecode({hexadecimal, hexadecimal_size}, {bytes, byte_size});

// Push the data, taking ownership of it.
(*deserializer)->Push(Bytes(&bytes[0], byte_size),
[bytes]() { delete[] bytes; });
auto bytes = HexadecimalDecode({hexadecimal, hexadecimal_size});
auto const bytes_size = bytes.size;
(*deserializer)->Push(std::move(bytes));

// If the data was empty, delete the deserializer. This ensures that
// |*plugin| is filled.
if (byte_size == 0) {
if (bytes_size == 0) {
LOG(INFO) << "End plugin deserialization";
TakeOwnership(deserializer);
}
@@ -857,10 +851,7 @@ char const* principia__SerializePlugin(Plugin const* const plugin,
}

// Convert to hexadecimal and return to the client.
std::int64_t const hexadecimal_size = (bytes.size << 1) + 1;
UniqueBytes hexadecimal(hexadecimal_size);
HexadecimalEncode(bytes, hexadecimal.get());
hexadecimal.data.get()[hexadecimal_size - 1] = '\0';
auto hexadecimal = HexadecimalEncode(bytes, /*null_terminated=*/true);
return m.Return(reinterpret_cast<char const*>(hexadecimal.data.release()));
}

4 changes: 1 addition & 3 deletions ksp_plugin/part.cpp
Original file line number Diff line number Diff line change
@@ -216,11 +216,9 @@ void Part::FillContainingPileUpFromMessage(
}

std::string Part::ShortDebugString() const {
UniqueBytes hex_id(sizeof(part_id_) * 2 + 1);
Array<std::uint8_t const> id_bytes(
reinterpret_cast<std::uint8_t const*>(&part_id_), sizeof(part_id_));
HexadecimalEncode(id_bytes, hex_id.get());
hex_id.data[sizeof(part_id_) * 2] = '\0';
auto const hex_id = HexadecimalEncode(id_bytes, /*null_terminated=*/true);
return name_ + " (" + reinterpret_cast<char const*>(hex_id.data.get()) + ")";
}

6 changes: 2 additions & 4 deletions ksp_plugin/plugin.cpp
Original file line number Diff line number Diff line change
@@ -268,12 +268,10 @@ void Plugin::EndInitialization() {
serialization::Ephemeris ephemeris_message;
ephemeris_->WriteToMessage(&ephemeris_message);
std::string const bytes = ephemeris_message.SerializeAsString();
base::UniqueArray<std::uint8_t> const hex((bytes.size() << 1) + 1);
base::HexadecimalEncode(
auto const hex = base::HexadecimalEncode(
base::Array<std::uint8_t const>(
reinterpret_cast<std::uint8_t const*>(bytes.data()), bytes.size()),
hex.get());
hex.data[hex.size - 1] = 0;
/*null_terminated=*/true);
// Begin and end markers to make sure the hex did not get clipped (this might
// happen if the message is very big).
LOG(INFO) << "Ephemeris at initialization:\nbegin\n"
6 changes: 2 additions & 4 deletions ksp_plugin_test/plugin_compatibility_test.cpp
Original file line number Diff line number Diff line change
@@ -92,11 +92,9 @@ class PluginCompatibilityTest : public testing::Test {
file.close();

// Parse the hexadecimal data and convert it to binary data.
UniqueBytes bin(hex.size() / 2);
HexadecimalDecode(
auto const bin = HexadecimalDecode(
Array<std::uint8_t const>(
reinterpret_cast<std::uint8_t const*>(hex.c_str()), hex.size()),
bin.get());
reinterpret_cast<std::uint8_t const*>(hex.c_str()), hex.size()));

// Construct a protocol buffer from the binary data.
google::protobuf::io::CodedInputStream coded_input_stream(
4 changes: 1 addition & 3 deletions mathematica/retrobop_dynamical_stability.cpp
Original file line number Diff line number Diff line change
@@ -152,9 +152,7 @@ std::unique_ptr<Message> Read(std::ifstream& file) {
std::uint8_t const* const hexadecimal =
reinterpret_cast<std::uint8_t const*>(line.data());
int const hexadecimal_size = line.size();
UniqueBytes bytes(hexadecimal_size >> 1);
HexadecimalDecode({hexadecimal, hexadecimal_size},
{bytes.data.get(), bytes.size});
auto const bytes = HexadecimalDecode({hexadecimal, hexadecimal_size});
auto message = std::make_unique<Message>();
CHECK(
message->ParseFromArray(bytes.data.get(), static_cast<int>(bytes.size)));