SwfUtil 0.01
Sign up to get free protection for your applications and to get access to all the features.
- data/README +30 -0
- data/lib/swf_util.rb +7 -0
- data/lib/swf_util/packed-bit-obj.rb +20 -0
- data/lib/swf_util/swf-compression.rb +24 -0
- data/lib/swf_util/swf-compressor.rb +28 -0
- data/lib/swf_util/swf-decompressor.rb +26 -0
- data/lib/swf_util/swf-header.rb +110 -0
- data/lib/swf_util/swf-util.rb +12 -0
- data/test/test.rb +11 -0
- data/test/test.swf +0 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
==Swf Util
|
2
|
+
swf util is a lightweight tool to read swf header��compress/decompress swf for Ruby.
|
3
|
+
|
4
|
+
==Author
|
5
|
+
Dennis Zane<killme2008@gmail.com>
|
6
|
+
|
7
|
+
==Requirements
|
8
|
+
Ruby>=1.8.0 on linux or jruby
|
9
|
+
bit-struct>=0.11 (homepage:http://redshift.sourceforge.net/bit-struct/)
|
10
|
+
|
11
|
+
==Install
|
12
|
+
At first,please install bit-struct(download it from:http://redshift.sourceforge.net/bit-struct/)
|
13
|
+
then, gem install SwfUtil-0.01.gem
|
14
|
+
|
15
|
+
==Example
|
16
|
+
Example:
|
17
|
+
#read swf head
|
18
|
+
require 'swf_util'
|
19
|
+
header=SwfUtil::read_header("test.swf")
|
20
|
+
puts header.inspect
|
21
|
+
header.version
|
22
|
+
header.frame_rate
|
23
|
+
......
|
24
|
+
|
25
|
+
#decompress swf
|
26
|
+
SwfUtil::decompress_swf("test.swf")
|
27
|
+
|
28
|
+
#compress swf
|
29
|
+
SwfUtil::compress_swf("test.swf")
|
30
|
+
|
data/lib/swf_util.rb
ADDED
@@ -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,24 @@
|
|
1
|
+
require 'bit-struct'
|
2
|
+
module SwfUtil
|
3
|
+
class SWFCompression
|
4
|
+
def read_full_size(file)
|
5
|
+
temp=nil
|
6
|
+
File.open(file,"r") do |f|
|
7
|
+
f.seek(4,IO::SEEK_CUR)
|
8
|
+
temp=f.read(4)
|
9
|
+
end
|
10
|
+
return read_size(temp)
|
11
|
+
end
|
12
|
+
def read_size(temp)
|
13
|
+
temp=temp.reverse
|
14
|
+
size=Size.new(temp).value
|
15
|
+
end
|
16
|
+
def strip(bytes)
|
17
|
+
bytes[8,bytes.size-8]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
class Size<BitStruct
|
21
|
+
unsigned :value,32
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,28 @@
|
|
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
|
+
size=read_full_size(file)
|
10
|
+
buffer=nil
|
11
|
+
File.open(file,"r") do |fin|
|
12
|
+
buffer=fin.read(size)
|
13
|
+
end
|
14
|
+
temp=compress(buffer)
|
15
|
+
File.open(to,"w+") do |fout|
|
16
|
+
fout.write(temp)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
def compress(buffer)
|
20
|
+
compressor=Zlib::Deflate.new
|
21
|
+
data=compressor.deflate(strip(buffer),Zlib::FINISH)
|
22
|
+
data=buffer[0,8]+data
|
23
|
+
data[0]=67
|
24
|
+
return data
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -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=nil
|
10
|
+
File.open(file,"r") do |f|
|
11
|
+
swf=f.read(read_full_size(file))
|
12
|
+
end
|
13
|
+
decomp=uncompress(swf)
|
14
|
+
File.open(to,"w+") do |f|
|
15
|
+
f.write(decomp)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
def uncompress(bytes)
|
19
|
+
decompressor =Zlib::Inflate.new
|
20
|
+
swf=decompressor.inflate(strip(bytes))
|
21
|
+
swf=bytes[0,8]+swf
|
22
|
+
swf[0] = 70
|
23
|
+
swf
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,110 @@
|
|
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
|
+
temp=nil
|
12
|
+
@size=read_full_size(file)
|
13
|
+
File.open(file,"r") do |f|
|
14
|
+
temp=f.read(@size)
|
15
|
+
end
|
16
|
+
if !is_swf?(temp)
|
17
|
+
raise RuntimeError.new,"File does not appear to be a swf",caller
|
18
|
+
else
|
19
|
+
@signature=temp[0,3]
|
20
|
+
end
|
21
|
+
if is_compressed?(temp[0])
|
22
|
+
temp=SWFDecompressor.new.uncompress(temp)
|
23
|
+
@compression_type=COMPRESSED
|
24
|
+
else
|
25
|
+
@compression_type=UNCOMPRESSED
|
26
|
+
end
|
27
|
+
@version=temp[3]
|
28
|
+
@nbits = ((temp[8]&0xff)>>3)
|
29
|
+
pbo = read_packed_bits( temp, 8, 5, @nbits )
|
30
|
+
pbo2 = read_packed_bits( temp, pbo.nextByteIndex,pbo.nextBitIndex, @nbits )
|
31
|
+
|
32
|
+
pbo3 = read_packed_bits( temp, pbo2.nextByteIndex,pbo2.nextBitIndex, @nbits )
|
33
|
+
|
34
|
+
pbo4 = read_packed_bits( temp, pbo3.nextByteIndex,pbo3.nextBitIndex, @nbits )
|
35
|
+
@xmax = pbo2.value
|
36
|
+
@ymax = pbo4.value
|
37
|
+
|
38
|
+
@width = convert_twips_to_pixels( @xmax )
|
39
|
+
@height = convert_twips_to_pixels( @ymax )
|
40
|
+
|
41
|
+
bytePointer = pbo4.nextByteIndex + 2
|
42
|
+
|
43
|
+
@frame_rate = temp[bytePointer]
|
44
|
+
bytePointer+=1
|
45
|
+
|
46
|
+
|
47
|
+
fc1 = temp[bytePointer] & 0xFF
|
48
|
+
bytePointer+=1
|
49
|
+
|
50
|
+
fc2 = temp[bytePointer] & 0xFF
|
51
|
+
bytePointer+=1
|
52
|
+
@frame_count=(fc2 <<8)+fc1
|
53
|
+
temp=nil
|
54
|
+
end
|
55
|
+
def is_swf?(bytes)
|
56
|
+
bytes[0,3]=="FWS" or bytes[0,3]=="CWS"
|
57
|
+
end
|
58
|
+
def is_compressed?(first_byte)
|
59
|
+
if first_byte==67
|
60
|
+
return true
|
61
|
+
else
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
def read_packed_bits(bytes,byteMarker,bitMarker,length)
|
66
|
+
total = 0
|
67
|
+
shift = 7 - bitMarker
|
68
|
+
counter = 0
|
69
|
+
bitIndex = bitMarker
|
70
|
+
byteIndex = byteMarker
|
71
|
+
while counter<length
|
72
|
+
(bitMarker...8).each do |i|
|
73
|
+
bit =((bytes[byteMarker] & 0xff ) >> shift ) & 1
|
74
|
+
total = ( total << 1 ) + bit
|
75
|
+
bitIndex = i
|
76
|
+
shift-=1
|
77
|
+
counter+=1
|
78
|
+
if counter==length
|
79
|
+
break
|
80
|
+
end
|
81
|
+
end
|
82
|
+
byteIndex = byteMarker
|
83
|
+
byteMarker+=1
|
84
|
+
bitMarker = 0
|
85
|
+
shift = 7
|
86
|
+
end
|
87
|
+
return PackedBitObj.new(bitIndex, byteIndex, total )
|
88
|
+
end
|
89
|
+
def convert_twips_to_pixels(twips)
|
90
|
+
twips / 20
|
91
|
+
end
|
92
|
+
def convert_pixels_to_twips( pixels )
|
93
|
+
return pixels * 20
|
94
|
+
end
|
95
|
+
def inspect
|
96
|
+
"signature: "+@signature.to_s+"\n"+
|
97
|
+
"version: "+@version.to_s+"\n"+
|
98
|
+
"compression: "+@compression_type.to_s+"\n"+
|
99
|
+
"size: "+@size.to_s+"\n"+
|
100
|
+
"nbits: "+@nbits.to_s+"\n"+
|
101
|
+
"xmax: "+@xmax.to_s+"\n"+
|
102
|
+
"ymax: "+@ymax.to_s+"\n"+
|
103
|
+
"width: "+@width.to_s+"\n"+
|
104
|
+
"height: "+@height.to_s+"\n"+
|
105
|
+
"frameRate: "+@frame_rate.to_s+"\n"+
|
106
|
+
"frameCount: "+@frame_count.to_s+"\n"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
@@ -0,0 +1,12 @@
|
|
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
|
+
SWFCompressor.new(from,to)
|
8
|
+
end
|
9
|
+
def self.decompress_swf(from,to=from.sub(/\.swf/,'de_comp.swf'))
|
10
|
+
SWFDecompressor.new(from,to)
|
11
|
+
end
|
12
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),"..","lib")
|
2
|
+
require 'swf_util'
|
3
|
+
#read swf header
|
4
|
+
puts SwfUtil::read_header(File.join(File.dirname(__FILE__),".","test.swf")).inspect
|
5
|
+
|
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"))
|
data/test/test.swf
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: SwfUtil
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.01"
|
7
|
+
date: 2008-03-12 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: swf_util
|
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 Zane
|
31
|
+
files:
|
32
|
+
- lib/swf_util
|
33
|
+
- lib/swf_util/packed-bit-obj.rb
|
34
|
+
- lib/swf_util/swf-compression.rb
|
35
|
+
- lib/swf_util/swf-compressor.rb
|
36
|
+
- lib/swf_util/swf-decompressor.rb
|
37
|
+
- lib/swf_util/swf-header.rb
|
38
|
+
- lib/swf_util/swf-util.rb
|
39
|
+
- lib/swf_util.rb
|
40
|
+
- test/test.rb
|
41
|
+
- test/test.swf
|
42
|
+
- README
|
43
|
+
test_files:
|
44
|
+
- test/test.rb
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
extra_rdoc_files:
|
48
|
+
- README
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
dependencies: []
|
56
|
+
|