packages feed

ntfs 0.1.0.0 → 0.1.0.1

raw patch · 10 files changed

+735/−13 lines, 10 filesPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

API changes (from Hackage documentation)

+ NTFS.Attribute.Data: NonResidentData :: [DataRun] -> Word64 -> Word64 -> Word64 -> FileData
+ NTFS.Attribute.Data: ResidentData :: ByteString -> FileData
+ NTFS.Attribute.Data: [dataAllocatedSize] :: FileData -> Word64
+ NTFS.Attribute.Data: [dataInitializedSize] :: FileData -> Word64
+ NTFS.Attribute.Data: [dataRealSize] :: FileData -> Word64
+ NTFS.Attribute.Data: [dataRuns] :: FileData -> [DataRun]
+ NTFS.Attribute.Data: data FileData
+ NTFS.Attribute.Data: instance GHC.Classes.Eq NTFS.Attribute.Data.FileData
+ NTFS.Attribute.Data: instance GHC.Internal.Show.Show NTFS.Attribute.Data.FileData
+ NTFS.Attribute.Data: parseDataAttribute :: Attribute -> Either ParseError FileData
+ NTFS.Attribute.FileName: FileName :: MFTRef -> Word64 -> Word64 -> Word64 -> Word64 -> Word64 -> Word64 -> Word32 -> Word32 -> Word8 -> Word8 -> String -> FileName
+ NTFS.Attribute.FileName: [fileNameAllocatedSize] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameCreationTime] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameFileAlterTime] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameFileReadTime] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameFlags] :: FileName -> Word32
+ NTFS.Attribute.FileName: [fileNameMFTChangeTime] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameNameLength] :: FileName -> Word8
+ NTFS.Attribute.FileName: [fileNameName] :: FileName -> String
+ NTFS.Attribute.FileName: [fileNameNamespace] :: FileName -> Word8
+ NTFS.Attribute.FileName: [fileNameParent] :: FileName -> MFTRef
+ NTFS.Attribute.FileName: [fileNameRealSize] :: FileName -> Word64
+ NTFS.Attribute.FileName: [fileNameReparse] :: FileName -> Word32
+ NTFS.Attribute.FileName: data FileName
+ NTFS.Attribute.FileName: decodeUTF16LE :: ByteString -> String
+ NTFS.Attribute.FileName: fileNameNamespaceDOS :: Word8
+ NTFS.Attribute.FileName: fileNameNamespacePosix :: Word8
+ NTFS.Attribute.FileName: fileNameNamespaceWin32 :: Word8
+ NTFS.Attribute.FileName: fileNameNamespaceWin32DOS :: Word8
+ NTFS.Attribute.FileName: instance GHC.Classes.Eq NTFS.Attribute.FileName.FileName
+ NTFS.Attribute.FileName: instance GHC.Internal.Show.Show NTFS.Attribute.FileName.FileName
+ NTFS.Attribute.FileName: parseFileName :: Attribute -> Either ParseError FileName
+ NTFS.Attribute.StandardInformation: StandardInformation :: Word64 -> Word64 -> Word64 -> Word64 -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> Word32 -> Word64 -> Word64 -> StandardInformation
+ NTFS.Attribute.StandardInformation: [standardClassId] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: [standardCreationTime] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardFileAlterTime] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardFileAttributes] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: [standardFileReadTime] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardMFTChangeTime] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardMaxVersion] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: [standardOwnerId] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: [standardQuotaCharged] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardSecurityId] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: [standardUpdateSequence] :: StandardInformation -> Word64
+ NTFS.Attribute.StandardInformation: [standardVersion] :: StandardInformation -> Word32
+ NTFS.Attribute.StandardInformation: data StandardInformation
+ NTFS.Attribute.StandardInformation: instance GHC.Classes.Eq NTFS.Attribute.StandardInformation.StandardInformation
+ NTFS.Attribute.StandardInformation: instance GHC.Internal.Show.Show NTFS.Attribute.StandardInformation.StandardInformation
+ NTFS.Attribute.StandardInformation: parseStandardInformation :: Attribute -> Either ParseError StandardInformation
+ NTFS.MFT.Attribute: Attribute :: Word32 -> Word32 -> Bool -> Maybe ByteString -> Word16 -> Word16 -> AttributeValue -> Attribute
+ NTFS.MFT.Attribute: NonResidentValue :: VCN -> VCN -> ByteString -> Word64 -> Word64 -> Word64 -> AttributeValue
+ NTFS.MFT.Attribute: ResidentValue :: ByteString -> AttributeValue
+ NTFS.MFT.Attribute: [attributeFlags] :: Attribute -> Word16
+ NTFS.MFT.Attribute: [attributeId] :: Attribute -> Word16
+ NTFS.MFT.Attribute: [attributeLength] :: Attribute -> Word32
+ NTFS.MFT.Attribute: [attributeName] :: Attribute -> Maybe ByteString
+ NTFS.MFT.Attribute: [attributeNonResident] :: Attribute -> Bool
+ NTFS.MFT.Attribute: [attributeTypeCode] :: Attribute -> Word32
+ NTFS.MFT.Attribute: [attributeValue] :: Attribute -> AttributeValue
+ NTFS.MFT.Attribute: [valueAllocatedSize] :: AttributeValue -> Word64
+ NTFS.MFT.Attribute: [valueFirstVCN] :: AttributeValue -> VCN
+ NTFS.MFT.Attribute: [valueInitializedSize] :: AttributeValue -> Word64
+ NTFS.MFT.Attribute: [valueLastVCN] :: AttributeValue -> VCN
+ NTFS.MFT.Attribute: [valueRealSize] :: AttributeValue -> Word64
+ NTFS.MFT.Attribute: [valueRunlist] :: AttributeValue -> ByteString
+ NTFS.MFT.Attribute: attributeTypeData :: Word32
+ NTFS.MFT.Attribute: attributeTypeFileName :: Word32
+ NTFS.MFT.Attribute: attributeTypeStandardInformation :: Word32
+ NTFS.MFT.Attribute: data Attribute
+ NTFS.MFT.Attribute: data AttributeValue
+ NTFS.MFT.Attribute: instance GHC.Classes.Eq NTFS.MFT.Attribute.Attribute
+ NTFS.MFT.Attribute: instance GHC.Classes.Eq NTFS.MFT.Attribute.AttributeValue
+ NTFS.MFT.Attribute: instance GHC.Internal.Show.Show NTFS.MFT.Attribute.Attribute
+ NTFS.MFT.Attribute: instance GHC.Internal.Show.Show NTFS.MFT.Attribute.AttributeValue
+ NTFS.MFT.Attribute: parseAttribute :: RawAttribute -> Either ParseError Attribute
+ NTFS.Runlist: DataRun :: VCN -> Maybe LCN -> Word64 -> DataRun
+ NTFS.Runlist: [runLCN] :: DataRun -> Maybe LCN
+ NTFS.Runlist: [runLength] :: DataRun -> Word64
+ NTFS.Runlist: [runVCN] :: DataRun -> VCN
+ NTFS.Runlist: data DataRun
+ NTFS.Runlist: instance GHC.Classes.Eq NTFS.Runlist.DataRun
+ NTFS.Runlist: instance GHC.Internal.Show.Show NTFS.Runlist.DataRun
+ NTFS.Runlist: parseRunlist :: ByteString -> Either ParseError [DataRun]
+ NTFS.Types: mftRefNumber :: MFTRef -> Word64
+ NTFS.Types: mftRefSequence :: MFTRef -> Word16

