{-# LINE 1 "src/Magic/Operations.hsc" #-}
{- -*- Mode: haskell; -*-
Haskell magic Interface
Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>

This code is under a 3-clause BSD license; see COPYING for details.
-}

{- |
   Module     : Magic.Operations
   Copyright  : Copyright (C) 2005 John Goerzen
   License    : BSD-3-Clause

   Maintainer : Philippe <philippedev101\@gmail.com>
   Stability  : provisional
   Portability: portable

Querying the type of files and in-memory data, and other operations on a magic
handle. The handle must first be created with @magicOpen@ and populated with
@magicLoadDefault@ or @magicLoad@ (see "Magic.Init").

Written by John Goerzen.
-}

module Magic.Operations(-- * Guessing the type
                        magicFile, magicStdin, magicDescriptor,
                        magicCString, magicByteString,
                        -- * Flags
                        magicSetFlags, magicGetFlags,
                        -- * Tunable parameters
                        magicGetParam, magicSetParam,
                        -- * Magic databases
                        magicCompile, magicCheck, magicLoadBuffers,
                        magicGetPath,
                        -- * Library information
                        magicVersion, magicErrno)
where

import Control.Concurrent.MVar (modifyMVar_)
import Control.Exception (throwIO)
import qualified Data.Text as T
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (withArray)
import Foreign.Marshal.Utils (with)
import Foreign.Storable (peek)
import Data.Word
import Data.ByteString (ByteString)
import qualified Data.ByteString.Unsafe as BSU
import Data.Text (Text)
import System.OsPath (OsPath)
import System.Posix.Types (Fd)
import Magic.Types (MagicParam(..))
import Magic.Data (MagicFlags(..))
import Magic.Internal (Magic(..), CMagic, MagicException(..), withMagicPtr, throwErrorIfNull, checkIntError, peekCStringText, withOsPathCString, withSearchPathCString, peekSearchPath)



{- | Identify the file at the given path. The result is in the form selected by
the handle's flags (a textual description, a MIME type, an encoding, and so on;
see t'MagicFlags'). Raises a @MagicException@ if the file cannot be
examined. -}
magicFile :: Magic -> OsPath -> IO Text
magicFile :: Magic -> OsPath -> IO Text
magicFile Magic
magic OsPath
fp =
    Magic -> (Ptr CMagic -> IO Text) -> IO Text
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
magic (\Ptr CMagic
cmagic ->
    OsPath -> (CString -> IO Text) -> IO Text
forall a. OsPath -> (CString -> IO a) -> IO a
withOsPathCString OsPath
fp (\CString
cfp ->
     do res <- String -> Magic -> IO CString -> IO CString
forall a. String -> Magic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
"magicFile" Magic
magic (Ptr CMagic -> CString -> IO CString
magic_file Ptr CMagic
cmagic CString
cfp)
        peekCStringText res
                    )
                       )

{- | Identify the data available on standard input, as 'magicFile' does for a
named file. Raises a @MagicException@ if the data cannot be
examined. -}
magicStdin :: Magic -> IO Text
magicStdin :: Magic -> IO Text
magicStdin Magic
magic =
    Magic -> (Ptr CMagic -> IO Text) -> IO Text
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
magic (\Ptr CMagic
cmagic ->
     do res <- String -> Magic -> IO CString -> IO CString
forall a. String -> Magic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
"magicStdin" Magic
magic (Ptr CMagic -> CString -> IO CString
magic_file Ptr CMagic
cmagic CString
forall a. Ptr a
nullPtr)
        peekCStringText res
                       )

{- | Identify the contents of a C string buffer (a pointer and a length). The
lowest-level in-memory primitive; for ordinary use prefer 'magicByteString'.
Raises a @MagicException@ if the data cannot be examined. -}
magicCString :: Magic -> CStringLen -> IO Text
magicCString :: Magic -> CStringLen -> IO Text
magicCString Magic
magic (CString
cstr, Int
len) =
    Magic -> (Ptr CMagic -> IO Text) -> IO Text
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
magic (\Ptr CMagic
cmagic ->
     do res <- String -> Magic -> IO CString -> IO CString
forall a. String -> Magic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
"magicCString" Magic
magic (Ptr CMagic -> CString -> Word64 -> IO CString
magic_buffer Ptr CMagic
cmagic CString
cstr (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len))
        peekCStringText res
                    )

