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 May 21, 2018
1 parent 9c5c950 commit b3023a6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
33 changes: 31 additions & 2 deletions lib/openpgp/packet.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
// 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/raw_packet.h>
#include <neopg/signature_packet.h>
#include <neopg/user_id_packet.h>

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

#include <neopg/intern/cplusplus.h>

using namespace NeoPG;

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

void Packet::write(std::ostream& out) const {
if (m_header) {
m_header->write(out);
Expand Down
10 changes: 8 additions & 2 deletions lib/openpgp/packet.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// 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 <memory>

namespace NeoPG {

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;

void write(std::ostream& out) const;
Expand Down

0 comments on commit b3023a6

Please sign in to comment.