summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwardKmett <>2013-08-14 21:40:27 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2013-08-14 21:40:27 (GMT)
commitae13e870fa684983d2ef2067262b31adb0b5269a (patch)
treecb43bc17371afc36e7781143a29a4a4b4fc85c73
parentd5459bd1aee883477b7f237db3fe76a30aa3c2e5 (diff)
version 0.100.10
-rw-r--r--CHANGELOG.markdown4
-rw-r--r--bytes.cabal3
-rw-r--r--cbits/i2d.c36
-rw-r--r--src/Data/Bytes/Serial.hs50
4 files changed, 70 insertions, 23 deletions
diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown
index a44cf97..3fdaa86 100644
--- a/CHANGELOG.markdown
+++ b/CHANGELOG.markdown
@@ -1,3 +1,7 @@
+0.10
+----
+* Changed all of the byte orders to big-endian by default *except* for `Word` and `Int`, which are variable sized.
+
0.9
-----
* Added proper support for `binary` 0.7.
diff --git a/bytes.cabal b/bytes.cabal
index 7b4149f..d81eeae 100644
--- a/bytes.cabal
+++ b/bytes.cabal
@@ -1,6 +1,6 @@
name: bytes
category: Data, Serialization
-version: 0.9
+version: 0.10
license: BSD3
cabal-version: >= 1.8
license-file: LICENSE
@@ -62,6 +62,7 @@ library
ghc-options: -Werror
ghc-options: -Wall -fwarn-tabs -O2
+ c-sources: cbits/i2d.c
hs-source-dirs: src
test-suite doctests
diff --git a/cbits/i2d.c b/cbits/i2d.c
new file mode 100644
index 0000000..0a9ea64
--- /dev/null
+++ b/cbits/i2d.c
@@ -0,0 +1,36 @@
+
+unsigned long long doubleToWord64(double input) {
+ union {
+ double d;
+ unsigned long long l;
+ } u;
+ u.d = input;
+ return u.l;
+}
+
+double word64ToDouble(unsigned long long input) {
+ union {
+ double d;
+ unsigned long long l;
+ } u;
+ u.l = input;
+ return u.d;
+}
+
+unsigned long long floatToWord32(float input) {
+ union {
+ float f;
+ unsigned long long l;
+ } u;
+ u.f = input;
+ return u.l;
+}
+
+float word32ToDouble(unsigned long long input) {
+ union {
+ float f;
+ unsigned long long l;
+ } u;
+ u.l = input;
+ return u.f;
+}
diff --git a/src/Data/Bytes/Serial.hs b/src/Data/Bytes/Serial.hs
index f1f4ebf..fd887a0 100644
--- a/src/Data/Bytes/Serial.hs
+++ b/src/Data/Bytes/Serial.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
+{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
@@ -76,15 +77,15 @@ class Serial a where
#endif
instance Serial Strict.ByteString where
- serialize bs = putWord32host (fromIntegral (Strict.length bs)) >> putByteString bs
+ serialize bs = putWord32be (fromIntegral (Strict.length bs)) >> putByteString bs
deserialize = do
- n <- getWord32host
+ n <- getWord32be
getByteString (fromIntegral n)
instance Serial Lazy.ByteString where
- serialize bs = putWord64host (fromIntegral (Lazy.length bs)) >> putLazyByteString bs
+ serialize bs = putWord64be (fromIntegral (Lazy.length bs)) >> putLazyByteString bs
deserialize = do
- n <- getWord64host
+ n <- getWord64be
getLazyByteString (fromIntegral n)
instance Serial SText.Text where
@@ -117,33 +118,38 @@ restore = do
unless (n >= required) $ fail "restore: Required more bytes"
return $ unsafePerformIO $ withForeignPtr fp $ \p -> peekByteOff p o
+foreign import ccall floatToWord32 :: Float -> Word32
+foreign import ccall word32ToFloat :: Word32 -> Float
+foreign import ccall doubleToWord64 :: Double -> Word64
+foreign import ccall word64ToDouble :: Word64 -> Double
+
instance Serial Double where
- serialize = store
- deserialize = restore
+ serialize = serialize . doubleToWord64
+ deserialize = liftM word64ToDouble restore
instance Serial Float where
- serialize = store
- deserialize = restore
+ serialize = serialize . floatToWord32
+ deserialize = liftM word32ToFloat restore
instance Serial Char where
- serialize = putWord32host . fromIntegral . fromEnum
- deserialize = liftM (toEnum . fromIntegral) getWord32host
+ serialize = putWord32be . fromIntegral . fromEnum
+ deserialize = liftM (toEnum . fromIntegral) getWord32be
instance Serial Word where
serialize = putWordhost
deserialize = getWordhost
instance Serial Word64 where
- serialize = putWord64host
- deserialize = getWord64host
+ serialize = putWord64be
+ deserialize = getWord64be
instance Serial Word32 where
- serialize = putWord32host
- deserialize = getWord32host
+ serialize = putWord32be
+ deserialize = getWord32be
instance Serial Word16 where
- serialize = putWord16host
- deserialize = getWord16host
+ serialize = putWord16be
+ deserialize = getWord16be
instance Serial Word8 where
serialize = putWord8
@@ -154,16 +160,16 @@ instance Serial Int where
deserialize = liftM fromIntegral getWordhost
instance Serial Int64 where
- serialize = putWord64host . fromIntegral
- deserialize = liftM fromIntegral getWord64host
+ serialize = putWord64be . fromIntegral
+ deserialize = liftM fromIntegral getWord64be
instance Serial Int32 where
- serialize = putWord32host . fromIntegral
- deserialize = liftM fromIntegral getWord32host
+ serialize = putWord32be . fromIntegral
+ deserialize = liftM fromIntegral getWord32be
instance Serial Int16 where
- serialize = putWord16host . fromIntegral
- deserialize = liftM fromIntegral getWord16host
+ serialize = putWord16be . fromIntegral
+ deserialize = liftM fromIntegral getWord16be
instance Serial Int8 where
serialize = putWord8 . fromIntegral