data-endian (empty) → 0.0.1
raw patch · 4 files changed
+277/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +27/−0
- Setup.hs +2/−0
- data-endian.cabal +31/−0
- src/Data/Endian.hs +217/−0
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2011, Mikhail Vorozhtsov+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 names of the copyright owners nor the names of the + 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.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ data-endian.cabal view
@@ -0,0 +1,31 @@+Name: data-endian+Version: 0.0.1+Category: Data+Stability: experimental+Synopsis: Endian-sensitive data+Description:+ This package provides helpers for converting endian-sensitive data.++Homepage: https://github.com/mvv/data-endian+Bug-Reports: https://github.com/mvv/data-endian/issues++Author: Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+Maintainer: Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+Copyright: 2011 Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+License: BSD3+License-File: LICENSE++Cabal-Version: >= 1.6.0+Build-Type: Simple++Source-Repository head+ Type: git+ Location: https://github.com/mvv/data-endian.git++Library+ Build-Depends: base < 5+ Hs-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules:+ Data.Endian+
+ src/Data/Endian.hs view
@@ -0,0 +1,217 @@+{-# LANGUAGE UnicodeSyntax #-}+{-# LANGUAGE CPP #-}++module Data.Endian (+ EndianSensitive(..),+ toBigEndian,+ fromBigEndian,+ toLittleEndian,+ fromLittleEndian+ ) where++import Data.Int+import Data.Word+import Data.Bits+import Foreign.C.Types+import Foreign.Ptr (IntPtr, WordPtr)+import System.Posix.Types (CSsize)++#include <HsBaseConfig.h>++-- | Raw, endian-sensitive data+class EndianSensitive α where+ -- | Change the endianness of the argument+ swapEndian ∷ α → α++-- | Convert from the native format to big-endian+toBigEndian ∷ EndianSensitive α ⇒ α → α+-- | Convert from big-endian to the native format+fromBigEndian ∷ EndianSensitive α ⇒ α → α+-- | Convert from the native format to little-endian+toLittleEndian ∷ EndianSensitive α ⇒ α → α+-- | Convert from little-endian to the native format+fromLittleEndian ∷ EndianSensitive α ⇒ α → α++#ifdef WORDS_BIGENDIAN+toBigEndian = id+toLittleEndian = swapEndian+#else+toBigEndian = swapEndian+toLittleEndian = id+#endif++fromBigEndian = toBigEndian+fromLittleEndian = toLittleEndian++{-# INLINE toBigEndian #-}+{-# INLINE fromBigEndian #-}+{-# INLINE toLittleEndian #-}+{-# INLINE fromLittleEndian #-}++instance EndianSensitive α ⇒ EndianSensitive [α] where+ swapEndian = map swapEndian++instance EndianSensitive Word16 where+ swapEndian = (`rotateR` 8)+ {-# INLINE swapEndian #-}++instance EndianSensitive Word32 where+ swapEndian x = (x `shiftR` 24)+ .|. ((x .&. 0x00FF0000) `shiftR` 8)+ .|. ((x .&. 0x0000FF00) `shiftL` 8)+ .|. (x `shiftL` 24)+ {-# INLINE swapEndian #-}++instance EndianSensitive Word64 where+ swapEndian x+#if WORD_SIZE_IN_BITS == 32+ = fromIntegral (swapEndian hi)+ .|. (fromIntegral (swapEndian lo) `shiftL` 32)+ where lo, hi ∷ Word32+ lo = fromIntegral x+ hi = fromIntegral (x `shiftR` 32)+#else+ = (x `shiftR` 56)+ .|. ((x .&. 0x00FF000000000000) `shiftR` 40)+ .|. ((x .&. 0x0000FF0000000000) `shiftR` 24)+ .|. ((x .&. 0x000000FF00000000) `shiftR` 8)+ .|. ((x .&. 0x00000000FF000000) `shiftL` 8)+ .|. ((x .&. 0x0000000000FF0000) `shiftL` 24)+ .|. ((x .&. 0x000000000000FF00) `shiftL` 40)+ .|. (x `shiftL` 56)+#endif+ {-# INLINE swapEndian #-}++instance EndianSensitive Int16 where+ swapEndian = (`rotateR` 8)+ {-# INLINE swapEndian #-}++instance EndianSensitive Int32 where+ swapEndian = fromIntegral . (swapEndian ∷ Word32 → Word32) . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive Int64 where+ swapEndian = fromIntegral . (swapEndian ∷ Word64 → Word64) . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CShort where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_SHORT → HTYPE_SHORT)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CUShort where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UNSIGNED_SHORT → HTYPE_UNSIGNED_SHORT)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CInt where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_INT → HTYPE_INT)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CUInt where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UNSIGNED_INT → HTYPE_UNSIGNED_INT)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CLong where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_LONG → HTYPE_LONG)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CULong where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UNSIGNED_LONG → HTYPE_UNSIGNED_LONG)+ . fromIntegral+ {-# INLINE swapEndian #-}++#ifdef HAVE_LONG_LONG+instance EndianSensitive CLLong where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_LONG_LONG → HTYPE_LONG_LONG)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CULLong where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UNSIGNED_LONG_LONG+ → HTYPE_UNSIGNED_LONG_LONG)+ . fromIntegral+ {-# INLINE swapEndian #-}+#endif++instance EndianSensitive CSize where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_SIZE_T → HTYPE_SIZE_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CSsize where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_SSIZE_T → HTYPE_SSIZE_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CUIntPtr where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UINTPTR_T → HTYPE_UINTPTR_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CIntPtr where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_INTPTR_T → HTYPE_INTPTR_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CPtrdiff where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_PTRDIFF_T → HTYPE_PTRDIFF_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CUIntMax where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UINTMAX_T → HTYPE_UINTMAX_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive CIntMax where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_INTMAX_T → HTYPE_INTMAX_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive WordPtr where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_UINTPTR_T → HTYPE_UINTPTR_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++instance EndianSensitive IntPtr where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_INTPTR_T → HTYPE_INTPTR_T)+ . fromIntegral+ {-# INLINE swapEndian #-}++#ifdef HTYPE_WCHAR_T+instance EndianSensitive CWchar where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_WCHAR_T → HTYPE_WCHAR_T)+ . fromIntegral+ {-# INLINE swapEndian #-}+#endif++#ifdef HTYPE_WINT_T+instance EndianSensitive CWint where+ swapEndian = fromIntegral+ . (swapEndian ∷ HTYPE_WINT_T → HTYPE_WINT_T)+ . fromIntegral+ {-# INLINE swapEndian #-}+#endif+