summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-26 03:08:42 +0200
committero <o@immerda.ch>2011-06-26 03:08:42 +0200
commit464d394a6f1b2fe180364e880badd49c5105e458 (patch)
tree807addbd2e752e8a33ffcf7ff1bf985a88d46847
parent8b325e7d08b267be508dbb5fc0b655fdcd447216 (diff)
major cleanup
implemented with_tag protocol to select a subclass depending on a tag.
-rw-r--r--lib/fpg/parse/_packet_header_numbers.rb51
-rw-r--r--lib/fpg/parse/packet.rb53
-rw-r--r--lib/fpg/parse/packet_header.rb39
-rw-r--r--lib/fpg/parse/parser.rb2
-rw-r--r--lib/fpg/parse/public_key.rb3
-rw-r--r--lib/fpg/parse/public_key_encrypted_session_key.rb3
-rw-r--r--lib/fpg/parse/public_subkey.rb3
-rw-r--r--lib/fpg/parse/secret_key.rb3
-rw-r--r--lib/fpg/parse/secret_subkey.rb3
-rw-r--r--lib/fpg/parse/signature.rb3
-rw-r--r--lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb3
-rw-r--r--lib/fpg/parse/user_id.rb3
12 files changed, 75 insertions, 94 deletions
diff --git a/lib/fpg/parse/_packet_header_numbers.rb b/lib/fpg/parse/_packet_header_numbers.rb
deleted file mode 100644
index 560d9e7..0000000
--- a/lib/fpg/parse/_packet_header_numbers.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-module FPG
- module Parse
- module PacketHeaderNumbers
- def packet_for( byte )
- case byte
- when 0
- raise "a packet tag must not be 0"
- when 1
- PublicKeyEncryptedSessionKey.new
- when 2
- Signature.new
- when 3
- SymmetricKeyEncryptedSession.new
- when 4
- OnePassSignature.new
- when 5
- SecretKey.new
- when 6
- PublicKey.new
- when 7
- SecretSubkey.new
- when 8
- CompressedData.new
- when 9
- SymmetricallyEncryptedData.new
- when 10
- Marker.new
- when 11
- Literal.new
- when 12
- Trust.new
- when 13
- UserId.new
- when 14
- PublicSubkey.new
- when 17
- UserAttribute.new
- when 18
- SymmetricEncryptedAndIntegrityProtectedData.new
- when 19
- MudificationDetectionCode.new
- when 60..63
- raise 'Private or Experimental Header detected'
- else
- raise byte.to_s+' is no valid Header'
- end
- end
- end
- end
-end
-
diff --git a/lib/fpg/parse/packet.rb b/lib/fpg/parse/packet.rb
index b35f4ff..4fd832b 100644
--- a/lib/fpg/parse/packet.rb
+++ b/lib/fpg/parse/packet.rb
@@ -1,11 +1,46 @@
module FPG
module Parse
module PacketParser
+ module PacketParserKlass
+ def parse( stream )
+ header = stream.getbyte
+ stream.seek(-1,IO::SEEK_CUR)
+ unless packet_header?(header)
+ debug_header_search(stream)
+ end
+ with_tag(packet_number(header)).parse(stream)
+ end
+ def packet_number(byte)
+ if new_packet?(byte)
+ byte-(1<<7)-(1<<6)
+ else
+ (byte-(1<<7))>>2
+ end
+ end
+ def packet_header?(byte)
+ byte>>7 == 1
+ end
+ def new_packet?(byte)
+ byte>>6 == 3
+ end
+ def debug_header_search(stream)
+ current_pos = stream.pos+1
+ stream.seek(-2,IO::SEEK_CUR)
+ 4.times do
+ puts packet_header?(stream.getbyte)
+ if packet_header?(stream.getbyte) then
+ raise "i expected a packet header but found none. it could to be off by #{current_pos-stream.pos}"
+ end
+ end
+ raise "i expected a packet header but there is none"
+ end
+ end
+ def self.included(base)
+ base.extend PacketParserKlass
+ end
+
attr_accessor :stream, :partial_length, :partial_byte_size
- def partial_length?
- partial_length
- end
def parse(stream)
self.stream = stream
self.byte_size = self.partial_byte_size = packet_size
@@ -16,9 +51,17 @@ module FPG
end
return self
end
+
+
def parse_content
stream.seek(partial_byte_size,IO::SEEK_CUR)
end
+
+ private
+
+ def partial_length?
+ partial_length
+ end
def packet_size
header = stream.getbyte
if new_packet?(header)
@@ -59,6 +102,10 @@ module FPG
end
class Packet < Packets::Packet
include PacketParser
+ include TaggedSubclasses
+ def self.subclasses
+ Packets::Packet.subclasses
+ end
end
end
end
diff --git a/lib/fpg/parse/packet_header.rb b/lib/fpg/parse/packet_header.rb
deleted file mode 100644
index 5d4a8c1..0000000
--- a/lib/fpg/parse/packet_header.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-module FPG
- module Parse
- class PacketHeader
- include PacketHeaderNumbers
- def parse( stream )
- header = stream.getbyte
- stream.seek(-1,IO::SEEK_CUR)
- unless packet_header?(header)
- debug_header_search(stream)
- end
- packet_for(packet_number(header)).parse(stream)
- end
- def packet_number(byte)
- if new_packet?(byte)
- byte-(1<<7)-(1<<6)
- else
- (byte-(1<<7))>>2
- end
- end
- def packet_header?(byte)
- byte>>7 == 1
- end
- def new_packet?(byte)
- byte>>6 == 3
- end
- def debug_header_search(stream)
- current_pos = stream.pos+1
- stream.seek(-2,IO::SEEK_CUR)
- 4.times do
- puts packet_header?(stream.getbyte)
- if packet_header?(stream.getbyte) then
- raise "i expected a packet header but found none. it could to be off by #{current_pos-stream.pos}"
- end
- end
- raise "i expected a packet header but there is none"
- end
- end
- end
-end
diff --git a/lib/fpg/parse/parser.rb b/lib/fpg/parse/parser.rb
index 87fe1ce..d729655 100644
--- a/lib/fpg/parse/parser.rb
+++ b/lib/fpg/parse/parser.rb
@@ -4,7 +4,7 @@ module FPG
def self.parse( stream )
packets = []
until stream.eof? do
- packets << PacketHeader.new.parse(stream)
+ packets << Packet.parse(stream)
#rescue Exception => e
# raise 'parsing the message failed at position '+stream.pos.to_s+'. reason: '+ e.message
#end
diff --git a/lib/fpg/parse/public_key.rb b/lib/fpg/parse/public_key.rb
index 55032a3..e2f859d 100644
--- a/lib/fpg/parse/public_key.rb
+++ b/lib/fpg/parse/public_key.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class PublicKey < Packets::PublicKey
+ def self.tag
+ 6
+ end
include PublicKeyParser
end
end
diff --git a/lib/fpg/parse/public_key_encrypted_session_key.rb b/lib/fpg/parse/public_key_encrypted_session_key.rb
index a25ce82..dff5861 100644
--- a/lib/fpg/parse/public_key_encrypted_session_key.rb
+++ b/lib/fpg/parse/public_key_encrypted_session_key.rb
@@ -22,6 +22,9 @@ module FPG
end
end
class PublicKeyEncryptedSessionKey < Packets::PublicKeyEncryptedSessionKey
+ def self.tag
+ 1
+ end
include PublicKeyEncryptedSessionKeyParser
end
end
diff --git a/lib/fpg/parse/public_subkey.rb b/lib/fpg/parse/public_subkey.rb
index cb3c2e8..acbe7ab 100644
--- a/lib/fpg/parse/public_subkey.rb
+++ b/lib/fpg/parse/public_subkey.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class PublicSubkey < Packets::PublicSubkey
+ def self.tag
+ 14
+ end
include PublicSubkeyParser
end
end
diff --git a/lib/fpg/parse/secret_key.rb b/lib/fpg/parse/secret_key.rb
index 48da670..e7d3743 100644
--- a/lib/fpg/parse/secret_key.rb
+++ b/lib/fpg/parse/secret_key.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class SecretKey < Packets::SecretKey
+ def self.tag
+ 5
+ end
include SecretKeyParser
end
end
diff --git a/lib/fpg/parse/secret_subkey.rb b/lib/fpg/parse/secret_subkey.rb
index 46d7f97..30724a2 100644
--- a/lib/fpg/parse/secret_subkey.rb
+++ b/lib/fpg/parse/secret_subkey.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class SecretSubkey < Packets::SecretSubkey
+ def self.tag
+ 7
+ end
include SecretSubkeyParser
end
end
diff --git a/lib/fpg/parse/signature.rb b/lib/fpg/parse/signature.rb
index 727092e..4e88deb 100644
--- a/lib/fpg/parse/signature.rb
+++ b/lib/fpg/parse/signature.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class Signature < Packets::Signature
+ def self.tag
+ 2
+ end
include SignatureParser
end
end
diff --git a/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb b/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
index 9185516..ed710b6 100644
--- a/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
+++ b/lib/fpg/parse/symmetric_encrypted_and_integrity_protected_data.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class SymmetricEncryptedAndIntegrityProtectedData < Packets::SymmetricEncryptedAndIntegrityProtectedData
+ def self.tag
+ 18
+ end
include SymmetricEncryptedAndIntegrityProtectedDataParser
end
end
diff --git a/lib/fpg/parse/user_id.rb b/lib/fpg/parse/user_id.rb
index 8849ea1..9a95808 100644
--- a/lib/fpg/parse/user_id.rb
+++ b/lib/fpg/parse/user_id.rb
@@ -4,6 +4,9 @@ module FPG
include PacketParser
end
class UserId < Packets::UserId
+ def self.tag
+ 13
+ end
include UserIdParser
end
end