swfheader 0.21
Sign up to get free protection for your applications and to get access to all the features.
- data/README +27 -0
- data/lib/swfheader/packed-bit-obj.rb +20 -0
- data/lib/swfheader/swf-compression.rb +18 -0
- data/lib/swfheader/swf-compressor.rb +27 -0
- data/lib/swfheader/swf-decompressor.rb +26 -0
- data/lib/swfheader/swf-header.rb +103 -0
- data/lib/swfheader/swf-util.rb +14 -0
- data/lib/swfheader.rb +7 -0
- data/test/test.rb +11 -0
- metadata +55 -0
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
==Swf Util
|
2
|
+
swf util is a lightweight tool to read swf header��compress/decompress swf for Ruby��It is implemented in pure Ruby��
|
3
|
+
|
4
|
+
==Author
|
5
|
+
Dennis Zhuang<killme2008@gmail.com>
|
6
|
+
|
7
|
+
==Requirements
|
8
|
+
Ruby>=1.8.0 and <=1.9.0 on linux,windows and jruby
|
9
|
+
==Install
|
10
|
+
gem install swfheader-0.10.gem
|
11
|
+
|
12
|
+
==Example
|
13
|
+
Example:
|
14
|
+
#read swf head
|
15
|
+
require 'swfheader'
|
16
|
+
header=SwfUtil::read_header("test.swf")
|
17
|
+
header.inspect
|
18
|
+
header.version
|
19
|
+
header.frame_rate
|
20
|
+
......
|
21
|
+
|
22
|
+
#decompress swf
|
23
|
+
SwfUtil::decompress_swf("test.swf","test_decompressed.swf")
|
24
|
+
|
25
|
+
#compress swf
|
26
|
+
SwfUtil::compress_swf("test.swf","test_compressed.swf")
|
27
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SwfUtil
|
2
|
+
class PackedBitObj
|
3
|
+
attr_accessor :bitIndex,:byteIndex,:value,:nextBitIndex,:nextByteIndex,:nextByteBoundary
|
4
|
+
def initialize(bitMarker,byteMarker,decimalValue)
|
5
|
+
@bitIndex = bitMarker;
|
6
|
+
@byteIndex = byteMarker;
|
7
|
+
@value = decimalValue;
|
8
|
+
@nextBitIndex = bitMarker;
|
9
|
+
if ( bitMarker <= 7 )
|
10
|
+
@nextBitIndex+=1
|
11
|
+
@nextByteIndex =byteMarker
|
12
|
+
@nextByteBoundary = (byteMarker+=1)
|
13
|
+
else
|
14
|
+
@nextBitIndex = 0
|
15
|
+
@nextByteIndex+=1
|
16
|
+
@nextByteBoundary = @nextByteIndex;
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SwfUtil
|
2
|
+
class SWFCompression
|
3
|
+
def read_full_size(file)
|
4
|
+
buff=File.open(file,"rb") do |f|
|
5
|
+
f.seek(4,IO::SEEK_CUR)
|
6
|
+
f.read 4
|
7
|
+
end
|
8
|
+
buff.unpack("L")[0]
|
9
|
+
end
|
10
|
+
def strip_header(bytes)
|
11
|
+
bytes[8,bytes.size-8]
|
12
|
+
end
|
13
|
+
def is_compressed?(first_byte)
|
14
|
+
first_byte==?C
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),".")
|
2
|
+
require 'zlib'
|
3
|
+
module SwfUtil
|
4
|
+
class SWFCompressor < SWFCompression
|
5
|
+
def initialize(from=nil,to=nil)
|
6
|
+
read_file(from,to) if !from.nil? and !to.nil?
|
7
|
+
end
|
8
|
+
def read_file(file,to)
|
9
|
+
buff=File.open(file,"rb") do |fin|
|
10
|
+
fin.read
|
11
|
+
end
|
12
|
+
raise RuntimeError.new,"The file have already been compressed",caller if is_compressed?(buff[0])
|
13
|
+
result=compress(buff)
|
14
|
+
File.open(to,"wb") do |fout|
|
15
|
+
fout.write(result)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
def compress(buffer)
|
19
|
+
compressor=Zlib::Deflate.new
|
20
|
+
data=compressor.deflate(strip_header(buffer),Zlib::FINISH)
|
21
|
+
data=buffer[0,8]+data
|
22
|
+
data[0]=?C
|
23
|
+
data
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),".")
|
2
|
+
require 'zlib'
|
3
|
+
module SwfUtil
|
4
|
+
class SWFDecompressor<SWFCompression
|
5
|
+
def initialize(from=nil,to=nil)
|
6
|
+
read_file(from,to) if !from.nil? and !to.nil?
|
7
|
+
end
|
8
|
+
def read_file(file,to)
|
9
|
+
swf=File.open(file,"rb") do |f|
|
10
|
+
f.read
|
11
|
+
end
|
12
|
+
raise RuntimeError.new,"The file have not been compressed",caller if !is_compressed?(swf[0])
|
13
|
+
decomp=uncompress(swf)
|
14
|
+
File.open(to,"wb") do |f|
|
15
|
+
f.write(decomp)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
def uncompress(bytes)
|
19
|
+
decompressor =Zlib::Inflate.new
|
20
|
+
swf=decompressor.inflate(strip_header(bytes))
|
21
|
+
swf=bytes[0,8]+swf
|
22
|
+
swf[0] = ?F
|
23
|
+
swf
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),".")
|
2
|
+
module SwfUtil
|
3
|
+
class SWFHeader < SWFCompression
|
4
|
+
def initialize(file)
|
5
|
+
parse_header(file)
|
6
|
+
end
|
7
|
+
attr_accessor :signature,:compression_type,:version,:size,:nbits,:xmax,:ymax,:width,:height,:frame_rate,:frame_count,
|
8
|
+
COMPRESSED = "compressed"
|
9
|
+
UNCOMPRESSED = "uncompressed"
|
10
|
+
def parse_header(file)
|
11
|
+
@size=read_full_size(file)
|
12
|
+
buffer=File.open(file,"rb") do |f|
|
13
|
+
f.read
|
14
|
+
end
|
15
|
+
if !is_swf?(buffer)
|
16
|
+
raise RuntimeError.new,"File does not appear to be a swf file",caller
|
17
|
+
else
|
18
|
+
@signature=buffer[0,3]
|
19
|
+
end
|
20
|
+
if is_compressed?(buffer[0])
|
21
|
+
buffer=SWFDecompressor.new.uncompress(buffer)
|
22
|
+
@compression_type=COMPRESSED
|
23
|
+
else
|
24
|
+
@compression_type=UNCOMPRESSED
|
25
|
+
end
|
26
|
+
@version=buffer[3]
|
27
|
+
@nbits = ((buffer[8]&0xff)>>3)
|
28
|
+
pbo = read_packed_bits( buffer, 8, 5, @nbits )
|
29
|
+
pbo2 = read_packed_bits( buffer, pbo.nextByteIndex,pbo.nextBitIndex, @nbits )
|
30
|
+
|
31
|
+
pbo3 = read_packed_bits( buffer, pbo2.nextByteIndex,pbo2.nextBitIndex, @nbits )
|
32
|
+
|
33
|
+
pbo4 = read_packed_bits( buffer, pbo3.nextByteIndex,pbo3.nextBitIndex, @nbits )
|
34
|
+
@xmax = pbo2.value
|
35
|
+
@ymax = pbo4.value
|
36
|
+
|
37
|
+
@width = convert_twips_to_pixels( @xmax )
|
38
|
+
@height = convert_twips_to_pixels( @ymax )
|
39
|
+
|
40
|
+
byte_pointer = pbo4.nextByteIndex + 2
|
41
|
+
|
42
|
+
@frame_rate = buffer[byte_pointer]
|
43
|
+
byte_pointer+=1
|
44
|
+
fc1 = buffer[byte_pointer] & 0xFF
|
45
|
+
byte_pointer+=1
|
46
|
+
|
47
|
+
fc2 = buffer[byte_pointer] & 0xFF
|
48
|
+
byte_pointer+=1
|
49
|
+
@frame_count=(fc2<<8)+fc1
|
50
|
+
buffer=nil
|
51
|
+
end
|
52
|
+
def is_swf?(bytes)
|
53
|
+
bytes[0,3]=="FWS" or bytes[0,3]=="CWS"
|
54
|
+
end
|
55
|
+
def read_packed_bits(bytes,byte_marker,bit_marker,length)
|
56
|
+
total = 0
|
57
|
+
shift = 7 - bit_marker
|
58
|
+
counter = 0
|
59
|
+
bit_index = bit_marker
|
60
|
+
byte_index = byte_marker
|
61
|
+
while counter<length
|
62
|
+
(bit_marker...8).each do |i|
|
63
|
+
bit =((bytes[byte_marker] & 0xff ) >> shift ) & 1
|
64
|
+
total = ( total << 1 ) + bit
|
65
|
+
bit_index = i
|
66
|
+
shift-=1
|
67
|
+
counter+=1
|
68
|
+
if counter==length
|
69
|
+
break
|
70
|
+
end
|
71
|
+
end
|
72
|
+
byte_index = byte_marker
|
73
|
+
byte_marker+=1
|
74
|
+
bit_marker = 0
|
75
|
+
shift = 7
|
76
|
+
end
|
77
|
+
return PackedBitObj.new(bit_index, byte_index, total )
|
78
|
+
end
|
79
|
+
def convert_twips_to_pixels(twips)
|
80
|
+
twips / 20
|
81
|
+
end
|
82
|
+
def convert_pixels_to_twips( pixels )
|
83
|
+
return pixels * 20
|
84
|
+
end
|
85
|
+
def to_s
|
86
|
+
inspect
|
87
|
+
end
|
88
|
+
def inspect
|
89
|
+
"signature: "+@signature.to_s+"\n"+
|
90
|
+
"version: "+@version.to_s+"\n"+
|
91
|
+
"compression: "+@compression_type.to_s+"\n"+
|
92
|
+
"size: "+@size.to_s+"\n"+
|
93
|
+
"nbits: "+@nbits.to_s+"\n"+
|
94
|
+
"xmax: "+@xmax.to_s+"\n"+
|
95
|
+
"ymax: "+@ymax.to_s+"\n"+
|
96
|
+
"width: "+@width.to_s+"\n"+
|
97
|
+
"height: "+@height.to_s+"\n"+
|
98
|
+
"frame_rate: "+@frame_rate.to_s+"\n"+
|
99
|
+
"frame_count: "+@frame_count.to_s+"\n"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SwfUtil
|
2
|
+
def self.read_header(file)
|
3
|
+
raise ArgumentError.new("file is nil",caller) if file.nil?
|
4
|
+
SWFHeader.new(file)
|
5
|
+
end
|
6
|
+
def self.compress_swf(from,to=from.sub(/\.swf/,'_comp.swf'))
|
7
|
+
raise ArgumentError.new("file is nil",caller) if from.nil?
|
8
|
+
SWFCompressor.new(from,to)
|
9
|
+
end
|
10
|
+
def self.decompress_swf(from,to=from.sub(/\.swf/,'de_comp.swf'))
|
11
|
+
raise ArgumentError.new("file is nil",caller) if from.nil?
|
12
|
+
SWFDecompressor.new(from,to)
|
13
|
+
end
|
14
|
+
end
|
data/lib/swfheader.rb
ADDED
data/test/test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),"..","lib")
|
2
|
+
require 'swfheader'
|
3
|
+
#read swf header
|
4
|
+
swfheader=SwfUtil::read_header(File.join(File.dirname(__FILE__),".","test.swf"))
|
5
|
+
puts swfheader.inspect
|
6
|
+
#decompress swf
|
7
|
+
SwfUtil::decompress_swf(File.join(File.dirname(__FILE__),".","test.swf"),
|
8
|
+
File.join(File.dirname(__FILE__),".","test2.swf"))
|
9
|
+
#compress swf
|
10
|
+
SwfUtil::compress_swf(File.join(File.dirname(__FILE__),".","test2.swf"),
|
11
|
+
File.join(File.dirname(__FILE__),".","test3.swf"))
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: swfheader
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.21"
|
7
|
+
date: 2009-06-10 00:00:00 +08:00
|
8
|
+
summary: a light weight tool for read swf header,compress/decompress swf for Ruby
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: killme2008@gmail.com
|
12
|
+
homepage: http://www.blogjava.net/killme2008
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: swfheader
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Dennis Zhuang
|
31
|
+
files:
|
32
|
+
- lib/swfheader
|
33
|
+
- lib/swfheader/packed-bit-obj.rb
|
34
|
+
- lib/swfheader/swf-compression.rb
|
35
|
+
- lib/swfheader/swf-compressor.rb
|
36
|
+
- lib/swfheader/swf-decompressor.rb
|
37
|
+
- lib/swfheader/swf-header.rb
|
38
|
+
- lib/swfheader/swf-util.rb
|
39
|
+
- lib/swfheader.rb
|
40
|
+
- test/test.rb
|
41
|
+
- README
|
42
|
+
test_files:
|
43
|
+
- test/test.rb
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
extra_rdoc_files:
|
47
|
+
- README
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies: []
|
55
|
+
|