summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-09-11 12:44:04 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-09-11 12:44:04 +0200
commitdd7b4138f2dba9abcff0ba8302fefb768d3f59c3 (patch)
treec4fd48e382d809f63502f9a12b420e88638b50b7 /openpgp
parent5ca99787dce34100e43b8e1a2bf51454c9a6601a (diff)
openpgp: Move packet::BodyLength to packet::header.
- Move the parser to the parse module.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/examples/statistics.rs2
-rw-r--r--openpgp/src/armor.rs2
-rw-r--r--openpgp/src/packet/header/ctb.rs2
-rw-r--r--openpgp/src/packet/header/mod.rs25
-rw-r--r--openpgp/src/packet/mod.rs122
-rw-r--r--openpgp/src/packet/user_attribute.rs2
-rw-r--r--openpgp/src/parse/parse.rs95
-rw-r--r--openpgp/src/parse/stream.rs2
-rw-r--r--openpgp/src/serialize/stream.rs2
9 files changed, 125 insertions, 129 deletions
diff --git a/openpgp/examples/statistics.rs b/openpgp/examples/statistics.rs
index 14a2ae90..9a3d3ed6 100644
--- a/openpgp/examples/statistics.rs
+++ b/openpgp/examples/statistics.rs
@@ -11,7 +11,7 @@ use std::env;
extern crate sequoia_openpgp as openpgp;
use crate::openpgp::Packet;
use crate::openpgp::constants::SignatureType;
-use crate::openpgp::packet::{user_attribute, BodyLength, Tag};
+use crate::openpgp::packet::{user_attribute, header::BodyLength, Tag};
use crate::openpgp::parse::{Parse, PacketParserResult, PacketParser};
fn main() {
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index 74e2b8ba..68d53176 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -36,7 +36,7 @@ use std::borrow::Cow;
use quickcheck::{Arbitrary, Gen};
use crate::packet::prelude::*;
-use crate::packet::BodyLength;
+use crate::packet::header::BodyLength;
use crate::packet::header::ctb::{CTBNew, CTBOld};
use crate::serialize::SerializeInto;
diff --git a/openpgp/src/packet/header/ctb.rs b/openpgp/src/packet/header/ctb.rs
index 50705c6f..6e43fecf 100644
--- a/openpgp/src/packet/header/ctb.rs
+++ b/openpgp/src/packet/header/ctb.rs
@@ -11,7 +11,7 @@ use crate::{
Error,
Result
};
-use crate::packet::BodyLength;
+use crate::packet::header::BodyLength;
/// OpenPGP defines two packet formats: the old and the new format.
/// They both include the packet's so-called tag.
diff --git a/openpgp/src/packet/header/mod.rs b/openpgp/src/packet/header/mod.rs
index 680d34e5..c799959a 100644
--- a/openpgp/src/packet/header/mod.rs
+++ b/openpgp/src/packet/header/mod.rs
@@ -5,7 +5,6 @@ use crate::{
Result,
};
use crate::packet::tag::Tag;
-pub use crate::packet::BodyLength;
pub mod ctb;
pub use self::ctb::CTB;
@@ -197,3 +196,27 @@ impl Header {
Ok(())
}
}
+
+/// The size of a packet.
+///
+/// A packet's size can be expressed in three different ways. Either
+/// the size of the packet is fully known (Full), the packet is
+/// chunked using OpenPGP's partial body encoding (Partial), or the
+/// packet extends to the end of the file (Indeterminate). See
+/// [Section 4.2 of RFC 4880] for more details.
+///
+/// [Section 4.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2
+#[derive(Debug)]
+// We need PartialEq so that assert_eq! works.
+#[derive(PartialEq)]
+#[derive(Clone, Copy)]
+pub enum BodyLength {
+ /// Packet size is fully known.
+ Full(u32),
+ /// The parameter is the number of bytes in the current chunk.
+ /// This type is only used with new format packets.
+ Partial(u32),
+ /// The packet extends until an EOF is encountered. This type is
+ /// only used with old format packets.
+ Indeterminate,
+}
diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs
index 1f5dae54..36157854 100644
--- a/openpgp/src/packet/mod.rs
+++ b/openpgp/src/packet/mod.rs
@@ -8,7 +8,6 @@ use std::fmt;
use std::ops::{Deref, DerefMut};
use std::slice;
use std::vec;
-use std::io;
use crate::Error;
use crate::Result;
@@ -16,11 +15,8 @@ use crate::Packet;
pub mod prelude;
-use self::header::ctb::PacketLengthType;
use crate::crypto::KeyPair;
-use buffered_reader::BufferedReader;
-
mod tag;
pub use self::tag::Tag;
pub mod header;
@@ -112,124 +108,6 @@ impl<'a> DerefMut for Packet {
}
}
-/// The size of a packet.
-///
-/// A packet's size can be expressed in three different ways. Either
-/// the size of the packet is fully known (Full), the packet is
-/// chunked using OpenPGP's partial body encoding (Partial), or the
-/// packet extends to the end of the file (Indeterminate). See
-/// [Section 4.2 of RFC 4880] for more details.
-///
-/// [Section 4.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2
-#[derive(Debug)]
-// We need PartialEq so that assert_eq! works.
-#[derive(PartialEq)]
-#[derive(Clone, Copy)]
-pub enum BodyLength {
- /// Packet size is fully known.
- Full(u32),
- /// The parameter is the number of bytes in the current chunk.
- /// This type is only used with new format packets.
- Partial(u32),
- /// The packet extends until an EOF is encountered. This type is
- /// only used with old format packets.
- Indeterminate,
-}
-
-impl BodyLength {
- /// Decodes a new format body length as described in [Section
- /// 4.2.2 of RFC 4880].
- ///
- /// [Section 4.2.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2.2
- pub(crate) fn parse_new_format<T: BufferedReader<C>, C> (bio: &mut T)
- -> io::Result<BodyLength>
- {
- let octet1 : u8 = bio.data_consume_hard(1)?[0];
- match octet1 {
- 0...191 => // One octet.
- Ok(BodyLength::Full(octet1 as u32)),
- 192...223 => { // Two octets length.
- let octet2 = bio.data_consume_hard(1)?[0];
- Ok(BodyLength::Full(((octet1 as u32 - 192) << 8)
- + octet2 as u32 + 192))
- },
- 224...254 => // Partial body length.
- Ok(BodyLength::Partial(1 << (octet1 & 0x1F))),
- 255 => // Five octets.
- Ok(BodyLength::Full(bio.read_be_u32()?)),
- }
- }
-
- /// Decodes an old format body length as described in [Section
- /// 4.2.1 of RFC 4880].
- ///
- /// [Section 4.2.1 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2.1
- pub(crate) fn parse_old_format<T: BufferedReader<C>, C>
- (bio: &mut T, length_type: PacketLengthType)
- -> Result<BodyLength>
- {
- match length_type {
- PacketLengthType::OneOctet =>
- Ok(BodyLength::Full(bio.data_consume_hard(1)?[0] as u32)),
- PacketLengthType::TwoOctets =>
- Ok(BodyLength::Full(bio.read_be_u16()? as u32)),
- PacketLengthType::FourOctets =>
- Ok(BodyLength::Full(bio.read_be_u32()? as u32)),
- PacketLengthType::Indeterminate =>
- Ok(BodyLength::Indeterminate),
- }
- }
-}
-
-#[test]
-fn body_length_new_format() {
- fn test(input: &[u8], expected_result: BodyLength) {
- assert_eq!(
- BodyLength::parse_new_format(
- &mut buffered_reader::Memory::new(input)).unwrap(),
- expected_result);
- }
-
- // Examples from Section 4.2.3 of RFC4880.
-
- // Example #1.
- test(&[0x64][..], BodyLength::Full(100));
-
- // Example #2.
- test(&[0xC5, 0xFB][..], BodyLength::Full(1723));
-
- // Example #3.
- test(&[0xFF, 0x00, 0x01, 0x86, 0xA0][..], BodyLength::Full(100000));
-
- // Example #4.
- test(&[0xEF][..], BodyLength::Partial(32768));
- test(&[0xE1][..], BodyLength::Partial(2));
- test(&[0xF0][..], BodyLength::Partial(65536));
- test(&[0xC5, 0xDD][..], BodyLength::Full(1693));
-}
-
-#[test]
-fn body_length_old_format() {
- fn test(input: &[u8], plt: PacketLengthType,
- expected_result: BodyLength, expected_rest: &[u8]) {
- let mut bio = buffered_reader::Memory::new(input);
- assert_eq!(BodyLength::parse_old_format(&mut bio, plt).unwrap(),
- expected_result);
- let rest = bio.data_eof();
- assert_eq!(rest.unwrap(), expected_rest);
- }
-
- test(&[1], PacketLengthType::OneOctet, BodyLength::Full(1), &b""[..]);
- test(&[1, 2], PacketLengthType::TwoOctets,
- BodyLength::Full((1 << 8) + 2), &b""[..]);
- test(&[1, 2, 3, 4], PacketLengthType::FourOctets,
- BodyLength::Full((1 << 24) + (2 << 16) + (3 << 8) + 4), &b""[..]);
- test(&[1, 2, 3, 4, 5, 6], PacketLengthType::FourOctets,
- BodyLength::Full((1 << 24) + (2 << 16) + (3 << 8) + 4), &[5, 6][..]);
- test(&[1, 2, 3, 4], PacketLengthType::Indeterminate,
- BodyLength::Indeterminate, &[1, 2, 3, 4][..]);
-}
-
/// Fields used by multiple packet types.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Common {
diff --git a/openpgp/src/packet/user_attribute.rs b/openpgp/src/packet/user_attribute.rs
index 8c3bdebb..4e50e1dc 100644
--- a/openpgp/src/packet/user_attribute.rs
+++ b/openpgp/src/packet/user_attribute.rs
@@ -16,7 +16,7 @@ use crate::Error;
use crate::Result;
use crate::packet::{
self,
- BodyLength,
+ header::BodyLength,
};
use crate::Packet;
use crate::serialize::Serialize;
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index 37acc8b6..9161c0a8 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -18,6 +18,7 @@ use crate::{
packet::header::{
CTB,
BodyLength,
+ ctb::PacketLengthType,
},
crypto::s2k::S2K,
Error,
@@ -816,6 +817,100 @@ impl<'a> Parse<'a, Header> for Header {
}
}
+impl BodyLength {
+ /// Decodes a new format body length as described in [Section
+ /// 4.2.2 of RFC 4880].
+ ///
+ /// [Section 4.2.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2.2
+ pub(crate) fn parse_new_format<T: BufferedReader<C>, C> (bio: &mut T)
+ -> io::Result<BodyLength>
+ {
+ let octet1 : u8 = bio.data_consume_hard(1)?[0];
+ match octet1 {
+ 0...191 => // One octet.
+ Ok(BodyLength::Full(octet1 as u32)),
+ 192...223 => { // Two octets length.
+ let octet2 = bio.data_consume_hard(1)?[0];
+ Ok(BodyLength::Full(((octet1 as u32 - 192) << 8)
+ + octet2 as u32 + 192))
+ },
+ 224...254 => // Partial body length.
+ Ok(BodyLength::Partial(1 << (octet1 & 0x1F))),
+ 255 => // Five octets.
+ Ok(BodyLength::Full(bio.read_be_u32()?)),
+ }
+ }
+
+ /// Decodes an old format body length as described in [Section
+ /// 4.2.1 of RFC 4880].
+ ///
+ /// [Section 4.2.1 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-4.2.1
+ pub(crate) fn parse_old_format<T: BufferedReader<C>, C>
+ (bio: &mut T, length_type: PacketLengthType)
+ -> Result<BodyLength>
+ {
+ match length_type {
+ PacketLengthType::OneOctet =>
+ Ok(BodyLength::Full(bio.data_consume_hard(1)?[0] as u32)),
+ PacketLengthType::TwoOctets =>
+ Ok(BodyLength::Full(bio.read_be_u16()? as u32)),
+ PacketLengthType::FourOctets =>
+ Ok(BodyLength::Full(bio.read_be_u32()? as u32)),
+ PacketLengthType::Indeterminate =>
+ Ok(BodyLength::Indeterminate),
+ }
+ }
+}
+
+#[test]
+fn body_length_new_format() {
+ fn test(input: &[u8], expected_result: BodyLength) {
+ assert_eq!(
+ BodyLength::parse_new_format(
+ &mut buffered_reader::Memory::new(input)).unwrap(),
+ expected_result);
+ }
+
+ // Examples from Section 4.2.3 of RFC4880.
+
+ // Example #1.
+ test(&[0x64][..], BodyLength::Full(100));
+
+ // Example #2.
+ test(&[0xC5, 0xFB][..], BodyLength::Full(1723));
+
+ // Example #3.
+ test(&[0xFF, 0x00, 0x01, 0x86, 0xA0][..], BodyLength::Full(100000));
+
+ // Example #4.
+ test(&[0xEF][..], BodyLength::Partial(32768));
+ test(&[0xE1][..], BodyLength::Partial(2));
+ test(&[0xF0][..], BodyLength::Partial(65536));
+ test(&[0xC5, 0xDD][..], BodyLength::Full(1693));
+}
+
+#[test]
+fn body_length_old_format() {
+ fn test(input: &[u8], plt: PacketLengthType,
+ expected_result: BodyLength, expected_rest: &[u8]) {
+ let mut bio = buffered_reader::Memory::new(input);
+ assert_eq!(BodyLength::parse_old_format(&mut bio, plt).unwrap(),
+ expected_result);
+ let rest = bio.data_eof();
+ assert_eq!(rest.unwrap(), expected_rest);
+ }
+
+ test(&[1], PacketLengthType::OneOctet, BodyLength::Full(1), &b""[..]);
+ test(&[1, 2], PacketLengthType::TwoOctets,
+ BodyLength::Full((1 << 8) + 2), &b""[..]);
+ test(&[1, 2, 3, 4], PacketLengthType::FourOctets,
+ BodyLength::Full((1 << 24) + (2 << 16) + (3 << 8) + 4), &b""[..]);
+ test(&[1, 2, 3, 4, 5, 6], PacketLengthType::FourOctets,
+ BodyLength::Full((1 << 24) + (2 << 16) + (3 << 8) + 4), &[5, 6][..]);
+ test(&[1, 2, 3, 4], PacketLengthType::Indeterminate,
+ BodyLength::Indeterminate, &[1, 2, 3, 4][..]);
+}
+
impl Unknown {
/// Parses the body of any packet and returns an Unknown.
fn parse<'a>(php: PacketHeaderParser<'a>, error: failure::Error)
diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs
index 2d6f0a70..d0805643 100644
--- a/openpgp/src/parse/stream.rs
+++ b/openpgp/src/parse/stream.rs
@@ -26,7 +26,7 @@ use crate::{
SymmetricAlgorithm,
},
packet::{
- BodyLength,
+ header::BodyLength,
header::CTB,
key,
Key,
diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs
index 21055184..59fc5f96 100644
--- a/openpgp/src/serialize/stream.rs
+++ b/openpgp/src/serialize/stream.rs
@@ -32,7 +32,7 @@ use crate::{
TPK,
};
use crate::packet::header::CTB;
-use crate::packet::BodyLength;
+use crate::packet::header::BodyLength;
use super::{
PartialBodyFilter,
Serialize,