swf-toolkit 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +125 -2
  3. package/README.tr.md +126 -0
  4. package/dist/abc/abc-file.d.ts +168 -0
  5. package/dist/abc/abc-file.js +656 -0
  6. package/dist/abc/analysis.d.ts +36 -0
  7. package/dist/abc/analysis.js +213 -0
  8. package/dist/abc/code.d.ts +59 -0
  9. package/dist/abc/code.js +379 -0
  10. package/dist/abc/constants.d.ts +80 -0
  11. package/dist/abc/constants.js +93 -0
  12. package/dist/abc/interner.d.ts +46 -0
  13. package/dist/abc/interner.js +168 -0
  14. package/dist/abc/model.d.ts +40 -0
  15. package/dist/abc/model.js +146 -0
  16. package/dist/abc/opcodes.d.ts +39 -0
  17. package/dist/abc/opcodes.js +196 -0
  18. package/dist/abc/pcode/assembler.d.ts +42 -0
  19. package/dist/abc/pcode/assembler.js +635 -0
  20. package/dist/abc/pcode/disassembler.d.ts +17 -0
  21. package/dist/abc/pcode/disassembler.js +147 -0
  22. package/dist/abc/pcode/format.d.ts +23 -0
  23. package/dist/abc/pcode/format.js +131 -0
  24. package/dist/abc/pcode/lexer.d.ts +34 -0
  25. package/dist/abc/pcode/lexer.js +183 -0
  26. package/dist/assets/assets.d.ts +26 -0
  27. package/dist/assets/assets.js +151 -0
  28. package/dist/assets/images.d.ts +31 -0
  29. package/dist/assets/images.js +238 -0
  30. package/dist/assets/png.d.ts +12 -0
  31. package/dist/assets/png.js +196 -0
  32. package/dist/assets/sounds.d.ts +37 -0
  33. package/dist/assets/sounds.js +236 -0
  34. package/dist/avm1/avm1.d.ts +55 -0
  35. package/dist/avm1/avm1.js +642 -0
  36. package/dist/avm1/project.d.ts +16 -0
  37. package/dist/avm1/project.js +81 -0
  38. package/dist/cjs/abc/abc-file.d.ts +168 -0
  39. package/dist/cjs/abc/abc-file.js +663 -0
  40. package/dist/cjs/abc/analysis.d.ts +36 -0
  41. package/dist/cjs/abc/analysis.js +221 -0
  42. package/dist/cjs/abc/code.d.ts +59 -0
  43. package/dist/cjs/abc/code.js +387 -0
  44. package/dist/cjs/abc/constants.d.ts +80 -0
  45. package/dist/cjs/abc/constants.js +97 -0
  46. package/dist/cjs/abc/interner.d.ts +46 -0
  47. package/dist/cjs/abc/interner.js +172 -0
  48. package/dist/cjs/abc/model.d.ts +40 -0
  49. package/dist/cjs/abc/model.js +155 -0
  50. package/dist/cjs/abc/opcodes.d.ts +39 -0
  51. package/dist/cjs/abc/opcodes.js +201 -0
  52. package/dist/cjs/abc/pcode/assembler.d.ts +42 -0
  53. package/dist/cjs/abc/pcode/assembler.js +640 -0
  54. package/dist/cjs/abc/pcode/disassembler.d.ts +17 -0
  55. package/dist/cjs/abc/pcode/disassembler.js +152 -0
  56. package/dist/cjs/abc/pcode/format.d.ts +23 -0
  57. package/dist/cjs/abc/pcode/format.js +141 -0
  58. package/dist/cjs/abc/pcode/lexer.d.ts +34 -0
  59. package/dist/cjs/abc/pcode/lexer.js +188 -0
  60. package/dist/cjs/assets/assets.d.ts +26 -0
  61. package/dist/cjs/assets/assets.js +159 -0
  62. package/dist/cjs/assets/images.d.ts +31 -0
  63. package/dist/cjs/assets/images.js +246 -0
  64. package/dist/cjs/assets/png.d.ts +12 -0
  65. package/dist/cjs/assets/png.js +202 -0
  66. package/dist/cjs/assets/sounds.d.ts +37 -0
  67. package/dist/cjs/assets/sounds.js +246 -0
  68. package/dist/cjs/avm1/avm1.d.ts +55 -0
  69. package/dist/cjs/avm1/avm1.js +649 -0
  70. package/dist/cjs/avm1/project.d.ts +16 -0
  71. package/dist/cjs/avm1/project.js +88 -0
  72. package/dist/cjs/compiler/ast.d.ts +285 -0
  73. package/dist/cjs/compiler/ast.js +3 -0
  74. package/dist/cjs/compiler/class-compiler.d.ts +34 -0
  75. package/dist/cjs/compiler/class-compiler.js +440 -0
  76. package/dist/cjs/compiler/codegen.d.ts +144 -0
  77. package/dist/cjs/compiler/codegen.js +1589 -0
  78. package/dist/cjs/compiler/lexer.d.ts +27 -0
  79. package/dist/cjs/compiler/lexer.js +254 -0
  80. package/dist/cjs/compiler/parser.d.ts +58 -0
  81. package/dist/cjs/compiler/parser.js +989 -0
  82. package/dist/cjs/compiler/program.d.ts +29 -0
  83. package/dist/cjs/compiler/program.js +36 -0
  84. package/dist/cjs/compiler/scope.d.ts +22 -0
  85. package/dist/cjs/compiler/scope.js +242 -0
  86. package/dist/cjs/compression/lzma.d.ts +37 -0
  87. package/dist/cjs/compression/lzma.js +575 -0
  88. package/dist/cjs/compression/swf-container.d.ts +19 -0
  89. package/dist/cjs/compression/swf-container.js +92 -0
  90. package/dist/cjs/decompiler/ast.d.ts +228 -0
  91. package/dist/cjs/decompiler/ast.js +455 -0
  92. package/dist/cjs/decompiler/cfg.d.ts +40 -0
  93. package/dist/cjs/decompiler/cfg.js +236 -0
  94. package/dist/cjs/decompiler/class-printer.d.ts +9 -0
  95. package/dist/cjs/decompiler/class-printer.js +173 -0
  96. package/dist/cjs/decompiler/method.d.ts +41 -0
  97. package/dist/cjs/decompiler/method.js +436 -0
  98. package/dist/cjs/decompiler/simulate.d.ts +55 -0
  99. package/dist/cjs/decompiler/simulate.js +591 -0
  100. package/dist/cjs/decompiler/structure.d.ts +48 -0
  101. package/dist/cjs/decompiler/structure.js +764 -0
  102. package/dist/cjs/errors.d.ts +11 -0
  103. package/dist/cjs/errors.js +25 -0
  104. package/dist/cjs/index.d.ts +41 -0
  105. package/dist/cjs/index.js +82 -0
  106. package/dist/cjs/io/reader.d.ts +48 -0
  107. package/dist/cjs/io/reader.js +180 -0
  108. package/dist/cjs/io/writer.d.ts +37 -0
  109. package/dist/cjs/io/writer.js +175 -0
  110. package/dist/cjs/package.json +1 -0
  111. package/dist/cjs/project/pcode-project.d.ts +32 -0
  112. package/dist/cjs/project/pcode-project.js +210 -0
  113. package/dist/cjs/project/source-export.d.ts +12 -0
  114. package/dist/cjs/project/source-export.js +51 -0
  115. package/dist/cjs/project/source-import.d.ts +13 -0
  116. package/dist/cjs/project/source-import.js +73 -0
  117. package/dist/cjs/swf/swf.d.ts +63 -0
  118. package/dist/cjs/swf/swf.js +147 -0
  119. package/dist/cjs/swf/tag-codes.d.ts +77 -0
  120. package/dist/cjs/swf/tag-codes.js +109 -0
  121. package/dist/cjs/swf/tags.d.ts +118 -0
  122. package/dist/cjs/swf/tags.js +330 -0
  123. package/dist/cli.d.ts +2 -0
  124. package/dist/cli.js +296 -0
  125. package/dist/compiler/ast.d.ts +285 -0
  126. package/dist/compiler/ast.js +2 -0
  127. package/dist/compiler/class-compiler.d.ts +34 -0
  128. package/dist/compiler/class-compiler.js +437 -0
  129. package/dist/compiler/codegen.d.ts +144 -0
  130. package/dist/compiler/codegen.js +1585 -0
  131. package/dist/compiler/lexer.d.ts +27 -0
  132. package/dist/compiler/lexer.js +249 -0
  133. package/dist/compiler/parser.d.ts +58 -0
  134. package/dist/compiler/parser.js +984 -0
  135. package/dist/compiler/program.d.ts +29 -0
  136. package/dist/compiler/program.js +32 -0
  137. package/dist/compiler/scope.d.ts +22 -0
  138. package/dist/compiler/scope.js +238 -0
  139. package/dist/compression/lzma.d.ts +37 -0
  140. package/dist/compression/lzma.js +569 -0
  141. package/dist/compression/swf-container.d.ts +19 -0
  142. package/dist/compression/swf-container.js +87 -0
  143. package/dist/decompiler/ast.d.ts +228 -0
  144. package/dist/decompiler/ast.js +441 -0
  145. package/dist/decompiler/cfg.d.ts +40 -0
  146. package/dist/decompiler/cfg.js +231 -0
  147. package/dist/decompiler/class-printer.d.ts +9 -0
  148. package/dist/decompiler/class-printer.js +170 -0
  149. package/dist/decompiler/method.d.ts +41 -0
  150. package/dist/decompiler/method.js +427 -0
  151. package/dist/decompiler/simulate.d.ts +55 -0
  152. package/dist/decompiler/simulate.js +586 -0
  153. package/dist/decompiler/structure.d.ts +48 -0
  154. package/dist/decompiler/structure.js +760 -0
  155. package/dist/errors.d.ts +11 -0
  156. package/dist/errors.js +20 -0
  157. package/dist/index.d.ts +41 -0
  158. package/dist/index.js +49 -0
  159. package/dist/io/reader.d.ts +48 -0
  160. package/dist/io/reader.js +176 -0
  161. package/dist/io/writer.d.ts +37 -0
  162. package/dist/io/writer.js +169 -0
  163. package/dist/project/pcode-project.d.ts +32 -0
  164. package/dist/project/pcode-project.js +203 -0
  165. package/dist/project/source-export.d.ts +12 -0
  166. package/dist/project/source-export.js +47 -0
  167. package/dist/project/source-import.d.ts +13 -0
  168. package/dist/project/source-import.js +70 -0
  169. package/dist/swf/swf.d.ts +63 -0
  170. package/dist/swf/swf.js +143 -0
  171. package/dist/swf/tag-codes.d.ts +77 -0
  172. package/dist/swf/tag-codes.js +105 -0
  173. package/dist/swf/tags.d.ts +118 -0
  174. package/dist/swf/tags.js +316 -0
  175. package/package.json +71 -4