{- | Change the flags (see t'MagicFlags') on an existing handle, for example to
switch between textual descriptions and MIME types. Raises a @MagicException@ on
failure. -}
magicSetFlags :: Magic -> MagicFlags -> IO ()
magicSetFlags :: Magic -> MagicFlags -> IO ()
magicSetFlags Magic
m (MagicFlags CInt
flags) = Magic -> (Ptr CMagic -> IO ()) -> IO ()
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cmagic ->
     String -> Magic -> IO CInt -> IO ()
checkIntError String
"magicSetFlags" Magic
m (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CMagic -> CInt -> IO CInt
magic_setflags Ptr CMagic
cmagic CInt
flags)

{- | Compile the given magic database file(s) into the binary @.mgc@ form. Each
compiled file is named after its source with @.mgc@ appended. Pass the empty
list to compile the default database. Raises a @MagicException@ on failure.

(libmagic joins the list with the platform search-path separator, so a path
that itself contains that separator cannot be represented.)
-}
magicCompile :: Magic -> [OsPath] -> IO ()
magicCompile :: Magic -> [OsPath] -> IO ()
magicCompile Magic
m [OsPath]
ps = Magic -> (Ptr CMagic -> IO ()) -> IO ()
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cm ->
     case [OsPath]
ps of
       [] -> Ptr CMagic -> CString -> IO ()
worker Ptr CMagic
cm CString
forall a. Ptr a
nullPtr
       [OsPath]
_  -> [OsPath] -> (CString -> IO ()) -> IO ()
forall a. [OsPath] -> (CString -> IO a) -> IO a
withSearchPathCString [OsPath]
ps (Ptr CMagic -> CString -> IO ()
worker Ptr CMagic
cm))
    where worker :: Ptr CMagic -> CString -> IO ()
worker Ptr CMagic
cm CString
cs = String -> Magic -> IO CInt -> IO ()
checkIntError String
"magicCompile" Magic
m (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CMagic -> CString -> IO CInt
magic_compile Ptr CMagic
cm CString
cs

{- | Identify the data behind an open file descriptor, as 'magicFile' does for a
named file. Useful for sockets, pipes, or files you have already opened. Raises
a @MagicException@ if the descriptor cannot be examined.

@since 1.1.2
-}
magicDescriptor :: Magic -> Fd -> IO Text
magicDescriptor :: Magic -> Fd -> IO Text
magicDescriptor Magic
magic Fd
fd =
    Magic -> (Ptr CMagic -> IO Text) -> IO Text
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
magic (\Ptr CMagic
cmagic ->
     do res <- String -> Magic -> IO CString -> IO CString
forall a. String -> Magic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
"magicDescriptor" Magic
magic
                 (Ptr CMagic -> CInt -> IO CString
magic_descriptor Ptr CMagic
cmagic (Fd -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Fd
fd))
        peekCStringText res)

{- | Identify the contents of a strict 'ByteString'. Does no encoding
conversion, so it is the right way to examine in-memory data. Raises a
@MagicException@ if the data cannot be examined.

@since 1.1.2
-}
magicByteString :: Magic -> ByteString -> IO Text
magicByteString :: Magic -> ByteString -> IO Text
magicByteString Magic
magic ByteString
bs =
    Magic -> (Ptr CMagic -> IO Text) -> IO Text
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
magic (\Ptr CMagic
cmagic ->
     ByteString -> (CStringLen -> IO Text) -> IO Text
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
bs (\(CString
cstr, Int
len) ->
      do res <- String -> Magic -> IO CString -> IO CString
forall a. String -> Magic -> IO (Ptr a) -> IO (Ptr a)
throwErrorIfNull String
"magicByteString" Magic
magic
                  (Ptr CMagic -> CString -> Word64 -> IO CString
magic_buffer Ptr CMagic
cmagic CString
cstr (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len))
         peekCStringText res))

{- | Read the flags currently set on the handle (see 'magicSetFlags'). Test the
result with @hasFlag@. Raises a @MagicException@ if the running @libmagic@ does not
support querying flags.

@since 1.1.2
-}
magicGetFlags :: Magic -> IO MagicFlags
magicGetFlags :: Magic -> IO MagicFlags
magicGetFlags Magic
m =
    Magic -> (Ptr CMagic -> IO MagicFlags) -> IO MagicFlags
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cmagic ->
     do fl <- Ptr CMagic -> IO CInt
magic_getflags Ptr CMagic
cmagic
        if fl < 0
           then throwIO (MagicException (T.pack "magicGetFlags")
                  (T.pack "magic_getflags is unsupported by this libmagic"))
           else return (MagicFlags fl))

