Commits

Alexander Konovalov committed 515f91b

Added fileLength fix

  • Participants
  • Parent commits 824536f

Comments (0)

Files changed (4)

File bytecode-to-swf.py

         codeTag.rawData = bytecode
         swf.tags.append(codeTag)
         
-        SWFWriter(f).write(swf)
+        writer = SWFWriter(f)
+        writer.write(swf)
+        writer.fixFileLength()
         f.close()
     else:
         print("Invalid arguments. Please use like this 'bytecode-to-swf.py file bytecode'")

File decompress.py

       swf = SWFReader(f).read()
       swf.header.compressed = False
       with io.FileIO(sys.argv[2], 'w+') as out:
-        SWFWriter(out).write(swf)
+        writer = SWFWriter(out)
+        writer.write(swf)
+        writer.fixFileLength()
   else:
     print("Usage: decompress.py input output'")
         bytecode = f.read()
         f.close()
         
-        tag = Tag()
-        tag.type = TAG_DOACTION
-        tag.rawData = bytecode
-        
         swf = None
         with io.FileIO(sys.argv[1]) as f:
           swf = SWFReader(f).read()
         swf.header.compressed = False
         
-        swf.tags.insert(1, tag)
+        frames = list(filter(lambda t: t.type == TAG_DOACTION, swf.tags))
+        if len(frames) == 0:
+          swf.tags.insert(1, tag)
+        else:
+          frames[0].rawData = bytecode + frames[0].rawData
         
         with io.FileIO(sys.argv[3], 'w+') as out:
-          SWFWriter(out).write(swf)
+          writer = SWFWriter(out)
+          writer.write(swf)
+          writer.fixFileLength()
     else:
         print("Usage: inject.py input bytecode output")

File swf/writer.py

 from .bitsio import *
 from .dataio import *
 from .structure import *
+import io
 
 class SWFWriter(object):
     def __init__(self, baseIO):
         self.dataIO.write(b"FWS")
         self.dataIO.uli8(header.version)
         # note that we write a zero length since the actual file size is not yet known
-        # wonder if it will cause any bugs..
+        # I wonder if it might cause any problems.
         self.dataIO.uli32(0)
         self.writeRect(header.frameSize)
         self.dataIO.uli16(header.frameRate)
         self.dataIO.uli16(header.frameCount)
     
+    def fixFileLength(self):
+        self.baseIO.seek(0, io.SEEK_END)
+        size = self.baseIO.tell()
+        self.baseIO.seek(4, io.SEEK_SET)
+        self.dataIO.uli32(size)
+    
     def writeTag(self, tag):
         length = len(tag.rawData)
         tagCodeAndLength = (tag.type << 6) | (63 if length >= 63 else length)