Class: SwfUtil::SWFHeader

Inherits:
SWFCompression show all
Defined in:
lib/swfheader/swf-header.rb

Constant Summary collapse

UNCOMPRESSED =
"uncompressed"

Instance Method Summary collapse

Methods inherited from SWFCompression

#is_compressed?, #read_full_size, #strip_header

Constructor Details

#initialize(file) ⇒ SWFHeader

Returns a new instance of SWFHeader.



4
5
6
# File 'lib/swfheader/swf-header.rb', line 4

def initialize(file)
  parse_header(file)
end

Instance Method Details

#convert_pixels_to_twips(pixels) ⇒ Object



87
88
89
# File 'lib/swfheader/swf-header.rb', line 87

def convert_pixels_to_twips( pixels )
  return pixels * 20 
end

#convert_twips_to_pixels(twips) ⇒ Object



84
85
86
# File 'lib/swfheader/swf-header.rb', line 84

def  convert_twips_to_pixels(twips)
  twips / 20 
end

#inspectObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/swfheader/swf-header.rb', line 93

def inspect
  "signature:   "+@signature.to_s+"\n"+
  "version:     "+@version.to_s+"\n"+
  "avm_version: "+@avm_version.to_s+"\n"+
  "compression: "+@compression_type.to_s+"\n"+
  "size:        "+@size.to_s+"\n"+
  "nbits:       "+@nbits.to_s+"\n"+
  "xmax:        "+@xmax.to_s+"\n"+
  "ymax:        "+@ymax.to_s+"\n"+
  "width:       "+@width.to_s+"\n"+
  "height:      "+@height.to_s+"\n"+
  "frame_rate:   "+@frame_rate.to_s+"\n"+
  "frame_count:  "+@frame_count.to_s+"\n"
end

#is_swf?(bytes) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/swfheader/swf-header.rb', line 57

def is_swf?(bytes)
  bytes[0,3]=="FWS" or bytes[0,3]=="CWS"
end

#parse_header(file) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/swfheader/swf-header.rb', line 10

def parse_header(file)
  @size=read_full_size(file)
  buffer=File.open(file,"rb") do |f|
    f.read
  end
  if !is_swf?(buffer)
    raise RuntimeError.new,"File does not appear to be a swf file",caller
  else
    @signature=buffer[0,3]
  end
  if is_compressed?(buffer[0])
    buffer=SWFDecompressor.new.uncompress(buffer)
    @compression_type=COMPRESSED
  else
    @compression_type=UNCOMPRESSED
  end
  @version=buffer[3]
  @nbits = ((buffer[8]&0xff)>>3)
  pbo = read_packed_bits( buffer, 8, 5, @nbits ) 
  pbo2 = read_packed_bits( buffer, pbo.nextByteIndex,pbo.nextBitIndex, @nbits ) 
  
  pbo3 = read_packed_bits( buffer, pbo2.nextByteIndex,pbo2.nextBitIndex, @nbits ) 
  
  pbo4 = read_packed_bits( buffer, pbo3.nextByteIndex,pbo3.nextBitIndex, @nbits ) 
  @xmax = pbo2.value
  @ymax = pbo4.value 
  
  @width = convert_twips_to_pixels( @xmax ) 
  @height = convert_twips_to_pixels( @ymax ) 
  
  byte_pointer = pbo4.nextByteIndex + 2 
  
  @frame_rate = buffer[byte_pointer]
  byte_pointer+=1
  fc1 = buffer[byte_pointer] & 0xFF
  byte_pointer+=1
  
  fc2 = buffer[byte_pointer] & 0xFF
  byte_pointer+=1
  @frame_count=(fc2<<8)+fc1 

  byte_pointer+=2
  avm_bit = buffer[byte_pointer] & 0x08
  @avm_version = (avm_bit == 0) ? "AVM1" : "AVM2"
  
  buffer=nil
end

#read_packed_bits(bytes, byte_marker, bit_marker, length) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/swfheader/swf-header.rb', line 60

def read_packed_bits(bytes,byte_marker,bit_marker,length)
  total = 0
  shift = 7 - bit_marker 
  counter = 0 
  bit_index = bit_marker 
  byte_index = byte_marker 
  while counter<length
   (bit_marker...8).each do |i|
      bit =((bytes[byte_marker] & 0xff ) >> shift ) & 1 
      total = ( total << 1 ) + bit 
      bit_index = i 
      shift-=1 
      counter+=1 
      if counter==length
        break 
      end
    end
    byte_index = byte_marker 
    byte_marker+=1
    bit_marker = 0 
    shift = 7 
  end
  return PackedBitObj.new(bit_index, byte_index, total ) 
end

#to_sObject



90
91
92
# File 'lib/swfheader/swf-header.rb', line 90

def to_s
  inspect
end