-----------------------------------------------------------------------------
-- |
-- Module      :  Etherbunny.Packet
-- Copyright   :  (c) Nicholas Burlett 2007
-- License     :  GPL (see the file LICENSE)
--
-- Maintainer  :  nickburlett@mac.com
-- Stability   :  experimental
-- Portability :  ghc
--
-- General Packet support Etherbunny.
--
-----------------------------------------------------------------------------

{-# OPTIONS_GHC -fglasgow-exts -funbox-strict-fields #-}

module Network.Etherbunny.Packet (
    -- * Type Classes
    Pkt,
    
    -- * Utility functions
    wordsToInt,
    wordsToWord16,
    wordsToWord32
) where

import Data.Word
import Bits

wordsToInt :: Bits a => Int -> [Word8] -> a
wordsToInt i l = 
    let z5 = reverse [0..i-1] 
        shifted = zipWith (\ b c -> (fromIntegral c) `shiftL`  (b * 8) ) z5 l
    in
        foldr1 (.|.) shifted

wordsToWord16 :: [Word8] -> Word16
wordsToWord16 = wordsToInt 2

wordsToWord32 :: [Word8] -> Word32
wordsToWord32 = wordsToInt 4

    
class Pkt a where
    headerLength :: a -> Int

instance Pkt Int where
    headerLength = const 0