@@ -0,0 +1,11 @@
1
+ /** Thrown when SWF / ABC binary data is malformed or truncated. */
2
+ export declare class SwfFormatError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ /** Thrown by the P-code assembler, with source position information. */
6
+ export declare class AssemblerError extends Error {
7
+ readonly line: number;
8
+ readonly column: number;
9
+ readonly source?: string;
10
+ constructor(message: string, line: number, column: number, source?: string);
11
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AssemblerError = exports.SwfFormatError = void 0;
4
+ /** Thrown when SWF / ABC binary data is malformed or truncated. */
5
+ class SwfFormatError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = 'SwfFormatError';
9
+ }
10
+ }
11
+ exports.SwfFormatError = SwfFormatError;
12
+ /** Thrown by the P-code assembler, with source position information. */
13
+ class AssemblerError extends Error {
14
+ line;
15
+ column;
16
+ source;
17
+ constructor(message, line, column, source) {
18
+ super(`${source ? source + ':' : ''}${line}:${column}: ${message}`);
19
+ this.name = 'AssemblerError';
20
+ this.line = line;
21
+ this.column = column;
22
+ this.source = source;
23
+ }
24
+ }
25
+ exports.AssemblerError = AssemblerError;
@@ -0,0 +1,41 @@
1
+ export * from './errors.js';
2
+ export { ByteReader } from './io/reader.js';
3
+ export { ByteWriter } from './io/writer.js';
4
+ export * from './compression/lzma.js';
5
+ export * from './compression/swf-container.js';
6
+ export * from './swf/tag-codes.js';
7
+ export * from './swf/tags.js';
8
+ export * from './swf/swf.js';
9
+ export * from './abc/constants.js';
10
+ export * from './abc/abc-file.js';
11
+ export * from './abc/opcodes.js';
12
+ export * from './abc/code.js';
13
+ export * from './abc/analysis.js';
14
+ export * from './abc/interner.js';
15
+ export * from './abc/model.js';
16
+ export * from './abc/pcode/format.js';
17
+ export * from './abc/pcode/disassembler.js';
18
+ export * from './abc/pcode/assembler.js';
19
+ export { tokenizeLine } from './abc/pcode/lexer.js';
20
+ export * from './project/pcode-project.js';
21
+ export * from './decompiler/ast.js';
22
+ export { buildCfg, findLoops, dominates, type Cfg, type Block, type Loop } from './decompiler/cfg.js';
23
+ export { simulate, SimulationError, type MethodContext, type Terminator } from './decompiler/simulate.js';
24
+ export { Structurer } from './decompiler/structure.js';
25
+ export * from './decompiler/method.js';
26
+ export * from './decompiler/class-printer.js';
27
+ export * from './project/source-export.js';
28
+ export { CompileError, tokenize as tokenizeAs3 } from './compiler/lexer.js';
29
+ export type * as As3Ast from './compiler/ast.js';
30
+ export { parseAs3, Parser as As3Parser } from './compiler/parser.js';
31
+ export { FunctionCompiler, TOPLEVEL_NAMES, type ClassScope, type FunctionOptions } from './compiler/codegen.js';
32
+ export { buildClassScope } from './compiler/scope.js';
33
+ export * from './compiler/class-compiler.js';
34
+ export * from './compiler/program.js';
35
+ export * from './project/source-import.js';
36
+ export * from './assets/png.js';
37
+ export * from './assets/images.js';
38
+ export * from './assets/sounds.js';
39
+ export * from './assets/assets.js';
40
+ export * from './avm1/avm1.js';
41
+ export * from './avm1/project.js';
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.buildClassScope = exports.TOPLEVEL_NAMES = exports.FunctionCompiler = exports.As3Parser = exports.parseAs3 = exports.tokenizeAs3 = exports.CompileError = exports.Structurer = exports.SimulationError = exports.simulate = exports.dominates = exports.findLoops = exports.buildCfg = exports.tokenizeLine = exports.ByteWriter = exports.ByteReader = void 0;
18
+ __exportStar(require("./errors.js"), exports);
19
+ var reader_js_1 = require("./io/reader.js");
20
+ Object.defineProperty(exports, "ByteReader", { enumerable: true, get: function () { return reader_js_1.ByteReader; } });
21
+ var writer_js_1 = require("./io/writer.js");
22
+ Object.defineProperty(exports, "ByteWriter", { enumerable: true, get: function () { return writer_js_1.ByteWriter; } });
23
+ // Compression
24
+ __exportStar(require("./compression/lzma.js"), exports);
25
+ __exportStar(require("./compression/swf-container.js"), exports);
26
+ // SWF container and tags
27
+ __exportStar(require("./swf/tag-codes.js"), exports);
28
+ __exportStar(require("./swf/tags.js"), exports);
29
+ __exportStar(require("./swf/swf.js"), exports);
30
+ // AVM2 / ABC
31
+ __exportStar(require("./abc/constants.js"), exports);
32
+ __exportStar(require("./abc/abc-file.js"), exports);
33
+ __exportStar(require("./abc/opcodes.js"), exports);
34
+ __exportStar(require("./abc/code.js"), exports);
35
+ __exportStar(require("./abc/analysis.js"), exports);
36
+ __exportStar(require("./abc/interner.js"), exports);
37
+ __exportStar(require("./abc/model.js"), exports);
38
+ // P-code
39
+ __exportStar(require("./abc/pcode/format.js"), exports);
40
+ __exportStar(require("./abc/pcode/disassembler.js"), exports);
41
+ __exportStar(require("./abc/pcode/assembler.js"), exports);
42
+ var lexer_js_1 = require("./abc/pcode/lexer.js");
43
+ Object.defineProperty(exports, "tokenizeLine", { enumerable: true, get: function () { return lexer_js_1.tokenizeLine; } });
44
+ // Project-level workflows
45
+ __exportStar(require("./project/pcode-project.js"), exports);
46
+ // Decompiler
47
+ __exportStar(require("./decompiler/ast.js"), exports);
48
+ var cfg_js_1 = require("./decompiler/cfg.js");
49
+ Object.defineProperty(exports, "buildCfg", { enumerable: true, get: function () { return cfg_js_1.buildCfg; } });
50
+ Object.defineProperty(exports, "findLoops", { enumerable: true, get: function () { return cfg_js_1.findLoops; } });
51
+ Object.defineProperty(exports, "dominates", { enumerable: true, get: function () { return cfg_js_1.dominates; } });
52
+ var simulate_js_1 = require("./decompiler/simulate.js");
53
+ Object.defineProperty(exports, "simulate", { enumerable: true, get: function () { return simulate_js_1.simulate; } });
54
+ Object.defineProperty(exports, "SimulationError", { enumerable: true, get: function () { return simulate_js_1.SimulationError; } });
55
+ var structure_js_1 = require("./decompiler/structure.js");
56
+ Object.defineProperty(exports, "Structurer", { enumerable: true, get: function () { return structure_js_1.Structurer; } });
57
+ __exportStar(require("./decompiler/method.js"), exports);
58
+ __exportStar(require("./decompiler/class-printer.js"), exports);
59
+ __exportStar(require("./project/source-export.js"), exports);
60
+ // Compiler
61
+ var lexer_js_2 = require("./compiler/lexer.js");
62
+ Object.defineProperty(exports, "CompileError", { enumerable: true, get: function () { return lexer_js_2.CompileError; } });
63
+ Object.defineProperty(exports, "tokenizeAs3", { enumerable: true, get: function () { return lexer_js_2.tokenize; } });
64
+ var parser_js_1 = require("./compiler/parser.js");
65
+ Object.defineProperty(exports, "parseAs3", { enumerable: true, get: function () { return parser_js_1.parseAs3; } });
66
+ Object.defineProperty(exports, "As3Parser", { enumerable: true, get: function () { return parser_js_1.Parser; } });
67
+ var codegen_js_1 = require("./compiler/codegen.js");
68
+ Object.defineProperty(exports, "FunctionCompiler", { enumerable: true, get: function () { return codegen_js_1.FunctionCompiler; } });
69
+ Object.defineProperty(exports, "TOPLEVEL_NAMES", { enumerable: true, get: function () { return codegen_js_1.TOPLEVEL_NAMES; } });
70
+ var scope_js_1 = require("./compiler/scope.js");
71
+ Object.defineProperty(exports, "buildClassScope", { enumerable: true, get: function () { return scope_js_1.buildClassScope; } });
72
+ __exportStar(require("./compiler/class-compiler.js"), exports);
73
+ __exportStar(require("./compiler/program.js"), exports);
74
+ __exportStar(require("./project/source-import.js"), exports);
75
+ // Assets
76
+ __exportStar(require("./assets/png.js"), exports);
77
+ __exportStar(require("./assets/images.js"), exports);
78
+ __exportStar(require("./assets/sounds.js"), exports);
79
+ __exportStar(require("./assets/assets.js"), exports);
80
+ // AVM1 (AS1/AS2)
81
+ __exportStar(require("./avm1/avm1.js"), exports);
82
+ __exportStar(require("./avm1/project.js"), exports);
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Little-endian binary reader with SWF bit-field support.
3
+ *
4
+ * Bit reads (`ub`, `sb`, `fb`) share an internal bit buffer; any byte-aligned
5
+ * read implicitly re-aligns to the next byte boundary, as required by the SWF
6
+ * specification.
7
+ */
8
+ export declare class ByteReader {
9
+ readonly bytes: Uint8Array;
10
+ readonly view: DataView;
11
+ pos: number;
12
+ readonly end: number;
13
+ private bitBuf;
14
+ private bitCount;
15
+ constructor(bytes: Uint8Array, start?: number, end?: number);
16
+ get remaining(): number;
17
+ get eof(): boolean;
18
+ private need;
19
+ align(): void;
20
+ u8(): number;
21
+ s8(): number;
22
+ u16(): number;
23
+ s16(): number;
24
+ u24(): number;
25
+ s24(): number;
26
+ u32(): number;
27
+ s32(): number;
28
+ f32(): number;
29
+ f64(): number;
30
+ /** 8.8 fixed point. */
31
+ fixed8(): number;
32
+ /** 16.16 fixed point. */
33
+ fixed(): number;
34
+ bytesView(n: number): Uint8Array;
35
+ /** Copy of the next `n` bytes. */
36
+ bytesCopy(n: number): Uint8Array;
37
+ rest(): Uint8Array;
38
+ /** Null-terminated UTF-8 string. */
39
+ cstring(): string;
40
+ /** Unsigned bit field (MSB first). */
41
+ ub(n: number): number;
42
+ /** Signed bit field. */
43
+ sb(n: number): number;
44
+ /** Signed 16.16 fixed-point bit field. */
45
+ fb(n: number): number;
46
+ /** SWF EncodedU32 / AVM2 variable-length unsigned 32-bit integer. */
47
+ encodedU32(): number;
48
+ }
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ByteReader = void 0;
4
+ const errors_js_1 = require("../errors.js");
5
+ const utf8Decoder = new TextDecoder('utf-8');
6
+ /**
7
+ * Little-endian binary reader with SWF bit-field support.
8
+ *
9
+ * Bit reads (`ub`, `sb`, `fb`) share an internal bit buffer; any byte-aligned
10
+ * read implicitly re-aligns to the next byte boundary, as required by the SWF
11
+ * specification.
12
+ */
13
+ class ByteReader {
14
+ bytes;
15
+ view;
16
+ pos;
17
+ end;
18
+ bitBuf = 0;
19
+ bitCount = 0;
20
+ constructor(bytes, start = 0, end = bytes.length) {
21
+ this.bytes = bytes;
22
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
23
+ this.pos = start;
24
+ this.end = end;
25
+ }
26
+ get remaining() {
27
+ return this.end - this.pos;
28
+ }
29
+ get eof() {
30
+ return this.pos >= this.end;
31
+ }
32
+ need(n) {
33
+ if (this.pos + n > this.end) {
34
+ throw new errors_js_1.SwfFormatError(`Unexpected end of data at offset ${this.pos} (need ${n} byte(s), ${this.remaining} left)`);
35
+ }
36
+ }
37
+ align() {
38
+ this.bitCount = 0;
39
+ this.bitBuf = 0;
40
+ }
41
+ u8() {
42
+ this.align();
43
+ this.need(1);
44
+ return this.bytes[this.pos++];
45
+ }
46
+ s8() {
47
+ const v = this.u8();
48
+ return v >= 0x80 ? v - 0x100 : v;
49
+ }
50
+ u16() {
51
+ this.align();
52
+ this.need(2);
53
+ const v = this.view.getUint16(this.pos, true);
54
+ this.pos += 2;
55
+ return v;
56
+ }
57
+ s16() {
58
+ this.align();
59
+ this.need(2);
60
+ const v = this.view.getInt16(this.pos, true);
61
+ this.pos += 2;
62
+ return v;
63
+ }
64
+ u24() {
65
+ this.align();
66
+ this.need(3);
67
+ const b = this.bytes;
68
+ const v = b[this.pos] | (b[this.pos + 1] << 8) | (b[this.pos + 2] << 16);
69
+ this.pos += 3;
70
+ return v;
71
+ }
72
+ s24() {
73
+ const v = this.u24();
74
+ return v & 0x800000 ? v - 0x1000000 : v;
75
+ }
76
+ u32() {
77
+ this.align();
78
+ this.need(4);
79
+ const v = this.view.getUint32(this.pos, true);
80
+ this.pos += 4;
81
+ return v;
82
+ }
83
+ s32() {
84
+ this.align();
85
+ this.need(4);
86
+ const v = this.view.getInt32(this.pos, true);
87
+ this.pos += 4;
88
+ return v;
89
+ }
90
+ f32() {
91
+ this.align();
92
+ this.need(4);
93
+ const v = this.view.getFloat32(this.pos, true);
94
+ this.pos += 4;
95
+ return v;
96
+ }
97
+ f64() {
98
+ this.align();
99
+ this.need(8);
100
+ const v = this.view.getFloat64(this.pos, true);
101
+ this.pos += 8;
102
+ return v;
103
+ }
104
+ /** 8.8 fixed point. */
105
+ fixed8() {
106
+ return this.s16() / 256;
107
+ }
108
+ /** 16.16 fixed point. */
109
+ fixed() {
110
+ return this.s32() / 65536;
111
+ }
112
+ bytesView(n) {
113
+ this.align();
114
+ this.need(n);
115
+ const v = this.bytes.subarray(this.pos, this.pos + n);
116
+ this.pos += n;
117
+ return v;
118
+ }
119
+ /** Copy of the next `n` bytes. */
120
+ bytesCopy(n) {
121
+ return this.bytesView(n).slice();
122
+ }
123
+ rest() {
124
+ return this.bytesView(this.remaining);
125
+ }
126
+ /** Null-terminated UTF-8 string. */
127
+ cstring() {
128
+ this.align();
129
+ const start = this.pos;
130
+ while (this.pos < this.end && this.bytes[this.pos] !== 0)
131
+ this.pos++;
132
+ if (this.pos >= this.end)
133
+ throw new errors_js_1.SwfFormatError(`Unterminated string at offset ${start}`);
134
+ const s = utf8Decoder.decode(this.bytes.subarray(start, this.pos));
135
+ this.pos++;
136
+ return s;
137
+ }
138
+ /** Unsigned bit field (MSB first). */
139
+ ub(n) {
140
+ let result = 0;
141
+ for (let i = 0; i < n; i++) {
142
+ if (this.bitCount === 0) {
143
+ this.need(1);
144
+ this.bitBuf = this.bytes[this.pos++];
145
+ this.bitCount = 8;
146
+ }
147
+ this.bitCount--;
148
+ result = result * 2 + ((this.bitBuf >> this.bitCount) & 1);
149
+ }
150
+ return result;
151
+ }
152
+ /** Signed bit field. */
153
+ sb(n) {
154
+ if (n === 0)
155
+ return 0;
156
+ const v = this.ub(n);
157
+ const sign = 2 ** (n - 1);
158
+ return v >= sign ? v - 2 * sign : v;
159
+ }
160
+ /** Signed 16.16 fixed-point bit field. */
161
+ fb(n) {
162
+ return this.sb(n) / 65536;
163
+ }
164
+ /** SWF EncodedU32 / AVM2 variable-length unsigned 32-bit integer. */
165
+ encodedU32() {
166
+ this.align();
167
+ let result = 0;
168
+ let shift = 0;
169
+ for (let i = 0; i < 5; i++) {
170
+ this.need(1);
171
+ const b = this.bytes[this.pos++];
172
+ result += (b & 0x7f) * 2 ** shift;
173
+ if ((b & 0x80) === 0)
174
+ break;
175
+ shift += 7;
176
+ }
177
+ return result >>> 0;
178
+ }
179
+ }
180
+ exports.ByteReader = ByteReader;
@@ -0,0 +1,37 @@
1
+ /** Growable little-endian binary writer with SWF bit-field support. */
2
+ export declare class ByteWriter {
3
+ private buf;
4
+ private view;
5
+ length: number;
6
+ private bitBuf;
7
+ private bitCount;
8
+ constructor(initialCapacity?: number);
9
+ private ensure;
10
+ /** Flush pending bits (pads the current byte with zeros). */
11
+ align(): void;
12
+ u8(v: number): this;
13
+ s8(v: number): this;
14
+ u16(v: number): this;
15
+ s16(v: number): this;
16
+ u24(v: number): this;
17
+ s24(v: number): this;
18
+ u32(v: number): this;
19
+ s32(v: number): this;
20
+ f32(v: number): this;
21
+ f64(v: number): this;
22
+ fixed8(v: number): this;
23
+ fixed(v: number): this;
24
+ bytes(data: Uint8Array): this;
25
+ cstring(s: string): this;
26
+ ub(n: number, v: number): this;
27
+ sb(n: number, v: number): this;
28
+ fb(n: number, v: number): this;
29
+ encodedU32(v: number): this;
30
+ /** Overwrite a little-endian u32 at an absolute offset. */
31
+ patchU32(offset: number, v: number): void;
32
+ toBytes(): Uint8Array;
33
+ }
34
+ /** Number of bits needed to store `v` as an unsigned bit field. */
35
+ export declare function unsignedBits(v: number): number;
36
+ /** Number of bits needed to store `v` as a signed bit field. */
37
+ export declare function signedBits(v: number): number;
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ByteWriter = void 0;
4
+ exports.unsignedBits = unsignedBits;
5
+ exports.signedBits = signedBits;
6
+ const utf8Encoder = new TextEncoder();
7
+ /** Growable little-endian binary writer with SWF bit-field support. */
8
+ class ByteWriter {
9
+ buf;
10
+ view;
11
+ length = 0;
12
+ bitBuf = 0;
13
+ bitCount = 0;
14
+ constructor(initialCapacity = 1024) {
15
+ this.buf = new Uint8Array(Math.max(16, initialCapacity));
16
+ this.view = new DataView(this.buf.buffer);
17
+ }
18
+ ensure(n) {
19
+ const required = this.length + n;
20
+ if (required <= this.buf.length)
21
+ return;
22
+ let cap = this.buf.length * 2;
23
+ while (cap < required)
24
+ cap *= 2;
25
+ const next = new Uint8Array(cap);
26
+ next.set(this.buf.subarray(0, this.length));
27
+ this.buf = next;
28
+ this.view = new DataView(next.buffer);
29
+ }
30
+ /** Flush pending bits (pads the current byte with zeros). */
31
+ align() {
32
+ if (this.bitCount > 0) {
33
+ this.ensure(1);
34
+ this.buf[this.length++] = (this.bitBuf << (8 - this.bitCount)) & 0xff;
35
+ this.bitBuf = 0;
36
+ this.bitCount = 0;
37
+ }
38
+ }
39
+ u8(v) {
40
+ this.align();
41
+ this.ensure(1);
42
+ this.buf[this.length++] = v & 0xff;
43
+ return this;
44
+ }
45
+ s8(v) {
46
+ return this.u8(v < 0 ? v + 0x100 : v);
47
+ }
48
+ u16(v) {
49
+ this.align();
50
+ this.ensure(2);
51
+ this.view.setUint16(this.length, v & 0xffff, true);
52
+ this.length += 2;
53
+ return this;
54
+ }
55
+ s16(v) {
56
+ this.align();
57
+ this.ensure(2);
58
+ this.view.setInt16(this.length, v, true);
59
+ this.length += 2;
60
+ return this;
61
+ }
62
+ u24(v) {
63
+ this.align();
64
+ this.ensure(3);
65
+ const x = v & 0xffffff;
66
+ this.buf[this.length++] = x & 0xff;
67
+ this.buf[this.length++] = (x >> 8) & 0xff;
68
+ this.buf[this.length++] = (x >> 16) & 0xff;
69
+ return this;
70
+ }
71
+ s24(v) {
72
+ return this.u24(v < 0 ? v + 0x1000000 : v);
73
+ }
74
+ u32(v) {
75
+ this.align();
76
+ this.ensure(4);
77
+ this.view.setUint32(this.length, v >>> 0, true);
78
+ this.length += 4;
79
+ return this;
80
+ }
81
+ s32(v) {
82
+ this.align();
83
+ this.ensure(4);
84
+ this.view.setInt32(this.length, v | 0, true);
85
+ this.length += 4;
86
+ return this;
87
+ }
88
+ f32(v) {
89
+ this.align();
90
+ this.ensure(4);
91
+ this.view.setFloat32(this.length, v, true);
92
+ this.length += 4;
93
+ return this;
94
+ }
95
+ f64(v) {
96
+ this.align();
97
+ this.ensure(8);
98
+ this.view.setFloat64(this.length, v, true);
99
+ this.length += 8;
100
+ return this;
101
+ }
102
+ fixed8(v) {
103
+ return this.s16(Math.round(v * 256));
104
+ }
105
+ fixed(v) {
106
+ return this.s32(Math.round(v * 65536));
107
+ }
108
+ bytes(data) {
109
+ this.align();
110
+ this.ensure(data.length);
111
+ this.buf.set(data, this.length);
112
+ this.length += data.length;
113
+ return this;
114
+ }
115
+ cstring(s) {
116
+ this.bytes(utf8Encoder.encode(s));
117
+ return this.u8(0);
118
+ }
119
+ ub(n, v) {
120
+ for (let i = n - 1; i >= 0; i--) {
121
+ const bit = Math.floor(v / 2 ** i) & 1;
122
+ this.bitBuf = (this.bitBuf << 1) | bit;
123
+ this.bitCount++;
124
+ if (this.bitCount === 8) {
125
+ this.ensure(1);
126
+ this.buf[this.length++] = this.bitBuf & 0xff;
127
+ this.bitBuf = 0;
128
+ this.bitCount = 0;
129
+ }
130
+ }
131
+ return this;
132
+ }
133
+ sb(n, v) {
134
+ return this.ub(n, v < 0 ? v + 2 ** n : v);
135
+ }
136
+ fb(n, v) {
137
+ return this.sb(n, Math.round(v * 65536));
138
+ }
139
+ encodedU32(v) {
140
+ this.align();
141
+ let x = v >>> 0;
142
+ do {
143
+ let b = x & 0x7f;
144
+ x = x >>> 7;
145
+ if (x !== 0)
146
+ b |= 0x80;
147
+ this.u8(b);
148
+ } while (x !== 0);
149
+ return this;
150
+ }
151
+ /** Overwrite a little-endian u32 at an absolute offset. */
152
+ patchU32(offset, v) {
153
+ this.view.setUint32(offset, v >>> 0, true);
154
+ }
155
+ toBytes() {
156
+ this.align();
157
+ return this.buf.slice(0, this.length);
158
+ }
159
+ }
160
+ exports.ByteWriter = ByteWriter;
161
+ /** Number of bits needed to store `v` as an unsigned bit field. */
162
+ function unsignedBits(v) {
163
+ let n = 0;
164
+ while (v > 0) {
165
+ n++;
166
+ v = Math.floor(v / 2);
167
+ }
168
+ return n;
169
+ }
170
+ /** Number of bits needed to store `v` as a signed bit field. */
171
+ function signedBits(v) {
172
+ if (v === 0)
173
+ return 0;
174
+ return unsignedBits(v < 0 ? -v - 1 : v) + 1;
175
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,32 @@
1
+ import type { Swf } from '../swf/swf.js';
2
+ export declare const MANIFEST_NAME = "swf-toolkit.json";
3
+ /** Make a class / package name safe as a path segment on all platforms. */
4
+ export declare function safeSegment(s: string): string;
5
+ export interface ExportPcodeResult {
6
+ outDir: string;
7
+ files: string[];
8
+ methodCount: number;
9
+ }
10
+ /** Writes one `.pcode` file per class (plus a manifest) into `outDir`. */
11
+ export declare function exportPcode(swf: Swf, outDir: string): Promise<ExportPcodeResult>;
12
+ /** Splits a P-code document into `method ... end` blocks with their method index. */
13
+ export declare function splitMethodBlocks(text: string): Array<{
14
+ text: string;
15
+ startLine: number;
16
+ methodIndex: number | 'new';
17
+ }>;
18
+ export interface ImportPcodeResult {
19
+ /** Methods that were re-assembled. */
20
+ changed: Array<{
21
+ file: string;
22
+ methodIndex: number;
23
+ }>;
24
+ /** Number of method blocks that were identical to the current SWF. */
25
+ unchanged: number;
26
+ }
27
+ export interface ImportPcodeOptions {
28
+ /** Only import these files (paths relative to the project dir). */
29
+ files?: string[];
30
+ }
31
+ /** Applies edited `.pcode` files from `dir` (created by {@link exportPcode}) to `swf`. */
32
+ export declare function importPcode(swf: Swf, dir: string, options?: ImportPcodeOptions): Promise<ImportPcodeResult>;