{- | Read a tunable parameter (see 'MagicParam').

@since 1.1.2
-}
magicGetParam :: Magic -> MagicParam -> IO Int
magicGetParam :: Magic -> MagicParam -> IO Int
magicGetParam Magic
m MagicParam
p =
    Magic -> (Ptr CMagic -> IO Int) -> IO Int
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cmagic ->
     (Ptr CSize -> IO Int) -> IO Int
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca (\Ptr CSize
ptr ->
      do String -> Magic -> IO CInt -> IO ()
checkIntError String
"magicGetParam" Magic
m
           (Ptr CMagic -> CInt -> Ptr () -> IO CInt
magic_getparam Ptr CMagic
cmagic (MagicParam -> CInt
paramToCInt MagicParam
p) (Ptr CSize -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr CSize
ptr))
         v <- Ptr CSize -> IO CSize
forall a. Storable a => Ptr a -> IO a
peek Ptr CSize
ptr
         return (fromIntegral (v :: CSize))))

{- | Set a tunable parameter (see 'MagicParam'), for example to cap the number
of bytes scanned in untrusted input. Raises a @MagicException@ on failure.

@since 1.1.2
-}
magicSetParam :: Magic -> MagicParam -> Int -> IO ()
magicSetParam :: Magic -> MagicParam -> Int -> IO ()
magicSetParam Magic
m MagicParam
p Int
val =
    Magic -> (Ptr CMagic -> IO ()) -> IO ()
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cmagic ->
     CSize -> (Ptr CSize -> IO ()) -> IO ()
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
val :: CSize) (\Ptr CSize
ptr ->
      String -> Magic -> IO CInt -> IO ()
checkIntError String
"magicSetParam" Magic
m
        (Ptr CMagic -> CInt -> Ptr () -> IO CInt
magic_setparam Ptr CMagic
cmagic (MagicParam -> CInt
paramToCInt MagicParam
p) (Ptr CSize -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr CSize
ptr))))

paramToCInt :: MagicParam -> CInt
paramToCInt :: MagicParam -> CInt
paramToCInt MagicParam
p = case MagicParam
p of
    MagicParam
