Class: ZMachine::HttpDecoders::GZipHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/z-http/decoders.rb

Overview

Partial implementation of RFC 1952 to extract the deflate stream from a gzip file

Instance Method Summary collapse

Constructor Details

#initializeGZipHeader

Returns a new instance of GZipHeader.



97
98
99
100
101
# File 'lib/z-http/decoders.rb', line 97

def initialize
  @state = :begin
  @data = ""
  @pos = 0
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/z-http/decoders.rb', line 124

def eof?
  @pos >= @data.size
end

#extract_stream(compressed) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/z-http/decoders.rb', line 128

def extract_stream(compressed)
  @data << compressed
  pos = @pos

  while !eof? && !finished?
    buffer = ""

    case @state
    when :begin
      break if !read(10, buffer)

      if buffer.getbyte(0) != 0x1f || buffer.getbyte(1) != 0x8b
        raise DecoderError.new("magic header not found")
      end

      if buffer.getbyte(2) != 0x08
        raise DecoderError.new("unknown compression method")
      end

      @flags = buffer.getbyte(3)
      if (@flags & 0xe0).nonzero?
        raise DecoderError.new("unknown header flags set")
      end

      # We don't care about these values, I'm leaving the code for reference
      # @time = buffer[4..7].unpack("V")[0] # little-endian uint32
      # @extra_flags = buffer.getbyte(8)
      # @os = buffer.getbyte(9)

      @state = :extra_length

    when :extra_length
      if (@flags & 0x04).nonzero?
        break if !read(2, buffer)
        @extra_length = buffer.unpack("v")[0] # little-endian uint16
        @state = :extra
      else
        @state = :extra
      end

    when :extra
      if (@flags & 0x04).nonzero?
        break if read(@extra_length, buffer)
        @state = :name
      else
        @state = :name
      end

    when :name
      if (@flags & 0x08).nonzero?
        while !(buffer = readbyte).nil?
          if buffer == 0
            @state = :comment
            break
          end
        end
      else
        @state = :comment
      end

    when :comment
      if (@flags & 0x10).nonzero?
        while !(buffer = readbyte).nil?
          if buffer == 0
            @state = :hcrc
            break
          end
        end
      else
        @state = :hcrc
      end

    when :hcrc
      if (@flags & 0x02).nonzero?
        break if !read(2, buffer)
        @state = :finish
      else
        @state = :finish
      end
    end
  end

  if finished?
    compressed[(@pos - pos)..-1]
  else
    ""
  end
end

#finished?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/z-http/decoders.rb', line 103

def finished?
  @state == :finish
end

#read(n, buffer) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/z-http/decoders.rb', line 107

def read(n, buffer)
  if (@pos + n) <= @data.size
    buffer << @data[@pos..(@pos + n - 1)]
    @pos += n
    return true
  else
    return false
  end
end

#readbyteObject



117
118
119
120
121
122
# File 'lib/z-http/decoders.rb', line 117

def readbyte
  if (@pos + 1) <= @data.size
    @pos += 1
    @data.getbyte(@pos - 1)
  end
end