Class: Origami::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/parser.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ParsingError

Constant Summary collapse

VERBOSE_QUIET =

Do not output debug information.

0
VERBOSE_INFO =

Output some useful information.

1
VERBOSE_DEBUG =

Output debug information.

2
VERBOSE_INSANE =

Output every objects read

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/origami/parser.rb', line 136

def initialize(options = {}) #:nodoc:
  
  #Default options values
  @options = 
  { 
    :verbosity => VERBOSE_INFO, # Verbose level.
    :ignore_errors => true,     # Try to keep on parsing when errors occur.
    :callback => Proc.new {},   # Callback procedure whenever a structure is read.
    :logger => STDERR,          # Where to output parser messages.
    :colorize_log => true       # Colorize parser output?
  }
 
  @options.update(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



134
135
136
# File 'lib/origami/parser.rb', line 134

def options
  @options
end

Instance Method Details

#parse(stream) ⇒ Object



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
# File 'lib/origami/parser.rb', line 151

def parse(stream)
  data = 
  if stream.respond_to? :read
    if ''.respond_to? :force_encoding
      StringScanner.new(stream.read.force_encoding('binary')) # 1.9 compat
    else
      StringScanner.new(stream.read)
    end
  elsif stream.is_a? ::String
    @filename = stream
    if ''.respond_to? :force_encoding
      StringScanner.new(File.open(stream, "r", :encoding => 'binary').binmode.read)
    else
      StringScanner.new(File.open(stream, "r").binmode.read)
    end
  elsif stream.is_a? StringScanner
    stream
  else
    raise TypeError
  end

  @logger = @options[:logger]
  @data = data
  @data.pos = 0
end

#parse_object(pos = @data.pos) ⇒ Object

:nodoc:



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
# File 'lib/origami/parser.rb', line 177

def parse_object(pos = @data.pos) #:nodoc:
  @data.pos = pos
  
  begin
    obj = Object.parse(@data)
    return if obj.nil?
    
    trace "Read #{obj.type} object#{
      if obj.type != obj.real_type
        " (" + obj.real_type.to_s.split('::').last + ")" 
      end
    }, #{obj.reference}"

    @options[:callback].call(obj)
    obj

  rescue UnterminatedObjectError => e
    error e.message
    obj = e.obj

    Object.skip_until_next_obj(@data)
    @options[:callback].call(obj)
    obj

  rescue Exception => e
    error "Breaking on: #{(@data.peek(10) + "...").inspect} at offset 0x#{@data.pos.to_s(16)}"
    error "Last exception: [#{e.class}] #{e.message}"
    if not @options[:ignore_errors]
      error "Manually fix the file or set :ignore_errors parameter."
      raise
    end

    debug 'Skipping this indirect object.'
    raise if not Object.skip_until_next_obj(@data)
        
    retry
  end
end

#parse_trailer(pos = @data.pos) ⇒ Object

:nodoc:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/origami/parser.rb', line 235

def parse_trailer(pos = @data.pos) #:nodoc:
  @data.pos = pos

  begin
    info "...Parsing trailer..."
    trailer = Trailer.parse(@data)

    @options[:callback].call(trailer)
    trailer
   
  rescue Exception => e
    debug "Exception caught while parsing trailer : " + e.message
    warn "Unable to parse trailer!"
        
    abort("Manually fix the file or set :ignore_errors parameter.") if not @options[:ignore_errors]

    raise
  end
end

#parse_xreftable(pos = @data.pos) ⇒ Object

:nodoc:



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/origami/parser.rb', line 216

def parse_xreftable(pos = @data.pos) #:nodoc:
  @data.pos = pos

  begin
    info "...Parsing xref table..."
    xreftable = XRef::Section.parse(@data)
    @options[:callback].call(xreftable)

    xreftable
  rescue Exception => e
    debug "Exception caught while parsing xref table : " + e.message
    warn "Unable to parse xref table! Xrefs might be stored into an XRef stream."

    @data.pos -= 'trailer'.length unless @data.skip_until(/trailer/).nil?

    nil
  end
end

#target_dataObject



263
264
265
# File 'lib/origami/parser.rb', line 263

def target_data
  @data.string.dup if @data
end

#target_filenameObject



255
256
257
# File 'lib/origami/parser.rb', line 255

def target_filename
  @filename
end

#target_filesizeObject



259
260
261
# File 'lib/origami/parser.rb', line 259

def target_filesize
  @data.string.size if @data
end