MagicParamIndirMax     -> CInt
0
{-# LINE 185 "src/Magic/Operations.hsc" #-}
    MagicParamNameMax      -> 1
{-# LINE 186 "src/Magic/Operations.hsc" #-}
    MagicParamElfPhnumMax  -> 2
{-# LINE 187 "src/Magic/Operations.hsc" #-}
    MagicParamElfShnumMax  -> 3
{-# LINE 188 "src/Magic/Operations.hsc" #-}
    MagicParamElfNotesMax  -> 4
{-# LINE 189 "src/Magic/Operations.hsc" #-}
    MagicParamRegexMax     -> 5
{-# LINE 190 "src/Magic/Operations.hsc" #-}
    MagicParamBytesMax     -> 6
{-# LINE 191 "src/Magic/Operations.hsc" #-}
    MagicParamEncodingMax  -> 7
{-# LINE 192 "src/Magic/Operations.hsc" #-}
    MagicParamElfShsizeMax -> 8
{-# LINE 193 "src/Magic/Operations.hsc" #-}

{- | Check the validity of the given magic database file(s) without compiling
them, as @file -c@ does. Pass the empty list for the default database. Returns
'True' if the database is valid. (libmagic joins the list with the platform
search-path separator, so a path that itself contains that separator cannot be
represented.)

@since 1.1.2
-}
magicCheck :: Magic -> [OsPath] -> IO Bool
magicCheck :: Magic -> [OsPath] -> IO Bool
magicCheck Magic
m [OsPath]
ps =
    Magic -> (Ptr CMagic -> IO Bool) -> IO Bool
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m (\Ptr CMagic
cmagic ->
     case [OsPath]
ps of
       [] -> (CInt -> Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0) (Ptr CMagic -> CString -> IO CInt
magic_check Ptr CMagic
cmagic CString
forall a. Ptr a
nullPtr)
       [OsPath]
_  -> [OsPath] -> (CString -> IO Bool) -> IO Bool
forall a. [OsPath] -> (CString -> IO a) -> IO a
withSearchPathCString [OsPath]
ps ((CInt -> Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0) (IO CInt -> IO Bool) -> (CString -> IO CInt) -> CString -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr CMagic -> CString -> IO CInt
magic_check Ptr CMagic
cmagic))

{- | Load magic from one or more in-memory compiled databases (the @.mgc@ binary
form produced by 'magicCompile'), rather than from files. This lets a program
embed its magic database.

@libmagic@ keeps (does not copy) the supplied buffers for the lifetime of the
handle, so the handle retains references to the given 'ByteString's to keep
their memory alive until it is closed. Raises a @MagicException@ on failure.

@since 2.0.0
-}
magicLoadBuffers :: Magic -> [ByteString] -> IO ()
magicLoadBuffers :: Magic -> [ByteString] -> IO ()
magicLoadBuffers (Magic ForeignPtr CMagic
fp MVar [ByteString]
mv) [ByteString]
bufs =
    MVar [ByteString] -> ([ByteString] -> IO [ByteString]) -> IO ()
forall a. MVar a -> (a -> IO a) -> IO ()
modifyMVar_ MVar [ByteString]
mv (([ByteString] -> IO [ByteString]) -> IO ())
-> ([ByteString] -> IO [ByteString]) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[ByteString]
retained ->
     do ForeignPtr CMagic -> (Ptr CMagic -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CMagic
fp ((Ptr CMagic -> IO ()) -> IO ()) -> (Ptr CMagic -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CMagic
cmagic ->
          [ByteString] -> ([(Ptr Word8, CSize)] -> IO ()) -> IO ()
forall a. [ByteString] -> ([(Ptr Word8, CSize)] -> IO a) -> IO a
withBuffers [ByteString]
bufs (([(Ptr Word8, CSize)] -> IO ()) -> IO ())
-> ([(Ptr Word8, CSize)] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[(Ptr Word8, CSize)]
pls ->
            [Ptr Word8] -> (Ptr (Ptr Word8) -> IO ()) -> IO ()
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray (((Ptr Word8, CSize) -> Ptr Word8)
-> [(Ptr Word8, CSize)] -> [Ptr Word8]
forall a b. (a -> b) -> [a] -> [b]
map (Ptr Word8, CSize) -> Ptr Word8
forall a b. (a, b) -> a
fst [(Ptr Word8, CSize)]
pls) ((Ptr (Ptr Word8) -> IO ()) -> IO ())
-> (Ptr (Ptr Word8) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Word8)
pptr ->
            [CSize] -> (Ptr CSize -> IO ()) -> IO ()
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray (((Ptr Word8, CSize) -> CSize) -> [(Ptr Word8, CSize)] -> [CSize]
forall a b. (a -> b) -> [a] -> [b]
map (Ptr Word8, CSize) -> CSize
forall a b. (a, b) -> b
snd [(Ptr Word8, CSize)]
pls) ((Ptr CSize -> IO ()) -> IO ()) -> (Ptr CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CSize
sptr ->
              String -> Magic -> IO CInt -> IO ()
checkIntError String
"magicLoadBuffers" (ForeignPtr CMagic -> MVar [ByteString] -> Magic
Magic ForeignPtr CMagic
fp MVar [ByteString]
mv)
                (Ptr CMagic -> Ptr (Ptr Word8) -> Ptr CSize -> CSize -> IO CInt
magic_load_buffers Ptr CMagic
cmagic Ptr (Ptr Word8)
pptr Ptr CSize
sptr (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([ByteString] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
bufs)))
        -- Keep the buffers reachable (and so, being pinned, validly addressable)
        -- for as long as the handle lives.
        [ByteString] -> IO [ByteString]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString]
bufs [ByteString] -> [ByteString] -> [ByteString]
forall a. [a] -> [a] -> [a]
++ [ByteString]
retained)
  where
    withBuffers :: [ByteString] -> ([(Ptr Word8, CSize)] -> IO a) -> IO a
    withBuffers :: forall a. [ByteString] -> ([(Ptr Word8, CSize)] -> IO a) -> IO a
withBuffers []     [(Ptr Word8, CSize)] -> IO a
k = [(Ptr Word8, CSize)] -> IO a
k []
    withBuffers (ByteString
b:[ByteString]
bs) [(Ptr Word8, CSize)] -> IO a
k =
        ByteString -> (CStringLen -> IO a) -> IO a
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BSU.unsafeUseAsCStringLen ByteString
b (\(CString
p, Int
l) ->
          [ByteString] -> ([(Ptr Word8, CSize)] -> IO a) -> IO a
forall a. [ByteString] -> ([(Ptr Word8, CSize)] -> IO a) -> IO a
withBuffers [ByteString]
bs (\[(Ptr Word8, CSize)]
rest -> [(Ptr Word8, CSize)] -> IO a
k ((CString -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr CString
p, Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l) (Ptr Word8, CSize) -> [(Ptr Word8, CSize)] -> [(Ptr Word8, CSize)]
forall a. a -> [a] -> [a]
: [(Ptr Word8, CSize)]
rest)))

{- | The default magic database search path, honouring the @MAGIC@ environment
variable, as a list of paths.

@since 1.1.2
-}
magicGetPath :: IO [OsPath]
magicGetPath :: IO [OsPath]
magicGetPath =
    do res <- CString -> CInt -> IO CString
magic_getpath CString
forall a. Ptr a
nullPtr CInt
0
       if res == nullPtr then return [] else peekSearchPath res

{- | The version of the @libmagic@ library in use, encoded as a single integer
(for example @545@ for version 5.45).

@since 1.1.2
-}
magicVersion :: IO Int
magicVersion :: IO Int
magicVersion = (CInt -> Int) -> IO CInt -> IO Int
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral IO CInt
magic_version

{- | The @errno@ recorded by the last failing operation on the handle, or @0@ if
the last failure was not caused by a system error.

@since 1.1.2
-}
magicErrno :: Magic -> IO Int
magicErrno :: Magic -> IO Int
magicErrno Magic
m = Magic -> (Ptr CMagic -> IO Int) -> IO Int
forall a. Magic -> (Ptr CMagic -> IO a) -> IO a
withMagicPtr Magic
m ((CInt -> Int) -> IO CInt -> IO Int
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (IO CInt -> IO Int)
-> (Ptr CMagic -> IO CInt) -> Ptr CMagic -> IO Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr CMagic -> IO CInt
magic_errno)

-- Does file I/O -> safe
foreign import ccall safe "magic.h magic_file"
  magic_file :: Ptr CMagic -> CString -> IO CString

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_buffer"
  magic_buffer :: Ptr CMagic -> CString -> Word64 -> IO CString
{-# LINE 271 "src/Magic/Operations.hsc" #-}

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_setflags"
  magic_setflags :: Ptr CMagic -> CInt -> IO CInt

-- Does file I/O -> safe
foreign import ccall safe "magic.h magic_compile"
  magic_compile :: Ptr CMagic -> CString -> IO CInt

-- Reads the descriptor -> safe
foreign import ccall safe "magic.h magic_descriptor"
  magic_descriptor :: Ptr CMagic -> CInt -> IO CString

-- Validates a database file -> safe
foreign import ccall safe "magic.h magic_check"
  magic_check :: Ptr CMagic -> CString -> IO CInt

-- Parses (potentially large) in-memory databases -> safe
foreign import ccall safe "magic.h magic_load_buffers"
  magic_load_buffers :: Ptr CMagic -> Ptr (Ptr Word8) -> Ptr CSize -> CSize -> IO CInt

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_getflags"
  magic_getflags :: Ptr CMagic -> IO CInt

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_getparam"
  magic_getparam :: Ptr CMagic -> CInt -> Ptr () -> IO CInt

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_setparam"
  magic_setparam :: Ptr CMagic -> CInt -> Ptr () -> IO CInt

-- Reads an environment variable -> unsafe
foreign import ccall unsafe "magic.h magic_getpath"
  magic_getpath :: CString -> CInt -> IO CString

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_version"
  magic_version :: IO CInt

-- Does not do I/O -> unsafe
foreign import ccall unsafe "magic.h magic_errno"
  magic_errno :: Ptr CMagic -> IO CInt