Files
unity-flash-tools/Assets/FlashTools/Scripts/Internal/Editor/SwfTools/SwfStreamReader.cs
2016-03-13 19:50:29 +06:00

184 lines
3.6 KiB
C#

using System.IO;
using System.Text;
using System.Collections.Generic;
namespace FlashTools.Internal.SwfTools {
class SwfStreamReader {
struct BitContext {
public byte CachedByte;
public byte BitIndex;
}
BitContext _bitContext;
BinaryReader _binaryReader;
long Length {
get { return _binaryReader.BaseStream.Length; }
}
long Position {
get { return _binaryReader.BaseStream.Position; }
}
long BytesLeft {
get { return Length - Position; }
}
// ---------------------------------------------------------------------
//
// Public
//
// ---------------------------------------------------------------------
public SwfStreamReader(string path) {
var file_stream = File.OpenRead(path);
_binaryReader = new BinaryReader(file_stream);
}
public SwfStreamReader(byte[] data) {
var memory_stream = new MemoryStream(data);
_binaryReader = new BinaryReader(memory_stream);
}
public SwfStreamReader(Stream stream) {
_binaryReader = new BinaryReader(stream);
}
public SwfStreamReader SeekToBegin() {
_binaryReader.BaseStream.Position = 0;
return this;
}
public bool IsEOF {
get { return Position >= Length; }
}
public void AlignToByte() {
_bitContext.BitIndex = 0;
_bitContext.CachedByte = 0;
}
public byte[] ReadRest() {
return ReadBytes((int)BytesLeft);
}
public bool ReadBit() {
var bitIndex = _bitContext.BitIndex & 0x07;
if ( bitIndex == 0 ) {
_bitContext.CachedByte = ReadByte();
}
++_bitContext.BitIndex;
return ((_bitContext.CachedByte << bitIndex) & 0x80) != 0;
}
public byte ReadByte() {
return _binaryReader.ReadByte();
}
public byte[] ReadBytes(int count) {
return _binaryReader.ReadBytes(count);
}
public char ReadChar() {
return _binaryReader.ReadChar();
}
public char[] ReadChars(int count) {
return _binaryReader.ReadChars(count);
}
public short ReadInt16() {
return _binaryReader.ReadInt16();
}
public int ReadInt32() {
return _binaryReader.ReadInt32();
}
public ushort ReadUInt16() {
return _binaryReader.ReadUInt16();
}
public uint ReadUInt32() {
return _binaryReader.ReadUInt32();
}
public int ReadSignedBits(uint count) {
if ( count == 0 ) {
return 0;
}
bool sign = ReadBit();
var res = sign ? uint.MaxValue : 0;
--count;
for ( var i = 0; i < count; ++i ) {
var bit = ReadBit();
res = (res << 1 | (bit ? 1u : 0u));
}
return (int)res;
}
public uint ReadUnsignedBits(uint count) {
if ( count == 0 ) {
return 0;
}
uint res = 0;
for ( var i = 0; i < count; ++i ) {
var bit = ReadBit();
res = (res << 1 | (bit ? 1u : 0u));
}
return res;
}
public string ReadString() {
var bytes = new List<byte>();
while ( true ) {
var bt = ReadByte();
if ( bt == 0 ) {
break;
}
bytes.Add(bt);
}
return Encoding.UTF8.GetString(bytes.ToArray());
}
public float ReadFixedPoint8() {
return ReadInt16() / 256.0f;
}
public float ReadFixedPoint16(uint bits) {
var value = ReadSignedBits(bits);
return value / 65536.0f;
}
public uint ReadEncodedU32() {
AlignToByte();
uint val = 0;
var bt = ReadByte();
val |= bt & 0x7fu;
if ( (bt & 0x80) == 0 ) {
return val;
}
bt = ReadByte();
val |= (bt & 0x7fu) << 7;
if ( (bt & 0x80) == 0 ) {
return val;
}
bt = ReadByte();
val |= (bt & 0x7fu) << 14;
if ( (bt & 0x80) == 0 ) {
return val;
}
bt = ReadByte();
val |= (bt & 0x7fu) << 21;
if ( (bt & 0x80) == 0 ) {
return val;
}
bt = ReadByte();
val |= (bt & 0x7fu) << 28;
return val;
}
}
}