Files

CHANGELOG.md view
@@ -8,6 +8,17 @@  ## [Unreleased] +## [0.1.0.1] - 2026-11-27++### Added++- `NTFS.MFT.Attribute` with resident and non-resident attribute headers.+- `NTFS.Runlist` with mapping pairs decoding, including sparse runs.+- `NTFS.Attribute.Data` with resident and non-resident @$DATA@ views.+- `NTFS.Attribute.StandardInformation` with timestamps and file flags.+- `NTFS.Attribute.FileName` with parent reference, timestamps, sizes and name.+- `mftRefNumber` and `mftRefSequence` helpers for MFT references.+ ## [0.1.0.0] - 2026-11-27  ### Added
ntfs.cabal view
@@ -1,6 +1,6 @@ cabal-version:      2.4 name:               ntfs-version:            0.1.0.0+version:            0.1.0.1 synopsis:           Pure Haskell library for reading NTFS disk images description:   Low-level parsers for NTFS volume geometry, MFT records, attributes and@@ -23,8 +23,13 @@  library   exposed-modules:    NTFS+                      NTFS.Attribute.Data+                      NTFS.Attribute.FileName+                      NTFS.Attribute.StandardInformation                       NTFS.BootSector+                      NTFS.MFT.Attribute                       NTFS.MFT.Record+                      NTFS.Runlist                       NTFS.Types                       NTFS.Volume   hs-source-dirs:     src
src/NTFS.hs view
@@ -1,12 +1,22 @@ -- | Public entry point for the NTFS library. module NTFS   ( module NTFS.Types+  , module NTFS.Attribute.Data+  , module NTFS.Attribute.FileName+  , module NTFS.Attribute.StandardInformation   , module NTFS.BootSector+  , module NTFS.MFT.Attribute   , module NTFS.MFT.Record+  , module NTFS.Runlist   , module NTFS.Volume   ) where +import           NTFS.Attribute.Data+import           NTFS.Attribute.FileName+import           NTFS.Attribute.StandardInformation import           NTFS.BootSector+import           NTFS.MFT.Attribute import           NTFS.MFT.Record+import           NTFS.Runlist import           NTFS.Types import           NTFS.Volume
+ src/NTFS/Attribute/Data.hs view
@@ -0,0 +1,41 @@+-- | High-level view of the @$DATA@ attribute.+module NTFS.Attribute.Data+  ( FileData (..)+  , parseDataAttribute+  ) where++import qualified Data.ByteString    as BS+import           Data.Word          (Word64)++import           NTFS.MFT.Attribute+import           NTFS.Runlist       (DataRun (..), parseRunlist)+import           NTFS.Types         (ParseError (..), VCN (..))++-- | Data stored inside a record, or the runs holding it on the volume.+data FileData+  = ResidentData BS.ByteString+  | NonResidentData+      { dataRuns            :: [DataRun]+      , dataAllocatedSize   :: Word64+      , dataRealSize        :: Word64+      , dataInitializedSize :: Word64+      }+  deriving (Eq, Show)++-- | Interpret an attribute as @$DATA@.+parseDataAttribute :: Attribute -> Either ParseError FileData+parseDataAttribute attr+  | attributeTypeCode attr /= attributeTypeData =+      Left (InvalidValue "attribute is not $DATA")+  | otherwise =+      case attributeValue attr of+        ResidentValue value -> Right (ResidentData value)+        NonResidentValue _firstVCN lastVCN runlistBytes allocated real initialized -> do+          runs <- parseRunlist runlistBytes+          let VCN lastIndex = lastVCN+          if null runs+            then Left (InvalidValue "$DATA runlist is empty")+            else+              if sum (map runLength runs) <= fromIntegral lastIndex+                then Left (InvalidValue "$DATA runlist does not cover its VCN range")+                else Right (NonResidentData runs allocated real initialized)
+ src/NTFS/Attribute/FileName.hs view
@@ -0,0 +1,102 @@+-- | High-level view of the @$FILE_NAME@ attribute.+module NTFS.Attribute.FileName+  ( FileName (..)+  , fileNameNamespacePosix+  , fileNameNamespaceWin32+  , fileNameNamespaceDOS+  , fileNameNamespaceWin32DOS+  , decodeUTF16LE+  , parseFileName+  ) where++import           Data.Bits          (shiftL, (.|.))+import qualified Data.ByteString    as BS+import           Data.Word          (Word32, Word64, Word8)++import           NTFS.MFT.Attribute+import           NTFS.Types         (MFTRef (..), ParseError (..))++-- | Namespace of a @$FILE_NAME@ attribute.+fileNameNamespacePosix, fileNameNamespaceWin32, fileNameNamespaceDOS, fileNameNamespaceWin32DOS :: Word8+fileNameNamespacePosix = 0++fileNameNamespaceWin32 = 1++fileNameNamespaceDOS = 2++fileNameNamespaceWin32DOS = 3++-- | Metadata attached to one name of a file or directory.+-- Timestamps are Windows @FILETIME@ values (100 ns since 1601-01-01).+data FileName = FileName+  { fileNameParent        :: MFTRef+  , fileNameCreationTime  :: Word64+  , fileNameFileAlterTime :: Word64+  , fileNameMFTChangeTime :: Word64+  , fileNameFileReadTime  :: Word64+  , fileNameAllocatedSize :: Word64+  , fileNameRealSize      :: Word64+  , fileNameFlags         :: Word32+  , fileNameReparse       :: Word32+  , fileNameNameLength    :: Word8+  , fileNameNamespace     :: Word8+  , fileNameName          :: String+  } deriving (Eq, Show)++-- | Decode UTF-16LE bytes into a string of code units.+-- Surrogate pairs are not combined.+decodeUTF16LE :: BS.ByteString -> String+decodeUTF16LE bytes = go 0+  where+    end = BS.length bytes+    go offset+      | offset + 1 >= end = []+      | otherwise = toEnum (fromIntegral (word16At bytes offset)) : go (offset + 2)++-- | Interpret an attribute as @$FILE_NAME@.+parseFileName :: Attribute -> Either ParseError FileName+parseFileName attr+  | attributeTypeCode attr /= attributeTypeFileName =+      Left (InvalidValue "attribute is not $FILE_NAME")+  | otherwise =+      case attributeValue attr of+        NonResidentValue{} ->+          Left (InvalidValue "$FILE_NAME is not resident")+        ResidentValue bytes+          | BS.length bytes < 0x42 ->+              Left (UnexpectedEndOfData "$FILE_NAME value is truncated")+          | 0x42 + 2 * fromIntegral nameLength > BS.length bytes ->+              Left (UnexpectedEndOfData "$FILE_NAME name is truncated")+          | otherwise ->+              Right+                FileName+                  { fileNameParent = MFTRef (word64At bytes 0x00)+                  , fileNameCreationTime = word64At bytes 0x08+                  , fileNameFileAlterTime = word64At bytes 0x10+                  , fileNameMFTChangeTime = word64At bytes 0x18+                  , fileNameFileReadTime = word64At bytes 0x20+                  , fileNameAllocatedSize = word64At bytes 0x28+                  , fileNameRealSize = word64At bytes 0x30+                  , fileNameFlags = word32At bytes 0x38+                  , fileNameReparse = word32At bytes 0x3c+                  , fileNameNameLength = nameLength+                  , fileNameNamespace = BS.index bytes 0x41+                  , fileNameName = decodeUTF16LE (BS.take (2 * fromIntegral nameLength) (BS.drop 0x42 bytes))+                  }+          where+            nameLength = BS.index bytes 0x40++word32At :: BS.ByteString -> Int -> Word32+word32At bytes offset =+  fromIntegral (word16At bytes offset)+    .|. (fromIntegral (word16At bytes (offset + 2)) `shiftL` 16)++word64At :: BS.ByteString -> Int -> Word64+word64At bytes offset =+  fromIntegral (word32At bytes offset)+    .|. (fromIntegral (word32At bytes (offset + 4)) `shiftL` 32)++word16At :: BS.ByteString -> Int -> Word32+word16At bytes offset =+  fromIntegral (BS.index bytes offset)+    .|. (fromIntegral (BS.index bytes (offset + 1)) `shiftL` 8)
+ src/NTFS/Attribute/StandardInformation.hs view
@@ -0,0 +1,73 @@+-- | High-level view of the @$STANDARD_INFORMATION@ attribute.+module NTFS.Attribute.StandardInformation+  ( StandardInformation (..)+  , parseStandardInformation+  ) where++import           Data.Bits          (shiftL, (.|.))+import qualified Data.ByteString    as BS+import           Data.Word          (Word32, Word64)++import           NTFS.MFT.Attribute+import           NTFS.Types         (ParseError (..))++-- | Metadata shared by all file names of a record.+-- Timestamps are Windows @FILETIME@ values (100 ns since 1601-01-01).+data StandardInformation = StandardInformation+  { standardCreationTime   :: Word64+  , standardFileAlterTime  :: Word64+  , standardMFTChangeTime  :: Word64+  , standardFileReadTime   :: Word64+  , standardFileAttributes :: Word32+  , standardMaxVersion     :: Word32+  , standardVersion        :: Word32+  , standardClassId        :: Word32+  , standardOwnerId        :: Word32+  , standardSecurityId     :: Word32+  , standardQuotaCharged   :: Word64+  , standardUpdateSequence :: Word64+  } deriving (Eq, Show)++-- | Interpret an attribute as @$STANDARD_INFORMATION@.+parseStandardInformation :: Attribute -> Either ParseError StandardInformation+parseStandardInformation attr+  | attributeTypeCode attr /= attributeTypeStandardInformation =+      Left (InvalidValue "attribute is not $STANDARD_INFORMATION")+  | otherwise =+      case attributeValue attr of+        NonResidentValue{} ->+          Left (InvalidValue "$STANDARD_INFORMATION is not resident")+        ResidentValue bytes+          | BS.length bytes < 0x48 ->+              Left (UnexpectedEndOfData "$STANDARD_INFORMATION value is truncated")+          | otherwise ->+              Right+                StandardInformation+                  { standardCreationTime = word64At bytes 0x00+                  , standardFileAlterTime = word64At bytes 0x08+                  , standardMFTChangeTime = word64At bytes 0x10+                  , standardFileReadTime = word64At bytes 0x18+                  , standardFileAttributes = word32At bytes 0x20+                  , standardMaxVersion = word32At bytes 0x24+                  , standardVersion = word32At bytes 0x28+                  , standardClassId = word32At bytes 0x2c+                  , standardOwnerId = word32At bytes 0x30+                  , standardSecurityId = word32At bytes 0x34+                  , standardQuotaCharged = word64At bytes 0x38+                  , standardUpdateSequence = word64At bytes 0x40+                  }++word32At :: BS.ByteString -> Int -> Word32+word32At bytes offset =+  fromIntegral (word16At bytes offset)+    .|. (fromIntegral (word16At bytes (offset + 2)) `shiftL` 16)++word64At :: BS.ByteString -> Int -> Word64+word64At bytes offset =+  fromIntegral (word32At bytes offset)+    .|. (fromIntegral (word32At bytes (offset + 4)) `shiftL` 32)++word16At :: BS.ByteString -> Int -> Word32+word16At bytes offset =+  fromIntegral (BS.index bytes offset)+    .|. (fromIntegral (BS.index bytes (offset + 1)) `shiftL` 8)
+ src/NTFS/MFT/Attribute.hs view
@@ -0,0 +1,141 @@+-- | Parsers for generic NTFS attribute headers.+module NTFS.MFT.Attribute+  ( Attribute (..)+  , AttributeValue (..)+  , parseAttribute+  , attributeTypeStandardInformation+  , attributeTypeFileName+  , attributeTypeData+  ) where++import           Data.Bits       (shiftL, (.|.))+import qualified Data.ByteString as BS+import           Data.Word       (Word16, Word32, Word64)++import           NTFS.MFT.Record (RawAttribute (..))+import           NTFS.Types      (ParseError (..), VCN (..))++-- | Type code of the @$STANDARD_INFORMATION@ attribute.+attributeTypeStandardInformation :: Word32+attributeTypeStandardInformation = 0x10++-- | Type code of the @$FILE_NAME@ attribute.+attributeTypeFileName :: Word32+attributeTypeFileName = 0x30++-- | Type code of the @$DATA@ attribute.+attributeTypeData :: Word32+attributeTypeData = 0x80++-- | A parsed attribute header and its value.+data Attribute = Attribute+  { attributeTypeCode    :: Word32+  , attributeLength      :: Word32+  , attributeNonResident :: Bool+  , attributeName        :: Maybe BS.ByteString+  , attributeFlags       :: Word16+  , attributeId          :: Word16+  , attributeValue       :: AttributeValue+  } deriving (Eq, Show)++-- | Resident data, or the cluster mapping of a non-resident attribute.+data AttributeValue+  = ResidentValue BS.ByteString+  | NonResidentValue+      { valueFirstVCN        :: VCN+      , valueLastVCN         :: VCN+      , valueRunlist         :: BS.ByteString+      , valueAllocatedSize   :: Word64+      , valueRealSize        :: Word64+      , valueInitializedSize :: Word64+      }+  deriving (Eq, Show)++-- | Parse one raw attribute from an MFT record.+parseAttribute :: RawAttribute -> Either ParseError Attribute+parseAttribute raw = do+  checkCommon raw+  name <- parseName bytes nameLength nameOffset limit+  value <-+    if nonResident+      then parseNonResident bytes limit+      else parseResident bytes limit+  pure+    Attribute+      { attributeTypeCode = word32At bytes 0x00+      , attributeLength = attributeLengthValue+      , attributeNonResident = nonResident+      , attributeName = name+      , attributeFlags = word16At bytes 0x0c+      , attributeId = word16At bytes 0x0e+      , attributeValue = value+      }+  where+    bytes = rawAttributeBytes raw+    attributeLengthValue = word32At bytes 0x04+    limit = fromIntegral attributeLengthValue+    nonResident = BS.index bytes 0x08 /= 0+    nameLength = fromIntegral (BS.index bytes 0x09)+    nameOffset = fromIntegral (word16At bytes 0x0a)++checkCommon :: RawAttribute -> Either ParseError ()+checkCommon raw+  | BS.length bytes < 0x18 = Left (InvalidValue "attribute header is truncated")+  | rawAttributeType raw /= word32At bytes 0x00 =+      Left (InvalidValue "attribute type does not match raw attribute")+  | attributeLengthValue < 0x18 = Left (InvalidValue "attribute length is too small")+  | fromIntegral attributeLengthValue > BS.length bytes =+      Left (UnexpectedEndOfData "attribute extends past record data")+  | otherwise = Right ()+  where+    bytes = rawAttributeBytes raw+    attributeLengthValue = word32At bytes 0x04++parseName :: BS.ByteString -> Int -> Int -> Int -> Either ParseError (Maybe BS.ByteString)+parseName bytes nameLength nameOffset limit+  | nameLength == 0 = Right Nothing+  | nameOffset < 0x18 = Left (InvalidValue "attribute name offset is too small")+  | nameOffset + 2 * nameLength > limit = Left (InvalidValue "attribute name is out of range")+  | otherwise = Right (Just (BS.take (2 * nameLength) (BS.drop nameOffset bytes)))++parseResident :: BS.ByteString -> Int -> Either ParseError AttributeValue+parseResident bytes limit+  | valueOffset < 0x18 = Left (InvalidValue "value offset is too small")+  | valueOffset + valueLength > limit = Left (InvalidValue "value is out of range")+  | otherwise = Right (ResidentValue (BS.take valueLength (BS.drop valueOffset bytes)))+  where+    valueLength = fromIntegral (word32At bytes 0x10)+    valueOffset = fromIntegral (word16At bytes 0x14)++parseNonResident :: BS.ByteString -> Int -> Either ParseError AttributeValue+parseNonResident bytes limit+  | limit < 0x40 = Left (InvalidValue "non-resident header is truncated")+  | mappingPairsOffset < 0x40 = Left (InvalidValue "mapping pairs offset is too small")+  | mappingPairsOffset > limit = Left (InvalidValue "mapping pairs offset is out of range")+  | otherwise =+      Right+        NonResidentValue+          { valueFirstVCN = VCN (fromIntegral (word64At bytes 0x10))+          , valueLastVCN = VCN (fromIntegral (word64At bytes 0x18))+          , valueRunlist = BS.take (limit - mappingPairsOffset) (BS.drop mappingPairsOffset bytes)+          , valueAllocatedSize = word64At bytes 0x28+          , valueRealSize = word64At bytes 0x30+          , valueInitializedSize = word64At bytes 0x38+          }+  where+    mappingPairsOffset = fromIntegral (word16At bytes 0x20)++word16At :: BS.ByteString -> Int -> Word16+word16At bytes offset =+  fromIntegral (BS.index bytes offset)+    .|. (fromIntegral (BS.index bytes (offset + 1)) `shiftL` 8)++word32At :: BS.ByteString -> Int -> Word32+word32At bytes offset =+  fromIntegral (word16At bytes offset)+    .|. (fromIntegral (word16At bytes (offset + 2)) `shiftL` 16)++word64At :: BS.ByteString -> Int -> Word64+word64At bytes offset =+  fromIntegral (word32At bytes offset)+    .|. (fromIntegral (word32At bytes (offset + 4)) `shiftL` 32)
+ src/NTFS/Runlist.hs view
@@ -0,0 +1,72 @@+-- | Decoder for NTFS mapping pairs (runlists).+module NTFS.Runlist+  ( DataRun (..)+  , parseRunlist+  ) where++import           Data.Bits       (complement, shiftL, shiftR, testBit, (.&.),+                                  (.|.))+import qualified Data.ByteString as BS+import           Data.Int        (Int64)+import           Data.Word       (Word64)++import           NTFS.Types      (LCN (..), ParseError (..), VCN (..))++-- | One decoded data run. A missing LCN denotes a sparse run.+data DataRun = DataRun+  { runVCN    :: VCN+  , runLCN    :: Maybe LCN+  , runLength :: Word64+  } deriving (Eq, Show)++-- | Decode a runlist into consecutive data runs.+parseRunlist :: BS.ByteString -> Either ParseError [DataRun]+parseRunlist bytes = go 0 0 0 []+  where+    end = BS.length bytes+    go offset vcn lcn acc+      | offset >= end = Right (reverse acc)+      | header == 0 = Right (reverse acc)+      | lengthSize == 0 = Left (InvalidValue "run length field is empty")+      | offset + 1 + lengthSize + offsetSize > end =+          Left (UnexpectedEndOfData "runlist entry is truncated")+      | runLen == 0 = Left (InvalidValue "run length is zero")+      | otherwise =+          let nextLCN =+                if offsetSize == 0+                  then lcn+                  else lcn + signedValue offsetSize (wordLE offsetSize bytes deltaOffset)+              run =+                DataRun+                  { runVCN = VCN vcn+                  , runLCN =+                      if offsetSize == 0+                        then Nothing+                        else Just (LCN nextLCN)+                  , runLength = runLen+                  }+           in go nextOffset (vcn + fromIntegral runLen) nextLCN (run : acc)+      where+        header = BS.index bytes offset+        lengthSize = fromIntegral (header .&. 0x0f)+        offsetSize = fromIntegral ((header `shiftR` 4) .&. 0x0f)+        runOffset = offset + 1+        deltaOffset = runOffset + lengthSize+        runLen = wordLE lengthSize bytes runOffset+        nextOffset = runOffset + lengthSize + offsetSize++wordLE :: Int -> BS.ByteString -> Int -> Word64+wordLE size bytes offset =+  foldl step 0 [0 .. size - 1]+  where+    step acc i =+      acc .|. (fromIntegral (BS.index bytes (offset + i)) `shiftL` (8 * i))++signedValue :: Int -> Word64 -> Int64+signedValue size raw+  | size == 0 = 0+  | size >= 8 = fromIntegral raw+  | testBit raw (size * 8 - 1) = fromIntegral (raw .|. mask)+  | otherwise = fromIntegral raw+  where+    mask = complement ((1 `shiftL` (size * 8)) - 1)
src/NTFS/Types.hs view
@@ -1,18 +1,29 @@ -- | Domain types shared by all NTFS parsers. module NTFS.Types   ( MFTRef (..)+  , mftRefNumber+  , mftRefSequence   , VCN (..)   , LCN (..)   , ClusterSize (..)   , ParseError (..)   ) where +import           Data.Bits (shiftR, (.&.)) import           Data.Int  (Int64)-import           Data.Word (Word64)+import           Data.Word (Word16, Word64) --- | Record number in the Master File Table.+-- | Record number and sequence number in the Master File Table. newtype MFTRef = MFTRef Word64   deriving (Eq, Ord, Show)++-- | Record number stored in an MFT reference.+mftRefNumber :: MFTRef -> Word64+mftRefNumber (MFTRef ref) = ref .&. 0x0000ffffffffffff++-- | Sequence number stored in an MFT reference.+mftRefSequence :: MFTRef -> Word16+mftRefSequence (MFTRef ref) = fromIntegral (ref `shiftR` 48)  -- | Virtual Cluster Number, relative to the start of a file's data runs. newtype VCN = VCN Int64
test/Spec.hs view
@@ -1,18 +1,29 @@ module Main (main) where -import           Control.Exception (finally)-import           Control.Monad     (unless)-import           Data.Bits         (shiftR)-import qualified Data.ByteString   as BS-import           Data.Word         (Word16, Word32, Word64, Word8)-import           System.Directory  (getTemporaryDirectory, removeFile)-import           System.Exit       (exitFailure)-import           System.IO         (SeekMode (AbsoluteSeek), hClose, hFlush,-                                    hSeek, openBinaryTempFile)+import           Control.Exception                  (finally)+import           Control.Monad                      (unless)+import           Data.Bits                          (shiftR)+import qualified Data.ByteString                    as BS+import           Data.Word                          (Word16, Word32, Word64,+                                                     Word8)+import           System.Directory                   (getTemporaryDirectory,+                                                     removeFile)+import           System.Exit                        (exitFailure)+import           System.IO                          (SeekMode (AbsoluteSeek),+                                                     hClose, hFlush, hSeek,+                                                     openBinaryTempFile) +import           NTFS.Attribute.Data+import           NTFS.Attribute.FileName+import           NTFS.Attribute.StandardInformation import           NTFS.BootSector+import           NTFS.MFT.Attribute import           NTFS.MFT.Record-import           NTFS.Types        (ClusterSize (..), LCN (..), MFTRef (..))+import           NTFS.Runlist+import           NTFS.Types                         (ClusterSize (..), LCN (..),+                                                     MFTRef (..), VCN (..),+                                                     mftRefNumber,+                                                     mftRefSequence) import           NTFS.Volume  main :: IO ()@@ -27,6 +38,17 @@       , ("volume opens and reads MFT record", testVolume)       , ("MFT record header and attributes", pure testMFTRecord)       , ("MFT record bad fixup", pure testBadFixup)+      , ("resident attribute header", pure testResidentAttribute)+      , ("non-resident attribute header", pure testNonResidentAttribute)+      , ("single data run", pure testSingleRun)+      , ("multiple data runs", pure testMultipleRuns)+      , ("sparse data run", pure testSparseRun)+      , ("truncated runlist", pure testTruncatedRunlist)+      , ("resident data attribute", pure testResidentData)+      , ("non-resident data attribute", pure testNonResidentData)+      , ("standard information attribute", pure testStandardInformation)+      , ("file name attribute", pure testFileName)+      , ("short file name attribute", pure testShortFileName)       ]   unless (and results) exitFailure @@ -114,6 +136,127 @@     Left _  -> True     Right _ -> False +testResidentAttribute :: Bool+testResidentAttribute =+  case parseAttribute residentAttribute of+    Right attr ->+      attributeTypeCode attr == 0x10+        && attributeLength attr == 0x60+        && not (attributeNonResident attr)+        && attributeName attr == Nothing+        && attributeFlags attr == 1+        && attributeId attr == 2+        && attributeValue attr == ResidentValue (BS.replicate 0x30 0xab)+    Left _ -> False++testNonResidentAttribute :: Bool+testNonResidentAttribute =+  case parseAttribute nonResidentAttribute of+    Right attr ->+      attributeTypeCode attr == 0x80+        && attributeLength attr == 0x80+        && attributeNonResident attr+        && attributeName attr == Just (BS.pack [0x44, 0x00, 0x61, 0x00, 0x74, 0x00, 0x61, 0x00])+        && attributeFlags attr == 0+        && attributeId attr == 1+        && attributeValue attr+          == NonResidentValue+            (VCN 3)+            (VCN 9)+            (BS.pack [0x11, 0x02, 0x03, 0x00])+            0x1000+            0x800+            0x800+    Left _ -> False++testSingleRun :: Bool+testSingleRun =+  parseRunlist (BS.pack [0x11, 0x07, 0x04, 0x00])+    == Right [DataRun (VCN 0) (Just (LCN 4)) 7]++testMultipleRuns :: Bool+testMultipleRuns =+  parseRunlist (BS.pack [0x11, 0x02, 0x04, 0x11, 0x03, 0xfe, 0x00])+    == Right+      [ DataRun (VCN 0) (Just (LCN 4)) 2+      , DataRun (VCN 2) (Just (LCN 2)) 3+      ]++testSparseRun :: Bool+testSparseRun =+  parseRunlist (BS.pack [0x01, 0x05, 0x11, 0x02, 0x03, 0x00])+    == Right+      [ DataRun (VCN 0) Nothing 5+      , DataRun (VCN 5) (Just (LCN 3)) 2+      ]++testTruncatedRunlist :: Bool+testTruncatedRunlist =+  case parseRunlist (BS.pack [0x11, 0x02]) of+    Left _  -> True+    Right _ -> False++testResidentData :: Bool+testResidentData =+  case parseAttribute residentDataAttribute >>= parseDataAttribute of+    Right value -> value == ResidentData (BS.replicate 0x30 0xab)+    Left _      -> False++testNonResidentData :: Bool+testNonResidentData =+  case parseAttribute nonResidentDataAttribute >>= parseDataAttribute of+    Right value ->+      value+        == NonResidentData+          [DataRun (VCN 0) (Just (LCN 3)) 2]+          0x1000+          0x800+          0x800+    Left _ -> False++testStandardInformation :: Bool+testStandardInformation =+  case parseAttribute+    (residentAttributeOf attributeTypeStandardInformation standardInformationFixture)+    >>= parseStandardInformation of+    Right info ->+      standardCreationTime info == 0x11+        && standardFileAlterTime info == 0x12+        && standardMFTChangeTime info == 0x13+        && standardFileReadTime info == 0x14+        && standardFileAttributes info == 0x20+        && standardMaxVersion info == 3+        && standardVersion info == 2+        && standardClassId info == 0x10+        && standardOwnerId info == 5+        && standardSecurityId info == 6+        && standardQuotaCharged info == 0x1000+        && standardUpdateSequence info == 0x1234+    Left _ -> False++testFileName :: Bool+testFileName =+  case parseAttribute (residentAttributeOf attributeTypeFileName fileNameFixture) >>= parseFileName of+    Right name ->+      mftRefNumber (fileNameParent name) == 9+        && mftRefSequence (fileNameParent name) == 2+        && fileNameCreationTime name == 0x21+        && fileNameAllocatedSize name == 0x2000+        && fileNameRealSize name == 0x1000+        && fileNameFlags name == 0x10+        && fileNameNameLength name == 2+        && fileNameNamespace name == fileNameNamespaceWin32+        && fileNameName name == "AB"+    Left _ -> False++testShortFileName :: Bool+testShortFileName =+  case parseAttribute+    (residentAttributeOf attributeTypeFileName (BS.take 0x45 fileNameFixture))+    >>= parseFileName of+    Left _  -> True+    Right _ -> False+ bootSector :: BS.ByteString bootSector =   build@@ -154,6 +297,119 @@     , (0x1fe, word16Bytes 0xaa55)     , (0x3fe, word16Bytes 0xaa55)     ]++residentAttribute :: RawAttribute+residentAttribute =+  RawAttribute+    0x10+    ( build+        (BS.replicate 0x60 0)+        [ (0x00, word32Bytes 0x10)+        , (0x04, word32Bytes 0x60)+        , (0x08, [0x00])+        , (0x09, [0x00])+        , (0x0a, word16Bytes 0x00)+        , (0x0c, word16Bytes 1)+        , (0x0e, word16Bytes 2)+        , (0x10, word32Bytes 0x30)+        , (0x14, word16Bytes 0x18)+        , (0x18, replicate 0x30 0xab)+        ]+    )++nonResidentAttribute :: RawAttribute+nonResidentAttribute =+  RawAttribute+    0x80+    ( build+        (BS.replicate 0x80 0)+        [ (0x00, word32Bytes 0x80)+        , (0x04, word32Bytes 0x80)+        , (0x08, [0x01])+        , (0x09, [0x04])+        , (0x0a, word16Bytes 0x40)+        , (0x0c, word16Bytes 0)+        , (0x0e, word16Bytes 1)+        , (0x10, word64Bytes 3)+        , (0x18, word64Bytes 9)+        , (0x20, word16Bytes 0x7c)+        , (0x22, [0x00])+        , (0x28, word64Bytes 0x1000)+        , (0x30, word64Bytes 0x800)+        , (0x38, word64Bytes 0x800)+        , (0x40, [0x44, 0x00, 0x61, 0x00, 0x74, 0x00, 0x61, 0x00])+        , (0x7c, [0x11, 0x02, 0x03, 0x00])+        ]+    )++residentDataAttribute :: RawAttribute+residentDataAttribute =+  RawAttribute+    0x80+    (putAt 0x00 (word32Bytes 0x80) (rawAttributeBytes residentAttribute))++nonResidentDataAttribute :: RawAttribute+nonResidentDataAttribute =+  RawAttribute+    0x80+    ( putAt 0x18 (word64Bytes 1)+        (putAt 0x10 (word64Bytes 0) (rawAttributeBytes nonResidentAttribute))+    )++standardInformationFixture :: BS.ByteString+standardInformationFixture =+  build+    (BS.replicate 0x48 0)+    [ (0x00, word64Bytes 0x11)+    , (0x08, word64Bytes 0x12)+    , (0x10, word64Bytes 0x13)+    , (0x18, word64Bytes 0x14)+    , (0x20, word32Bytes 0x20)+    , (0x24, word32Bytes 3)+    , (0x28, word32Bytes 2)+    , (0x2c, word32Bytes 0x10)+    , (0x30, word32Bytes 5)+    , (0x34, word32Bytes 6)+    , (0x38, word64Bytes 0x1000)+    , (0x40, word64Bytes 0x1234)+    ]++fileNameFixture :: BS.ByteString+fileNameFixture =+  build+    (BS.replicate 0x46 0)+    [ (0x00, word64Bytes 0x0002000000000009)+    , (0x08, word64Bytes 0x21)+    , (0x10, word64Bytes 0x22)+    , (0x18, word64Bytes 0x23)+    , (0x20, word64Bytes 0x24)+    , (0x28, word64Bytes 0x2000)+    , (0x30, word64Bytes 0x1000)+    , (0x38, word32Bytes 0x10)+    , (0x3c, word32Bytes 0)+    , (0x40, [0x02])+    , (0x41, [0x01])+    , (0x42, [0x41, 0x00, 0x42, 0x00])+    ]++residentAttributeOf :: Word32 -> BS.ByteString -> RawAttribute+residentAttributeOf typeCode value =+  RawAttribute+    typeCode+    ( build+        (BS.replicate 0x18 0)+        [ (0x00, word32Bytes typeCode)+        , (0x04, word32Bytes (fromIntegral (0x18 + BS.length value)))+        , (0x08, [0x00])+        , (0x09, [0x00])+        , (0x0a, word16Bytes 0)+        , (0x0c, word16Bytes 0)+        , (0x0e, word16Bytes 0)+        , (0x10, word32Bytes (fromIntegral (BS.length value)))+        , (0x14, word16Bytes 0x18)+        ]+        <> value+    )  build :: BS.ByteString -> [(Int, [Word8])] -> BS.ByteString build = foldl (\bytes (offset, xs) -> putAt offset xs bytes)