magic-1.1/0000755000175000017500000000000012423533524014166 5ustar00jgoerzenjgoerzen00000000000000magic-1.1/Setup.lhs0000644000175000017500000000015012423533524015772 0ustar00jgoerzenjgoerzen00000000000000#!/usr/bin/env runhugs arch-tag: Main setup script > import Distribution.Simple > main = defaultMain magic-1.1/Magic/0000755000175000017500000000000012423533524015206 5ustar00jgoerzenjgoerzen00000000000000magic-1.1/Magic/TypesLL.hs0000644000175000017500000000103212423533524017072 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic.TypesLL Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Low-Types for magic programs. Written by John Goerzen, jgoerzen\@complete.org -} module Magic.TypesLL(CMagic) where data CMagic magic-1.1/Magic/Data.hsc0000644000175000017500000000427212423533524016563 0ustar00jgoerzenjgoerzen00000000000000-- AUTO-GENERATED FILE, DO NOT EDIT. GENERATED BY utils/genconsts.hs {- | Module : Magic.Data Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Haskell types for libmagic constants Written by John Goerzen, jgoerzen\@complete.org -} module Magic.Data (module Magic.Data) where #include "magic.h" data MagicFlag = MagicNone | MagicDebug | MagicSymlink | MagicCompress | MagicDevices | MagicMimeType | MagicMimeEncoding | MagicMime | MagicContinue | MagicCheck | MagicPreserveAtime | MagicRaw | MagicError | UnknownMagicFlag Int deriving (Show) instance Enum MagicFlag where toEnum (#{const MAGIC_NONE}) = MagicNone toEnum (#{const MAGIC_DEBUG}) = MagicDebug toEnum (#{const MAGIC_SYMLINK}) = MagicSymlink toEnum (#{const MAGIC_COMPRESS}) = MagicCompress toEnum (#{const MAGIC_DEVICES}) = MagicDevices toEnum (#{const MAGIC_MIME_TYPE}) = MagicMimeType toEnum (#{const MAGIC_MIME_ENCODING}) = MagicMimeEncoding toEnum (#{const MAGIC_MIME}) = MagicMime toEnum (#{const MAGIC_CONTINUE}) = MagicContinue toEnum (#{const MAGIC_CHECK}) = MagicCheck toEnum (#{const MAGIC_PRESERVE_ATIME}) = MagicPreserveAtime toEnum (#{const MAGIC_RAW}) = MagicRaw toEnum (#{const MAGIC_ERROR}) = MagicError toEnum x = UnknownMagicFlag x fromEnum MagicNone = (#{const MAGIC_NONE}) fromEnum MagicDebug = (#{const MAGIC_DEBUG}) fromEnum MagicSymlink = (#{const MAGIC_SYMLINK}) fromEnum MagicCompress = (#{const MAGIC_COMPRESS}) fromEnum MagicDevices = (#{const MAGIC_DEVICES}) fromEnum MagicMimeType = (#{const MAGIC_MIME_TYPE}) fromEnum MagicMimeEncoding = (#{const MAGIC_MIME_ENCODING}) fromEnum MagicMime = (#{const MAGIC_MIME}) fromEnum MagicContinue = (#{const MAGIC_CONTINUE}) fromEnum MagicCheck = (#{const MAGIC_CHECK}) fromEnum MagicPreserveAtime = (#{const MAGIC_PRESERVE_ATIME}) fromEnum MagicRaw = (#{const MAGIC_RAW}) fromEnum MagicError = (#{const MAGIC_ERROR}) fromEnum (UnknownMagicFlag x) = x instance Ord MagicFlag where compare x y = compare (fromEnum x) (fromEnum y) instance Eq MagicFlag where x == y = (fromEnum x) == (fromEnum y) magic-1.1/Magic/Types.hsc0000644000175000017500000000163212423533524017013 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic.Types Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Types for magic programs. Written by John Goerzen, jgoerzen\@complete.org -} module Magic.Types(Magic, MagicFlag(..)) where import Foreign.Ptr import Data.Word import Data.Int import Foreign.C.Types import Foreign.ForeignPtr import Magic.Data import Magic.TypesLL #include {- | Main Magic object type. Magic objects are automatically closed (and memory freed) when they are garbage-collected by Haskell. There is no need to explicitly close them. -} type Magic = ForeignPtr CMagic magic-1.1/Magic/Utils.hsc0000644000175000017500000000413712423533524017012 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic.Utils Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Utils Written by John Goerzen, jgoerzen\@complete.org -} module Magic.Utils (flaglist2int, fromMagicPtr, withMagicPtr, checkIntError, throwErrorIfNull) where import Foreign import Foreign.C.Error import Foreign.C.String import Foreign.ForeignPtr import Magic.TypesLL import Magic.Types import Data.Bits import Foreign.C.Types import Magic.Data flaglist2int :: [MagicFlag] -> CInt flaglist2int mfl = foldl (\c f -> c .|. (fromIntegral . fromEnum $ f)) 0 mfl fromMagicPtr :: String -> IO (Ptr CMagic) -> IO Magic fromMagicPtr caller action = do ptr <- throwErrnoIfNull caller action newForeignPtr magic_close ptr throwErrorIfNull :: String -> Magic -> IO (Ptr a) -> IO (Ptr a) throwErrorIfNull caller m action = do res <- action if res == nullPtr then throwError caller m else return res withMagicPtr :: Magic -> (Ptr CMagic -> IO a) -> IO a withMagicPtr m = withForeignPtr m throwError :: String -> Magic -> IO a throwError caller m = withMagicPtr m (\cmagic -> do errormsg <- magic_error cmagic if errormsg /= nullPtr then do em <- peekCString errormsg fail $ caller ++ ": " ++ em else fail $ caller ++ ": got error code but no error message" ) checkIntError :: String -> Magic -> IO CInt -> IO () checkIntError caller m action = do res <- action if res == 0 then return () else throwError caller m foreign import ccall unsafe "magic.h &magic_close" magic_close :: FunPtr (Ptr CMagic -> IO ()) foreign import ccall unsafe "magic.h magic_error" magic_error :: Ptr CMagic -> IO CString magic-1.1/Magic/Operations.hsc0000644000175000017500000000645412423533524020041 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic.Init Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Initialization and shutdown for magic programs Written by John Goerzen, jgoerzen\@complete.org -} module Magic.Operations(-- * Guessing the type magicFile, magicStdin, magicString, magicCString, -- * Other operations magicSetFlags, magicCompile) where import Foreign.Ptr import Foreign.C.String import Magic.Types import Foreign.C.Types import Data.Word import Foreign.C.String import Foreign.C.Error import Magic.Utils import Magic.TypesLL import Foreign.Marshal.Utils {- | Calls the Magic system on the specified file. -} magicFile :: Magic -> FilePath -> IO String magicFile magic fp = withMagicPtr magic (\cmagic -> withCString fp (\cfp -> do res <- throwErrorIfNull "magicFile" magic (magic_file cmagic cfp) peekCString res ) ) {- | Calls the Magic system on stdin. -} magicStdin :: Magic -> IO String magicStdin magic = withMagicPtr magic (\cmagic -> do res <- throwErrorIfNull "magicStdin" magic (magic_file cmagic nullPtr) peekCString res ) {- | Calls the Magic system to process the given String. Please note: it is not evaluated lazily. -} magicString :: Magic -> String -> IO String magicString m s = withCStringLen s (magicCString m) {- | Lower-level function used to call the Magic system to process a C string. -} magicCString :: Magic -> CStringLen -> IO String magicCString magic (cstr, len) = withMagicPtr magic (\cmagic -> do res <- throwErrorIfNull "magicCString" magic (magic_buffer cmagic cstr (fromIntegral len)) peekCString res ) {- | Change the flags on an already-created object. -} magicSetFlags :: Magic -> [MagicFlag] -> IO () magicSetFlags m mfl = withMagicPtr m (\cmagic -> checkIntError "magicSetFlags" m $ magic_setflags cmagic flags) where flags = flaglist2int mfl {- | Compile the colon-separated list of database file(s). The compiled files created have .mgc added to the names of the argument. -} magicCompile :: Magic -- ^ Object to use -> Maybe String -- ^ Colon separated list of databases, or Nothing for default -> IO () magicCompile m mstr = withMagicPtr m (\cm -> case mstr of Nothing -> worker cm nullPtr Just x -> withCString x (worker cm) ) where worker cm cs = checkIntError "magicCompile" m $ magic_compile cm cs foreign import ccall unsafe "magic.h magic_file" magic_file :: Ptr CMagic -> CString -> IO CString foreign import ccall unsafe "magic.h magic_buffer" magic_buffer :: Ptr CMagic -> CString -> #{type size_t} -> IO CString foreign import ccall unsafe "magic.h magic_setflags" magic_setflags :: Ptr CMagic -> CInt -> IO CInt foreign import ccall unsafe "magic.h magic_compile" magic_compile :: Ptr CMagic -> CString -> IO CInt magic-1.1/Magic/Init.hsc0000644000175000017500000000313612423533524016613 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic.Init Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Initialization and shutdown for magic programs Written by John Goerzen, jgoerzen\@complete.org -} module Magic.Init(magicOpen, magicLoad, magicLoadDefault) where import Foreign.Ptr import Foreign.C.String import Magic.Types import Foreign.C.Types import Magic.Utils import Magic.TypesLL import Foreign.Marshal.Utils {- | Create a Magic object. You must call either 'magicLoadDefault' or 'magicLoad' after this. -} magicOpen :: [MagicFlag] -> IO Magic magicOpen mfl = fromMagicPtr "magicOpen" (magic_open flags) where flags = flaglist2int mfl {- | Load the system's default magic database. -} magicLoadDefault :: Magic -> IO () magicLoadDefault m = withMagicPtr m (\cmagic -> checkIntError "magicLoadDefault" m $ magic_load cmagic nullPtr) {- | Load the specified magic database(s). The given string may contain multiple colon-separated pathnames. -} magicLoad :: Magic -> String -> IO () magicLoad m s = withMagicPtr m (\cmagic -> withCString s (\cs -> checkIntError "magicLoad" m $ magic_load cmagic cs)) foreign import ccall unsafe "magic.h magic_open" magic_open :: CInt -> IO (Ptr CMagic) foreign import ccall unsafe "magic.h magic_load" magic_load :: Ptr CMagic -> CString -> IO CIntmagic-1.1/Magic.hs0000644000175000017500000000136712423533524015551 0ustar00jgoerzenjgoerzen00000000000000{- -*- Mode: haskell; -*- Haskell Magic Interface Copyright (C) 2005 John Goerzen This code is under a 3-clause BSD license; see COPYING for details. -} {- | Module : Magic Copyright : Copyright (C) 2005 John Goerzen License : BSD Maintainer : John Goerzen, Maintainer : jgoerzen\@complete.org Stability : provisional Portability: portable Top-level Magic module. Written by John Goerzen, jgoerzen\@complete.org Foo bar -} module Magic (-- * Basic Types module Magic.Types, -- * Initialization module Magic.Init, -- * Operation module Magic.Operations ) where import Magic.Types import Magic.Init import Magic.Operations magic-1.1/magic.cabal0000644000175000017500000000164012423533524016233 0ustar00jgoerzenjgoerzen00000000000000Extra-Libraries: magic -- End detected settings section. Everything below here should not -- need editing. Name: magic Version: 1.1 license: BSD3 Maintainer: John Goerzen Author: John Goerzen Stability: Alpha Copyright: Copyright (c) 2005-2009 John Goerzen build-type: Simple license-file: COPYING Category: Text Synopsis: Interface to C file/magic library Description: This package provides a Haskell interface to the C libmagic library. With it, you can determine the type of a file by examining its contents rather than its name. The Haskell interface provides a full-featured binding. -- C-Sources: glue/glue.c Exposed-Modules: Magic, Magic.Data, Magic.Types, Magic.Init, Magic.Operations Other-Modules: Magic.Utils, Magic.TypesLL Build-Depends: base >= 3 && < 5 GHC-Options: -O2 -- CC-Options: -Iglue Extensions: ForeignFunctionInterface, TypeSynonymInstances, EmptyDataDecls magic-1.1/COPYING0000644000175000017500000000267312423533524015231 0ustar00jgoerzenjgoerzen00000000000000Copyright (c) 2005 John Goerzen. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. 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. 3. Neither the name of John Goerzen nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.