1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
-- |
-- Copyright : (c) Sam Truzjan 2013
-- License : BSD3
-- Maintainer : pxqr.sta@gmail.com
-- Stability : experimental
-- Portability : portable
--
-- Blocks are used to transfer pieces.
--
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.Torrent.Block
( -- * Piece attributes
PieceIx
, PieceSize
-- * Block attributes
, BlockOffset
, BlockCount
, BlockSize
, defaultTransferSize
-- * Block index
, BlockIx(..)
, ppBlockIx
, blockIxRange
-- * Block data
, Block(..)
, ppBlock
, blockIx
, blockSize
, blockRange
) where
import Control.Applicative
import Data.Aeson.TH
import qualified Data.ByteString.Lazy as Lazy
import Data.Char
import Data.List as L
import Data.Binary as B
import Data.Binary.Get as B
import Data.Binary.Put as B
import Data.Serialize as S
import Text.PrettyPrint
{-----------------------------------------------------------------------
-- Piece attributes
-----------------------------------------------------------------------}
-- | Zero-based index of piece in torrent content.
type PieceIx = Int
-- | Size of piece in bytes. Should be a power of 2.
--
-- NOTE: Have max and min size constrained to wide used
-- semi-standard values. This bounds should be used to make decision
-- about piece size for new torrents.
--
type PieceSize = Int
{-----------------------------------------------------------------------
-- Block attributes
-----------------------------------------------------------------------}
-- | Offset of a block in a piece in bytes. Should be multiple of
-- the choosen block size.
type BlockOffset = Int
-- | Size of a block in bytes. Should be power of 2.
--
-- Normally block size is equal to 'defaultTransferSize'.
--
type BlockSize = Int
-- | Number of block in a piece of a torrent. Used to distinguish
-- block count from piece count.
type BlockCount = Int
-- | Widely used semi-official block size. Some clients can ignore if
-- block size of BlockIx in Request message is not equal to this
-- value.
--
defaultTransferSize :: BlockSize
defaultTransferSize = 16 * 1024
{-----------------------------------------------------------------------
Block Index
-----------------------------------------------------------------------}
-- | BlockIx correspond.
data BlockIx = BlockIx {
-- | Zero-based piece index.
ixPiece :: {-# UNPACK #-} !PieceIx
-- | Zero-based byte offset within the piece.
, ixOffset :: {-# UNPACK #-} !BlockOffset
-- | Block size starting from offset.
, ixLength :: {-# UNPACK #-} !BlockSize
} deriving (Show, Eq)
$(deriveJSON (L.map toLower . L.dropWhile isLower) ''BlockIx)
getInt :: S.Get Int
getInt = fromIntegral <$> S.getWord32be
{-# INLINE getInt #-}
putInt :: S.Putter Int
putInt = S.putWord32be . fromIntegral
{-# INLINE putInt #-}
getIntB :: B.Get Int
getIntB = fromIntegral <$> B.getWord32be
{-# INLINE getIntB #-}
putIntB :: Int -> B.Put
putIntB = B.putWord32be . fromIntegral
{-# INLINE putIntB #-}
instance Serialize BlockIx where
{-# SPECIALIZE instance Serialize BlockIx #-}
get = BlockIx <$> getInt
<*> getInt
<*> getInt
{-# INLINE get #-}
put BlockIx {..} = do
putInt ixPiece
putInt ixOffset
putInt ixLength
{-# INLINE put #-}
instance Binary BlockIx where
{-# SPECIALIZE instance Binary BlockIx #-}
get = BlockIx <$> getIntB
<*> getIntB
<*> getIntB
{-# INLINE get #-}
put BlockIx {..} = do
putIntB ixPiece
putIntB ixOffset
putIntB ixLength
-- | Format block index in human readable form.
ppBlockIx :: BlockIx -> Doc
ppBlockIx BlockIx {..} =
"piece = " <> int ixPiece <> "," <+>
"offset = " <> int ixOffset <> "," <+>
"length = " <> int ixLength
-- | Get location of payload bytes in the torrent content.
blockIxRange :: (Num a, Integral a) => PieceSize -> BlockIx -> (a, a)
blockIxRange pieceSize BlockIx {..} = (offset, offset + len)
where
offset = fromIntegral pieceSize * fromIntegral ixPiece
+ fromIntegral ixOffset
len = fromIntegral ixLength
{-# INLINE blockIxRange #-}
{-----------------------------------------------------------------------
Block
-----------------------------------------------------------------------}
data Block payload = Block {
-- | Zero-based piece index.
blkPiece :: {-# UNPACK #-} !PieceIx
-- | Zero-based byte offset within the piece.
, blkOffset :: {-# UNPACK #-} !BlockOffset
-- | Payload bytes.
, blkData :: !payload
} deriving (Show, Eq)
-- | Format block in human readable form. Payload is ommitted.
ppBlock :: Block Lazy.ByteString -> Doc
ppBlock = ppBlockIx . blockIx
{-# INLINE ppBlock #-}
-- | Get size of block /payload/ in bytes.
blockSize :: Block Lazy.ByteString -> BlockSize
blockSize blk = fromIntegral (Lazy.length (blkData blk))
{-# INLINE blockSize #-}
-- | Get block index of a block.
blockIx :: Block Lazy.ByteString -> BlockIx
blockIx = BlockIx <$> blkPiece <*> blkOffset <*> blockSize
-- | Get location of payload bytes in the torrent content.
blockRange :: (Num a, Integral a) => PieceSize -> Block Lazy.ByteString -> (a, a)
blockRange pieceSize = blockIxRange pieceSize . blockIx
{-# INLINE blockRange #-}
|