MODULE {ByteCode} {} {} INCLUDE "ByteCodeAst.ag" imports { import Data.Word import Data.ByteString.Lazy(ByteString) import Data.Bits } DERIVING MethodFlag : Eq DERIVING TraitAttr : Eq DERIVING ValueKind : Eq DERIVING InstanceFlag : Eq DERIVING * : Show, Eq, Ord { fromSigned :: Word32 -> Int fromSigned w | testBit w 31 = negate (fromIntegral (0xFFFFFFFF - w + 1)) | otherwise = fromIntegral w toSigned :: Int -> Word32 toSigned n | n < 0 = 0xFFFFFFFF - fromIntegral (abs n) + 1 | otherwise = fromIntegral n } -- Some types for lists { type AbcFiles = [AbcFile] type SwfFiles = [SwfFile] } { -- Wraps a sequence of tags as .swf file. -- This is done rather straightforwardly: -- a begin and end tag is added to the list. wrapTagsAsSwf :: Tags -> SwfFile wrapTagsAsSwf tags = swf where swf = SwfFile_File False 9 0 rect 15 0 tags' rect = Rect_Rect 15 0 0 10000 7500 tags' = [begin] ++ tags ++ [end] begin = Tag_FileAttributes True True False True False end = Tag_End emptySwf :: SwfFile emptySwf = wrapTagsAsSwf [] } { -- Can be used to remove "non-code" tags from the SWF file. -- This can be used if the input has rather large (e.g. graphics) tags. -- However, if the result is ever outputted, then it will not have the -- tags that were removed. removeOpaqueTags :: Tags -> Tags removeOpaqueTags = filter (not . isOpaqueTag) isOpaqueTag :: Tag -> Bool isOpaqueTag (Tag_Opaque _ _ _) = True isOpaqueTag _ = False -- Merges the tags of two SWF files and preserves only the -- code blocks, and the fileattributes of the left SWF file. mergeSwf :: SwfFile -> SwfFile -> SwfFile mergeSwf (SwfFile_File c v1 v2 r rt res tags1) (SwfFile_File _ _ _ _ _ _ tags2) = SwfFile_File c v1 v2 r rt res (left ++ right) where left = removeOpaqueTags $ trimEnd tags1 right = removeOpaqueTags $ trimBegin tags2 trimBegin :: Tags -> Tags trimBegin [] = [] trimBegin (Tag_FileAttributes _ _ _ _ _ : rest) = rest trimEnd :: Tags -> Tags trimEnd = reverse . trim . reverse where trim [] = [] trim (Tag_End : rest) = rest }