From d04ee2a3f65ba8d191bba778a66f771e4530b99b Mon Sep 17 00:00:00 2001 From: Sam T Date: Sun, 7 Apr 2013 05:11:42 +0400 Subject: ~ fix bugs in encoding --- src/Network/Torrent/Handshake.hs | 2 +- src/Network/Torrent/PeerID.hs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Network/Torrent/Handshake.hs b/src/Network/Torrent/Handshake.hs index 7643b2e9..b17afaf7 100644 --- a/src/Network/Torrent/Handshake.hs +++ b/src/Network/Torrent/Handshake.hs @@ -34,7 +34,7 @@ instance Serialize Handshake where get = do len <- getWord8 - Handshake <$> getBytes (fromIntegral len) + Handshake <$> getBytes (fromIntegral (len - 49)) <*> getWord64be <*> getBytes 20 <*> get diff --git a/src/Network/Torrent/PeerID.hs b/src/Network/Torrent/PeerID.hs index 551b0dcc..0eb4cbbb 100644 --- a/src/Network/Torrent/PeerID.hs +++ b/src/Network/Torrent/PeerID.hs @@ -24,7 +24,7 @@ import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Builder as B import Data.Foldable (foldMap) import Data.Monoid ((<>)) -import Data.Serialize (Serialize) +import Data.Serialize import Data.Version (Version(Version), versionBranch) import Data.Time.Clock (getCurrentTime) import Data.Time.Format (formatTime) @@ -37,7 +37,11 @@ version = Version [0, 10, 0, 0] [] -- | Peer identifier is exactly 20 bytes long bytestring. newtype PeerID = PeerID { getPeerID :: ByteString } - deriving (Show, Eq, Ord, BEncodable, Serialize) + deriving (Show, Eq, Ord, BEncodable) + +instance Serialize PeerID where + put = putByteString . getPeerID + get = PeerID <$> getBytes 20 -- | Azureus-style encoding: -- * 1 byte : '-' @@ -115,4 +119,4 @@ byteStringPadded bs s c = B.byteString (B.take s bs) <> B.byteString (BC.replicate padLen c) where - padLen = s - max (B.length bs) s \ No newline at end of file + padLen = s - min (B.length bs) s \ No newline at end of file -- cgit v1.2.3