From 8e035fded43f92dcc9984e56c5fb230dd59c4a87 Mon Sep 17 00:00:00 2001 From: "Scott R. Parish" Date: Wed, 10 Mar 2010 20:34:53 -0600 Subject: [PATCH] DataBinary's subtype ByteArray alone has an extra length ByteArray has the extra length, aparently as a historical artifact, requiring it to be special cased. Thanks to Mathias Stearn for pointing this out! --- Database/MongoDB/BSON.hs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Database/MongoDB/BSON.hs b/Database/MongoDB/BSON.hs index 5793c2d..5a5c5c3 100644 --- a/Database/MongoDB/BSON.hs +++ b/Database/MongoDB/BSON.hs @@ -156,11 +156,17 @@ getVal DataArray = do getNull return (fromIntegral bytes, BsonArray arr) getVal DataBinary = do - skip 4 - st <- getI8 - len2 <- getI32 - bs <- getLazyByteString $ fromIntegral len2 - return (4 + 1 + 4 + fromIntegral len2, BsonBinary (toBinarySubType st) bs) + len1 <- getI32 + st <- getI8 + (len, hdrLen) <- if toBinarySubType st == BSTByteArray + then do + len2 <- getI32 + assert (len1 - 4 == len2) $ return () + return (len2, 4 + 1 + 4) + else return (len1, 4 + 1) + bs <- getLazyByteString $ fromIntegral len + return (hdrLen + fromIntegral len, BsonBinary (toBinarySubType st) bs) + getVal DataUndefined = return (1, BsonUndefined) getVal DataOid = liftM ((,) 12 . BsonObjectId) $ getLazyByteString 12 getVal DataBoolean = liftM ((,) (1::Integer) . BsonBool . (/= (0::Int))) getI8