summaryrefslogtreecommitdiff
path: root/haskell/Data/VCDIFF/Types.hsc
diff options
context:
space:
mode:
Diffstat (limited to 'haskell/Data/VCDIFF/Types.hsc')
-rw-r--r--haskell/Data/VCDIFF/Types.hsc208
1 files changed, 208 insertions, 0 deletions
diff --git a/haskell/Data/VCDIFF/Types.hsc b/haskell/Data/VCDIFF/Types.hsc
new file mode 100644
index 0000000..015f406
--- /dev/null
+++ b/haskell/Data/VCDIFF/Types.hsc
@@ -0,0 +1,208 @@
1{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE PatternSynonyms #-}
6module Data.VCDIFF.Types where
7
8import Control.Exception
9import Control.Monad
10import Data.Bits
11import qualified Data.ByteString as B
12import qualified Data.ByteString.Internal as B
13import Data.Function
14import Data.Int
15import Data.Monoid
16import Data.Primitive.ByteArray
17import qualified Data.Text as T
18import Data.Text.Encoding
19import Data.Word
20import Foreign.C.String
21import Foreign.C.Types
22import Foreign.ForeignPtr
23import Foreign.Marshal.Alloc
24import Foreign.Marshal.Utils
25import Foreign.Ptr
26import Foreign.Storable
27import System.IO
28import System.IO.Error
29import System.IO.Unsafe
30
31
32
33#ifndef SIZEOF_SIZE_T
34#define SIZEOF_SIZE_T __SIZEOF_SIZE_T__
35#define SIZEOF_UNSIGNED_INT __SIZEOF_INT__
36#define SIZEOF_UNSIGNED_LONG __SIZEOF_LONG__
37#define SIZEOF_UNSIGNED_LONG_LONG __SIZEOF_LONG_LONG__
38#define static_assert(...)
39#endif
40#include <xdelta3.h>
41
42type Usize_t = #type usize_t
43-- | Printf code for type Usize_t
44pattern W :: String
45pattern W = #const_str W ""
46
47type Xoff_t = #type xoff_t
48-- | Printf code for type Xoff_t
49pattern Q :: String
50pattern Q = #const_str Q ""
51
52
53-- | These are the five ordinary status codes returned by the
54-- xd3_encode_input() and xd3_decode_input() state machines.
55--
56-- An application must be prepared to handle these five return
57-- values from either xd3_encode_input or xd3_decode_input except
58-- in the case of no-source compression in which case XD3_GETSRCBLK
59-- is never returned. More detailed comments for these are given in
60-- xd3_encode_input and xd3_decode_input comments below.
61newtype ErrorCode = ErrorCode CInt
62 deriving Show
63
64pattern XD3_SUCCESS = ErrorCode 0
65
66-- | need input
67pattern XD3_INPUT = ErrorCode (#const XD3_INPUT)
68
69-- | have output
70pattern XD3_OUTPUT = ErrorCode (#const XD3_OUTPUT)
71
72-- | need a block of source input (with no xd3_getblk function) a chance to do non-blocking read.
73pattern XD3_GETSRCBLK = ErrorCode (#const XD3_GETSRCBLK)
74
75-- | (decode-only) after the initial VCDIFF & first window header
76pattern XD3_GOTHEADER = ErrorCode (#const XD3_GOTHEADER)
77
78-- | notification: returned before a window is processed giving a chance to XD3_SKIP_WINDOW or not XD3_SKIP_EMIT that window.
79pattern XD3_WINSTART = ErrorCode (#const XD3_WINSTART)
80
81-- | notification: returned after encode/decode & output for a window
82pattern XD3_WINFINISH = ErrorCode (#const XD3_WINFINISH)
83
84-- | (encoder only) may be returned by getblk() if the block is too old
85pattern XD3_TOOFARBACK = ErrorCode (#const XD3_TOOFARBACK)
86
87-- | internal error
88pattern XD3_INTERNAL = ErrorCode (#const XD3_INTERNAL)
89
90-- | invalid config
91pattern XD3_INVALID = ErrorCode (#const XD3_INVALID)
92
93-- | invalid input/decoder error
94pattern XD3_INVALID_INPUT = ErrorCode (#const XD3_INVALID_INPUT)
95
96-- | when secondary compression finds no improvement.
97pattern XD3_NOSECOND = ErrorCode (#const XD3_NOSECOND)
98
99-- | currently VCD_TARGET VCD_CODETABLE
100pattern XD3_UNIMPLEMENTED = ErrorCode (#const XD3_UNIMPLEMENTED)
101
102instance Exception ErrorCode
103
104data Config = Config
105 { winsize :: Usize_t -- ^ The encoder window size.
106 -- The encoder allocates a buffer of this size if the
107 -- program supplies input in smaller units (unless the
108 -- XD3_FLUSH flag is set).
109 , sprevsz :: Usize_t -- ^ How far back small string matching goes
110 , iopt_size :: Usize_t -- ^ entries in the instruction-optimizing buffer
111 , flags :: Flags -- ^ stream->flags are initialized from xd3_config & never modified by the library. Use xd3_set_flags to modify flags settings mid-stream.
112 , sec_data :: CompressorConfig -- ^ Secondary compressor config: data
113 , sec_inst :: CompressorConfig -- ^ Secondary compressor config: inst
114 , sec_addr :: CompressorConfig -- ^ Secondary compressor config: addr
115 , smatch_cfg :: Either StringMatcher SMatchSelect -- ^ See enum: use fields below for soft config
116 , chunk_size :: Usize_t -- ^ Suggested chunking size for streaming.
117 }
118
119pattern XD3_DEFAULT_WINSIZE = #const XD3_DEFAULT_WINSIZE
120pattern XD3_DEFAULT_SPREVSZ = #const XD3_DEFAULT_SPREVSZ
121pattern XD3_DEFAULT_IOPT_SIZE = #const XD3_DEFAULT_IOPT_SIZE
122
123newtype Flags = Flags Word32
124 deriving (Storable,Eq,Bits,FiniteBits)
125
126-- used by VCDIFF tools, see xdelta3-main.h.--/
127pattern XD3_JUST_HDR = Flags (#const XD3_JUST_HDR)
128-- used by VCDIFF tools see xdelta3-main.h.--/
129pattern XD3_SKIP_WINDOW = Flags (#const XD3_SKIP_WINDOW)
130-- | used by VCDIFF tools, see xdelta3-main.h. */
131pattern XD3_SKIP_EMIT = Flags (#const XD3_SKIP_EMIT)
132-- | flush the stream buffer to prepare for xd3_stream_close(). */
133pattern XD3_FLUSH = Flags (#const XD3_FLUSH)
134-- | use DJW static huffman */
135pattern XD3_SEC_DJW = Flags (#const XD3_SEC_DJW)
136-- | use FGK adaptive huffman */
137pattern XD3_SEC_FGK = Flags (#const XD3_SEC_FGK)
138-- | use LZMA secondary */
139pattern XD3_SEC_LZMA = Flags (#const XD3_SEC_LZMA)
140pattern XD3_SEC_TYPE = Flags (#const XD3_SEC_TYPE)
141-- | disable secondary compression of the data section. */
142pattern XD3_SEC_NODATA = Flags (#const XD3_SEC_NODATA)
143-- | disable secondary compression of the inst section. */
144pattern XD3_SEC_NOINST = Flags (#const XD3_SEC_NOINST)
145-- | disable secondary compression of the addr section. */
146pattern XD3_SEC_NOADDR = Flags (#const XD3_SEC_NOADDR)
147pattern XD3_SEC_NOALL = Flags (#const XD3_SEC_NOALL)
148-- | enable checksum computation in the encoder. */
149pattern XD3_ADLER32 = Flags (#const XD3_ADLER32)
150-- | disable checksum verification in the decoder. */
151pattern XD3_ADLER32_NOVER = Flags (#const XD3_ADLER32_NOVER)
152-- | disable ordinary data * compression feature, only search * the source, not the target. */
153pattern XD3_NOCOMPRESS = Flags (#const XD3_NOCOMPRESS)
154-- | disable the "1.5-pass * algorithm", instead use greedy * matching. Greedy is off by * default. */
155pattern XD3_BEGREEDY = Flags (#const XD3_BEGREEDY)
156-- | used by "recode". */
157pattern XD3_ADLER32_RECODE = Flags (#const XD3_ADLER32_RECODE)
158-- 4 bits to set the compression level the same as the command-line
159-- setting -1 through -9 Flags (-0 corresponds to the XD3_NOCOMPRESS flag
160-- and is independent of compression level). This is for
161-- convenience especially with xd3_encode_memoryFlags (). */
162pattern XD3_COMPLEVEL_SHIFT = #const XD3_COMPLEVEL_SHIFT
163pattern XD3_COMPLEVEL_MASK = Flags (#const XD3_COMPLEVEL_MASK)
164pattern XD3_COMPLEVEL_1 = Flags (#const XD3_COMPLEVEL_1)
165pattern XD3_COMPLEVEL_2 = Flags (#const XD3_COMPLEVEL_2)
166pattern XD3_COMPLEVEL_3 = Flags (#const XD3_COMPLEVEL_3)
167pattern XD3_COMPLEVEL_6 = Flags (#const XD3_COMPLEVEL_6)
168pattern XD3_COMPLEVEL_9 = Flags (#const XD3_COMPLEVEL_9)
169
170instance Monoid Flags where
171 mempty = Flags 0
172 Flags a `mappend` Flags b = Flags (a .|. b)
173
174-- | Settings for the secondary compressor.
175data CompressorConfig = CompressorConfig
176 { ngroups :: Usize_t -- ^ Number of DJW Huffman groups.
177 , sector_size :: Usize_t -- ^ Sector size.
178 , inefficient :: #{type int} -- ^ If true, ignore efficiency check [avoid XD3_NOSECOND].
179 }
180
181-- | The values of this enumeration are set in xd3_config using the
182-- 'smatch_cfg' variable. It can be set to default, slow, fast, etc.,
183-- and soft.
184data SMatchSelect
185 = SMATCH_DEFAULT -- ^ Flags may contain XD3_COMPLEVEL bits, else default.
186 | SMATCH_SLOW
187 | SMATCH_FAST
188 | SMATCH_FASTER
189 | SMATCH_FASTEST
190 deriving Enum
191
192-- | This type exists only to be a tag for Ptr to an underlying C-struct called
193-- xd3_stream.
194data Xd3Stream
195
196-- | This is the record of a pre-compiled configuration, a subset of
197-- xd3_config. (struct _xd3_smatcher)
198data StringMatcher = StringMatcher
199 { smName :: String
200 , smStringMatch :: FunPtr (Ptr Xd3Stream -> ErrorCode)
201 , smLargeLook :: Usize_t
202 , smLargeStep :: Usize_t
203 , smSmallLook :: Usize_t
204 , smSmallChain :: Usize_t
205 , smSmallLchain :: Usize_t
206 , smMaxLazy :: Usize_t
207 , smLongEnough :: Usize_t
208 }