Skip to content

Commit

Permalink
Add Packet::create_or_throw.
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdafu committed Jun 26, 2018
1 parent 7b908d8 commit b1bfeb4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
68 changes: 66 additions & 2 deletions lib/openpgp/packet.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
// OpenPGP format
// Copyright 2017 The NeoPG developers
// OpenPGP packet (implementation)
// Copyright 2017-2018 The NeoPG developers
//
// NeoPG is released under the Simplified BSD License (see license.txt)

#include <neopg/packet.h>

#include <neopg/marker_packet.h>
#include <neopg/public_key_packet.h>
#include <neopg/public_subkey_packet.h>
#include <neopg/raw_packet.h>
#include <neopg/signature_packet.h>
#include <neopg/user_id_packet.h>

#include <neopg/parser_input.h>
#include <neopg/stream.h>

#include <assert.h>
#include <neopg/intern/cplusplus.h>

#ifndef NDEBUG
#include <botan/hex.h>
#include <sstream>
#endif

using namespace NeoPG;

std::unique_ptr<Packet> Packet::create_or_throw(PacketType type,
ParserInput& in) {
std::unique_ptr<Packet> packet;
#ifndef NDEBUG
std::string orig_data{in.current(), in.size()};
#endif

switch (type) {
case PacketType::Marker:
packet = MarkerPacket::create_or_throw(in);
break;
case PacketType::UserId:
packet = UserIdPacket::create_or_throw(in);
break;
case PacketType::PublicKey:
packet = PublicKeyPacket::create_or_throw(in);
break;
case PacketType::PublicSubkey:
packet = PublicSubkeyPacket::create_or_throw(in);
break;
case PacketType::Signature:
packet = SignaturePacket::create_or_throw(in);
break;
default:
// Should we do this?
packet = NeoPG::make_unique<RawPacket>(
type, std::string(in.current(), in.size()));
break;
}

#ifndef NDEBUG
/// Output the packet data and verify that it outputs to
/// exactly the same bytes as the original data.
std::stringstream out;
packet->write_body(out);
std::cout << "ORIG: "
<< Botan::hex_encode((const uint8_t*)orig_data.data(),
orig_data.size())
<< "\n";
std::string o = out.str();
std::cout << "PARS: " << Botan::hex_encode((const uint8_t*)o.data(), o.size())
<< "\n";

assert(orig_data == out.str());
#endif
return packet;
}

void Packet::write(std::ostream& out,
packet_header_factory header_factory) const {
if (m_header) {
Expand Down
10 changes: 8 additions & 2 deletions lib/openpgp/packet.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// OpenPGP format
// Copyright 2017 The NeoPG developers
// OpenPGP packet
// Copyright 2017-2018 The NeoPG developers
//
// NeoPG is released under the Simplified BSD License (see license.txt)

#pragma once

#include <neopg/packet_header.h>
#include <neopg/parser_input.h>

#include <functional>
#include <memory>
Expand All @@ -16,7 +17,12 @@ using packet_header_factory = std::function<std::unique_ptr<PacketHeader>(
PacketType type, uint32_t length)>;

struct NEOPG_UNSTABLE_API Packet {
static std::unique_ptr<Packet> create_or_throw(PacketType type,
ParserInput& in);

/// Use this to overwrite the default header.
// FIXME: Replace this with a header-generator that comes in different
// flavors, see issue #66.
std::unique_ptr<PacketHeader> m_header;

/// Write the packet to \p out. If \p m_header is set, use that. Otherwise,
Expand Down

0 comments on commit b1bfeb4

Please sign in to comment.