Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 240 additions & 0 deletions bin/add-opt-in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
#!/usr/bin/env python
# Copyright (c) 2012, Adobe Systems Incorporated
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Adobe Systems Incorporated nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''See readme or run with no args for usage'''

import os
import sys
import tempfile
import shutil
import struct
import zlib
import hashlib
import inspect

supportsLZMA = False
try:
import pylzma
supportsLZMA = True
except:
pass

####################################
# Helpers
####################################

class stringFile(object):
def __init__(self, data):
self.data = data

def read(self, num=-1):
result = self.data[:num]
self.data = self.data[num:]
return result

def close(self):
self.data = None

def flush(self):
pass

def consumeSwfTag(f):
tagBytes = b""

recordHeaderRaw = f.read(2)
tagBytes += recordHeaderRaw

if recordHeaderRaw == "":
raise Exception("Bad SWF: Unexpected end of file")
recordHeader = struct.unpack("BB", recordHeaderRaw)
tagCode = ((recordHeader[1] & 0xff) << 8) | (recordHeader[0] & 0xff)
tagType = (tagCode >> 6)
tagLength = tagCode & 0x3f
if tagLength == 0x3f:
ll = f.read(4)
longlength = struct.unpack("BBBB", ll)
tagLength = ((longlength[3]&0xff) << 24) | ((longlength[2]&0xff) << 16) | ((longlength[1]&0xff) << 8) | (longlength[0]&0xff)
tagBytes += ll
tagBytes += f.read(tagLength)
return (tagType, tagBytes)

def outputInt(o, i):
o.write(struct.pack('I', i))

def outputTelemetryTag(o, passwordClear):

lengthBytes = 2 # reserve
if passwordClear:
sha = hashlib.sha256()
sha.update(passwordClear)
passwordDigest = sha.digest()
lengthBytes += len(passwordDigest)

# Record header
code = 93
if lengthBytes >= 63:
o.write(struct.pack('<HI', code << 6 | 0x3f, lengthBytes))
else:
o.write(struct.pack('<H', code << 6 | lengthBytes))

# Reserve
o.write(struct.pack('<H', 0))

# Password
if passwordClear:
o.write(passwordDigest)

####################################
# main()
####################################

if __name__ == "__main__":

####################################
# Parse command line
####################################

if len(sys.argv) < 2:
print("Usage: %s SWF_FILE [PASSWORD]" % os.path.basename(inspect.getfile(inspect.currentframe())))
print("\nIf PASSWORD is provided, then a password will be required to view advanced telemetry in Adobe 'Monocle'.")
sys.exit(-1)

infile = sys.argv[1]
passwordClear = sys.argv[2] if len(sys.argv) >= 3 else None

####################################
# Process SWF header
####################################

swfFH = open(infile, 'rb')
signature = swfFH.read(3)
swfVersion = swfFH.read(1)
struct.unpack("<I", swfFH.read(4))[0] # uncompressed length of file

if signature == b"FWS":
pass
elif signature == b"CWS":
decompressedFH = stringFile(zlib.decompressobj().decompress(swfFH.read()))
swfFH.close()
swfFH = decompressedFH
elif signature == b"ZWS":
if not supportsLZMA:
raise Exception("You need the PyLZMA package to use this script on \
LZMA-compressed SWFs. http://www.joachim-bauch.de/projects/pylzma/")
swfFH.read(4) # compressed length
decompressedFH = stringFile(pylzma.decompress(swfFH.read()))
swfFH.close()
swfFH = decompressedFH
else:
raise Exception("Bad SWF: Unrecognized signature: %s" % signature)

f = swfFH
o = tempfile.TemporaryFile()

o.write(signature)
o.write(swfVersion)
outputInt(o, 0) # FileLength - we'll fix this up later

# FrameSize - this is nasty to read because its size can vary
rs = f.read(1)
r = struct.unpack("B", rs)
rbits = (r[0] & 0xff) >> 3
rrbytes = (7 + (rbits*4) - 3) / 8;
o.write(rs)
o.write(f.read((int)(rrbytes)))

o.write(f.read(4)) # FrameRate and FrameCount

####################################
# Process each SWF tag
####################################

while True:
(tagType, tagBytes) = consumeSwfTag(f)
if tagType == 93:
raise Exception("Bad SWF: already has EnableTelemetry tag")
elif tagType == 92:
raise Exception("Bad SWF: Signed SWFs are not supported")
elif tagType == 69:
# FileAttributes tag
o.write(tagBytes)

# Look ahead for Metadata tag. If present, put our tag after it
(nextTagType, nextTagBytes) = consumeSwfTag(f)
writeAfterNextTag = nextTagType == 77
if writeAfterNextTag:
o.write(nextTagBytes)

outputTelemetryTag(o, passwordClear)

# If there was no Metadata tag, we still need to write that tag out
if not writeAfterNextTag:
o.write(nextTagBytes)

(tagType, tagBytes) = consumeSwfTag(f)

o.write(tagBytes)

if tagType == 0:
break

####################################
# Finish up
####################################

# Fix the FileLength header
uncompressedLength = o.tell()
o.seek(4)
o.write(struct.pack("I", uncompressedLength))
o.flush()
o.seek(0)

# Copy the temp file to the outFile, compressing if necessary
outFile = open(infile, "wb")
if signature == b"FWS":
shutil.copyfileobj(o, outFile)
else:
outFile.write(o.read(8)) # File is compressed after header
if signature == b"CWS":
outFile.write(zlib.compress(o.read()))
elif signature == b"ZWS":
compressed = pylzma.compress(o.read())
outputInt(outFile, len(compressed)-5) # LZMA SWF has CompressedLength header field
outFile.write(compressed)
else:
assert(false)

outFile.close()

if passwordClear:
print("Added opt-in flag with encrypted password " + passwordClear)
else:
print("Added opt-in flag with no password")
1 change: 1 addition & 0 deletions build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ flexdir=/opt/flowplayer/flex3sdk
mxmlc_bin= ${flexbindir}/mxmlc
compc_bin= ${flexbindir}/compc
asdoc_bin= /Users/Api/flex_sdk_3/bin/asdoc
scout_telemetry_bin=bin/add-opt-in.py

devkit-dir=../flash/lib/devkit
plugins.dir=../flash/plugins
Expand Down
20 changes: 20 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@
<copy file="${build-dir}/${player-binary}" tofile="${build-dir}/${player-binary-versioned}"/>
</target>



<!-- Post processes the build with Adobe Scount Telemetry -->
<target name="compile-scout" depends="prepare">
<property name="licenselib" value="${lib-dir}/licensekey"/>
<compile-player binary="${player-binary}" commercial="false" free="true" license-lib="${licenselib}"
debug="false"/>
<scout player-swf="${build-dir}/${player-binary}"/>
<copy file="${build-dir}/${player-binary}" tofile="${build-dir}/${player-binary-versioned}"/>
</target>

<target name="build-debug" description="builds the debug version of the player" depends="prepare">
<property name="licenselib" value="${lib-dir}/licensekey"/>
<compile-player binary="${debug-player-binary}" commercial="false" free="true" license-lib="${licenselib}"
Expand All @@ -171,6 +182,15 @@
tofile="${build-dir}/${commercial-player-binary-versioned}"/>
</target>

<macrodef name="scout">
<attribute name="player-swf"/>
<sequential>
<exec executable="${scout_telemetry_bin}" failonerror="true">
<arg line="@{player-swf}"/>
</exec>
</sequential>
</macrodef>

<macrodef name="example">
<attribute name="player-swf"/>
<attribute name="example-dir"/>
Expand Down