-
Notifications
You must be signed in to change notification settings - Fork 4
/
Numbers.hs
43 lines (38 loc) · 1.82 KB
/
Numbers.hs
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
-- Copyright (C) 2016 Red Hat, Inc.
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, see <http://www.gnu.org/licenses/>.
module Codec.RPM.Internal.Numbers(asWord16,
asWord32,
asWord64)
where
import Data.Bits((.|.), shift)
import qualified Data.ByteString as BS
import Data.Word
asWord16 :: BS.ByteString -> Word16
asWord16 bs = fromIntegral (bs `BS.index` 0) `shift` 8 .|.
fromIntegral (bs `BS.index` 1)
asWord32 :: BS.ByteString -> Word32
asWord32 bs = fromIntegral (bs `BS.index` 0) `shift` 24 .|.
fromIntegral (bs `BS.index` 1) `shift` 16 .|.
fromIntegral (bs `BS.index` 2) `shift` 8 .|.
fromIntegral (bs `BS.index` 3)
asWord64 :: BS.ByteString -> Word64
asWord64 bs = fromIntegral (bs `BS.index` 0) `shift` 56 .|.
fromIntegral (bs `BS.index` 1) `shift` 48 .|.
fromIntegral (bs `BS.index` 2) `shift` 40 .|.
fromIntegral (bs `BS.index` 3) `shift` 32 .|.
fromIntegral (bs `BS.index` 4) `shift` 24 .|.
fromIntegral (bs `BS.index` 5) `shift` 16 .|.
fromIntegral (bs `BS.index` 6) `shift` 8 .|.
fromIntegral (bs `BS.index` 7)