asn1-types (empty) → 0.1.0
raw patch · 7 files changed
+287/−0 lines, 7 filesdep +basedep +bytestringdep +timesetup-changed
Dependencies added: base, bytestring, time
Files
- Data/ASN1/BitArray.hs +80/−0
- Data/ASN1/OID.hs +21/−0
- Data/ASN1/Types.hs +81/−0
- Data/ASN1/Types/Lowlevel.hs +47/−0
- LICENSE +27/−0
- Setup.hs +2/−0
- asn1-types.cabal +29/−0
+ Data/ASN1/BitArray.hs view
@@ -0,0 +1,80 @@+-- |+-- Module : Data.ASN1.BitArray+-- License : BSD-style+-- Maintainer : Vincent Hanquez <vincent@snarc.org>+-- Stability : experimental+-- Portability : unknown+--+{-# LANGUAGE DeriveDataTypeable #-}+module Data.ASN1.BitArray+ ( BitArray(..)+ , BitArrayOutOfBound(..)+ , bitArrayLength+ , bitArrayGetBit+ , bitArraySetBitValue+ , bitArraySetBit+ , bitArrayClearBit+ , bitArrayGetData+ , toBitArray+ ) where++import Data.Bits+import Data.Word+import Data.Maybe+import Data.ByteString (ByteString)+import qualified Data.ByteString as B+import Data.Typeable+import Control.Exception (Exception, throw)++-- | throwed in case of out of bounds in the bitarray.+data BitArrayOutOfBound = BitArrayOutOfBound Word64+ deriving (Show,Eq,Typeable)+instance Exception BitArrayOutOfBound++-- | represent a bitarray / bitmap+data BitArray = BitArray Word64 ByteString+ deriving (Show,Eq)++-- | returns the length of bits in this bitarray+bitArrayLength :: BitArray -> Word64+bitArrayLength (BitArray l _) = l++bitArrayOutOfBound :: Word64 -> a+bitArrayOutOfBound n = throw $ BitArrayOutOfBound n++-- | get the nth bits+bitArrayGetBit :: BitArray -> Word64 -> Bool+bitArrayGetBit (BitArray l d) n+ | n >= l = bitArrayOutOfBound n+ | otherwise = flip testBit (7-fromIntegral bitn) $ B.index d (fromIntegral offset)+ where (offset, bitn) = n `divMod` 8++-- | set the nth bit to the value specified+bitArraySetBitValue :: BitArray -> Word64 -> Bool -> BitArray+bitArraySetBitValue (BitArray l d) n v+ | n >= l = bitArrayOutOfBound n+ | otherwise =+ let (before,after) = B.splitAt (fromIntegral offset) d in+ -- array bound check before prevent fromJust from failing.+ let (w,remaining) = fromJust $ B.uncons after in+ BitArray l (before `B.append` (setter w (fromIntegral bitn) `B.cons` remaining))+ where+ (offset, bitn) = n `divMod` 8+ setter = if v then setBit else clearBit++-- | set the nth bits+bitArraySetBit :: BitArray -> Word64 -> BitArray+bitArraySetBit bitarray n = bitArraySetBitValue bitarray n True++-- | clear the nth bits+bitArrayClearBit :: BitArray -> Word64 -> BitArray+bitArrayClearBit bitarray n = bitArraySetBitValue bitarray n False++-- | get padded bytestring of the bitarray+bitArrayGetData :: BitArray -> ByteString+bitArrayGetData (BitArray _ d) = d++-- | number of bit to skip at the end (padding)+toBitArray :: ByteString -> Int -> BitArray+toBitArray l toSkip =+ BitArray (fromIntegral (B.length l * 8 - fromIntegral toSkip)) l
+ Data/ASN1/OID.hs view
@@ -0,0 +1,21 @@+-- |+-- Module : Data.ASN1.OID+-- License : BSD-style+-- Maintainer : Vincent Hanquez <vincent@snarc.org>+-- Stability : experimental+-- Portability : unknown+--+{-# LANGUAGE DeriveDataTypeable #-}+module Data.ASN1.OID+ ( OID+ -- * class+ , ObjectIdable(..)+ ) where++-- | Standard ASN.1 Object ID (OID)+type OID = [Integer]++-- | Class of things that have an Object ID+class ObjectIdable a where+ -- | return the object ID of an Object from the ObjectIdentifiable class.+ getObjectID :: a -> OID
+ Data/ASN1/Types.hs view
@@ -0,0 +1,81 @@+-- |+-- Module : Data.ASN1.Stream+-- License : BSD-style+-- Maintainer : Vincent Hanquez <vincent@snarc.org>+-- Stability : experimental+-- Portability : unknown+--+module Data.ASN1.Types+ ( ASN1(..)+ , ASN1S+ , ASN1Class(..)+ , ASN1Tag+ , ASN1ConstructionType(..)+ , ASN1StringEncoding(..)+ , ASN1TimeType(..)+ , ASN1Object(..)+ , module Data.ASN1.OID+ ) where++import Data.Time.Clock (UTCTime)+import Data.Time.LocalTime (TimeZone)+import Data.ASN1.BitArray+import Data.ASN1.OID+import Data.ASN1.Types.Lowlevel+import Data.ByteString (ByteString)++-- | Define the type of container+data ASN1ConstructionType = Sequence+ | Set+ | Container ASN1Class ASN1Tag+ deriving (Show,Eq)++-- T61 encoding : http://www.mail-archive.com/asn1@asn1.org/msg00460.html++-- | Define all possible ASN1 String encoding.+data ASN1StringEncoding =+ IA5 -- ^ 128 characters equivalent to the ASCII alphabet+ | UTF8 -- ^ UTF8+ | General -- ^ all registered graphic and character sets (see ISO 2375) plus SPACE and DELETE.+ | Graphic -- ^ all registered G sets and SPACE+ | Numeric -- ^ encoding containing numeric [0-9] and space+ | Printable -- ^ printable [a-z] [A-Z] [()+,-.?:/=] and space.+ | VideoTex -- ^ CCITT's T.100 and T.101 character sets+ | Visible -- ^ International ASCII printing character sets+ | T61 -- ^ teletext+ | UTF32 -- ^ UTF32+ | BMP -- ^ UCS2+ deriving (Show,Eq)++data ASN1TimeType = TimeUTC | TimeGeneralized+ deriving (Show,Eq)++-- | Define high level ASN1 object.+data ASN1 =+ Boolean Bool+ | IntVal Integer+ | BitString BitArray+ | OctetString ByteString+ | Null+ | OID OID+ | Real Double+ | Enumerated Int+ | ASN1String ASN1StringEncoding ByteString+ | ASN1Time ASN1TimeType UTCTime (Maybe TimeZone)+ | Other ASN1Class ASN1Tag ByteString+ | Start ASN1ConstructionType+ | End ASN1ConstructionType+ deriving (Show, Eq)++-- | represent a chunk of ASN1 Stream.+-- this is equivalent to ShowS but for an ASN1 Stream.+type ASN1S = [ASN1] -> [ASN1]++-- | Define an object that can be converted to and from ASN.1+class ASN1Object a where+ -- | transform an object into a chunk of ASN1 stream.+ toASN1 :: a -> ASN1S++ -- | returns either an object along the remaining ASN1 stream,+ -- or an error.+ fromASN1 :: [ASN1] -> Either String (a, [ASN1])
+ Data/ASN1/Types/Lowlevel.hs view
@@ -0,0 +1,47 @@+-- |+-- Module : Data.ASN1.Types.Lowlevel+-- License : BSD-style+-- Maintainer : Vincent Hanquez <vincent@snarc.org>+-- Stability : experimental+-- Portability : unknown+--+{-# LANGUAGE BangPatterns #-}+module Data.ASN1.Types.Lowlevel+ (+ -- * Raw types+ ASN1Class(..)+ , ASN1Tag+ , ASN1Length(..)+ , ASN1Header(..)+ -- * Events types+ , ASN1Event(..)+ ) where++import Data.ByteString (ByteString)++-- | Element class+data ASN1Class = Universal+ | Application+ | Context+ | Private+ deriving (Show,Eq,Ord,Enum)++-- | ASN1 Tag+type ASN1Tag = Int++-- | ASN1 Length with all different formats+data ASN1Length = LenShort Int -- ^ Short form with only one byte. length has to be < 127.+ | LenLong Int Int -- ^ Long form of N bytes+ | LenIndefinite -- ^ Length is indefinite expect an EOC in the stream to finish the type+ deriving (Show,Eq)++-- | ASN1 Header with the class, tag, constructed flag and length.+data ASN1Header = ASN1Header !ASN1Class !ASN1Tag !Bool !ASN1Length+ deriving (Show,Eq)++-- | represent one event from an asn1 data stream+data ASN1Event = Header ASN1Header -- ^ ASN1 Header+ | Primitive !ByteString -- ^ Primitive+ | ConstructionBegin -- ^ Constructed value start+ | ConstructionEnd -- ^ Constructed value end+ deriving (Show,Eq)
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2010-2013 Vincent Hanquez <vincent@snarc.org>++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 the author nor the names of his 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 AUTHORS 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ asn1-types.cabal view
@@ -0,0 +1,29 @@+Name: asn1-types+Version: 0.1.0+Description: ASN.1 standard types+License: BSD3+License-file: LICENSE+Copyright: Vincent Hanquez <vincent@snarc.org>+Author: Vincent Hanquez <vincent@snarc.org>+Maintainer: Vincent Hanquez <vincent@snarc.org>+Synopsis: ASN.1 types+Build-Type: Simple+Category: Data+stability: experimental+Cabal-Version: >=1.6+Homepage: http://github.com/vincenthz/hs-asn1-types++Library+ Build-Depends: base >= 3 && < 5+ , bytestring+ , time++ Exposed-modules: Data.ASN1.BitArray+ Data.ASN1.OID+ Data.ASN1.Types+ Data.ASN1.Types.Lowlevel+ ghc-options: -Wall++source-repository head+ type: git+ location: git://github.com/vincenthz/hs-asn1-types