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

def initialize(options = {}) #:nodoc:

  # Type information for indirect objects.
  @deferred_casts = {}
  
  #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

#defer_type_cast(reference, type) ⇒ Object

:nodoc:



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

def defer_type_cast(reference, type) #:nodoc:
  @deferred_casts[reference] = type
end

#parse(stream) ⇒ Object



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

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:



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

def parse_object(pos = @data.pos) #:nodoc:
  @data.pos = pos
  
  begin
    obj = Object.parse(@data, self)
    return if obj.nil?
    
    trace "Read #{obj.type} object#{
      if obj.class != obj.native_type
        " (" + obj.native_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:



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

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

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

    @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:



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

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



270
271
272
# File 'lib/origami/parser.rb', line 270

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

#target_filenameObject



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

def target_filename
  @filename
end

#target_filesizeObject



266
267
268
# File 'lib/origami/parser.rb', line 266

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