K      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJportable to Hugs and GHC experimental,Lennart Kolmodin <kolmodin@dtek.chalmers.se>#KLA # is an efficient way to build lazy Ms. . There are several functions for constructing s, but only one G to inspect them: to extract any data, you have to turn them into lazy  Ms using . Internally, a  constructs a lazy  L.Bytestring by filling byte 9 arrays piece by piece. As each buffer is filled, it is 'popped' 2 off, to become a new chunk of the resulting lazy M. ) All this is hidden from the user of the . NOO(1). The empty Builder, satisfying    = PO(1)., A Builder taking a single byte, satisfying   ( b) = Q bO(1).= The concatenation of two Builders, an associative operation  with identity  , satisfying   ( x y) = R ( x) ( y)O(1). A Builder taking a S , satisfying   ( bs) = T [bs]O(1). A Builder taking a lazy M , satisfying   ( bs) = bsO(n). Extract a lazy M from a . D The construction work takes place if and when the relevant part of  the lazy M is demanded. O(1). Pop the S% we have constructed so far, if any, ) yielding a new chunk in the result lazy M. UV'Sequence an IO operation on the buffer WGet the size of the buffer X'Map the resulting list of bytestrings. YEnsure that there are at least n many bytes available. Z Ensure that n( many bytes are available, and then use f to write some  bytes into the memory. [\] Ensure that n( many bytes are available, and then use f to write some " storable values into the memory. ^$Write a Word16 in big endian format 'Write a Word16 in little endian format $Write a Word32 in big endian format 'Write a Word32 in little endian format $Write a Word64 in big endian format 'Write a Word64 in little endian format O(1).< A Builder taking a single native machine word. The word is = written in host order, host endian form, for the machine you're on. G On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 6 4 bytes. Values written this way are not portable to > different endian or word sized machines, without conversion. 9Write a Word16 in native host order and host endianness. % 2 bytes will be written, unaligned. 9Write a Word32 in native host order and host endianness. % 4 bytes will be written, unaligned. %Write a Word64 in native host order. J On a 32 bit machine we write two host order Word32s, in big endian form. % 8 bytes will be written, unaligned. _`a   portable to Hugs and GHC. experimental,Lennart Kolmodin <kolmodin@dtek.chalmers.se>,IThe Get monad is just a State monad carrying around the input ByteString ' We treat it as a strict state monad. bcdThe parse state efghiRun the Get monad applies a f&-based parser on the input ByteString Run the Get monad applies a f-based parser on the input F ByteString. Additional to the result of get it returns the number of + consumed bytes and the rest of the input. j Skip ahead n bytes. Fails if fewer than n bytes are available.  Skip ahead n bytes. No error if there isn't enough bytes. Run ga*, but return without consuming its input.  Fails if ga fails. Like , but consume the input if gma returns 'Just _'.  Fails if gma fails. Like , but consume the input if gea returns 'Right _'.  Fails if gea fails. Get the next up to n6 bytes as a lazy ByteString, without consuming them. 2Get the total number of bytes read to this point. ,Get the number of remaining unparsed bytes. : Useful for checking whether all input has been consumed. . Note that this forces the rest of the input. *Test whether all input has been consumed, - i.e. there are no remaining unparsed bytes.  An efficient f/ method for strict ByteStrings. Fails if fewer  than n bytes are left in the input.  An efficient f: method for lazy ByteStrings. Does not fail if fewer than  n bytes are left in the input. @Get a lazy ByteString that is terminated with a NUL byte. Fails 7 if it reaches the end of input without hitting a NUL. !-Get the remaining bytes as a lazy ByteString "Pull n/ bytes from the input, as a strict ByteString.  important klBSplit a ByteString. If the first result is consumed before the -- + second, this runs in constant heap space. 9You must force the returned tuple for that to work, e.g.  case splitAtST n xs of ) (ys,zs) -> consume ys ... consume zs m important n#"Read a Word8 from the monad state $#Read a Word16 in big endian format %&Read a Word16 in little endian format &#Read a Word32 in big endian format '&Read a Word32 in little endian format (#Read a Word64 in big endian format )&Read a Word64 in little endian format *O(1).8 Read a single native machine word. The word is read in 2 host order, host endian form, for the machine you're on. On a 64 bit D machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes. +O(1).@ Read a 2 byte Word16 in native host order and host endianness. ,O(1).9 Read a Word32 in native host order and host endianness. -O(1).8 Read a Word64 in native host order and host endianess. opq !"#$%&'()*+,-"# !$&(%')*+,- !"#$%&'()*+,-(Portable to Hugs and GHC. Requires MPTCsstable,Lennart Kolmodin <kolmodin@dtek.chalmers.se>.=Put merely lifts Builder into a Writer monad, applied to (). /AThe PutM type. A Writer monad over the efficient Builder monoid. 01rstu23Run the 0. monad 4Run the 0. monad with a serialiser 5Run the 0., monad with a serialiser and get its result 6BPop the ByteString we have constructed so far, if any, yielding a % new chunk in the result ByteString. 70Efficiently write a byte into the output buffer 8LAn efficient primitive to write a strict ByteString into the output buffer. J It flushes the current buffer, and writes the argument into a new chunk. 9?Write a lazy ByteString efficiently, simply appending the lazy ( ByteString chunks to the output buffer :$Write a Word16 in big endian format ;'Write a Word16 in little endian format <$Write a Word32 in big endian format ='Write a Word32 in little endian format >$Write a Word64 in big endian format ?'Write a Word64 in little endian format @O(1).1 Write a single native machine word. The word is = written in host order, host endian form, for the machine you're on. G On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 6 4 bytes. Values written this way are not portable to > different endian or word sized machines, without conversion. AO(1).: Write a Word16 in native host order and host endianness.  For portability issues see  putWordhost. BO(1).: Write a Word32 in native host order and host endianness.  For portability issues see  putWordhost. CO(1).% Write a Word64 in native host order J On a 32 bit machine we write two host order Word32s, in big endian form.  For portability issues see  putWordhost. ./0123456789:;<=>?@ABC./0145236789:<>;=?@ABC./010123456789:;<=>?@ABCFportable to Hugs and GHC. Requires the FFI and some flexible instancesunstable,Lennart Kolmodin <kolmodin@dtek.chalmers.se> vDThe Binary class provides E and F, methods to encode and F decode a Haskell value to a lazy ByteString. It mirrors the Read and B Show classes for textual representation of Haskell types, and is D suitable for serialising Haskell values to disk, over the network. BFor parsing and generating simple external binary formats (e.g. C A structures), Binary may be used, but in general is not suitable ? for complex protocols. Instead use the Put and Get primitives  directly. ;Instances of Binary should satisfy the following property:   decode . encode == id  That is, the F and E' methods should be the inverse of each D other. A range of instances are provided for basic Haskell types. E!Encode a value in the Put monad. F Decode a value in the Get monad G@Encode a value using binary serialisation to a lazy ByteString. HNDecode a value from a lazy ByteString, reconstructing the original structure. I#Lazily serialise a value to a file 'This is just a convenience function, it's defined simply as:  ' encodeFile f = B.writeFile f . encode ASo for example if you wanted to compress as well, you could use: # B.writeFile f . compress . encode J9Lazily reconstruct a value previously written to a file. 'This is just a convenience function, it's defined simply as:  1 decodeFile f = return . decode =<< B.readFile f CSo for example if you wanted to decompress as well, you could use:  / return . decode . decompress =<< B.readFile f 0After contructing the data from the input file, J checks F if the file is empty, and in doing so will force the associated file B handle closed, if it is indeed empty. If the file is not empty, D it is up to the decoding instance to consume the rest of the data, % or otherwise finalise the resource. wxy' getMany n' get n/ elements in order, without blowing the stack. z{|}~#.7DEFGHIJ DEF.7#GHIJDEFEFGHIJ      !"#$%&'()*+,-./01234356789 :;< =>?@ABCDDEFGHEIEIEIEJGEIKLMNOPQRSTUVWXYZZ?>[\]^_`abcdeefghijklmnlmolmplmqlmrsbinary-0.5.0.1Data.Binary.BuilderData.Binary.GetData.Binary.Put Data.BinaryBuilderempty singletonappendfromByteStringfromLazyByteStringtoLazyByteStringflush putWord16be putWord16le putWord32be putWord32le putWord64be putWord64le putWordhost putWord16host putWord32host putWord64hostGetrunGet runGetStateskip uncheckedSkip lookAhead lookAheadM lookAheadEuncheckedLookAhead bytesRead remainingisEmpty getByteStringgetLazyByteStringgetLazyByteStringNulgetRemainingLazyByteStringgetBytesgetWord8 getWord16be getWord16le getWord32be getWord32le getWord64be getWord64le getWordhost getWord16host getWord32host getWord64hostPutPutMunPut putBuilderexecPutrunPutrunPutMputWord8 putByteStringputLazyByteStringBinaryputgetencodedecode encodeFile decodeFileBufferbytestring-0.9.1.7Data.ByteString.Lazy.Internal ByteString runBuilderData.ByteString.LazyData.ByteString.Internal fromChunks defaultSize unsafeLiftIOwithSize mapBuilder ensureFreewriteN writeNBuffer newBuffer writeNbyteswriteNBufferBytes shiftr_w16 shiftr_w32 shiftr_w64unGetS initStatemkStatefailDescjoin splitAtSTreadNgetPtr shiftl_w16 shiftl_w32 shiftl_w64PairSsndStellSmallIntunrollrollgetManybaseGHC.WordWordWord8Word16Word32Word64