diff options
author | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 19:23:32 -0500 |
---|---|---|
committer | Stephen Paul Weber <singpolyma@singpolyma.net> | 2012-04-25 19:23:32 -0500 |
commit | 91e6b9448b36f4775026597e0df1f7b28b5db906 (patch) | |
tree | 73043cb5c05d3570bac8a5ab9604369def82716d /Data | |
parent | 9cb589a33a09f42afa921ed6667f652b0c52a3f0 (diff) |
Higher-order "get until end of input as list"
Diffstat (limited to 'Data')
-rw-r--r-- | Data/OpenPGP.hs | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/Data/OpenPGP.hs b/Data/OpenPGP.hs index 8e1979b..818c125 100644 --- a/Data/OpenPGP.hs +++ b/Data/OpenPGP.hs | |||
@@ -323,12 +323,12 @@ parse_packet 2 = do | |||
323 | hash_algorithm <- get | 323 | hash_algorithm <- get |
324 | hashed_size <- fmap fromIntegral (get :: Get Word16) | 324 | hashed_size <- fmap fromIntegral (get :: Get Word16) |
325 | hashed_data <- getLazyByteString hashed_size | 325 | hashed_data <- getLazyByteString hashed_size |
326 | let hashed = runGet get_signature_subpackets hashed_data | 326 | let hashed = runGet listUntilEnd hashed_data |
327 | unhashed_size <- fmap fromIntegral (get :: Get Word16) | 327 | unhashed_size <- fmap fromIntegral (get :: Get Word16) |
328 | unhashed_data <- getLazyByteString unhashed_size | 328 | unhashed_data <- getLazyByteString unhashed_size |
329 | let unhashed = runGet get_signature_subpackets unhashed_data | 329 | let unhashed = runGet listUntilEnd unhashed_data |
330 | hash_head <- get | 330 | hash_head <- get |
331 | signature <- get | 331 | signature <- listUntilEnd |
332 | return SignaturePacket { | 332 | return SignaturePacket { |
333 | version = version, | 333 | version = version, |
334 | signature_type = signature_type, | 334 | signature_type = signature_type, |
@@ -558,12 +558,7 @@ instance Binary Message where | |||
558 | put (Message (x:xs)) = do | 558 | put (Message (x:xs)) = do |
559 | put x | 559 | put x |
560 | put (Message xs) | 560 | put (Message xs) |
561 | get = do | 561 | get = fmap Message listUntilEnd |
562 | done <- isEmpty | ||
563 | if done then return (Message []) else do | ||
564 | next_packet <- get | ||
565 | (Message tail) <- get | ||
566 | return $ Message (next_packet:tail) | ||
567 | 562 | ||
568 | -- | Extract all signature and data packets from a 'Message' | 563 | -- | Extract all signature and data packets from a 'Message' |
569 | signatures_and_data :: Message -> ([Packet], [Packet]) | 564 | signatures_and_data :: Message -> ([Packet], [Packet]) |
@@ -593,6 +588,14 @@ instance Binary MPI where | |||
593 | return (MPI (LZ.foldl (\a b -> | 588 | return (MPI (LZ.foldl (\a b -> |
594 | a `shiftL` 8 .|. fromIntegral b) 0 bytes)) | 589 | a `shiftL` 8 .|. fromIntegral b) 0 bytes)) |
595 | 590 | ||
591 | listUntilEnd :: (Binary a) => Get [a] | ||
592 | listUntilEnd = do | ||
593 | done <- isEmpty | ||
594 | if done then return [] else do | ||
595 | next <- get | ||
596 | rest <- listUntilEnd | ||
597 | return (next:rest) | ||
598 | |||
596 | data SignatureSubpacket = | 599 | data SignatureSubpacket = |
597 | SignatureCreationTimePacket Word32 | | 600 | SignatureCreationTimePacket Word32 | |
598 | IssuerPacket String | | 601 | IssuerPacket String | |
@@ -642,15 +645,6 @@ put_signature_subpacket (IssuerPacket keyid) = | |||
642 | put_signature_subpacket (UnsupportedSignatureSubpacket tag bytes) = | 645 | put_signature_subpacket (UnsupportedSignatureSubpacket tag bytes) = |
643 | (bytes, tag) | 646 | (bytes, tag) |
644 | 647 | ||
645 | get_signature_subpackets :: Get [SignatureSubpacket] | ||
646 | get_signature_subpackets = do | ||
647 | done <- isEmpty | ||
648 | if done then return [] else do { | ||
649 | next_packet <- get :: Get SignatureSubpacket; | ||
650 | tail <- get_signature_subpackets; | ||
651 | return (next_packet:tail); | ||
652 | } | ||
653 | |||
654 | parse_signature_subpacket :: Word8 -> Get SignatureSubpacket | 648 | parse_signature_subpacket :: Word8 -> Get SignatureSubpacket |
655 | -- SignatureCreationTimePacket, http://tools.ietf.org/html/rfc4880#section-5.2.3.4 | 649 | -- SignatureCreationTimePacket, http://tools.ietf.org/html/rfc4880#section-5.2.3.4 |
656 | parse_signature_subpacket 2 = fmap SignatureCreationTimePacket get | 650 | parse_signature_subpacket 2 = fmap SignatureCreationTimePacket get |