mirror of
https://github.com/billsonnn/nitro-converter.git
synced 2024-11-26 09:20:51 +01:00
Skip bad buffers
This commit is contained in:
parent
e681a316a1
commit
5603c4a010
@ -5,265 +5,282 @@
|
|||||||
* MIT LICENCE
|
* MIT LICENCE
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
import { fs } from 'fs';
|
||||||
|
import { lzma } from 'lzma-purejs';
|
||||||
|
import { Stream } from 'stream';
|
||||||
|
import { zlib } from 'zlib';
|
||||||
|
import { SWFBuffer } from './lib/swf-buffer';
|
||||||
|
import { SWFTags } from './lib/swf-tags';
|
||||||
|
const SWFReader = exports;
|
||||||
|
|
||||||
var fs = require('fs')
|
function readSWFTags(buff, swf)
|
||||||
, zlib = require('zlib')
|
{
|
||||||
, lzma = require('lzma-purejs')
|
var tags = []
|
||||||
, Stream = require('stream')
|
, tag
|
||||||
, SWFBuffer = require('./lib/swf-buffer')
|
, tagHeader
|
||||||
, SWFTags = require('./lib/swf-tags')
|
, flag
|
||||||
, SWFReader = exports;
|
, l
|
||||||
|
, sc
|
||||||
|
, fc;
|
||||||
|
|
||||||
function readSWFTags(buff, swf) {
|
/* Reads TagCodeAndLength from Tag's RECORDHEADER */
|
||||||
var tags = []
|
while( (tagHeader = buff.readTagCodeAndLength()) )
|
||||||
, tag
|
{
|
||||||
, tagHeader
|
tag = {
|
||||||
, flag
|
header : tagHeader
|
||||||
, l
|
};
|
||||||
, sc
|
switch( tagHeader.code )
|
||||||
, fc;
|
{
|
||||||
|
case SWFTags.FileAttributes: {
|
||||||
|
const flag = buff.readUIntLE(32);
|
||||||
|
const fileAttrs = {};
|
||||||
|
|
||||||
/* Reads TagCodeAndLength from Tag's RECORDHEADER */
|
fileAttrs.useNetwork = tag.useNetwork = !!(flag & 0x1);
|
||||||
while( (tagHeader = buff.readTagCodeAndLength()) ) {
|
fileAttrs.as3 = tag.as3 = !!(flag & 0x8);
|
||||||
tag = {
|
fileAttrs.hasMetaData = tag.hasMetaData = !!(flag & 0x10);
|
||||||
header : tagHeader
|
fileAttrs.useGPU = tag.useGPU = !!(flag & 0x20);
|
||||||
};
|
fileAttrs.useDirectBit = tag.useDirectBlit = !!(flag & 0x40);
|
||||||
switch( tagHeader.code ) {
|
|
||||||
case SWFTags.FileAttributes :
|
|
||||||
flag = buff.readUIntLE(32);
|
|
||||||
fileAttrs = {}
|
|
||||||
|
|
||||||
fileAttrs.useNetwork = tag.useNetwork = !!(flag & 0x1);
|
swf.fileAttributes = fileAttrs;
|
||||||
fileAttrs.as3 = tag.as3 = !!(flag & 0x8);
|
break;
|
||||||
fileAttrs.hasMetaData = tag.hasMetaData = !!(flag & 0x10);
|
}
|
||||||
fileAttrs.useGPU = tag.useGPU = !!(flag & 0x20);
|
case SWFTags.Metadata: {
|
||||||
fileAttrs.useDirectBit = tag.useDirectBlit = !!(flag & 0x40);
|
swf.metadata = tag.metadata = buff.readString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SWFTags.SetBackgroundColor: {
|
||||||
|
tag.RGB = buff.readRGB();
|
||||||
|
swf.backgroundColor = '#' + (tag.RGB[0]*65536 + tag.RGB[1]*256 + tag.RGB[0]).toString(16);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SWFTags.Protect :
|
||||||
|
swf.protect = tagHeader.length && buff.readString();
|
||||||
|
break;
|
||||||
|
case SWFTags.DefineSceneAndFrameLabelData :
|
||||||
|
sc = tag.sceneCount = buff.readEncodedU32();
|
||||||
|
tag.scenes = [];
|
||||||
|
|
||||||
swf.fileAttributes = fileAttrs;
|
while(sc--)
|
||||||
break;
|
tag.scenes.push({
|
||||||
case SWFTags.Metadata :
|
offset : buff.readEncodedU32(),
|
||||||
swf.metadata = tag.metadata = buff.readString()
|
name : buff.readString()
|
||||||
break;
|
});
|
||||||
case SWFTags.SetBackgroundColor :
|
|
||||||
tag.RGB = buff.readRGB();
|
|
||||||
swf.backgroundColor = '#' + (tag.RGB[0]*65536 + tag.RGB[1]*256 + tag.RGB[0]).toString(16);
|
|
||||||
break;
|
|
||||||
case SWFTags.Protect :
|
|
||||||
swf.protect = tagHeader.length && buff.readString();
|
|
||||||
break;
|
|
||||||
case SWFTags.DefineSceneAndFrameLabelData :
|
|
||||||
sc = tag.sceneCount = buff.readEncodedU32();
|
|
||||||
tag.scenes = [];
|
|
||||||
|
|
||||||
while (sc--)
|
fc = tag.frameLabelCount = buff.readEncodedU32();
|
||||||
tag.scenes.push({
|
tag.labels = [];
|
||||||
offset : buff.readEncodedU32(),
|
|
||||||
name : buff.readString()
|
|
||||||
});
|
|
||||||
|
|
||||||
fc = tag.frameLabelCount = buff.readEncodedU32();
|
while(fc--)
|
||||||
tag.labels = [];
|
tag.labels.push({
|
||||||
|
frameNum : buff.readEncodedU32(),
|
||||||
while (fc--)
|
frameLabel : buff.readString()
|
||||||
tag.labels.push({
|
});
|
||||||
frameNum : buff.readEncodedU32(),
|
break;
|
||||||
frameLabel : buff.readString()
|
/**
|
||||||
});
|
|
||||||
break;
|
|
||||||
/**
|
|
||||||
* DefineShape4 extends the capabilities of
|
* DefineShape4 extends the capabilities of
|
||||||
* DefineShape3 by using a new line style
|
* DefineShape3 by using a new line style
|
||||||
* record in the shape
|
* record in the shape
|
||||||
*/
|
*/
|
||||||
//case SWFTags.DefineShape4 :
|
//case SWFTags.DefineShape4 :
|
||||||
// /* id for this character */
|
// /* id for this character */
|
||||||
// tag.ShapeId = buff.readUIntLE(16);
|
// tag.ShapeId = buff.readUIntLE(16);
|
||||||
// /* bounds of the shape */
|
// /* bounds of the shape */
|
||||||
// tag.ShapeBounds = buff.readRect();
|
// tag.ShapeBounds = buff.readRect();
|
||||||
// /* bounds of the shape, excluding the strokes */
|
// /* bounds of the shape, excluding the strokes */
|
||||||
// tag.EdgeBounds = buff.readRect();
|
// tag.EdgeBounds = buff.readRect();
|
||||||
// /* reserved, must be 0 */
|
// /* reserved, must be 0 */
|
||||||
// if (0 !== buff.readBits(5))
|
// if (0 !== buff.readBits(5))
|
||||||
// throw new Error('Reserved bit used.');
|
// throw new Error('Reserved bit used.');
|
||||||
// /* if 1, use fill winding. >= SWF 10 */
|
// /* if 1, use fill winding. >= SWF 10 */
|
||||||
// if (swf.version >= 10)
|
// if (swf.version >= 10)
|
||||||
// tag.UsesFillWindingRule = buff.readBits(1);
|
// tag.UsesFillWindingRule = buff.readBits(1);
|
||||||
// /**
|
// /**
|
||||||
// * if 1, shape contains at least one
|
// * if 1, shape contains at least one
|
||||||
// * non-scaling stroke.
|
// * non-scaling stroke.
|
||||||
// */
|
// */
|
||||||
// tag.UsesNonScallingStrokes = buff.readBits(1);
|
// tag.UsesNonScallingStrokes = buff.readBits(1);
|
||||||
// /**
|
// /**
|
||||||
// * if 1, shape contains at least one
|
// * if 1, shape contains at least one
|
||||||
// * scaling stroke
|
// * scaling stroke
|
||||||
// */
|
// */
|
||||||
// tag.UsesScalingStrokes = buff.readBits(1);
|
// tag.UsesScalingStrokes = buff.readBits(1);
|
||||||
// tag.shapes = buff.readShapeWithStyle();
|
// tag.shapes = buff.readShapeWithStyle();
|
||||||
// break;
|
// break;
|
||||||
case SWFTags.FrameLabel :
|
case SWFTags.FrameLabel :
|
||||||
tag.name = buff.readString()
|
tag.name = buff.readString();
|
||||||
l = Buffer.byteLength(tag.name);
|
l = Buffer.byteLength(tag.name);
|
||||||
/* check if it's an named anchor */
|
/* check if it's an named anchor */
|
||||||
if (l & (tagHeader.length - 1) != l)
|
if(l & (tagHeader.length - 1) != l)
|
||||||
tag.anchor = buff.readUInt8();
|
tag.anchor = buff.readUInt8();
|
||||||
break;
|
break;
|
||||||
case SWFTags.DefineSprite :
|
case SWFTags.DefineSprite :
|
||||||
tag.SpriteID = buff.readUIntLE(16);
|
tag.SpriteID = buff.readUIntLE(16);
|
||||||
tag.FrameCount = buff.readUIntLE(16);
|
tag.FrameCount = buff.readUIntLE(16);
|
||||||
tag.ControlTags = readSWFTags(buff, swf);
|
tag.ControlTags = readSWFTags(buff, swf);
|
||||||
break;
|
break;
|
||||||
case SWFTags.ExportAssets :
|
case SWFTags.ExportAssets :
|
||||||
tag.count = buff.readUIntLE(16);
|
tag.count = buff.readUIntLE(16);
|
||||||
tag.assets = [];
|
tag.assets = [];
|
||||||
|
|
||||||
l = 0;
|
l = 0;
|
||||||
|
|
||||||
while (l++ < tag.count)
|
while(l++ < tag.count)
|
||||||
tag.assets.push({
|
tag.assets.push({
|
||||||
id : buff.readUIntLE(16),
|
id : buff.readUIntLE(16),
|
||||||
name : buff.readString()
|
name : buff.readString()
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case SWFTags.ImportAssets :
|
case SWFTags.ImportAssets :
|
||||||
/**
|
/**
|
||||||
* URL where the source SWF file can be found
|
* URL where the source SWF file can be found
|
||||||
*/
|
*/
|
||||||
tag.url = buff.readString();
|
tag.url = buff.readString();
|
||||||
/**
|
/**
|
||||||
* Number of assets to import
|
* Number of assets to import
|
||||||
*/
|
*/
|
||||||
tag.count = buff.readUIntLE(16);
|
tag.count = buff.readUIntLE(16);
|
||||||
tag.assets = [];
|
tag.assets = [];
|
||||||
|
|
||||||
l = 0;
|
l = 0;
|
||||||
|
|
||||||
while (l++ < tag.count)
|
while(l++ < tag.count)
|
||||||
tag.assets.push({
|
tag.assets.push({
|
||||||
/**
|
/**
|
||||||
* Character ID for the l-th item
|
* Character ID for the l-th item
|
||||||
* in importing SWF file
|
* in importing SWF file
|
||||||
*/
|
*/
|
||||||
id : buff.readUIntLE(16),
|
id : buff.readUIntLE(16),
|
||||||
/**
|
/**
|
||||||
* Identifies for the l-th
|
* Identifies for the l-th
|
||||||
* imported character
|
* imported character
|
||||||
*/
|
*/
|
||||||
name : buff.readString()
|
name : buff.readString()
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case SWFTags.ImportAssets2 :
|
case SWFTags.ImportAssets2 :
|
||||||
tag.url = buff.readString();
|
tag.url = buff.readString();
|
||||||
|
|
||||||
if ( !(1 === buff.readUInt8() && 0 === buff.readUInt8()) ) {
|
if( !(1 === buff.readUInt8() && 0 === buff.readUInt8()) )
|
||||||
throw new Error('Reserved bits for ImportAssets2 used');
|
{
|
||||||
}
|
throw new Error('Reserved bits for ImportAssets2 used');
|
||||||
|
}
|
||||||
|
|
||||||
tag.count = buff.readUIntLE(16);
|
tag.count = buff.readUIntLE(16);
|
||||||
tag.assets = [];
|
tag.assets = [];
|
||||||
|
|
||||||
l = 0;
|
l = 0;
|
||||||
|
|
||||||
while (l++ < tag.count)
|
while(l++ < tag.count)
|
||||||
tag.assets({
|
tag.assets({
|
||||||
id : buff.readUIntLE(16),
|
id : buff.readUIntLE(16),
|
||||||
name : buff.readString()
|
name : buff.readString()
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case SWFTags.EnableDebbuger :
|
case SWFTags.EnableDebbuger :
|
||||||
tag.password = buff.readString()
|
tag.password = buff.readString();
|
||||||
break;
|
break;
|
||||||
case SWFTags.EnableDebugger2 :
|
case SWFTags.EnableDebugger2 :
|
||||||
if (0 !== buff.readUIntLE(16)) {
|
if(0 !== buff.readUIntLE(16))
|
||||||
//throw new Error('Reserved bit for EnableDebugger2 used.');
|
{
|
||||||
}
|
//throw new Error('Reserved bit for EnableDebugger2 used.');
|
||||||
tag.password = buff.readString()
|
}
|
||||||
break;
|
tag.password = buff.readString();
|
||||||
case SWFTags.ScriptLimits :
|
break;
|
||||||
/**
|
case SWFTags.ScriptLimits :
|
||||||
|
/**
|
||||||
* Maximum recursion Depth
|
* Maximum recursion Depth
|
||||||
*/
|
*/
|
||||||
tag.maxRecursionDepth = buff.readUIntLE(16);
|
tag.maxRecursionDepth = buff.readUIntLE(16);
|
||||||
/**
|
/**
|
||||||
* Maximum ActionScript processing time before script
|
* Maximum ActionScript processing time before script
|
||||||
* stuck dialog box displays
|
* stuck dialog box displays
|
||||||
*/
|
*/
|
||||||
tag.scriptTimeoutSeconds = buff.readUIntLE(16);
|
tag.scriptTimeoutSeconds = buff.readUIntLE(16);
|
||||||
break;
|
break;
|
||||||
case SWFTags.SymbolClass :
|
case SWFTags.SymbolClass: {
|
||||||
tag.numSymbols = buff.readUIntLE(16);
|
tag.numSymbols = buff.readUIntLE(16);
|
||||||
tag.symbols = [];
|
tag.symbols = [];
|
||||||
|
|
||||||
l = 0;
|
l = 0;
|
||||||
|
|
||||||
while (l++ < tag.numSymbols)
|
while(l++ < tag.numSymbols)
|
||||||
tag.symbols.push({
|
tag.symbols.push({
|
||||||
id : buff.readUIntLE(16),
|
id : buff.readUIntLE(16),
|
||||||
name : buff.readString()
|
name : buff.readString()
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case SWFTags.DefineScalingGrid :
|
}
|
||||||
tag.characterId = buff.readUIntLE(16);
|
case SWFTags.DefineScalingGrid: {
|
||||||
tag.splitter = buff.readRect();
|
tag.characterId = buff.readUIntLE(16);
|
||||||
break;
|
tag.splitter = buff.readRect();
|
||||||
case SWFTags.setTabIndex :
|
break;
|
||||||
tag.depth = buff.readUIntLE(16);
|
}
|
||||||
tag.tabIndex = buff.readUIntLE(16);
|
case SWFTags.setTabIndex: {
|
||||||
break;
|
tag.depth = buff.readUIntLE(16);
|
||||||
case SWFTags.JPEGTables:
|
tag.tabIndex = buff.readUIntLE(16);
|
||||||
tag.jpegData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length);
|
break;
|
||||||
buff.pointer += tagHeader.length;
|
}
|
||||||
break;
|
case SWFTags.JPEGTables: {
|
||||||
case SWFTags.DefineBits:
|
tag.jpegData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length);
|
||||||
tag.characterId = buff.readUIntLE(16);
|
buff.pointer += tagHeader.length;
|
||||||
tag.jpegData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length - 2);
|
break;
|
||||||
buff.pointer += tagHeader.length - 2;
|
}
|
||||||
break;
|
case SWFTags.DefineBits: {
|
||||||
case SWFTags.DefineBitsJPEG2:
|
tag.characterId = buff.readUIntLE(16);
|
||||||
tag.characterId = buff.readUIntLE(16);
|
tag.jpegData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length - 2);
|
||||||
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length - 2);
|
buff.pointer += tagHeader.length - 2;
|
||||||
buff.pointer += tagHeader.length - 2;
|
break;
|
||||||
break;
|
}
|
||||||
case SWFTags.DefineBitsJPEG3:
|
case SWFTags.DefineBitsJPEG2: {
|
||||||
tag.characterId = buff.readUIntLE(16);
|
tag.characterId = buff.readUIntLE(16);
|
||||||
var alphaDataOffset = buff.readUIntLE(32);
|
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length - 2);
|
||||||
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
|
buff.pointer += tagHeader.length - 2;
|
||||||
buff.pointer += alphaDataOffset;
|
break;
|
||||||
var restLength = tagHeader.length - 6 - alphaDataOffset;
|
}
|
||||||
tag.bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
case SWFTags.DefineBitsJPEG3: {
|
||||||
buff.pointer += restLength;
|
tag.characterId = buff.readUIntLE(16);
|
||||||
break;
|
var alphaDataOffset = buff.readUIntLE(32);
|
||||||
case SWFTags.DefineBitsJPEG4:
|
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
|
||||||
tag.characterId = buff.readUIntLE(16);
|
buff.pointer += alphaDataOffset;
|
||||||
var alphaDataOffset = buff.readUIntLE(32);
|
var restLength = tagHeader.length - 6 - alphaDataOffset;
|
||||||
tag.deblockParam = buff.readUIntLE(16);
|
tag.bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
||||||
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
|
buff.pointer += restLength;
|
||||||
buff.pointer += alphaDataOffset;
|
break;
|
||||||
var restLength = tagHeader.length - 8 - alphaDataOffset;
|
}
|
||||||
tag.bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
case SWFTags.DefineBitsJPEG4: {
|
||||||
buff.pointer += restLength;
|
tag.characterId = buff.readUIntLE(16);
|
||||||
break;
|
const alphaDataOffset = buff.readUIntLE(32);
|
||||||
case SWFTags.DefineBitsLossless:
|
tag.deblockParam = buff.readUIntLE(16);
|
||||||
case SWFTags.DefineBitsLossless2:
|
tag.imageData = buff.buffer.slice(buff.pointer, buff.pointer + alphaDataOffset);
|
||||||
tag.characterId = buff.readUIntLE(16);
|
buff.pointer += alphaDataOffset;
|
||||||
tag.bitmapFormat = buff.readUInt8();
|
const restLength = tagHeader.length - 8 - alphaDataOffset;
|
||||||
tag.bitmapWidth = buff.readUIntLE(16);
|
tag.bitmapAlphaData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
||||||
tag.bitmapHeight = buff.readUIntLE(16);
|
buff.pointer += restLength;
|
||||||
var restLength = tagHeader.length - 7;
|
break;
|
||||||
if (tag.bitmapFormat == 3) {
|
}
|
||||||
tag.bitmapColorTableSize = buff.readUInt8();
|
case SWFTags.DefineBitsLossless:
|
||||||
restLength--;
|
case SWFTags.DefineBitsLossless2: {
|
||||||
|
tag.characterId = buff.readUIntLE(16);
|
||||||
|
tag.bitmapFormat = buff.readUInt8();
|
||||||
|
tag.bitmapWidth = buff.readUIntLE(16);
|
||||||
|
tag.bitmapHeight = buff.readUIntLE(16);
|
||||||
|
let restLength = tagHeader.length - 7;
|
||||||
|
if(tag.bitmapFormat == 3)
|
||||||
|
{
|
||||||
|
tag.bitmapColorTableSize = buff.readUInt8();
|
||||||
|
restLength--;
|
||||||
|
}
|
||||||
|
tag.zlibBitmapData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
||||||
|
buff.pointer += restLength;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
tag.data = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length);
|
||||||
|
buff.pointer += tagHeader.length;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
tag.zlibBitmapData = buff.buffer.slice(buff.pointer, buff.pointer + restLength);
|
tags.push(tag);
|
||||||
buff.pointer += restLength;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
tag.data = buff.buffer.slice(buff.pointer, buff.pointer + tagHeader.length);
|
|
||||||
buff.pointer += tagHeader.length;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
tags.push(tag);
|
return tags;
|
||||||
}
|
|
||||||
return tags;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -275,33 +292,38 @@ function readSWFTags(buff, swf) {
|
|||||||
* @api private
|
* @api private
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function readSWFBuff(buff, compressed_buff, next) {
|
function readSWFBuff(buff, compressed_buff, next)
|
||||||
buff.seek(3);// start
|
{
|
||||||
|
buff.seek(3);// start
|
||||||
|
|
||||||
if (buff.length < 9) {
|
if(buff.length < 9)
|
||||||
if (isSync) throw new Error("Buffer is to small, must be greater than 9 bytes.");
|
{
|
||||||
return next(new Error("Buffer is to small, must be greater than 9 bytes."));
|
if(isSync) throw new Error('Buffer is to small, must be greater than 9 bytes.');
|
||||||
|
return next(new Error('Buffer is to small, must be greater than 9 bytes.'));
|
||||||
}
|
}
|
||||||
var swf = {
|
var swf = {
|
||||||
version : buff.readUInt8(),
|
version : buff.readUInt8(),
|
||||||
fileLength : {
|
fileLength : {
|
||||||
compressed : compressed_buff.length,
|
compressed : compressed_buff.length,
|
||||||
uncompressed : buff.readUIntLE(32)
|
uncompressed : buff.readUIntLE(32)
|
||||||
},
|
},
|
||||||
frameSize : buff.readRect(), // Returns a RECT object. i.e : { x : 0, y : 0, width : 200, height: 300 }
|
frameSize : buff.readRect(), // Returns a RECT object. i.e : { x : 0, y : 0, width : 200, height: 300 }
|
||||||
frameRate : buff.readUIntLE(16)/256,
|
frameRate : buff.readUIntLE(16)/256,
|
||||||
frameCount : buff.readUIntLE(16)
|
frameCount : buff.readUIntLE(16)
|
||||||
}
|
}
|
||||||
, isSync = 'function' !== typeof next;
|
, isSync = 'function' !== typeof next;
|
||||||
|
|
||||||
try {
|
try
|
||||||
swf.tags = readSWFTags(buff, swf);
|
{
|
||||||
} catch(e) {
|
swf.tags = readSWFTags(buff, swf);
|
||||||
if (isSync) throw e;
|
}
|
||||||
return next(e);
|
catch (e)
|
||||||
}
|
{
|
||||||
|
if(isSync) throw e;
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
|
||||||
return isSync && swf || next( null, swf );
|
return isSync && swf || next( null, swf );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -310,8 +332,9 @@ function readSWFBuff(buff, compressed_buff, next) {
|
|||||||
* @param {Buffer|ArrayBuffer} buff
|
* @param {Buffer|ArrayBuffer} buff
|
||||||
* @param {Buffer|ArrayBuffer} swf
|
* @param {Buffer|ArrayBuffer} swf
|
||||||
*/
|
*/
|
||||||
function concatSWFHeader(buff, swf) {
|
function concatSWFHeader(buff, swf)
|
||||||
return Buffer.concat([swf.slice(0, 8), buff]);
|
{
|
||||||
|
return Buffer.concat([swf.slice(0, 8), buff]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -321,73 +344,97 @@ function concatSWFHeader(buff, swf) {
|
|||||||
* @param {function} callback
|
* @param {function} callback
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function uncompress(swf, next) {
|
function uncompress(swf, next)
|
||||||
var compressed_buff = swf.slice(8)
|
{
|
||||||
, uncompressed_buff
|
var compressed_buff = swf.slice(8)
|
||||||
, isSync = 'function' !== typeof next
|
, uncompressed_buff
|
||||||
, e;
|
, isSync = 'function' !== typeof next
|
||||||
|
, e;
|
||||||
|
|
||||||
// uncompress buffer
|
// uncompress buffer
|
||||||
switch( swf[0] ) {
|
switch(swf[0])
|
||||||
case 0x43 : // zlib compressed
|
{
|
||||||
if (isSync) {
|
case 0x43 : // zlib compressed
|
||||||
uncompressed_buff = concatSWFHeader(zlib.unzipSync(compressed_buff), swf);
|
if(isSync)
|
||||||
return readSWFBuff(new SWFBuffer(uncompressed_buff), swf);
|
{
|
||||||
}
|
uncompressed_buff = concatSWFHeader(zlib.unzipSync(compressed_buff), swf);
|
||||||
|
|
||||||
zlib.inflate(compressed_buff, function(err, buf) {
|
if(!Buffer.isBuffer(uncompressed_buff))
|
||||||
readSWFBuff(new SWFBuffer(buf), swf, next);
|
{
|
||||||
});
|
console.log('invalid_buffer');
|
||||||
break;
|
|
||||||
case 0x46 : // uncompressed
|
|
||||||
return readSWFBuff(new SWFBuffer( swf ), swf, next);
|
|
||||||
break;
|
|
||||||
case 0x5a : // LZMA compressed
|
|
||||||
var lzmaProperties = compressed_buff.slice(4, 9);
|
|
||||||
compressed_buff = compressed_buff.slice(9);
|
|
||||||
|
|
||||||
var input_stream = new Stream();
|
return null;
|
||||||
input_stream.pos = 0;
|
}
|
||||||
input_stream.readByte = function() {
|
return readSWFBuff(new SWFBuffer(uncompressed_buff), swf);
|
||||||
return this.pos >= compressed_buff.length ? -1 : compressed_buff[this.pos++];
|
}
|
||||||
};
|
|
||||||
|
|
||||||
var output_stream = new Stream();
|
if(!Buffer.isBuffer(compressed_buff))
|
||||||
output_stream.buffer = new Buffer(16384);
|
{
|
||||||
output_stream.pos = 0;
|
console.log('invalid_buffer');
|
||||||
output_stream.writeByte = function(_byte) {
|
|
||||||
if (this.pos >= this.buffer.length) {
|
|
||||||
var newBuffer = new Buffer(this.buffer.length * 2);
|
|
||||||
this.buffer.copy(newBuffer);
|
|
||||||
this.buffer = newBuffer;
|
|
||||||
}
|
|
||||||
this.buffer[this.pos++] = _byte;
|
|
||||||
};
|
|
||||||
output_stream.getBuffer = function() {
|
|
||||||
// trim buffer
|
|
||||||
if (this.pos !== this.buffer.length) {
|
|
||||||
var newBuffer = new Buffer(this.pos);
|
|
||||||
this.buffer.copy(newBuffer, 0, 0, this.pos);
|
|
||||||
this.buffer = newBuffer;
|
|
||||||
}
|
|
||||||
return this.buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
lzma.decompress(lzmaProperties, input_stream, output_stream, -1);
|
return null;
|
||||||
uncompressed_buff = Buffer.concat([swf.slice(0, 8), output_stream.getBuffer()]);
|
}
|
||||||
|
|
||||||
return readSWFBuff(new SWFBuffer(uncompressed_buff), swf, next);
|
zlib.inflate(compressed_buff, function(err, buf)
|
||||||
break;
|
{
|
||||||
default :
|
readSWFBuff(new SWFBuffer(buf), swf, next);
|
||||||
e = new Error('Unknown SWF compressions');
|
});
|
||||||
|
break;
|
||||||
|
case 0x46 : // uncompressed
|
||||||
|
return readSWFBuff(new SWFBuffer( swf ), swf, next);
|
||||||
|
case 0x5a : // LZMA compressed
|
||||||
|
var lzmaProperties = compressed_buff.slice(4, 9);
|
||||||
|
compressed_buff = compressed_buff.slice(9);
|
||||||
|
|
||||||
if (isSync) {
|
var input_stream = new Stream();
|
||||||
throw e;
|
input_stream.pos = 0;
|
||||||
} else {
|
input_stream.readByte = function()
|
||||||
next(e);
|
{
|
||||||
}
|
return this.pos >= compressed_buff.length ? -1 : compressed_buff[this.pos++];
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
var output_stream = new Stream();
|
||||||
|
output_stream.buffer = new Buffer(16384);
|
||||||
|
output_stream.pos = 0;
|
||||||
|
output_stream.writeByte = function(_byte)
|
||||||
|
{
|
||||||
|
if(this.pos >= this.buffer.length)
|
||||||
|
{
|
||||||
|
var newBuffer = new Buffer(this.buffer.length * 2);
|
||||||
|
this.buffer.copy(newBuffer);
|
||||||
|
this.buffer = newBuffer;
|
||||||
|
}
|
||||||
|
this.buffer[this.pos++] = _byte;
|
||||||
|
};
|
||||||
|
output_stream.getBuffer = function()
|
||||||
|
{
|
||||||
|
// trim buffer
|
||||||
|
if(this.pos !== this.buffer.length)
|
||||||
|
{
|
||||||
|
var newBuffer = new Buffer(this.pos);
|
||||||
|
this.buffer.copy(newBuffer, 0, 0, this.pos);
|
||||||
|
this.buffer = newBuffer;
|
||||||
|
}
|
||||||
|
return this.buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
lzma.decompress(lzmaProperties, input_stream, output_stream, -1);
|
||||||
|
uncompressed_buff = Buffer.concat([swf.slice(0, 8), output_stream.getBuffer()]);
|
||||||
|
|
||||||
|
return readSWFBuff(new SWFBuffer(uncompressed_buff), swf, next);
|
||||||
|
default :
|
||||||
|
e = new Error('Unknown SWF compressions');
|
||||||
|
|
||||||
|
if(isSync)
|
||||||
|
{
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if file is Buffer or ArrayBuffer
|
* Check if file is Buffer or ArrayBuffer
|
||||||
@ -396,8 +443,9 @@ function uncompress(swf, next) {
|
|||||||
* @api private
|
* @api private
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function isBuffer(b) {
|
function isBuffer(b)
|
||||||
return typeof Buffer !== "undefined" && Buffer.isBuffer(b) || b instanceof ArrayBuffer;
|
{
|
||||||
|
return typeof Buffer !== 'undefined' && Buffer.isBuffer(b) || b instanceof ArrayBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Exposes Tags constants */
|
/* Exposes Tags constants */
|
||||||
@ -411,22 +459,31 @@ SWFReader.TAGS = SWFTags;
|
|||||||
* @api public
|
* @api public
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
SWFReader.read = SWFReader.readSync = function(file, next) {
|
SWFReader.read = SWFReader.readSync = function(file, next)
|
||||||
if (isBuffer(file)) {
|
{
|
||||||
|
if(isBuffer(file))
|
||||||
|
{
|
||||||
/* File is already a buffer */
|
/* File is already a buffer */
|
||||||
return uncompress(file, next);
|
return uncompress(file, next);
|
||||||
} else {
|
}
|
||||||
/* Get the buffer */
|
else
|
||||||
if ('function' === typeof next) {
|
{
|
||||||
fs.readFile(file, function(err, swf) {
|
/* Get the buffer */
|
||||||
if ( err ) {
|
if('function' === typeof next)
|
||||||
next(err);
|
{
|
||||||
return;
|
fs.readFile(file, function(err, swf)
|
||||||
}
|
{
|
||||||
uncompress(swf, next);
|
if( err )
|
||||||
});
|
{
|
||||||
} else {
|
next(err);
|
||||||
return uncompress(fs.readFileSync(file));
|
return;
|
||||||
|
}
|
||||||
|
uncompress(swf, next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return uncompress(fs.readFileSync(file));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
225
src/swf-reader/lib/SWFBuffer.ts
Normal file
225
src/swf-reader/lib/SWFBuffer.ts
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
export class SWFBuffer
|
||||||
|
{
|
||||||
|
public static RECORDHEADER_LENTH_FULL: number = 0x3f;
|
||||||
|
public static EOS: number = 0x00;
|
||||||
|
public static STYLE_COUNT_EXT: number = 0xFF;
|
||||||
|
|
||||||
|
public buffer: Buffer
|
||||||
|
public pointer: number = 0;
|
||||||
|
public position: number = 1;
|
||||||
|
public current: number = 0;
|
||||||
|
public length: number = 0;
|
||||||
|
|
||||||
|
constructor(buffer: Buffer)
|
||||||
|
{
|
||||||
|
if(!Buffer.isBuffer(buffer))
|
||||||
|
{
|
||||||
|
throw new Error('invalid_buffer');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.buffer = buffer;
|
||||||
|
this.length = buffer.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readUIntLE(bits: number): number
|
||||||
|
{
|
||||||
|
let value = 0;
|
||||||
|
|
||||||
|
switch(bits)
|
||||||
|
{
|
||||||
|
case 16:
|
||||||
|
value = this.buffer.readUInt16LE(this.pointer);
|
||||||
|
break;
|
||||||
|
case 32:
|
||||||
|
value = this.buffer.readUInt32LE(this.pointer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pointer += bits / 8;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readUInt8(): number
|
||||||
|
{
|
||||||
|
return this.buffer.readUInt8(this.pointer++);
|
||||||
|
}
|
||||||
|
|
||||||
|
public readEncodedU32(): number
|
||||||
|
{
|
||||||
|
let i = 5;
|
||||||
|
let result = 0;
|
||||||
|
let nb = 0;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
result += (nb = this.nextByte());
|
||||||
|
} while((nb & 128) && --i);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readRGB(): [ number, number, number ]
|
||||||
|
{
|
||||||
|
return [ this.readUInt8(), this.readUInt8(), this.readUInt8() ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public readRGBA(): [ number, number, number, number ]
|
||||||
|
{
|
||||||
|
return [ ...this.readRGB(), this.readUInt8() ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public readString(encoding: BufferEncoding): string
|
||||||
|
{
|
||||||
|
const init = this.pointer;
|
||||||
|
|
||||||
|
while(this.readUInt8() !== SWFBuffer.EOS);
|
||||||
|
|
||||||
|
return this.buffer.toString(encoding || 'utf8', init, this.pointer - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public readStyleArray(buffer: SWFBuffer, next)
|
||||||
|
{
|
||||||
|
let styleArrayCount = buffer.readUInt8();
|
||||||
|
const styles = [];
|
||||||
|
|
||||||
|
if(styleArrayCount === SWFBuffer.STYLE_COUNT_EXT) styleArrayCount = buffer.readUIntLE(16);
|
||||||
|
|
||||||
|
for(let i = 0; i < styleArrayCount; i++) styles.push(next(buffer));
|
||||||
|
|
||||||
|
return styles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readFillStyle(buffer: SWFBuffer): { fillStyleType: number, color?: [ number, number, number, number ], bitmapId?: number }
|
||||||
|
{
|
||||||
|
const type = buffer.readUInt8();
|
||||||
|
|
||||||
|
const fillStyle: { fillStyleType: number, color?: [ number, number, number, number ], bitmapId?: number } = {
|
||||||
|
fillStyleType: type
|
||||||
|
};
|
||||||
|
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case 0x00:
|
||||||
|
fillStyle.color = buffer.readRGBA();
|
||||||
|
break;
|
||||||
|
case 0x10:
|
||||||
|
case 0x12:
|
||||||
|
case 0x13:
|
||||||
|
console.log('Gradient');
|
||||||
|
break;
|
||||||
|
case 0x40:
|
||||||
|
case 0x41:
|
||||||
|
case 0x42:
|
||||||
|
case 0x43:
|
||||||
|
fillStyle.bitmapId = buffer.readUIntLE(16);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fillStyle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readLineStyle(buffer: SWFBuffer): { width: number, color: [ number, number, number, number ]}
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
width: buffer.readUIntLE(16)/20,
|
||||||
|
color: buffer.readRGBA()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public readShapeRecords(buffer: SWFBuffer)
|
||||||
|
{
|
||||||
|
let shapeRecords = null;
|
||||||
|
const typeFlag = buffer.readBits(1);
|
||||||
|
let eos = 0;
|
||||||
|
|
||||||
|
while((eos = buffer.readBits(5)))
|
||||||
|
{
|
||||||
|
if(0 === typeFlag)
|
||||||
|
{
|
||||||
|
shapeRecords = {
|
||||||
|
type: 'STYLECHANGERECORD'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return shapeRecords;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readShapeWithStyle()
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
fillStyles : this.readStyleArray(this, this.readFillStyle),
|
||||||
|
lineStyles : this.readStyleArray(this, this.readLineStyle),
|
||||||
|
numFillBits : this.readBits(4),
|
||||||
|
numLineBits : this.readBits(4),
|
||||||
|
shapeRecords: this.readShapeRecords(this)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public readTagCodeAndLength(): { code: number, length: number }
|
||||||
|
{
|
||||||
|
if(this.pointer === this.length) return null;
|
||||||
|
|
||||||
|
const n = this.readUIntLE(16);
|
||||||
|
const tagType = n >> 6;
|
||||||
|
let tagLength = n & SWFBuffer.RECORDHEADER_LENTH_FULL;
|
||||||
|
|
||||||
|
if(n === 0) return null;
|
||||||
|
|
||||||
|
if(tagLength === SWFBuffer.RECORDHEADER_LENTH_FULL ) tagLength = this.readUIntLE(32);
|
||||||
|
|
||||||
|
return { code: tagType, length: tagLength };
|
||||||
|
}
|
||||||
|
|
||||||
|
public readRect(): { x: number, y: number, width: number, height: number }
|
||||||
|
{
|
||||||
|
this.start();
|
||||||
|
|
||||||
|
const NBits = this.readBits(5);
|
||||||
|
const Xmin = this.readBits(NBits, true) / 20;
|
||||||
|
const Xmax = this.readBits(NBits, true) / 20;
|
||||||
|
const Ymin = this.readBits(NBits, true) / 20;
|
||||||
|
const Ymax = this.readBits(NBits, true) / 20;
|
||||||
|
|
||||||
|
return {
|
||||||
|
x : Xmin,
|
||||||
|
y : Ymin,
|
||||||
|
width : (Xmax > Xmin ? Xmax - Xmin : Xmin - Xmax),
|
||||||
|
height : (Ymax > Ymin ? Ymax - Ymin : Ymin - Ymax)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public seek(pos: number): void
|
||||||
|
{
|
||||||
|
this.pointer = pos % this.buffer.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start(): void
|
||||||
|
{
|
||||||
|
this.current = this.nextByte();
|
||||||
|
this.position = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public nextByte(): number
|
||||||
|
{
|
||||||
|
return this.pointer > this.buffer.length ? null : this.buffer[ this.pointer++ ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public readBits(b: number, signed: boolean = false): number
|
||||||
|
{
|
||||||
|
let n = 0;
|
||||||
|
let r = 0;
|
||||||
|
|
||||||
|
const sign = signed && ++n && ((this.current >> (8 - this.position++)) & 1) ? -1 : 1;
|
||||||
|
|
||||||
|
while(n++ < b)
|
||||||
|
{
|
||||||
|
if( this.position > 8 ) this.start();
|
||||||
|
|
||||||
|
r = (r << 1 ) + ((this.current >> (8 - this.position++)) & 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sign * r;
|
||||||
|
}
|
||||||
|
}
|
@ -1,291 +0,0 @@
|
|||||||
|
|
||||||
var RECORDHEADER_LENTH_FULL = 0x3f
|
|
||||||
// null-character
|
|
||||||
, EOS = 0x00
|
|
||||||
, styleCountExt = 0xFF;
|
|
||||||
|
|
||||||
function readStyleArray(buffer, next) {
|
|
||||||
var styleArrayCount = buffer.readUInt8()
|
|
||||||
, styles = [];
|
|
||||||
|
|
||||||
if (styleArrayCount === styleCountExt)
|
|
||||||
styleArrayCount = buffer.readUIntLE(16);
|
|
||||||
|
|
||||||
for (var i = 0; i < styleArrayCount; i++)
|
|
||||||
styles.push(next(buffer));
|
|
||||||
|
|
||||||
return styles;
|
|
||||||
}
|
|
||||||
|
|
||||||
function readFillStyle(buffer) {
|
|
||||||
var type = buffer.readUInt8()
|
|
||||||
, fillStyle = {
|
|
||||||
/**
|
|
||||||
* 0x00 = solid
|
|
||||||
* 0x10 = linear gradient fill
|
|
||||||
* 0x12 = radial gradient fill
|
|
||||||
* 0x13 = focal radial gradient fill (SWF 8 or later)
|
|
||||||
* 0x40 = repeating bitmap fill
|
|
||||||
* 0x41 = clipped bitmap fill
|
|
||||||
* 0x42 = non-smoothed repeating bitmap
|
|
||||||
* 0x43 = non-smoothed clipped bitmap
|
|
||||||
*/
|
|
||||||
fillStyleType : type
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case 0x00:
|
|
||||||
fillStyle.color = buffer.readRGBA();
|
|
||||||
break;
|
|
||||||
case 0x10, 0x12, 0x13:
|
|
||||||
console.log('Gradient');
|
|
||||||
break;
|
|
||||||
case 0x40, 0x41, 0x42, 0x43:
|
|
||||||
fillStyle.bitmapId = buffer.readUIntLE(16);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fillStyle;
|
|
||||||
}
|
|
||||||
|
|
||||||
function readLineStyle(buffer) {
|
|
||||||
return {
|
|
||||||
width: buffer.readUIntLE(16)/20,
|
|
||||||
color: buffer.readRGBA()
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function readShapeRecords(buffer) {
|
|
||||||
var shapeRecords = []
|
|
||||||
, typeFlag = buffer.readBits(1)
|
|
||||||
, shapeRecord
|
|
||||||
, eos;
|
|
||||||
|
|
||||||
while ((eos = buffer.readBits(5))) {
|
|
||||||
if (0 === typeFlag) {
|
|
||||||
shaperecord = {
|
|
||||||
type: 'STYLECHANGERECORD'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return shapeRecords;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Constructor of SWFBuffer object
|
|
||||||
*
|
|
||||||
* @param {Buffer} buffer
|
|
||||||
* @return Instance of SWFBuffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
function SWFBuffer( buffer ) {
|
|
||||||
if ( !Buffer.isBuffer( buffer ) ) {
|
|
||||||
throw new Error('Invalid buffer');
|
|
||||||
}
|
|
||||||
this.buffer = buffer;
|
|
||||||
this.pointer = 0;
|
|
||||||
this.position = 1;
|
|
||||||
this.current = 0;
|
|
||||||
this.length = buffer.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads unsigned 16 or 32 Little Endian Bits
|
|
||||||
* and advance pointer to next bits / 8 bytes
|
|
||||||
*
|
|
||||||
* @param {Number} bits
|
|
||||||
* @return {Number} Value read from buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readUIntLE = function( bits ) {
|
|
||||||
var value = 0;
|
|
||||||
try {
|
|
||||||
value = this.buffer['readUInt' + bits + 'LE'](this.pointer);
|
|
||||||
this.pointer += bits / 8;
|
|
||||||
} catch ( e ) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads unsigned 8 bit from the buffer
|
|
||||||
*
|
|
||||||
* @return {Number} Value read from buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readUInt8 = function() {
|
|
||||||
return this.buffer.readUInt8( this.pointer++ );
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads 32-bit unsigned integers value encoded (1-5 bytes)
|
|
||||||
*
|
|
||||||
* @return {Number} 32-bit unsigned integer
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readEncodedU32 = function() {
|
|
||||||
var i = 5
|
|
||||||
, result = 0
|
|
||||||
, nb;
|
|
||||||
|
|
||||||
do
|
|
||||||
result += (nb = this.nextByte());
|
|
||||||
while((nb & 128) && --i);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads an encoded data from buffer and returns a
|
|
||||||
* string using the specified character set.
|
|
||||||
*
|
|
||||||
* @param {String} encoding - defaults to 'utf8'
|
|
||||||
* @returns {String} Decoded string
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readString = function(encoding) {
|
|
||||||
var init = this.pointer;
|
|
||||||
while(this.readUInt8() !== EOS);
|
|
||||||
return this.buffer.toString(encoding || 'utf8', init, this.pointer - 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads RGB value
|
|
||||||
*
|
|
||||||
* @return {Array} Array of RGB value
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readRGB = function() {
|
|
||||||
return [this.readUInt8(), this.readUInt8(), this.readUInt8()];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads RGBA value
|
|
||||||
*
|
|
||||||
* @return {Array} Array of RGBA value
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readRGBA = function() {
|
|
||||||
var rgba = this.readRGB();
|
|
||||||
rgba.push(this.readUInt8());
|
|
||||||
return rgba;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads ShapeWithStyle structure
|
|
||||||
* used by the DefineShape tag.
|
|
||||||
*
|
|
||||||
* @return ShapeWithStyle structure
|
|
||||||
*/
|
|
||||||
SWFBuffer.prototype.readShapeWithStyle = function() {
|
|
||||||
return {
|
|
||||||
fillStyles : readStyleArray(this, readFillStyle),
|
|
||||||
lineStyles : readStyleArray(this, readLineStyle),
|
|
||||||
numFillBits : this.readBits(4),
|
|
||||||
numLineBits : this.readBits(4),
|
|
||||||
shapeRecords: readShapeRecords(this)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads RECORDHEADER from next tag in the buffer
|
|
||||||
*
|
|
||||||
* @return {Object} Tag code and length
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readTagCodeAndLength = function() {
|
|
||||||
if (this.pointer === this.length) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var n = this.readUIntLE(16)
|
|
||||||
, tagType = n >> 6
|
|
||||||
, tagLength = n & RECORDHEADER_LENTH_FULL;
|
|
||||||
|
|
||||||
if ( n === 0 )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if ( tagLength === RECORDHEADER_LENTH_FULL )
|
|
||||||
tagLength = this.readUIntLE(32);
|
|
||||||
|
|
||||||
return { code : tagType, length : tagLength };
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads RECT format
|
|
||||||
*
|
|
||||||
* @return {Object} x, y, width and height of the RECT
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readRect = function() {
|
|
||||||
|
|
||||||
this.start();
|
|
||||||
|
|
||||||
var NBits = this.readBits(5)
|
|
||||||
, Xmin = this.readBits(NBits, true)/20
|
|
||||||
, Xmax = this.readBits(NBits, true)/20
|
|
||||||
, Ymin = this.readBits(NBits, true)/20
|
|
||||||
, Ymax = this.readBits(NBits, true)/20;
|
|
||||||
|
|
||||||
return {
|
|
||||||
x : Xmin,
|
|
||||||
y : Ymin,
|
|
||||||
width : (Xmax > Xmin ? Xmax - Xmin : Xmin - Xmax),
|
|
||||||
height : (Ymax > Ymin ? Ymax - Ymin : Ymin - Ymax)
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets internal pointer to the specified position;
|
|
||||||
*
|
|
||||||
* @param {Number} pos
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.seek = function( pos ) {
|
|
||||||
this.pointer = pos % this.buffer.length;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resets position and sets current to next Byte in buffer
|
|
||||||
*/
|
|
||||||
SWFBuffer.prototype.start = function() {
|
|
||||||
this.current = this.nextByte();
|
|
||||||
this.position = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets next Byte in the buffer and Increment internal pointer
|
|
||||||
*
|
|
||||||
* @return {Number} Next byte in buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.nextByte = function() {
|
|
||||||
return this.pointer > this.buffer.length ? null : this.buffer[ this.pointer++ ];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads b bits from current byte in buffer
|
|
||||||
*
|
|
||||||
* @param {Number} b
|
|
||||||
* @return {Number} Bits read from buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
SWFBuffer.prototype.readBits = function( b, signed ) {
|
|
||||||
var n = 0
|
|
||||||
, r = 0
|
|
||||||
, sign = signed && ++n && ((this.current >> (8-this.position++)) & 1) ? -1 : 1;
|
|
||||||
|
|
||||||
while( n++ < b ) {
|
|
||||||
if ( this.position > 8 ) this.start();
|
|
||||||
|
|
||||||
r = (r << 1 ) + ((this.current >> (8-this.position++)) & 1);
|
|
||||||
}
|
|
||||||
return sign * r;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Exposes class */
|
|
||||||
exports = module.exports = SWFBuffer;
|
|
@ -5,11 +5,12 @@
|
|||||||
* @param {Mixed} value
|
* @param {Mixed} value
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function define(name, value) {
|
function define(name, value)
|
||||||
Object.defineProperty(exports, name, {
|
{
|
||||||
value : value,
|
Object.defineProperty(exports, name, {
|
||||||
enumerable : true
|
value : value,
|
||||||
});
|
enumerable : true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SWF Tags Type */
|
/* SWF Tags Type */
|
||||||
|
Loading…
Reference in New Issue
Block a user