byteorder-1.0.4/0000755000000000000000000000000012135645445011712 5ustar0000000000000000byteorder-1.0.4/byteorder.cabal0000644000000000000000000000117412135645445014700 0ustar0000000000000000Name: byteorder Version: 1.0.4 Cabal-Version: >= 1.6 Synopsis: Exposes the native endianness or byte ordering of the system. Description: This package is for working with the native byte-ordering of the system. License: BSD3 License-file: LICENSE Author: Antoine Latter Maintainer: Antoine Latter Homepage: http://community.haskell.org/~aslatter/code/byteorder Build-type: Simple Category: System Source-Repository head type: darcs location: http://community.haskell.org/~aslatter/code/byteorder/ Library Build-depends: base == 4.* Exposed-modules: System.ByteOrder byteorder-1.0.4/LICENSE0000644000000000000000000000275612135645445012731 0ustar0000000000000000Copyright 2009, Antoine Latter All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. byteorder-1.0.4/Setup.hs0000644000000000000000000000005612135645445013347 0ustar0000000000000000import Distribution.Simple main = defaultMain byteorder-1.0.4/System/0000755000000000000000000000000012135645445013176 5ustar0000000000000000byteorder-1.0.4/System/ByteOrder.hs0000644000000000000000000000305312135645445015432 0ustar0000000000000000{- Module : System.ByteOrder Copyright : (c) Antoine Latter 2009 License : BSD3 Maintainer : Antoine Latter -} {-# OPTIONS_GHC -fno-cse #-} module System.ByteOrder(byteOrder, ByteOrder(..)) where import Foreign.Marshal.Alloc (alloca) import Foreign.Marshal.Array (peekArray) import Foreign.Ptr (castPtr) import Foreign.Storable (poke) import Data.Word import System.IO.Unsafe (unsafePerformIO) -- |Indicates the byte-ordering for a 4-byte value, where '1' -- indicates the most-significant byte and '4' indicates the -- least significant byte. -- -- In this format, big endian byte order would be represented as: -- (1,2,3,4). -- -- For convinience, the most common cases (BigEndian and LittleEndian) -- are provided their own constructors. data ByteOrder = BigEndian | LittleEndian | Mixed (Word8, Word8, Word8, Word8) deriving (Eq, Show, Read, Ord) input :: Word32 input = 0x01020304 -- |Returns the native byte ordering of the system. byteOrder :: ByteOrder byteOrder = unsafePerformIO byteOrderIO {-# NOINLINE byteOrder #-} byteOrderIO :: IO ByteOrder byteOrderIO = bytesToByteOrder `fmap` wordToBytes input wordToBytes :: Word32 -> IO (Word8,Word8,Word8,Word8) wordToBytes word = alloca $ \wordPtr -> do poke wordPtr word [x1,x2,x3,x4] <- peekArray 4 (castPtr wordPtr) return (x1,x2,x3,x4) bytesToByteOrder :: (Word8,Word8,Word8,Word8) -> ByteOrder bytesToByteOrder (1, 2, 3, 4) = BigEndian bytesToByteOrder (4, 3, 2, 1) = LittleEndian bytesToByteOrder (x1, x2, x3, x4) = Mixed (x1,x2,x3,x4)