39 Commits
Author SHA1 Message Date
Vladimir Panteleev 99dfd797ba Update Changelog 2012-09-08 20:57:34 +03:00
Vladimir Panteleev f4824cd3b4 assembler: Distinguish unlabeled and first-label homonym namespace 2012-09-07 00:52:25 +03:00
Vladimir Panteleev 87047b920c disassembler: Fix absolute UNIX paths 2012-09-07 00:50:22 +03:00
Vladimir Panteleev f17e97848b DMD 2.060 (work around issue 8526) 2012-09-07 00:01:47 +03:00
Vladimir Panteleev 336665a213 Documentation updates 2012-09-04 20:14:04 +03:00
Vladimir Panteleev fdb8489382 Method ID debug code 2012-09-04 20:13:59 +03:00
Vladimir Panteleev afe42b5d1f disassembler: improve ContextItem combining 2012-09-04 20:13:54 +03:00
Vladimir Panteleev 3a9829467f disassembler: dead code cleanup 2012-09-04 20:13:50 +03:00
Vladimir Panteleev 8e969d9e76 disassembler: Reduce memory allocations 2012-09-04 20:13:45 +03:00
Vladimir Panteleev d755065785 refid generation overhaul (support non-private homonym namespaces) 2012-09-04 20:13:40 +03:00
Vladimir Panteleev ea3dfd30b1 Documentation: rewrap Markdown to 79 columns 2012-09-03 18:25:40 +03:00
Vladimir Panteleev 86a4a2e0b8 Documentation: dos2unix 2012-09-03 18:05:49 +03:00
Vladimir Panteleev 704045594a Update Changelog 2012-09-03 16:01:00 +03:00
Vladimir Panteleev 5b4c31d2a0 Documentation: Explain #version a bit more 2012-09-03 16:00:04 +03:00
Vladimir Panteleev a56d074dad disassembler: Fix handling of recursively-nested functions 2012-09-03 14:38:11 +03:00
Vladimir Panteleev d722bdcb3c abcfile: Add coerce_b, coerce_i and coerce_d opcodes 2012-09-03 14:37:51 +03:00
Vladimir Panteleev ef9e768b07 DMD 2.060 2012-08-20 15:15:59 +03:00
Vladimir Panteleev d321be2b25 build_rabcdasm: Force rebuild of test programs 2012-08-10 06:57:23 +03:00
Vladimir Panteleev 6543d17fc4 Attempted fix for DMD 2.060 2012-08-09 08:42:18 +03:00
Vladimir Panteleev e20332aeba autodata: Fix AutoToString for strings 2012-08-09 06:36:58 +03:00
Vladimir Panteleev d77e2b679e @property fixes 2012-08-09 06:36:55 +03:00
Vladimir Panteleev 9d0d0355e5 autodata: Use .toString when present when dumping arrays 2012-08-09 06:36:38 +03:00
Vladimir Panteleev 5795d5f374 assembler: Don't accept out-of-range ubyte literals 2012-05-01 01:17:16 +03:00
Vladimir Panteleev 8e8d204269 disassembler: Relax filename sanitization for names like "controller" 2012-04-10 01:50:44 +03:00
Vladimir Panteleev 20f416d9d8 DMD 2.059 fixes 2012-04-09 10:22:33 +03:00
Vladimir Panteleev e3634e59cd Update Changelog 2012-03-15 22:44:02 +02:00
Vladimir Panteleev 3c5101f6c0 Update Changelog 2012-03-15 02:55:03 +02:00
Vladimir Panteleev c2042660f2 Fix metadata handling (caused by error in ABC format specification) 2012-03-15 02:47:48 +02:00
Vladimir Panteleev 57d05c6c35 disassembler: Use Windows filename logic on all platforms
This is to allow files disassembled on one platform to
work on other platforms.
2012-03-15 02:23:44 +02:00
Vladimir Panteleev 532700df03 disassembler: Use more Windows-friendly filenames 2012-03-15 02:21:26 +02:00
Vladimir Panteleev fe9b49bfd3 Use UNC paths on Windows to avoid the MAX_PATH limitation 2012-03-15 01:35:26 +02:00
Vladimir Panteleev e7b65d6d50 Handle empty contexts 2012-03-15 01:12:27 +02:00
Vladimir Panteleev a304843b5b Improve handling of out-of-range signed integers 2012-03-15 01:00:32 +02:00
Vladimir Panteleev ebe6639e8a Update asasm.hrc 2012-03-15 00:39:15 +02:00
Vladimir Panteleev 09be72ea9c Improve refid generation for protected namespaces 2012-03-15 00:39:08 +02:00
Vladimir Panteleev 151bf87038 Fix unintentional hierarchy flattening 2012-03-07 05:09:27 +02:00
Vladimir Panteleev 950f16fd23 Update Changelog 2012-02-29 14:01:22 +02:00
Vladimir Panteleev 0dd9dcdb2f Escape empty path segments 2012-02-29 13:56:37 +02:00
Vladimir Panteleev 5e69e44c17 Fix compilation with latest DMD 2012-02-27 21:54:51 +02:00
12 changed files with 1149 additions and 750 deletions
+45 -13
View File
@@ -1,6 +1,38 @@
RABCDAsm Changelog
==================
RABCDAsm v1.12 (2012.09.08)
---------------------------
* Relaxed filename sanitization for names like "controller"
* Added range validation for byte literals
* Fixed LZMA support misdetection caused by stale rdmd cache
* Added coerce_b, coerce_i and coerce_d opcodes
* Fixed handling of recursively-nested function literals
* Fixed handling of homonym non-private namespaces
* #privatens directives are now ignored, and no longer generated
* Improved refid generation
* Bumped #version to 3 (v1.12 disassemblies are not backwards-compatible)
* Documentation updates
RABCDAsm v1.11 (2012.03.15)
---------------------------
* Fixed v1.10 hierarchy flattening regression
* Improved refid generation for protected namespaces
* Improved handling of overflown signed integers
* Fixed metadata handling (caused by error in ABC format specification)
* Added #version directive (v1.11 disassemblies are not backwards-compatible)
* Removed path length limitation on Windows
* Improved filename sanitization
* Updated asasm.hrc
* Documentation updates
RABCDAsm v1.10 (2012.02.29)
---------------------------
* Fixed escaping of paths with empty segments
RABCDAsm v1.9 (2012.02.07)
--------------------------
@@ -35,27 +67,27 @@ RABCDAsm v1.6 (2011.05.13)
--------------------------
* Overhaul of the structure of disassembler-generated files
* Private namespace names and `refid`s for nested classes are now generated
* Private namespace names and `refid`s for nested classes are now generated
from their referenced contexts
* This also fixes problems caused by private namespaces being referenced
* This also fixes problems caused by private namespaces being referenced
in multiple distinct contexts
* One `#include`d file per script instead of per class
* Nested classes, script initializers and other script traits are now in
* Nested classes, script initializers and other script traits are now in
the same file as their main class
* Inline methods are now in separate files, placed according to their usage
* Inline methods are now in separate files, placed according to their usage
context
* Private namespaces are sorted by their autogenerated names in the
* Private namespaces are sorted by their autogenerated names in the
`.privatens.asasm` file
* No syntax changes, output is backwards-compatible
* Improved escaping of file names
* Output correct opcode error locations
* Disassembler will now refuse to overwrite files
* `rabcdasm` will now always generate output in the same directory as the
* `rabcdasm` will now always generate output in the same directory as the
input file
* Failure to decode a method body is now not a fatal error
* Aggressively try to shorten double literals to their shortest representation
* Added the `swfbinexport` and `swfbinreplace` utilities, to aid in manipulating
contents of `DefineBinaryData` tags
* Added the `swfbinexport` and `swfbinreplace` utilities, to aid in
manipulating contents of `DefineBinaryData` tags
* The package can now be built using a simple, dedicated build tool
* Documentation updates
@@ -72,7 +104,7 @@ RABCDAsm v1.4 (2011.03.07)
* Source code ported to D2
* Add support for forward-references for TypeName-kind Multinames
* Correctly order classes by dependencies (extends/implements) and reference
* Correctly order classes by dependencies (extends/implements) and reference
count
* Finish Metadata support
* Documentation updates
@@ -81,7 +113,7 @@ RABCDAsm v1.3 (2010.11.11)
--------------------------
* Fixed double precision problem
* This also fixes problems with illegal default values for function
* This also fixes problems with illegal default values for function
parameters (default values for integer parameters are stored as doubles,
which might become out-of-range due to inadequate double precision)
* Added Changelog
@@ -90,8 +122,8 @@ RABCDAsm v1.3 (2010.11.11)
RABCDAsm v1.2 (2010.11.06)
--------------------------
* Fixed ref generation for orphan objects which were only referenced
by other orphans
* Fixed ref generation for orphan objects which were only referenced by other
orphans
* Better error handling in `abcexport`; warn when no DoABC tags found
* Documentation updates
@@ -101,7 +133,7 @@ RABCDAsm v1.1 (2010.06.30)
* Private namespaces are now referenced by auto-generated names
* Use `:` to delimit namespace and name in QNames for consistency
* Fixed relative include paths
* Add optional byte offsets to labels, which allows lossless representation
* Add optional byte offsets to labels, which allows lossless representation
of jumps inside instructions and outside the function bounds
* Documentation updates
+455 -450
View File
@@ -1,450 +1,455 @@
Robust ABC (ActionScript Bytecode) [Dis-]Assembler
==================================================
[RABCDAsm][] is a collection of utilities including an ActionScript 3
assembler/disassembler, and a few tools to manipulate SWF files.
These are:
* `rabcdasm` - ABC disassembler
* `rabcasm` - ABC assembler
* `abcexport` - extracts ABC from SWF files
* `abcreplace` - replaces ABC in SWF files
* `swfdecompress` - decompresses zlib-compressed SWF files
* `swf7zcompress` - (re-)compress the contents of a SWF using 7-Zip
* `swflzmacompress` - compress the contents of a SWF using LZMA
* `swfbinexport` / `swfbinreplace` - extract/replace contents of binary
data tags from SWF files
`abcexport` and `abcreplace` are reimplementations of similar utilities from
my [swfutilsex][] Java package, however these work faster as they do not parse
the SWF files as deeply.
`swfdecompress` is ancillary and is only useful for debugging and studying of
the SWF file format, and not required for ABC manipulation. It is functionally
equivalent to [flasm][]'s `-x` option. If you frequently work on compressed
SWF files, you may want to decompress them to speed processing up.
`swf7zcompress` is an utility to further reduce the size of SWF files. It uses
[7-Zip][] to compress the data better than the standard zlib library would. It
requires that the `7z` command-line program be installed and in `PATH`.
`swflzmacompress` compresses SWF files using the [LZMA][] algorithm, support
for which was introduced in Flash 11. It will only work with SWF files with
version 13 or higher.
`swfbinexport` and `swfbinreplace` aid in the manipulation of
`DefineBinaryData` tags in SWF files (some files may contain nested SWF files
stored in these tags).
[RABCDAsm]: http://github.com/CyberShadow/RABCDAsm
[swfutilsex]: http://github.com/CyberShadow/swfutilsex
[flasm]: http://flasm.sourceforge.net/
[7-Zip]: http://www.7-zip.org/
[LZMA]: http://en.wikipedia.org/wiki/Lempel-Ziv-Markov_chain_algorithm
Motivation and goals
--------------------
This package was created due to lack of similar software out there.
Particularly, I needed an utility which would allow me to edit ActionScript 3
bytecode with the following properties:
1. Speed. Less waiting means more productivity. `rabcasm` can assemble large
projects (>200000 LOC) in under a second on modern machines.
2. Comfortably-editable output. Each class is decompiled to its own file, with
files arranged in subdirectories representing the package hierarchy. Class
files are `#include`d from the main file.
3. Most importantly - robustness! If the Adobe AVM can load and run the file,
then it must be editable - no matter if the file is obfuscated or otherwise
mutilated to prevent reverse-engineering. RABCDAsm achieves this by using
a textual representation closer to the ABC file format, rather than to what
an ActionScript compiler would generate.
Compiling from source
---------------------
RABCDAsm is written in the [D programming language, version 2][d2].
Assuming you have [git][] and a D2 compiler, such as [dmd][] or [gdc][]
installed, compiling should be as straight-forward as:
git clone git://github.com/CyberShadow/RABCDAsm.git
cd RABCDAsm
dmd -run build_rabcdasm.d
Substitute `dmd` with `gdmd` if you're using gdc. You can use the `DC` and
`DCFLAGS` environment variables to override the detected compiler and default
compilation flags (`-w -O -inline`).
To be able to manipulate SWF files packed with LZMA compression, you'll need to
have the liblzma library and development files installed on your system.
[d2]: http://dlang.org/
[dmd]: http://www.digitalmars.com/d/download.html
[gdc]: http://bitbucket.org/goshawk/gdc/
[git]: http://git-scm.com/
Pre-compiled binaries
---------------------
You can find pre-compiled Windows binaries in the [Downloads section on
GitHub][downloads]. However, please don't expect them to be up-to-date
with the latest source versions.
[downloads]: http://github.com/CyberShadow/RABCDAsm/downloads
Usage
-----
To begin hacking on a SWF file:
abcexport file.swf
This will create `file-0.abc` ... `file-N.abc` (often just `file-0.abc`). Each
file corresponds to an ABC block inside the SWF file.
To disassemble one of the `.abc` files:
rabcdasm file-0.abc
This will create a `file-0` directory, which will contain `file-0.main.asasm`
(the main program file), `file-0.privatens.asasm` (private namespace alias
definitions), and files for ActionScript scripts, classes, and orphan and
script-level methods.
To assemble the `.asasm` files back, and update the SWF file:
rabcasm file-0/file-0.main.asasm
abcreplace file.swf 0 file-0/file-0.main.abc
The second `abcreplace` argument represents the index of the ABC block in the
SWF file, and corresponds to the number in the filename created by `abcexport`.
`swfbinexport` and `swfbinreplace` are used in the same manner as `abcexport`
and `abcreplace`.
Syntax
======
The syntax of the disassembly was designed to be very simple and allow fast
and easy parsing. It is a close representation of the `.abc` file format, and
thus it is somewhat verbose. All constant pool elements (signed/unsigned
integers, doubles, strings, namespaces, namespace sets, multinames) are always
*expanded inline*, for ease of editing. Similarly, classes, instances, methods
and method bodies are also defined inline, in the context of their "parent"
object. By-index references of classes and methods (used in the `newclass`,
`newfunction` and `callstatic` instructions) are represented via
automatically-generated unique "reference strings", declared as `refid` fields.
If you haven't yet, I strongly recommend that you look through Adobe's
[ActionScript Virtual Machine 2 (AVM2) Overview][avm2]. You will most likely
need to consult it for the instruction reference anyway (although you can also
use [this handy list][avm2i] as well). You will find it difficult to understand
the disassembly without good understanding of concepts such as namespaces and
multinames.
[avm2]: http://www.adobe.com/devnet-archive/actionscript/articles/avm2overview.pdf
[avm2i]: http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html
Overview
--------
In order to guarantee unambiguity and data preservation, all strings read from
the input file - including identifiers (variable/function/class names) - are
represented as string literals. Thus, the syntax does not have any "reserved
words" or such - an unrecognized word is treated as an error, not as an
identifier.
Whitespace (outside string literals, of course) is completely ignored, except
where required to separate words. Comments are Intel-assembler-style: a single
`;` demarks a comment until the next end-of-line. Control directives (such as
`#include`) are allowed anywhere where whitespace is allowed.
The syntax is comprised of hierarchical blocks. Each block contains a number
of fields - starting with a keyword specifying the field type. A block is
terminated with the `end` keyword. Some fields contain a limited number of
parameters, and others are, or contain blocks.
Hierarchy
---------
The topmost block in the hierarchy is the `program` block. This must be the
first block in the file (thus, `program` must be the first word in the file as
well). The `program` block contains `script` fields, and `class` / `method`
fields for "orphan" classes and methods (not owned by other objects in the
hierarchy). Orphan methods are usually anonymous functions. The file version is
also specified in the `program` block, using the `minorversion` and
`majorversion` fields (both unsigned integers).
`script` blocks have one mandatory `sinit` field (the script initialization
method) and `trait` fields.
A "trait" can be one of several kinds. The kind is specified right after the
`trait` keyword, followed by the trait name (a multiname). Following the name
are the trait fields, varying by trait kind:
* `slot` / `const` : `slotid` (unsigned integer), `type` (multiname), `value`
* `class` : `slotid`, `class` (the actual class block)
* `function` : `slotid`, `method` (the actual method block)
* `method` / `getter` / `setter` : `dispid` (unsigned integer), `method`
Additionally, all traits may have `flag` fields, describing the trait's
attributes (`FINAL` / `OVERRIDE` / `METADATA`), and `metadata` blocks.
`metadata` blocks (which are ignored by the AVM) consist of a name string, and
a series of `item` fields - each item having a key and value string.
`class` blocks have mandatory `instance` and `cinit` fields, defining the class
instance and the class initializer method respectively. They may also have
`trait` fields and a `refid` field (the `refid` field is not part of the file
format - it's an unique string to allow referencing the class, see above).
`instance` blocks - always declared inline of their `class` block - must
contain one `iinit` field (the instance initializer method), and may contain
one `extends` field (multiname), `implements` fields (multinames), `flag`
fields (`SEALED` / `FINAL` / `INTERFACE` / `PROTECTEDNS`), one `protectedns`
field (namespace), and `trait` fields.
`method` blocks may contain one `name` field (multiname), a `refid` field,
`param` fields (multinames - this represents the parameter types), one
`returns` field (multiname), `flag` fields (`NEED_ARGUMENTS` /
`NEED_ACTIVATION` / `NEED_REST` / `HAS_OPTIONAL` / `SET_DXNS` /
`HAS_PARAM_NAMES`), `optional` fields (values), `paramname` fields (strings),
and a `body` field (method body).
`body` blocks - always declared inline of their `method` block - must contain
the `maxstack`, `localcount`, `initscopedepth` and `maxscopedepth` fields
(unsigned integers), and a `code` field. It may also contain `try` and `trait`
fields.
`code` blocks - always declared inline of their `body` block - are somewhat
different in syntax from other blocks - mostly in that they may contain labels.
Labels follow the most common syntax - a word followed by a `:` character,
optionally followed by a relative byte offset (in case of pointers inside
instructions). Multiple instruction arguments are comma-separated. Instruction
arguments' types depend on the instruction - see the `OpcodeInfo` array in
`abcfile.d` for a reference.
`try` blocks - always declared inline of their `body` block - represent an
"exception" (try/catch) block. They contain five mandatory fields: `from`,
`to` and `target` (names of labels representing start and end of the "try"
block, and start of the "catch" block respectively), and `type` and `name`
(multinames), representing the type and name of the exception variable.
Values have the syntax *type* `(` *value* `)` . *type* can be one of `Integer`,
`UInteger`, `Double`, `Utf8`, `Namespace`, `PackageNamespace`,
`PackageInternalNs`, `ProtectedNamespace`, `ExplicitNamespace`,
`StaticProtectedNs`, `PrivateNamespace`, `True`, `False`, `Null` or
`Undefined`. The type of the value depends on *type*. Types `True`, `False`,
`Null` and `Undefined` have no value.
Constants
---------
Multinames have the syntax *type* `(` *parameters* `)` . *type* can be one of
`QName` / `QNameA`, `RTQName` / `RTQNameA`, `RTQNameL` / `RTQNameLA`,
`Multiname` / `MultinameA`, `MultinameL` / `MultinameLA`, or `TypeName`.
*parameters* depends on *type*:
* `QName` / `QNameA` `(` *namespace* `,` *string* `)`
* `RTQName` / `RTQNameA` `(` *string* `)`
* `RTQNameL` / `RTQNameLA` `(` `)`
* `Multiname` / `MultinameA` `(` *string* `,` *namespace-set* `)`
* `MultinameL` / `MultinameLA` `(` *namespace-set* `)`
* `TypeName` `(` *multiname* `<` *multiname [* `,` *multiname ... ]* `>` `)`
Namespace sets have the syntax `[` *[ namespace [* `,` *namespace ... ] ]* `]`
(that is, a comma-separated list of namespaces in square brackets). Empty
namespace sets can be specified using `[]`.
Namespaces have the syntax *type* `(` *parameters* `)` . For types other than
`PrivateNamespace` there is only one parameter - a string. `PrivateNamespace`
namespaces have a second parameter, a named alias for a particular private
namespace. Internally (the ABC file format), private namespaces are
distinguished by a numerical index - `rabcdasm` will attempt to give them
descriptive names based on their context. Aliases can be defined using the
`#privatens` directive. `rabcdasm` will create a separate file containing the
aliases (`file-0.privatens.asasm`).
Strings have a syntax similar to C string literals. Strings start and end with
a `"`. Supported escape sequences (a backslash followed by a letter) are `\n`
(generates ASCII 0x0A), `\r` (ASCII 0x0D), and `\x` followed by two hexadecimal
digits, which inserts the ASCII character with that code. Any other characters
following a backslash generate that character - thus, you can escape
backslashes using `\\` and double quotes using `\"`. When decompiling,
high-ASCII characters (usually UTF-8) are not escaped - if you see gibberish
instead of international text, configure your editor to open the files in UTF-8
encoding.
Additionally, constant pool types (signed/unsigned integers, doubles, strings,
namespaces, namespace sets and multinames) may also have the value `null`
(which represents the index 0 in the ABC file). Note that `null` is
conceptually different from zero, an empty string or empty namespace set.
Macros
------
RABCDAsm has some basic macro-like capabilities, controlled by directives and
variables. These bear some similarity to the C preprocessor, however these
are processed in-loop rather than as a separate pre-processing step.
### Directives
Directives start with a `#`, followed by a word identifying the directive:
* `#include` *string* - inserts the contents of the file by the specified
filename inline. Functionally equivalent to `#mixin #get` *string* , but
faster.
* `#mixin` *string* - inserts the contents of the specified string inline.
Not very useful on its own.
* `#call` *string* `(` *[ string [* `,` *string ... ] ]* `)` - same as
`#mixin`, however it additionally sets the special variables `$1`, `$2`
etc. to the contents of the specified arguments. When the end of the
inserted string is reached, the old values of `$1`, `$2` etc. are restored.
* `#get` *string* - inserts **a string containing** the contents of the
file by the specified filename inline. Similar to #include, but it inserts
a string (surrounded by `"` etc.) instead.
* `#set` *word* *string* - assigns the contents of the string to the
variable *word*.
* `#unset` *word* - deletes the variable *word*.
* `#privatens` defines a private namespace alias, as described above.
### Variables
Variables are manipulated with the `#set` and `#unset` directives, and can
be instantiated in two ways:
1. `$`*name* - this inserts the contents of the variable inline. Note that
although variables are defined using a string syntax, they are not
inserted as a string using this syntax. Thus, the code:
#set str "Hello, world!"
...
pushstring $str
will expand to `pushstring Hello, world!`, which will result in an error.
To correct the problem, add escaped quotes around the variable contents
( `#set str "\"Hello, world!\""` ), or use the second syntax:
2. `$"`*name*`"` - this inserts a string containing the contents of the
variable inline. This syntax also works for `#call` arguments (e.g.
`$"1"`).
### Example
Here's an example of how to use the above features to create a macro which
logs a string literal and the contents of a register:
#set log "
findpropstrict QName(PackageNamespace(\"\"), \"log\")
pushstring $\"1\"
getlocal $2
callpropvoid QName(PackageNamespace(\"\"), \"log\"), 2
"
; ...
pushbyte 2
pushbyte 2
add_i
setlocal1
#call $"log"("two plus two equals", "1")
Highlighting
------------
Included with the project is the file `asasm.hrc`, a simple syntax definition
for the [Colorer take5][] syntax highlighting library. It should be
straight-forward to adapt it to other syntax highlighting systems.
[Colorer take5]: http://colorer.sourceforge.net/
Hacking
=======
ABC is internally represented in two forms. The `ABCFile` class stores the raw
data structures, as they appear in the binary file. `ASProgram` uses pointers
instead of indexes, allowing easy manipulation without having to worry about
record order or constant pools. Conversion between various states is done as
follows:
file.abc
| ^
------ ABCReader | | ABCWriter ----
/ v | \
/ ABCFile \
/ | ^ \
rabcdasm---------- ABCtoAS | | AStoABC --------rabcasm
\ v | /
\ ASProgram /
\ | ^ /
--- Disassembler | | Assembler ----
v |
file.asasm
`AStoABC` will rebuild the constant pools, in a manner similar to Adobe's
compilers (reverse-sorted by reference count). The exact order will almost
surely be different, however.
Should you need to write an utility to manipulate ABC, you can use the existing
code to load the file to either an `ABCFile` or `ASProgram` instance, and
perform the necessary manipulations using those classes.
Tips
====
The following tips come from the author's experience and may be useful for
RABCDAsm users.
1. Once you have disassembled a SWF file you intend to modify, you should
immediately add the directory to a distributed source control system, such as
[Git][] or [Mercurial][]. This will allow you to easily track and undo your
changes, and easily merge your changes with new versions of SWF files.
[Git]: http://git-scm.com/
[Mercurial]: http://mercurial.selenic.com/
2. If you plan on making non-trivial changes to SWF files, you should install
the [debug Flash Player][]. This will allow you to see validation and
run-time error messages, instead of simply getting an empty window.
[debug Flash Player]: http://www.adobe.com/support/flashplayer/downloads.html
3. The [Fiddler][] Web Debugging Proxy can be very useful for analyzing
websites with SWF content. The following script fragment (which is to be
placed in the `OnBeforeResponse` function) will automatically save all SWF
files while preserving the directory structure.
if (oSession.oResponse.headers.ExistsAndContains("Content-Type",
"application/x-shockwave-flash")) {
// Set desired path here
var path:String = "C:\\Temp\\FiddlerCapture\\" +
oSession.host + oSession.PathAndQuery;
if (path.Contains('?'))
path = path.Substring(0, path.IndexOf('?'));
var dir:String = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
oSession.utilDecodeResponse();
oSession.SaveResponseBody(path);
}
A more robust version of the above snippet is available as a Fiddler plugin
[here][FiddlerAutoCapture].
Once you have edited a SWF file, you can use Fiddler's [AutoResponder][] to
replace the original file with your modified version.
[Fiddler]: http://www.fiddler2.com/fiddler2/
[AutoResponder]: http://www.fiddler2.com/fiddler2/help/AutoResponder.asp
[FiddlerAutoCapture]: https://github.com/CyberShadow/FiddlerAutoCapture
Limitations
===========
* None known.
License
=======
RABCDAsm is distributed under the terms of the GPL v3 or later, with the
exception of `murmurhash2a.d`, `zlibx.d` and LZMA components, which are in the
public domain, and `asasm.hrc`, which is tri-licensed under the MPL 1.1/GPL
2.0/LGPL 2.1. The full text of the GNU General Public License can be found in
the file `COPYING`.
Robust ABC (ActionScript Bytecode) [Dis-]Assembler
==================================================
[RABCDAsm][] is a collection of utilities including an ActionScript 3
assembler/disassembler, and a few tools to manipulate SWF files. These are:
* `rabcdasm` - ABC disassembler
* `rabcasm` - ABC assembler
* `abcexport` - extracts ABC from SWF files
* `abcreplace` - replaces ABC in SWF files
* `swfdecompress` - decompresses zlib-compressed SWF files
* `swf7zcompress` - (re-)compress the contents of a SWF using 7-Zip
* `swflzmacompress` - compress the contents of a SWF using LZMA
* `swfbinexport` / `swfbinreplace` - extract/replace contents of binary data
tags from SWF files
`abcexport` and `abcreplace` are reimplementations of similar utilities from
my [swfutilsex][] Java package, however these work faster as they do not parse
the SWF files as deeply.
`swfdecompress` is ancillary and is only useful for debugging and studying of
the SWF file format, and not required for ABC manipulation. It is functionally
equivalent to [flasm][]'s `-x` option. If you frequently work on compressed
SWF files, you may want to decompress them to speed processing up.
`swf7zcompress` is an utility to further reduce the size of SWF files. It uses
[7-Zip][] to compress the data better than the standard zlib library would. It
requires that the `7z` command-line program be installed and in `PATH`.
`swflzmacompress` compresses SWF files using the [LZMA][] algorithm, support
for which was introduced in Flash 11. It will only work with SWF files with
version 13 or higher.
`swfbinexport` and `swfbinreplace` aid in the manipulation of
`DefineBinaryData` tags in SWF files (some files may contain nested SWF files
stored in these tags).
[RABCDAsm]: http://github.com/CyberShadow/RABCDAsm
[swfutilsex]: http://github.com/CyberShadow/swfutilsex
[flasm]: http://flasm.sourceforge.net/
[7-Zip]: http://www.7-zip.org/
[LZMA]: http://en.wikipedia.org/wiki/Lempel-Ziv-Markov_chain_algorithm
Motivation and goals
--------------------
This package was created due to lack of similar software out there.
Particularly, I needed an utility which would allow me to edit ActionScript 3
bytecode with the following properties:
1. Speed. Less waiting means more productivity. `rabcasm` can assemble large
projects (>200000 LOC) in under a second on modern machines.
2. Comfortably-editable output. Each class is decompiled to its own file,
with files arranged in subdirectories representing the package hierarchy.
Class files are `#include`d from the main file.
3. Most importantly - robustness! If the Adobe AVM can load and run the file,
then it must be editable - no matter if the file is obfuscated or
otherwise mutilated to prevent reverse-engineering. RABCDAsm achieves this
by using a textual representation closer to the ABC file format, rather
than to what an ActionScript compiler would generate.
Compiling from source
---------------------
RABCDAsm is written in the [D programming language, version 2][d2].
Assuming you have [git][] and a D2 compiler, such as [dmd][] or [gdc][]
installed, compiling should be as straight-forward as:
git clone git://github.com/CyberShadow/RABCDAsm.git
cd RABCDAsm
dmd -run build_rabcdasm.d
Substitute `dmd` with `gdmd` if you're using gdc. You can use the `DC` and
`DCFLAGS` environment variables to override the detected compiler and default
compilation flags (`-w -O -inline`).
To be able to manipulate SWF files packed with LZMA compression, you'll need
to have the liblzma library and development files installed on your system.
[d2]: http://dlang.org/
[dmd]: http://www.digitalmars.com/d/download.html
[gdc]: http://bitbucket.org/goshawk/gdc/
[git]: http://git-scm.com/
Pre-compiled binaries
---------------------
You can find pre-compiled Windows binaries in the [Downloads section on
GitHub][downloads]. However, please don't expect them to be up-to-date with
the latest source versions.
[downloads]: http://github.com/CyberShadow/RABCDAsm/downloads
Usage
-----
To begin hacking on a SWF file:
abcexport file.swf
This will create `file-0.abc` ... `file-N.abc` (often just `file-0.abc`). Each
file corresponds to an ABC block inside the SWF file.
To disassemble one of the `.abc` files:
rabcdasm file-0.abc
This will create a `file-0` directory, which will contain `file-0.main.asasm`
(the main program file) and files for ActionScript scripts, classes, and
orphan and script-level methods.
To assemble the `.asasm` files back, and update the SWF file:
rabcasm file-0/file-0.main.asasm
abcreplace file.swf 0 file-0/file-0.main.abc
The second `abcreplace` argument represents the index of the ABC block in the
SWF file, and corresponds to the number in the filename created by `abcexport`.
`swfbinexport` and `swfbinreplace` are used in the same manner as `abcexport`
and `abcreplace`.
Syntax
======
The syntax of the disassembly was designed to be very simple and allow fast
and easy parsing. It is a close representation of the `.abc` file format, and
thus it is somewhat verbose. All constant pool elements (signed/unsigned
integers, doubles, strings, namespaces, namespace sets, multinames) are always
*expanded inline*, for ease of editing. Similarly, classes, instances, methods
and method bodies are also defined inline, in the context of their "parent"
object. By-index references of classes and methods (used in the `newclass`,
`newfunction` and `callstatic` instructions) are represented via
automatically-generated unique "reference strings", declared as `refid` fields.
If you haven't yet, I strongly recommend that you look through Adobe's
[ActionScript Virtual Machine 2 (AVM2) Overview][avm2]. You will most likely
need to consult it for the instruction reference anyway (although you can also
use [this handy list][avm2i] as well). You will find it difficult to
understand the disassembly without good understanding of concepts such as
namespaces and multinames.
[avm2]: http://www.adobe.com/devnet-archive/actionscript/articles/avm2overview.pdf
[avm2i]: http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html
Overview
--------
In order to guarantee unambiguity and data preservation, all strings read from
the input file - including identifiers (variable/function/class names) - are
represented as string literals. Thus, the syntax does not have any "reserved
words" or such - an unrecognized word is treated as an error, not as an
identifier.
Whitespace (outside string literals, of course) is completely ignored, except
where required to separate words. Comments are Intel-assembler-style: a single
`;` demarks a comment until the next end-of-line. Control directives (such as
`#include`) are allowed anywhere where whitespace is allowed.
The syntax is comprised of hierarchical blocks. Each block contains a number
of fields - starting with a keyword specifying the field type. A block is
terminated with the `end` keyword. Some fields contain a limited number of
parameters, and others are, or contain blocks.
Hierarchy
---------
The topmost block in the hierarchy is the `program` block. This must be the
first block in the file (thus, `program` must be the first word in the file as
well). The `program` block contains `script` fields, and `class` / `method`
fields for "orphan" classes and methods (not owned by other objects in the
hierarchy). Orphan methods are usually anonymous functions. The file version
is also specified in the `program` block, using the `minorversion` and
`majorversion` fields (both unsigned integers).
`script` blocks have one mandatory `sinit` field (the script initialization
method) and `trait` fields.
A "trait" can be one of several kinds. The kind is specified right after the
`trait` keyword, followed by the trait name (a multiname). Following the name
are the trait fields, varying by trait kind:
* `slot` / `const` : `slotid` (unsigned integer), `type` (multiname), `value`
* `class` : `slotid`, `class` (the actual class block)
* `function` : `slotid`, `method` (the actual method block)
* `method` / `getter` / `setter` : `dispid` (unsigned integer), `method`
Additionally, all traits may have `flag` fields, describing the trait's
attributes (`FINAL` / `OVERRIDE` / `METADATA`), and `metadata` blocks.
`metadata` blocks (which are ignored by the AVM) consist of a name string, and
a series of `item` fields - each item having a key and value string.
`class` blocks have mandatory `instance` and `cinit` fields, defining the
class instance and the class initializer method respectively. They may also
have `trait` fields and a `refid` field (the `refid` field is not part of the
file format - it's an unique string to allow referencing the class, see above).
`instance` blocks - always declared inline of their `class` block - must
contain one `iinit` field (the instance initializer method), and may contain
one `extends` field (multiname), `implements` fields (multinames), `flag`
fields (`SEALED` / `FINAL` / `INTERFACE` / `PROTECTEDNS`), one `protectedns`
field (namespace), and `trait` fields.
`method` blocks may contain one `name` field (multiname), a `refid` field,
`param` fields (multinames - this represents the parameter types), one
`returns` field (multiname), `flag` fields (`NEED_ARGUMENTS` /
`NEED_ACTIVATION` / `NEED_REST` / `HAS_OPTIONAL` / `SET_DXNS` /
`HAS_PARAM_NAMES`), `optional` fields (values), `paramname` fields (strings),
and a `body` field (method body).
`body` blocks - always declared inline of their `method` block - must contain
the `maxstack`, `localcount`, `initscopedepth` and `maxscopedepth` fields
(unsigned integers), and a `code` field. It may also contain `try` and `trait`
fields.
`code` blocks - always declared inline of their `body` block - are somewhat
different in syntax from other blocks - mostly in that they may contain
labels. Labels follow the most common syntax - a word followed by a `:`
character, optionally followed by a relative byte offset (in case of pointers
inside instructions). Multiple instruction arguments are comma-separated.
Instruction arguments' types depend on the instruction - see the `OpcodeInfo`
array in `abcfile.d` for a reference.
`try` blocks - always declared inline of their `body` block - represent an
"exception" (try/catch) block. They contain five mandatory fields: `from`,
`to` and `target` (names of labels representing start and end of the "try"
block, and start of the "catch" block respectively), and `type` and `name`
(multinames), representing the type and name of the exception variable.
Values have the syntax *type* `(` *value* `)` . *type* can be one of
`Integer`, `UInteger`, `Double`, `Utf8`, `Namespace`, `PackageNamespace`,
`PackageInternalNs`, `ProtectedNamespace`, `ExplicitNamespace`,
`StaticProtectedNs`, `PrivateNamespace`, `True`, `False`, `Null` or
`Undefined`. The type of the value depends on *type*. Types `True`, `False`,
`Null` and `Undefined` have no value.
Constants
---------
Multinames have the syntax *type* `(` *parameters* `)` . *type* can be one of
`QName` / `QNameA`, `RTQName` / `RTQNameA`, `RTQNameL` / `RTQNameLA`,
`Multiname` / `MultinameA`, `MultinameL` / `MultinameLA`, or `TypeName`.
*parameters* depends on *type*:
* `QName` / `QNameA` `(` *namespace* `,` *string* `)`
* `RTQName` / `RTQNameA` `(` *string* `)`
* `RTQNameL` / `RTQNameLA` `(` `)`
* `Multiname` / `MultinameA` `(` *string* `,` *namespace-set* `)`
* `MultinameL` / `MultinameLA` `(` *namespace-set* `)`
* `TypeName` `(` *multiname* `<` *multiname [* `,` *multiname ... ]* `>` `)`
Namespace sets have the syntax `[` *[ namespace [* `,` *namespace ... ] ]* `]`
(that is, a comma-separated list of namespaces in square brackets). Empty
namespace sets can be specified using `[]`.
Namespaces have the syntax *type* `(` *string [* `,` *string ]* `)` . The
first string indicates the namespace name. In the case that there are multiple
distinct namespaces with the same type and name (as `PrivateNamespace`
namespaces usually are), a second parameter may be present to uniquely
distinguish them. Internally (the ABC file format), namespaces are
distinguished by their numerical index. When disassembling, `rabcdasm` will
attempt to assign descriptive labels to homonym namespaces based on their
context.
Strings have a syntax similar to C string literals. Strings start and end with
a `"`. Supported escape sequences (a backslash followed by a letter) are `\n`
(generates ASCII 0x0A), `\r` (ASCII 0x0D), and `\x` followed by two
hexadecimal digits, which inserts the ASCII character with that code. Any
other characters following a backslash generate that character - thus, you can
escape backslashes using `\\` and double quotes using `\"`. When decompiling,
high-ASCII characters (usually UTF-8) are not escaped - if you see gibberish
instead of international text, configure your editor to open the files in
UTF-8 encoding.
Additionally, constant pool types (signed/unsigned integers, doubles, strings,
namespaces, namespace sets and multinames) may also have the value `null`
(which represents the index 0 in the ABC file). Note that `null` is
conceptually different from zero, an empty string or empty namespace set.
Macros
------
RABCDAsm has some basic macro-like capabilities, controlled by directives and
variables. These bear some similarity to the C preprocessor, however these are
processed in-loop rather than as a separate pre-processing step.
### Directives
Directives start with a `#`, followed by a word identifying the directive:
* `#include` *string* - inserts the contents of the file by the specified
filename inline. Functionally equivalent to `#mixin #get` *string* , but
faster.
* `#mixin` *string* - inserts the contents of the specified string inline.
Not very useful on its own.
* `#call` *string* `(` *[ string [* `,` *string ... ] ]* `)` - same as
`#mixin`, however it additionally sets the special variables `$1`, `$2`
etc. to the contents of the specified arguments. When the end of the
inserted string is reached, the old values of `$1`, `$2` etc. are restored.
* `#get` *string* - inserts **a string containing** the contents of the file
by the specified filename inline. Similar to #include, but it inserts a
string (surrounded by `"` etc.) instead.
* `#set` *word* *string* - assigns the contents of the string to the
variable *word*.
* `#unset` *word* - deletes the variable *word*.
* `#privatens` *number* *string* - deprecated, currently ignored.
* `#version` specifies the syntax version of the disassembly. Newer RABCDAsm
versions may emit disassembly output that is not backwards-compatible, but
should still understand older disassemblies. The versions are:
1. The first version.
2. Introduced in v1.11 to work around error in ABC format specification.
3. Introduced in v1.12 to support multiple non-private namespaces with
the same name. This is the current version.
### Variables
Variables are manipulated with the `#set` and `#unset` directives, and can be
instantiated in two ways:
1. `$`*name* - this inserts the contents of the variable inline. Note that
although variables are defined using a string syntax, they are not
inserted as a string using this syntax. Thus, the code:
#set str "Hello, world!"
...
pushstring $str
will expand to `pushstring Hello, world!`, which will result in an error.
To correct the problem, add escaped quotes around the variable contents
( `#set str "\"Hello, world!\""` ), or use the second syntax:
2. `$"`*name*`"` - this inserts a string containing the contents of the
variable inline. This syntax also works for `#call` arguments (e.g.
`$"1"`).
### Example
Here's an example of how to use the above features to create a macro which
logs a string literal and the contents of a register:
#set log "
findpropstrict QName(PackageNamespace(\"\"), \"log\")
pushstring $\"1\"
getlocal $2
callpropvoid QName(PackageNamespace(\"\"), \"log\"), 2
"
; ...
pushbyte 2
pushbyte 2
add_i
setlocal1
#call $"log"("two plus two equals", "1")
Highlighting
------------
Included with the project is the file `asasm.hrc`, a simple syntax definition
for the [Colorer take5][] syntax highlighting library. It should be
straight-forward to adapt it to other syntax highlighting systems.
[Colorer take5]: http://colorer.sourceforge.net/
Hacking
=======
ABC is internally represented in two forms. The `ABCFile` class stores the raw
data structures, as they appear in the binary file. `ASProgram` uses pointers
instead of indexes, allowing easy manipulation without having to worry about
record order or constant pools. Conversion between various states is done as
follows:
file.abc
| ^
------ ABCReader | | ABCWriter ----
/ v | \
/ ABCFile \
/ | ^ \
rabcdasm---------- ABCtoAS | | AStoABC --------rabcasm
\ v | /
\ ASProgram /
\ | ^ /
--- Disassembler | | Assembler ----
v |
file.asasm
`AStoABC` will rebuild the constant pools, in a manner similar to Adobe's
compilers (reverse-sorted by reference count). The exact order will almost
surely be different, however.
Should you need to write an utility to manipulate ABC, you can use the
existing code to load the file to either an `ABCFile` or `ASProgram` instance,
and perform the necessary manipulations using those classes.
Tips
====
The following tips come from the author's experience and may be useful for
RABCDAsm users.
1. Once you have disassembled a SWF file you intend to modify, you should
immediately add the directory to a distributed source control system, such
as [Git][] or [Mercurial][]. This will allow you to easily track and undo
your changes, and easily merge your changes with new versions of SWF files.
[Git]: http://git-scm.com/
[Mercurial]: http://mercurial.selenic.com/
2. If you plan on making non-trivial changes to SWF files, you should install
the [debug Flash Player][]. This will allow you to see validation and
run-time error messages, instead of simply getting an empty window.
[debug Flash Player]: http://www.adobe.com/support/flashplayer/downloads.html
3. The [Fiddler][] Web Debugging Proxy can be very useful for analyzing
websites with SWF content. The following script fragment (which is to be
placed in the `OnBeforeResponse` function) will automatically save all SWF
files while preserving the directory structure.
if (oSession.oResponse.headers.ExistsAndContains("Content-Type",
"application/x-shockwave-flash")) {
// Set desired path here
var path:String = "C:\\Temp\\FiddlerCapture\\" +
oSession.host + oSession.PathAndQuery;
if (path.Contains('?'))
path = path.Substring(0, path.IndexOf('?'));
var dir:String = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
oSession.utilDecodeResponse();
oSession.SaveResponseBody(path);
}
A more robust version of the above snippet is available as a Fiddler plugin
[here][FiddlerAutoCapture].
Once you have edited a SWF file, you can use Fiddler's [AutoResponder][] to
replace the original file with your modified version.
[Fiddler]: http://www.fiddler2.com/fiddler2/
[AutoResponder]: http://www.fiddler2.com/fiddler2/help/AutoResponder.asp
[FiddlerAutoCapture]: https://github.com/CyberShadow/FiddlerAutoCapture
Limitations
===========
* None known.
License
=======
RABCDAsm is distributed under the terms of the GPL v3 or later, with the
exception of `murmurhash2a.d`, `zlibx.d` and LZMA components, which are in the
public domain, and `asasm.hrc`, which is tri-licensed under the MPL 1.1/GPL
2.0/LGPL 2.1. The full text of the GNU General Public License can be found in
the file `COPYING`.
+33 -29
View File
@@ -131,13 +131,8 @@ class ABCFile
struct Metadata
{
struct Item
{
uint key, value;
}
uint name;
Item[] items;
uint[] keys, values;
}
struct Instance
@@ -182,12 +177,12 @@ class ABCFile
}
uint[] metadata;
TraitKind kind() { return cast(TraitKind)(kindAttr&0xF); }
void kind(TraitKind value) { kindAttr = (kindAttr&0xF0) | value; }
@property TraitKind kind() { return cast(TraitKind)(kindAttr&0xF); }
@property void kind(TraitKind value) { kindAttr = (kindAttr&0xF0) | value; }
// TraitAttributes bitmask
ubyte attr() { return cast(ubyte)(kindAttr >> 4); }
void attr(ubyte value) { kindAttr = cast(ubyte)((kindAttr&0xF) | (value<<4)); }
@property ubyte attr() { return cast(ubyte)(kindAttr >> 4); }
@property void attr(ubyte value) { kindAttr = cast(ubyte)((kindAttr&0xF) | (value<<4)); }
}
struct Class
@@ -793,10 +788,10 @@ const OpcodeInfo[256] opcodeInfo = [
/* 0x7E */ {"0x7E", [OpcodeArgumentType.Unknown]},
/* 0x7F */ {"0x7F", [OpcodeArgumentType.Unknown]},
/* 0x80 */ {"coerce", [OpcodeArgumentType.Multiname]},
/* 0x81 */ {"coerce_b", [OpcodeArgumentType.Unknown]},
/* 0x81 */ {"coerce_b", []},
/* 0x82 */ {"coerce_a", []},
/* 0x83 */ {"coerce_i", [OpcodeArgumentType.Unknown]},
/* 0x84 */ {"coerce_d", [OpcodeArgumentType.Unknown]},
/* 0x83 */ {"coerce_i", []},
/* 0x84 */ {"coerce_d", []},
/* 0x85 */ {"coerce_s", []},
/* 0x86 */ {"astype", [OpcodeArgumentType.Multiname]},
/* 0x87 */ {"astypelate", []},
@@ -1023,6 +1018,11 @@ private final class ABCReader
/// Note: may return values larger than 0xFFFFFFFF.
ulong readU32()
out(result)
{
assert(result <= ABCFile.MAX_UINT);
}
body
{
ulong next() { return readU8(); } // force ulong
@@ -1042,10 +1042,15 @@ private final class ABCReader
}
long readS32()
out(result)
{
ulong l = readU32();
if (l & 0xFFFFFFFF00000000) // preserve unused bits
return cast(long)l;
assert(result >= ABCFile.MIN_INT && result <= ABCFile.MAX_INT);
}
body
{
auto l = readU32();
if (l & 0xFFFFFFFF_00000000) // preserve unused bits
return l | 0xFFFFFFF0_00000000;
else
return cast(int)l;
}
@@ -1185,12 +1190,11 @@ private final class ABCReader
{
ABCFile.Metadata r;
r.name = readU30();
r.items.length = readU30();
foreach (ref value; r.items)
{
value.key = readU30();
value.value = readU30();
}
r.keys.length = r.values.length = readU30();
foreach (ref key; r.keys)
key = readU30();
foreach (ref value; r.values)
value = readU30();
return r;
}
@@ -1321,7 +1325,7 @@ private final class ABCReader
size_t start = pos;
size_t end = pos + len;
size_t offset() { return pos - start; }
@property size_t offset() { return pos - start; }
try
{
@@ -1676,12 +1680,12 @@ private final class ABCWriter
void writeMetadata(ref ABCFile.Metadata v)
{
writeU30(v.name);
writeU30(v.items.length);
foreach (ref value; v.items)
{
writeU30(value.key);
writeU30(value.value);
}
assert(v.keys.length == v.values.length);
writeU30(v.keys.length);
foreach (key; v.keys)
writeU30(key);
foreach (value; v.values)
writeU30(value);
}
void writeInstance(ref ABCFile.Instance v)
+9 -4
View File
@@ -4,7 +4,7 @@
<hrc version="take5" xmlns="http://colorer.sf.net/2003/hrc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://colorer.sf.net/2003/hrc http://colorer.sf.net/2003/hrc.xsd">
<type name="asasm">
<annotation><documentation>
ActionScript Assembler Syntax (RABCDAsm variant)
@@ -31,20 +31,24 @@
<!-- Decimal numbers -->
<regexp match="/\b[0-9]+\b/" region0="def:NumberDec"/>
<!-- Paired brackets -->
<block start="/(\()/" end="/(\))/" scheme="asasm" region00="def:Symbol" region01="def:PairStart" region10="def:Symbol" region11="def:PairEnd"/>
<block start="/(\[)/" end="/(\])/" scheme="asasm" region00="def:Symbol" region01="def:PairStart" region10="def:Symbol" region11="def:PairEnd"/>
<block start="/(\<)/" end="/(\>)/" scheme="asasm" region00="def:Symbol" region01="def:PairStart" region10="def:Symbol" region11="def:PairEnd"/>
<!-- Outliner for refids -->
<regexp match="/(\brefid\b)\s*((?{def:StringEdge}&#34;))((?{def:String}[^&#34;]*))((?{def:StringEdge}&#34;))/" region1="def:Keyword" region2="def:PairStart" region3="def:Outlined" region4="def:PairEnd"/> <!-- BUG: does not support escape sequences -->
<!--regexp match="/(\brefid\b)\s*((?{def:StringEdge}&#34;))((?{def:String}[^&#34;]*))((?{def:StringEdge}&#34;))/" region1="def:Keyword" region2="def:PairStart" region3="def:Outlined" region4="def:PairEnd"/--> <!-- BUG: does not support escape sequences -->
<regexp
match="/(\brefid\b)\M\s*&#34;(.*)&#34;$/"
region1="def:Keyword"
region2="def:Outlined"/>
<!--block start="/(\brefid\b)\s*(?{def:StringEdge}&#34;)/" end="/(?{def:StringEdge}&#34;)/" scheme="StringContent" region="def:Outlined" inner-region="yes" region00="def:PairStart" region10="def:PairEnd"/-->
<!-- Symbol/keyword highlighting below -->
<keywords region="def:Symbol">
<symb name=","/>
<symb name=","/>
</keywords>
<keywords ignorecase="no" region="def:Directive">
@@ -55,6 +59,7 @@
<word name="#privatens"/>
<word name="#set"/>
<word name="#unset"/>
<word name="#version"/>
</keywords>
<keywords ignorecase="no" region="def:Keyword">
+26 -32
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010, 2011 Vladimir Panteleev <vladimir@thecybershadow.net>
* Copyright 2010, 2011, 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
* This file is part of RABCDAsm.
*
* RABCDAsm is free software: you can redistribute it and/or modify
@@ -40,7 +40,7 @@ final class ASProgram
ASType kind;
string name;
uint privateIndex; // unique index for private namespaces
uint id; // unique index for private/homonym namespaces
mixin AutoCompare;
mixin AutoToString;
@@ -154,21 +154,15 @@ final class ASProgram
Value[] options;
string[] paramNames;
uint id; // file index
MethodBody vbody;
}
static class Metadata
{
struct Item
{
string key, value;
mixin AutoCompare;
mixin ProcessAllData;
}
string name;
Item[] items;
string[] keys, values;
mixin AutoCompare;
mixin ProcessAllData;
@@ -379,12 +373,12 @@ private final class ABCtoAS
return o;
}
ASProgram.Namespace convertNamespace(ref ABCFile.Namespace namespace, int privateIndex)
ASProgram.Namespace convertNamespace(ref ABCFile.Namespace namespace, int id)
{
auto n = new ASProgram.Namespace();
n.kind = namespace.kind;
n.name = abc.strings[namespace.name];
n.privateIndex = privateIndex;
n.id = id;
return n;
}
@@ -455,7 +449,7 @@ private final class ABCtoAS
}
}
ASProgram.Method convertMethod(ref ABCFile.MethodInfo method)
ASProgram.Method convertMethod(ref ABCFile.MethodInfo method, uint id)
{
auto n = new ASProgram.Method();
n.paramTypes.length = method.paramTypes.length;
@@ -470,6 +464,7 @@ private final class ABCtoAS
n.paramNames.length = method.paramNames.length;
foreach (j, name; method.paramNames)
n.paramNames[j] = abc.strings[name];
n.id = id;
return n;
}
@@ -477,12 +472,12 @@ private final class ABCtoAS
{
auto n = new ASProgram.Metadata();
n.name = abc.strings[md.name];
n.items.length = md.items.length;
foreach (j, ref item; md.items)
{
n.items[j].key = abc.strings[item.key];
n.items[j].value = abc.strings[item.value];
}
n.keys.length = md.keys.length;
foreach (j, key; md.keys)
n.keys[j] = abc.strings[key];
n.values.length = md.values.length;
foreach (j, value; md.values)
n.values[j] = abc.strings[value];
return n;
}
@@ -674,7 +669,7 @@ private final class ABCtoAS
methods.length = methodAdded.length = abc.methods.length;
foreach (i, ref method; abc.methods)
methods[i] = convertMethod(method);
methods[i] = convertMethod(method, i);
metadata.length = abc.metadata.length;
foreach (i, ref md; abc.metadata)
@@ -1110,12 +1105,12 @@ private final class AStoABC : ASVisitor
{
auto n = &abc.metadata[i];
n.name = strings.get(m.name);
n.items.length = m.items.length;
foreach (j, ref item; m.items)
{
n.items[j].key = strings.get(m.items[j].key);
n.items[j].value = strings.get(m.items[j].value);
}
n.keys.length = m.keys.length;
foreach (j, key; m.keys)
n.keys[j] = strings.get(key);
n.values.length = m.values.length;
foreach (j, value; m.values)
n.values[j] = strings.get(value);
}
ASProgram.MethodBody[] bodies;
@@ -1458,11 +1453,10 @@ class ASVisitor : ASTraitsVisitor
if (metadata)
{
visitString(metadata.name);
foreach (ref item; metadata.items)
{
visitString(item.key);
visitString(item.value);
}
foreach (key; metadata.keys)
visitString(key);
foreach (value; metadata.values)
visitString(value);
}
}
+38 -13
View File
@@ -25,6 +25,7 @@ import std.path;
import std.exception;
import abcfile;
import asprogram;
import common;
final class Assembler
{
@@ -52,7 +53,7 @@ final class Assembler
static File load(string filename, string[] arguments = null)
{
return fromFile(filename, cast(string)read(filename), arguments);
return fromFile(filename, cast(string)read(longPath(filename)), arguments);
}
static File fromFile(string filename, string data, string[] arguments = null)
@@ -132,7 +133,8 @@ final class Assembler
}
string[string] vars;
uint[string] privateNamespaces;
uint[string] namespaceLabels; // for homonym namespaces
uint sourceVersion = 1;
void handlePreprocessor()
{
@@ -151,7 +153,7 @@ final class Assembler
break;
case "get":
auto filename = convertFilename(readString());
pushFile(File.fromFile(filename, toStringLiteral(cast(string)read(filename))));
pushFile(File.fromFile(filename, toStringLiteral(cast(string)read(longPath(filename)))));
break;
case "set":
vars[readWord()] = readString();
@@ -160,8 +162,13 @@ final class Assembler
vars.remove(readWord());
break;
case "privatens":
uint index = cast(uint)readUInt();
privateNamespaces[readString()] = index;
enforce(sourceVersion < 3, "#privatens is deprecated");
readUInt();
readString();
break;
case "version":
sourceVersion = cast(uint)readUInt();
enforce(sourceVersion >= 1 && sourceVersion <= 3, "Invalid/unknown #version");
break;
default:
files[0].pos -= word.length;
@@ -558,14 +565,15 @@ final class Assembler
n.kind = toASType(word);
expectChar('(');
n.name = readString();
if (n.kind == ASType.PrivateNamespace)
if (peekChar() == ',')
{
expectChar(',');
skipChar();
string name = readString();
auto pindex = name in privateNamespaces;
if (pindex is null)
throw new Exception("Unknown private namespace name");
n.privateIndex = *pindex;
auto pindex = name in namespaceLabels;
if (pindex)
n.id = *pindex;
else
n.id = namespaceLabels[name] = namespaceLabels.length+1;
}
expectChar(')');
return n;
@@ -756,13 +764,30 @@ final class Assembler
{
auto metadata = new ASProgram.Metadata;
metadata.name = readString();
string[] items;
while (true)
switch (readWord())
{
case "item":
metadata.items ~= ASProgram.Metadata.Item(readString(), readString());
items ~= readString();
items ~= readString();
break;
case "end":
if (sourceVersion < 2)
{
metadata.keys = items[0..$/2];
metadata.values = items[$/2..$];
}
else
{
metadata.keys .length = items.length/2;
metadata.values.length = items.length/2;
foreach (i; 0..items.length/2)
{
metadata.keys [i] = items[i*2 ];
metadata.values[i] = items[i*2+1];
}
}
return metadata;
default:
throw new Exception("Expected item or end");
@@ -1020,7 +1045,7 @@ final class Assembler
throw new Exception("Don't know how to assemble OP_" ~ opcodeInfo[instruction.opcode].name);
case OpcodeArgumentType.UByteLiteral:
instruction.arguments[i].ubytev = cast(ubyte)readUInt();
instruction.arguments[i].ubytev = to!ubyte(readUInt());
break;
case OpcodeArgumentType.IntLiteral:
instruction.arguments[i].intv = readInt();
+44 -6
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010, 2011 Vladimir Panteleev <vladimir@thecybershadow.net>
* Copyright 2010, 2011, 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
* This file is part of RABCDAsm.
*
* RABCDAsm is free software: you can redistribute it and/or modify
@@ -34,7 +34,7 @@ template AutoCompare()
alias typeof(this) _AutoDataTypeReference;
alias Object _AutoDataOtherTypeReference;
override hash_t toHash() const { return _AutoDataHash(); }
override hash_t toHash() const { try { return _AutoDataHash(); } catch(object.Exception e) { assert(0, e.msg); } }
override bool opEquals(Object o) const { return _AutoDataEquals(o); }
override int opCmp(Object o) const { return _AutoDataCmp(o); }
}
@@ -48,7 +48,7 @@ template AutoCompare()
int opCmp(ref const typeof(this) s) const { return _AutoDataCmp(&s); }
}
private hash_t _AutoDataHash() const
@trusted private hash_t _AutoDataHash() const
{
HashDataHandler handler;
handler.hasher.Begin();
@@ -78,7 +78,7 @@ template AutoToString()
static if (is(typeof(this)==class))
override string toString() const { return _AutoDataToString(); }
else // struct
string toString() { return _AutoDataToString(); }
string toString() const { return _AutoDataToString(); }
string _AutoDataToString() const
{
@@ -202,7 +202,7 @@ struct CmpDataHandler(O)
else
static if (is(typeof(T.opCmp)))
enum dataCode = nullCheck!(T, name, reverseStr)
~ "{ int _AutoDataCmp = this." ~ name ~ ".opCmp(_AutoDataOther." ~ name ~ "); if (_AutoDataCmp != 0) return " ~ reverseStr ~ "_AutoDataCmp; }";
~ "{ int _AutoDataCmp = this." ~ name ~ ".opCmp(cast()_AutoDataOther." ~ name ~ "); if (_AutoDataCmp != 0) return " ~ reverseStr ~ "_AutoDataCmp; }";
else
enum dataCode = "if (this." ~ name ~ " < _AutoDataOther." ~ name ~ ") return " ~ reverseStr ~ "(-1);" ~
"if (this." ~ name ~ " > _AutoDataOther." ~ name ~ ") return " ~ reverseStr ~ "( 1);";
@@ -212,8 +212,46 @@ struct CmpDataHandler(O)
struct ToStringDataHandler
{
template getMixinSingle(T, string name)
{
/*
enum getMixinSingle = "
static if (is(typeof(_AutoDataResult ~= " ~ name ~ ".toString())))
_AutoDataResult ~= " ~ name ~ ".toString();
else
_AutoDataResult ~= to!string(" ~ name ~ ");
";
*/
static if (is(typeof(T.init.toString())))
enum getMixinSingle = "_AutoDataResult ~= " ~ name ~ ".toString();";
else
enum getMixinSingle = "_AutoDataResult ~= to!string(" ~ name ~ ");";
}
template getMixinBody(T, string name)
{
// TODO: arrays of arrays
static if (is(T U : U[]) && !is(T : const(char)[]))
{
enum getMixinBody = "
_AutoDataResult ~= ` [ `;
foreach (_AutoDataArrayIndex, _AutoDataArrayItem; " ~ name ~ ")
{
if (_AutoDataArrayIndex) _AutoDataResult ~= ` , `;
" ~ getMixinSingle!(U, "_AutoDataArrayItem") ~ "
}
_AutoDataResult ~= ` ] `;
";
}
else
enum getMixinBody = getMixinSingle!(T, name);
}
template getMixin(T, string name, bool reverseSort)
{
enum getMixin = "_AutoDataResult ~= `" ~ name ~ " = ` ~ to!string(this." ~ name ~ ") ~ ` `;";
enum getMixin =
"_AutoDataResult ~= `" ~ name ~ " = `;" ~
getMixinBody!(T, name) ~
"_AutoDataResult ~= ` `;";
}
}
+2 -2
View File
@@ -33,7 +33,7 @@ version(DigitalMars)
else
const DEFAULT_COMPILER = "gdmd";
const DEFAULT_FLAGS = "-w -O -inline";
const DEFAULT_FLAGS = "-O -inline";
const LZMA_FLAGS = "-version=HAVE_LZMA";
import std.exception;
@@ -55,7 +55,7 @@ void test(string code, string extraFlags=null)
const FN = "test.d";
std.file.write(FN, code);
scope(exit) remove(FN);
enforce(system(format("rdmd --compiler=%s %s %s %s", compiler, flags, extraFlags, FN)) == 0, "Test failed");
enforce(system(format("rdmd --force --compiler=%s %s %s %s", compiler, flags, extraFlags, FN)) == 0, "Test failed");
stderr.writeln(" >>> OK");
}
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
* This file is part of RABCDAsm.
*
* RABCDAsm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RABCDAsm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RABCDAsm. If not, see <http://www.gnu.org/licenses/>.
*/
module common;
import std.path;
import std.string;
import std.array;
string longPath(string s)
{
version(Windows)
{
if (s.startsWith(`\\`))
return s;
else
return `\\?\` ~ s.absolutePath().buildNormalizedPath().replace(`/`, `\`);
}
else
return s;
}
+457 -199
View File
@@ -25,9 +25,11 @@ import std.conv;
import std.exception;
import std.algorithm;
import std.path;
import std.md5;
import abcfile;
import asprogram;
import autodata;
import common;
alias std.array.join join;
@@ -39,7 +41,7 @@ final class StringBuilder
this(string filename)
{
if (exists(filename))
if (exists(longPath(filename)))
throw new Exception(filename ~ " exists");
this.filename = filename;
buf.length = 1024;
@@ -68,10 +70,10 @@ final class StringBuilder
for (int l=0; l<dirSegments.length-1; l++)
{
auto subdir = join(dirSegments[0..l+1], "/");
if (!exists(subdir))
mkdir(subdir);
if (subdir.length && !exists(longPath(subdir)))
mkdir(longPath(subdir));
}
write(filename, buf[0..pos]);
write(longPath(filename), buf[0..pos]);
}
int indent;
@@ -104,9 +106,35 @@ final class StringBuilder
final class RefBuilder : ASTraitsVisitor
{
bool[uint][string][ASType.Max] homonyms;
debug bool homonymsBuilt;
bool hasHomonyms(ASProgram.Namespace ns)
{
debug assert(homonymsBuilt);
auto pnsHomonyms = ns.name in homonyms[ns.kind];
auto nsHomonyms = pnsHomonyms ? *pnsHomonyms : null;
return nsHomonyms.length > 1;
}
void addHomonym(ASProgram.Namespace ns)
{
debug assert(!homonymsBuilt);
homonyms[ns.kind][ns.name][ns.id] = true;
}
/// Represents a link in a "context chain", which represents the context in which an object is encountered.
/// The common root of contexts is used to build ref strings, namespace labels, file paths etc.
/// Expansion is recursive and delayed until decompilation (an object may be encountered inside a private namespace,
/// all contexts of which are unknown until the whole program is scanned).
struct ContextItem
{
enum Type { Multiname, String }
enum Type
{
Multiname, /// An ASProgram.Multiname (may be a private namespace, etc.)
String, /// Fixed string
Group, /// Multiple ContextItem[]s (which are expanded and the common root is taken as the result)
}
Type type;
union
@@ -117,6 +145,11 @@ final class RefBuilder : ASTraitsVisitor
string str;
bool filenameSuffix;
}
struct
{
ContextItem[] group;
string groupFallback;
}
}
struct Segment
@@ -125,60 +158,126 @@ final class RefBuilder : ASTraitsVisitor
string str;
}
Segment[] toSegments(bool filename)
ContextItem[] reduceGroup(RefBuilder refs)
{
assert(type==Type.Group);
ContextItem[][] contexts;
foreach (context; group)
contexts ~= ContextItem.expand(refs, [context]);
ContextItem[] context;
if (contexts.length)
context = reduce!contextRoot(contexts);
if (!context.length)
context = /*null*/[ContextItem(groupFallback)];
return context;
}
Segment[] toSegments(RefBuilder refs, bool filename)
out(result)
{
debug(CONTEXTS) std.stdio.writefln("Segmented:\n\t%s\nto\n\t%s\n", this, result);
}
body
{
final switch(type)
{
case Type.Multiname:
assert(multiname.kind == ASType.QName);
auto nsName = multiname.vQName.ns.name;
if (multiname.vQName.ns.kind == ASType.PrivateNamespace) // unexpanded private namespace
nsName ~= '#' ~ to!string(multiname.vQName.ns.privateIndex);
if (nsName.length)
if (multiname.vQName.name.length)
return [Segment('/', nsName), Segment(filename ? '/' : ':', multiname.vQName.name)];
case Type.Multiname:
{
assert(multiname.kind == ASType.QName);
auto ns = multiname.vQName.ns;
auto nsName = ns.name;
// if (refs.hasHomonyms(ns))
// nsName ~= '#' ~ to!string(ns.id);
if (nsName.length)
if (multiname.vQName.name.length)
return [Segment('/', nsName), Segment(filename ? '/' : ':', multiname.vQName.name)];
else
return [Segment('/', nsName)];
else
return [Segment('/', nsName)];
else
if (multiname.vQName.name.length)
return [Segment('/', multiname.vQName.name)];
else
assert(0);
case Type.String:
return [Segment(filenameSuffix ? '.' : '/', str)];
if (multiname.vQName.name.length)
return [Segment('/', multiname.vQName.name)];
else
assert(0);
}
case Type.String:
return [Segment(filenameSuffix ? '.' : '/', str)];
case Type.Group:
{
Segment[] segments;
foreach (context; reduceGroup(refs))
segments ~= context.toSegments(refs, filename);
return segments;
}
}
}
static ContextItem[] expand(RefBuilder refs, ContextItem[] context, bool abortOnUnknown) /// recursively expand all private namespaces
static ContextItem[] expand(RefBuilder refs, ContextItem[] context)
{
ContextItem[] newContext;
foreach (ref c; context)
{
auto cEx = c.expand(refs, abortOnUnknown);
if (cEx is null) return null;
newContext ~= cEx;
auto cEx = c.expand(refs);
if (cEx)
newContext ~= cEx;
}
return newContext;
}
ContextItem[] expand(RefBuilder refs, bool abortOnUnknown)
bool expanding;
/// Recursively expand contexts (lower to Type.String ContextItems)
ContextItem[] expand(RefBuilder refs)
in
{
if (type==Type.Multiname && multiname.vQName.ns.kind == ASType.PrivateNamespace)
{
auto pcontext = multiname.vQName.ns.privateIndex in refs.privateNamespaces.contexts;
if (pcontext is null)
debug(CONTEXTS) std.stdio.writefln("Expanding:\n\t%s\n", this);
}
out(result)
{
debug(CONTEXTS) std.stdio.writefln("Expanded:\n\t%s\nto\n\t%s\n", this, result);
}
body
{
if (expanding)
final switch (type)
{
if (abortOnUnknown)
return null;
else
case Type.String:
assert(0);
case Type.Multiname:
return (&this)[0..1];
case Type.Group:
return [ContextItem(groupFallback)];
}
auto expanded = expand(refs, *pcontext, abortOnUnknown);
if (expanded is null) return null;
return expanded ~ (multiname.vQName.name.length ? [ContextItem(multiname.vQName.name)] : null); // hack
assert(!expanding);
expanding = true;
scope(exit) expanding = false;
final switch (type)
{
case Type.Multiname:
auto ns = multiname.vQName.ns;
if (ns.kind == ASType.PrivateNamespace)
{
// auto pcontext = ns.id in refs.namespaces[ns.kind].contexts;
// if (pcontext is null)
// return (&this)[0..1];
// assert(pcontext);
auto context = refs.namespaces[ns.kind].getContext(refs, ns.id);
debug(CONTEXTS) std.stdio.writefln("Context of namespace %s is:\n\t%s\n", ns, context);
// auto expanded = expand(refs, context);
// if (expanded is null) return null;
return /*expanded*/context ~ (multiname.vQName.name.length ? [ContextItem(multiname.vQName.name)] : null); // hack
}
break;
case Type.String:
break;
case Type.Group:
return reduceGroup(refs);
}
else
return (&this)[0..1];
return (&this)[0..1];
}
this(ASProgram.Multiname m)
@@ -194,6 +293,13 @@ final class RefBuilder : ASTraitsVisitor
this.filenameSuffix = filenameSuffix;
}
this(ContextItem[] group, string groupFallback)
{
this.type = Type.Group;
this.group = group;
this.groupFallback = groupFallback;
}
mixin AutoCompare;
mixin AutoToString;
@@ -203,16 +309,111 @@ final class RefBuilder : ASTraitsVisitor
mixin(addAutoField("type"));
final switch (type)
{
case Type.Multiname:
mixin(addAutoField("multiname"));
break;
case Type.String:
mixin(addAutoField("str"));
mixin(addAutoField("filenameSuffix"));
break;
case Type.Multiname:
mixin(addAutoField("multiname"));
break;
case Type.String:
mixin(addAutoField("str"));
mixin(addAutoField("filenameSuffix"));
break;
case Type.Group:
mixin(addAutoField("group"));
mixin(addAutoField("groupFallback"));
break;
}
mixin(epilog);
}
static bool similar(ref ContextItem i1, ref ContextItem i2)
{
if (i1.type != i2.type) return false;
final switch (i1.type)
{
case ContextItem.Type.String:
return i1.str == i2.str;
case ContextItem.Type.Multiname:
if (i1.multiname.vQName.name != i2.multiname.vQName.name) return false;
return nsSimilar(i1.multiname.vQName.ns, i2.multiname.vQName.ns);
case ContextItem.Type.Group:
return i1.group == i2.group;
}
}
// truncate=true -> return partial ContextItem
// truncate=false -> return null on partial match
static ContextItem[] combine(bool truncate)(ref ContextItem c1, ref ContextItem c2)
{
if (similar(c1, c2))
return [c1];
if (c1.type != ContextItem.Type.Multiname || c2.type != ContextItem.Type.Multiname)
return null;
if (c1.multiname.kind != ASType.QName || c2.multiname.kind != ASType.QName)
return null;
auto name1 = c1.multiname.vQName.name;
auto name2 = c2.multiname.vQName.name;
auto ns1 = c1.multiname.vQName.ns;
auto ns2 = c2.multiname.vQName.ns;
if (nsSimilar(ns1, ns2) && ns1.name.length && truncate)
{
auto m = new ASProgram.Multiname;
m.kind = ASType.QName;
m.vQName.ns = ns1;
return [ContextItem(m)];
}
if (name1 && !name2 && truncate)
{
swap(c1, c2);
swap(ns1, ns2);
swap(name1, name2);
}
if (!name1 && name2 && nsSimilar(ns1, ns2))
{
if (truncate)
{
auto m = new ASProgram.Multiname;
m.kind = ASType.QName;
m.vQName.ns = ns1;
return [ContextItem(m)];
}
else
return [c2];
}
if (ns1.name.length && ns2.name.length)
{
if (nsSimilar(ns1, ns2))
{
assert(name1 != name2); // handled by similar() check
assert(!truncate); // handled above
if (name2.length)
return [c1, ContextItem(name2)];
else
return [c1];
}
if (ns1.name.length > ns2.name.length && truncate)
{
swap(c1, c2);
swap(ns1, ns2);
swap(name1, name2);
}
auto fullName1 = ns1.name ~ (name1 ? ':' ~ name1 : "");
auto fullName2 = ns2.name ~ (name2 ? ':' ~ name2 : "");
if (fullName2.startsWith(fullName1 ~ ":"))
return [truncate ? c1 : c2];
}
return null;
}
alias combine!true commonRoot;
alias combine!false deduplicate;
}
ContextItem[] context; // potential optimization: use array-based stack
@@ -223,36 +424,52 @@ final class RefBuilder : ASTraitsVisitor
struct ContextSet(T, bool ALLOW_DUPLICATES)
{
ContextItem[][T] contexts;
string[T] names, filenames;
ContextItem[][][T] contextSets;
debug bool contextsSealed;
void add(U)(U obj, ContextItem[] context)
string[T] names, filenames;
debug bool coagulated;
bool add(U)(U obj, ContextItem[] context)
{
debug assert(!coagulated);
debug assert(!contextsSealed);
auto p = cast(T)obj;
static if (ALLOW_DUPLICATES)
auto pset = p in contextSets;
if (!pset)
{
auto pexisting = p in contexts;
if (pexisting)
{
auto rootContext = contextRoot(*pexisting, context);
contexts[p] = rootContext;
}
else
contexts[p] = context;
contextSets[p] ~= context.dup;
return true;
}
else
{
if (p in contexts)
contexts[p] = [ContextItem("multireferenced")];
else
contexts[p] = context.dup;
if ((*pset)[$-1] != context)
*pset ~= context.dup;
return false;
}
}
bool addIfNew(U)(U obj, ContextItem[] context)
{
if (isAdded(obj))
return false;
else
return add(obj, context);
}
void coagulate(RefBuilder refs)
{
debug assert(!coagulated);
int[string] collisionCounter;
T[string] first;
foreach (obj, set; contextSets)
if (obj !in contexts)
getContext(refs, obj);
foreach (obj; contexts.keys.sort)
{
auto context = contexts[obj];
@@ -276,51 +493,81 @@ final class RefBuilder : ASTraitsVisitor
filenames[obj] = bfilename ~ suffix;
collisionCounter[bname] = counter+1;
}
debug coagulated = true;
}
bool isAdded(U)(U obj) { return (cast(T)obj in contexts) ? true : false; }
ContextItem[] getContext(U)(U obj) { return contexts[cast(T)obj]; }
bool isAdded(U)(U obj) { return (cast(T)obj in contextSets) ? true : false; }
ContextItem[] getContext(U)(RefBuilder refs, U obj)
{
// debug assert(coagulated);
debug contextsSealed = true;
auto p = cast(T)obj;
auto pcontext = p in contexts;
if (pcontext)
return *pcontext;
auto set = contextSets[p];
static if (ALLOW_DUPLICATES)
{
auto context = ContextItem.expand(refs, set[0]);
foreach (setContext; set[1..$])
context = contextRoot(context, ContextItem.expand(refs, setContext));
return contexts[p] = context;
}
else
{
if (set.length > 1)
return contexts[p] = [ContextItem("multireferenced")];
else
return contexts[p] = ContextItem.expand(refs, set[0]);
}
}
string getName(U)(U obj)
{
debug assert(coagulated);
auto pname = cast(T)obj in names;
// return pname?*pname:"##" ~ format(obj);
assert(pname, format("Unscanned object: ", obj));
return *pname;
}
version (Windows)
static string[string] filenameMappings;
static string[string] filenameMappings;
string getFilename(U)(U obj, string suffix)
{
debug assert(coagulated);
auto pname = cast(T)obj in filenames;
assert(pname, format("Unscanned object: ", obj));
auto filename = *pname;
version (Windows)
string[] dirSegments = split(filename, "/");
for (int l=0; l<dirSegments.length; l++)
{
string[] dirSegments = split(filename, "/");
for (int l=0; l<dirSegments.length; l++)
again:
string subpath = join(dirSegments[0..l+1], "/");
string subpathl = toLower(subpath);
string* canonicalp = subpathl in filenameMappings;
if (canonicalp && *canonicalp != subpath)
{
again:
string subpath = join(dirSegments[0..l+1], "/");
string subpathl = toLower(subpath);
string* canonicalp = subpathl in filenameMappings;
if (canonicalp && *canonicalp != subpath)
{
dirSegments[l] = dirSegments[l] ~ "_"; // not ~=
goto again;
}
filenameMappings[subpathl] = subpath;
dirSegments[l] = dirSegments[l] ~ "_"; // not ~=
goto again;
}
filename = join(dirSegments, "/");
filenameMappings[subpathl] = subpath;
}
filename = join(dirSegments, "/");
return filename ~ "." ~ suffix ~ ".asasm";
}
}
ContextSet!(uint, true) privateNamespaces;
ContextSet!(uint, true)[ASType.Max] namespaces;
ContextSet!(void*, false) objects, scripts;
this(ASProgram as)
@@ -340,21 +587,20 @@ final class RefBuilder : ASTraitsVisitor
addOrphan(method);
super.run();
foreach (i, v; as.scripts)
{
ContextItem[][] classContexts;
ContextItem[] classContexts;
foreach (trait; v.traits)
{
auto c = ContextItem.expand(this, [ContextItem(trait.name)], true);
if (c)
classContexts ~= c;
}
if (classContexts.length)
context = reduce!contextRoot(classContexts);
else
context = null;
if (context.length==0)
pushContext("script_" ~ to!string(i));
if (trait.name.vQName.ns.kind != ASType.PrivateNamespace)
classContexts ~= ContextItem(trait.name);
if (!classContexts.length)
foreach (trait; v.traits)
classContexts ~= ContextItem(trait.name);
context = [ContextItem(classContexts, "script_" ~ to!string(i))];
scripts.add(v, context);
pushContext("sinit", true);
addMethod(v.sinit);
@@ -375,23 +621,26 @@ final class RefBuilder : ASTraitsVisitor
popContext();
}
// add private namespaces referenced only at script level
scripts.coagulate(this);
// add namespaces referenced only at script level
foreach (v; as.scripts)
foreach (trait; v.traits)
if (trait.name.kind == ASType.QName && trait.name.vQName.ns.kind == ASType.PrivateNamespace && !privateNamespaces.isAdded(trait.name.vQName.ns.privateIndex))
privateNamespaces.add(trait.name.vQName.ns.privateIndex, scripts.getContext(v));
if (trait.name.kind == ASType.QName)
namespaces[trait.name.vQName.ns.kind].addIfNew(trait.name.vQName.ns.id, scripts.getContext(this, v));
foreach (privateIndex, b; possibleOrphanPrivateNamespaces)
if (!privateNamespaces.isAdded(privateIndex))
foreach (id, b; possibleOrphanPrivateNamespaces)
if (!namespaces[ASType.PrivateNamespace].isAdded(id))
{
pushContext("private_namespace_" ~ to!string(privateIndex));
privateNamespaces.add(privateIndex, context);
pushContext("orphan_namespace_" ~ to!string(id));
namespaces[ASType.PrivateNamespace].add(id, context);
popContext();
}
privateNamespaces.coagulate(this);
debug homonymsBuilt = true;
foreach (ref namespace; namespaces)
namespace.coagulate(this);
objects.coagulate(this);
scripts.coagulate(this);
}
override void visitTrait(ref ASProgram.Trait trait)
@@ -435,6 +684,10 @@ final class RefBuilder : ASTraitsVisitor
{
static bool uninteresting(ContextItem[] c)
{
// Do some validation while we're at it
foreach (cc; c)
assert(cc.type != ContextItem.Type.Group, "Groups should be expanded by now");
return
(c.length==1 && c[0].type==ContextItem.Type.String && c[0].str.startsWith("script_") && c[0].str.endsWith("_sinit")) ||
(c.length==1 && c[0].type==ContextItem.Type.String && c[0].str.startsWith("orphan_method_")) ||
@@ -444,32 +697,15 @@ final class RefBuilder : ASTraitsVisitor
if (uninteresting(c1)) return c2;
if (uninteresting(c2)) return c1;
static bool nsSimilar(ASProgram.Namespace ns1, ASProgram.Namespace ns2)
ContextItem[] c;
while (c.length<c1.length && c.length<c2.length)
{
if (ns1.kind==ASType.PrivateNamespace || ns2.kind==ASType.PrivateNamespace)
return ns1.kind==ns2.kind && ns1.privateIndex==ns2.privateIndex;
// ignore ns kind in other cases
return ns1.name == ns2.name;
}
static bool similar(ref ContextItem i1, ref ContextItem i2)
{
if (i1.type != i2.type) return false;
if (i1.type == ContextItem.Type.String)
return i1.str == i2.str;
if (i1.multiname.vQName.name != i2.multiname.vQName.name) return false;
return nsSimilar(i1.multiname.vQName.ns, i2.multiname.vQName.ns);
}
int i=0;
while (i<c1.length && i<c2.length && similar(c1[i], c2[i])) i++;
auto c = c1[0..i];
if (i<c1.length && i<c2.length && c1[i].type==ContextItem.Type.Multiname && c2[i].type==ContextItem.Type.Multiname && nsSimilar(c1[i].multiname.vQName.ns, c2[i].multiname.vQName.ns) && c1[i].multiname.vQName.ns.name.length)
{
auto m = new ASProgram.Multiname;
m.kind = ASType.QName;
m.vQName.ns = c1[i].multiname.vQName.ns;
c ~= ContextItem(m);
auto root = ContextItem.commonRoot(c1[c.length], c2[c.length]);
assert(root.length <= 1);
if (root.length)
c ~= root;
else
break;
}
return c;
}
@@ -479,27 +715,30 @@ final class RefBuilder : ASTraitsVisitor
void visitNamespace(ASProgram.Namespace ns)
{
if (ns is null) return;
if (ns.kind == ASType.PrivateNamespace)
{
assert(context.length > 0, "No context");
//assert(ns.name is null, "Named private namespace");
auto myPos = context.length;
foreach (i, ref item; context)
if (item.type == ContextItem.Type.Multiname && item.multiname.vQName.ns == ns)
{
myPos = i;
break;
}
if (myPos == 0)
// Add to homonyms
addHomonym(ns);
// Add to namespaces (for context)
assert(context.length > 0, "No context");
auto myPos = context.length;
foreach (i, ref item; context)
if (item.type == ContextItem.Type.Multiname && item.multiname.vQName.ns == ns)
{
possibleOrphanPrivateNamespaces[ns.privateIndex] = true;
return;
myPos = i;
break;
}
auto myContext = context[0..myPos].dup;
privateNamespaces.add(ns.privateIndex, myContext);
if (ns.kind == ASType.PrivateNamespace && myPos == 0)
{
possibleOrphanPrivateNamespaces[ns.id] = true;
return;
}
auto myContext = context[0..myPos].dup;
namespaces[ns.kind].add(ns.id, myContext);
}
void visitNamespaceSet(ASProgram.Namespace[] nsSet)
@@ -566,16 +805,30 @@ final class RefBuilder : ASTraitsVisitor
}
string contextToString(ContextItem[] context, bool filename)
in
{
context = ContextItem.expand(this, context, false);
debug(CONTEXTS) std.stdio.writefln("Stringizing:\n\t%s\n", context);
}
out(result)
{
debug(CONTEXTS) std.stdio.writefln("Stringized:\n\t%s\nto\n\t%s\n------------------\n", context, result);
}
body
{
context = ContextItem.expand(this, context);
if (!context.length)
return null;
foreach_reverse (i, c; context)
if (i>0 && c==context[i-1])
context = context[0..i] ~ context[i+1..$];
foreach_reverse (i; 0..context.length-1)
{
auto root = ContextItem.deduplicate(context[i], context[i+1]);
if (root.length)
context = context[0..i] ~ root ~ context[i+2..$];
}
ContextItem.Segment[] segments;
foreach (ci; context)
segments ~= ci.toSegments(filename);
segments ~= ci.toSegments(this, filename);
string escape(string s)
{
@@ -584,14 +837,36 @@ final class RefBuilder : ASTraitsVisitor
string result;
foreach (c; s)
if (c == '.')
if (c == '.' || c == ':')
result ~= '/';
else
if (c == ':' || c == '\\' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|' || c < 0x20 || c >= 0x7F || c == ' ' || c == '%')
if (c == '\\' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|' || c < 0x20 || c >= 0x7F || c == ' ' || c == '%')
result ~= format("%%%02X", c);
else
result ~= c;
return result;
auto pathSegments = result.split("/");
if (!pathSegments.length)
pathSegments = [""];
foreach (ref pathSegment; pathSegments)
{
if (pathSegment == "")
pathSegment = "%";
static const reservedNames = ["CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"];
auto pathSegmentU = pathSegment.toUpper();
foreach (reservedName; reservedNames)
if (pathSegmentU.startsWith(reservedName) && (pathSegmentU.length == reservedName.length || pathSegmentU[reservedName.length]=='.'))
{
pathSegment = "%" ~ pathSegment;
break;
}
if (pathSegment.length > 240)
pathSegment = pathSegment[0..200] ~ '-' ~ getDigestString(pathSegment);
}
return arrayJoin(pathSegments, "/");
}
string[] strings = new string[segments.length];
@@ -601,7 +876,7 @@ final class RefBuilder : ASTraitsVisitor
return arrayJoin(strings);
}
void addObject(T)(T obj) { objects.add(obj, context); }
bool addObject(T)(T obj) { return objects.add(obj, context); }
void addClass(ASProgram.Class vclass)
{
@@ -625,9 +900,14 @@ final class RefBuilder : ASTraitsVisitor
void addMethod(ASProgram.Method method)
{
addObject(method);
if (method.vbody)
visitMethodBody(method.vbody);
if (addObject(method))
{
foreach (paramType; method.paramTypes)
visitMultiname(paramType);
visitMultiname(method.returnType);
if (method.vbody)
visitMethodBody(method.vbody);
}
}
}
@@ -674,8 +954,7 @@ final class Disassembler
StringBuilder sb = new StringBuilder(dir ~ "/" ~ name ~ ".main.asasm");
sb ~= "#include ";
dumpString(sb, name ~ ".privatens.asasm");
sb ~= "#version 3";
sb.newLine();
sb ~= "program";
@@ -691,8 +970,9 @@ final class Disassembler
foreach (uint i, script; as.scripts)
{
auto ii = i; // http://d.puremagic.com/issues/show_bug.cgi?id=8526
newInclude(sb, refs.scripts.getFilename(script, "script"), (StringBuilder sb) {
dumpScript(sb, script, i);
dumpScript(sb, script, ii);
});
}
sb.newLine();
@@ -729,39 +1009,6 @@ final class Disassembler
sb ~= "end ; program"; sb.newLine();
sb.save();
// now dump the private namespace indices
sb = new StringBuilder(dir ~ "/" ~ name ~ ".privatens.asasm");
uint[] indices = refs.privateNamespaces.names.keys;
bool alphaSortDelegate(uint a, uint b) { return refs.privateNamespaces.names[a] < refs.privateNamespaces.names[b]; }
sort!alphaSortDelegate(indices);
foreach (index; indices)
{
void dumpContext(RefBuilder.ContextItem[] context)
{
sb ~= "; ";
foreach (i, c; context)
{
if (c.type == RefBuilder.ContextItem.Type.Multiname)
dumpMultiname(sb, c.multiname);
else
sb ~= c.str;
if (i < context.length-1)
sb ~= " -> ";
}
sb.newLine();
}
auto context = refs.privateNamespaces.contexts[index];
dumpContext(context);
/*auto contextEx = refs.ContextItem.expand(refs, context);
if (context != contextEx)
dumpContext(contextEx);*/
sb ~= format("#privatens %4d ", index);
dumpString(sb, refs.privateNamespaces.names[index]);
sb.newLine();
}
sb.save();
}
void dumpInt(StringBuilder sb, long v)
@@ -840,15 +1087,16 @@ final class Disassembler
if (namespace is null)
sb ~= "null";
else
with (namespace)
{
sb ~= ASTypeNames[kind];
sb ~= ASTypeNames[namespace.kind];
sb ~= '(';
dumpString(sb, name);
if (kind == ASType.PrivateNamespace)
dumpString(sb, namespace.name);
if (refs.hasHomonyms(namespace))
{
sb ~= ", ";
dumpString(sb, refs.privateNamespaces.getName(privateIndex));
auto label = refs.namespaces[namespace.kind].getName(namespace.id);
// label ~= format(" (%d)", namespace.id);
dumpString(sb, label);
}
sb ~= ')';
}
@@ -1017,12 +1265,13 @@ final class Disassembler
sb ~= "metadata ";
dumpString(sb, metadata.name);
sb.indent++; sb.newLine();
foreach (ref item; metadata.items)
assert(metadata.keys.length == metadata.values.length);
foreach (i; 0..metadata.keys.length)
{
sb ~= "item ";
dumpString(sb, item.key);
dumpString(sb, metadata.keys[i]);
sb ~= " ";
dumpString(sb, item.value);
dumpString(sb, metadata.values[i]);
sb.newLine();
}
sb.indent--; sb ~= "end ; metadata"; sb.newLine();
@@ -1088,6 +1337,7 @@ final class Disassembler
void dumpMethod(StringBuilder sb, ASProgram.Method method, string label)
{
sb ~= label;
// sb ~= format(" ; %d", method.id);
sb.indent++; sb.newLine();
if (method.name !is null)
{
@@ -1389,6 +1639,14 @@ final class Disassembler
private:
bool nsSimilar(ASProgram.Namespace ns1, ASProgram.Namespace ns2)
{
if (ns1.kind==ASType.PrivateNamespace || ns2.kind==ASType.PrivateNamespace)
return ns1.kind==ns2.kind && ns1.id==ns2.id;
// ignore ns kind in other cases
return ns1.name == ns2.name;
}
bool[256] newLineAfter;
static this()
+2 -2
View File
@@ -29,9 +29,9 @@ version (Windows)
else
{ pragma(lib, "lzma"); }
align(1)
struct LZMAHeader
align(1) struct LZMAHeader
{
align(1):
ubyte compressionParameters;
uint dictionarySize;
long decompressedSize = -1;
+2
View File
@@ -39,6 +39,7 @@ final class SWFFile
align(1) struct Header
{
align(1):
char[3] signature;
ubyte ver;
uint fileLength;
@@ -47,6 +48,7 @@ final class SWFFile
align(1) struct LZMAHeader
{
align(1):
uint compressedLength;
ubyte compressionParameters;
uint dictionarySize;