git.iko.soy/typeable/file-embed
on master help sign in
Merge pull request #8 from mgsloan/magic-postfixes

Allow custom postfix for the magic string #7

commite0959613ea2963bd870a0ee3a901667a9969de23 authorMichael Snoyman <michael@snoyman.com> dateWed 28 Jan 2015, 18:14 (12 years ago) parent48572bcf361b4a
1 file changed+44−15
Data/FileEmbed.hs+44−15
Data/FileEmbed.hs+44−15
@@ -1,5 +1,6 @@
11{-# LANGUAGE TemplateHaskell #-}
22{-# LANGUAGE CPP #-}
3+{-# LANGUAGE OverloadedStrings #-}
34-- | This module uses template Haskell. Following is a simplified explanation of usage for those unfamiliar with calling Template Haskell functions.
45--
56-- The function @embedFile@ in this modules embeds a file into the exceutable
@@ -24,9 +25,12 @@ module Data.FileEmbed
2425 -- * Inject into an executable
2526#if MIN_VERSION_template_haskell(2,5,0)
2627 , dummySpace
28+ , dummySpaceWith
2729#endif
2830 , inject
2931 , injectFile
32+ , injectWith
33+ , injectFileWith
3034 -- * Internal
3135 , stringToBs
3236 , bsToExp
@@ -159,15 +163,15 @@ fileList' realTop top = do
159163liftPair2 :: Monad m => (a, m b) -> m (a, b)
160164liftPair2 (a, b) = b >>= \b' -> return (a, b')
161165
162−magic :: String
163−magic = concat ["fe", "MS"]
166+magic :: B.ByteString -> B.ByteString
167+magic x = B8.concat ["fe", x]
164168
165169sizeLen :: Int
166170sizeLen = 20
167171
168172getInner :: B.ByteString -> B.ByteString
169173getInner b =
170− let (sizeBS, rest) = B.splitAt sizeLen $ B.drop (length magic) b
174+ let (sizeBS, rest) = B.splitAt sizeLen b
171175 in case reads $ B8.unpack sizeBS of
172176 (i, _):_ -> B.take i rest
173177 [] -> error "Data.FileEmbed (getInner): Your dummy space has been corrupted."
@@ -179,40 +183,65 @@ padSize i =
179183
180184#if MIN_VERSION_template_haskell(2,5,0)
181185dummySpace :: Int -> Q Exp
182−dummySpace space = do
186+dummySpace = dummySpaceWith "MS"
187+
188+-- | Like 'dummySpace', but takes a postfix for the magic string. In
189+-- order for this to work, the same postfix must be used by 'inject' /
190+-- 'injectFile'. This allows an executable to have multiple
191+-- 'ByteString's injected into it, without encountering collisions.
192+dummySpaceWith :: B.ByteString -> Int -> Q Exp
193+dummySpaceWith postfix space = do
183194 let size = padSize space
184− let start = magic ++ size
185− let chars = LitE $ StringPrimL $
195+ magic' = magic postfix
196+ start = B8.unpack magic' ++ size
197+ magicLen = B8.length magic'
198+ len = magicLen + sizeLen + space
199+ chars = LitE $ StringPrimL $
186200#if MIN_VERSION_template_haskell(2,6,0)
187201 map (toEnum . fromEnum) $
188202#endif
189203 start ++ replicate space '0'
190− let len = fromIntegral $ length start + space :: Int
191− [| getInner (unsafePerformIO (unsafePackAddressLen len $(return chars))) |]
204+ [| getInner (B.drop magicLen (unsafePerformIO (unsafePackAddressLen len $(return chars)))) |]
192205#endif
193206
194207inject :: B.ByteString -- ^ bs to inject
195208 -> B.ByteString -- ^ original BS containing dummy
196209 -> Maybe B.ByteString -- ^ new BS, or Nothing if there is insufficient dummy space
197−inject toInj orig =
210+inject = injectWith "MS"
211+
212+-- | Like 'inject', but takes a postfix for the magic string.
213+injectWith :: B.ByteString -- ^ postfix of magic string
214+ -> B.ByteString -- ^ bs to inject
215+ -> B.ByteString -- ^ original BS containing dummy
216+ -> Maybe B.ByteString -- ^ new BS, or Nothing if there is insufficient dummy space
217+injectWith postfix toInj orig =
198218 if toInjL > size
199219 then Nothing
200− else Just $ B.concat [before, B8.pack magic, B8.pack $ padSize toInjL, toInj, B8.pack $ replicate (size - toInjL) '0', after]
220+ else Just $ B.concat [before, magic', B8.pack $ padSize toInjL, toInj, B8.pack $ replicate (size - toInjL) '0', after]
201221 where
222+ magic' = magic postfix
202223 toInjL = B.length toInj
203− (before, rest) = B.breakSubstring (B8.pack magic) orig
204− (sizeBS, rest') = B.splitAt sizeLen $ B.drop (length magic) rest
224+ (before, rest) = B.breakSubstring magic' orig
225+ (sizeBS, rest') = B.splitAt sizeLen $ B.drop (B8.length magic') rest
205226 size = case reads $ B8.unpack sizeBS of
206227 (i, _):_ -> i
207228 [] -> error $ "Data.FileEmbed (inject): Your dummy space has been corrupted. Size is: " ++ show sizeBS
208229 after = B.drop size rest'
209230
210−injectFile :: B.ByteString
231+injectFile :: B.ByteString -- ^ bs to inject
211232 -> FilePath -- ^ template file
212233 -> FilePath -- ^ output file
213234 -> IO ()
214−injectFile inj srcFP dstFP = do
235+injectFile = injectFileWith "MS"
236+
237+-- | Like 'injectFile', but takes a postfix for the magic string.
238+injectFileWith :: B.ByteString -- ^ postfix of magic string
239+ -> B.ByteString -- ^ bs to inject
240+ -> FilePath -- ^ template file
241+ -> FilePath -- ^ output file
242+ -> IO ()
243+injectFileWith postfix inj srcFP dstFP = do
215244 src <- B.readFile srcFP
216− case inject inj src of
245+ case injectWith postfix inj src of
217246 Nothing -> error "Insufficient dummy space"
218247 Just dst -> B.writeFile dstFP dst