-- |-- Module : Codec.Binary.Util-- Copyright : (c) 2009 Magnus Therning-- License : BSD3---- Utility functions used in the other module.moduleCodec.Binary.Util(toHex,fromHex,EncIncData(..),EncIncRes(..),DecIncData(..),DecIncRes(..),encoder,decoder)whereimportData.ArrayimportData.BitsimportData.CharimportData.WordimportqualifiedData.MapasM-- {{{1 hex enc/dec assoc list and mapshexEncMap=zip[0..]"0123456789ABCDEF"hexEncodeArray::ArrayWord8CharhexEncodeArray=array(0,16)hexEncMaphexDecodeMap::M.MapCharWord8hexDecodeMap=M.fromList[(b,a)|(a,b)<-hexEncMap]-- {{{1 toHextoHex::Word8->StringtoHexo=lethn=o`shiftR`4ln=o.&.0xfin[hexEncodeArray!hn,hexEncodeArray!ln]-- {{{1 fromHexfromHex::String->MaybeWord8fromHex=letdec[Justhn,Justln]=leto=hn`shiftL`4.|.lninJustodec_=Nothingindec.map(flipM.lookuphexDecodeMap.toUpper)-- {{{1 incremental coding-- | Data type for the incremental encoding functions.dataEncIncData=EChunk[Word8]-- ^ a chunk of data to be encoded|EDone-- ^ the signal to the encoder that the stream of data is ending-- | Data type for the result of calling the incremental encoding functions.dataEncIncResi=EParti(EncIncData->EncIncResi)-- ^ a partial result together with the continuation to use for further encoding|EFinali-- ^ the final result of encoding (the response to 'EDone')encoderfos=casef(EChunkos)ofEPartr1f'->casef'EDoneofEFinalr2->r1++r2-- | Data type for the incremental decoding functions.dataDecIncDatai=DChunki-- ^ a chunk of data to be decoded|DDone-- ^ the signal to the decoder that the stream of data is ending-- | Data type for the result of calling the incremental encoding functions.dataDecIncResi=DPart[Word8](DecIncDatai->DecIncResi)-- ^ a partial result together with the continuation to user for further decoding|DFinal[Word8]i-- ^ the final result of decoding (the response to 'DDone')|DFail[Word8]i-- ^ a partial result for a failed decoding, together with the remainder of the data passed in so fardecoder::(DecIncDatai->DecIncResi)->i->Maybe[Word8]decoderfs=letd=f(DChunks)incasedofDFinalda_->JustdaDFail__->NothingDPartdaf->letd'=fDDoneincased'ofDFinalda'_->Just$da++da'DFail__->NothingDPart__->Nothing-- should never happen