class Origami::PDF::Parser

def cast_trailer_objects(pdf) #:nodoc:

:nodoc:
def cast_trailer_objects(pdf) #:nodoc:
    trailer = pdf.trailer
    if trailer[:Root].is_a?(Reference)
        pdf.cast_object(trailer[:Root], Catalog)
    end
    if trailer[:Info].is_a?(Reference)
        pdf.cast_object(trailer[:Info], Metadata)
    end
    if trailer[:Encrypt].is_a?(Reference)
        pdf.cast_object(trailer[:Encrypt], Encryption::Standard::Dictionary)
    end
end

def decrypt_document(pdf) #:nodoc:

:nodoc:
def decrypt_document(pdf) #:nodoc:
    passwd = @options[:password]
    begin
        pdf.decrypt(passwd)
    rescue EncryptionInvalidPasswordError
        if passwd.empty?
            passwd = @options[:prompt_password].call
            retry unless passwd.empty?
        end
        raise
    end
end

def initialize(params = {})

def initialize(params = {})
    options =
    {
        decrypt: true,                # Attempt to decrypt to document if encrypted (recommended).
        password: '',                 # Default password being tried when opening a protected document.
        prompt_password: lambda do    # Callback procedure to prompt password when document is encrypted.
            require 'io/console'
            STDERR.print "Password: "
            STDIN.noecho(&:gets).chomp
        end,
        force: false                  # Force PDF header detection
    }.update(params)
    super(options)
end

def parse_finalize(pdf) #:nodoc:

:nodoc:
def parse_finalize(pdf) #:nodoc:
    cast_trailer_objects(pdf)
    warn "This file has been linearized." if pdf.linearized?
    propagate_types(pdf) if Origami::OPTIONS[:enable_type_propagation]
    #
    # Decrypt encrypted file contents
    #
    if pdf.encrypted?
        warn "This document contains encrypted data!"
        decrypt_document(pdf) if @options[:decrypt]
    end
    warn "This document has been signed!" if pdf.signed?
    pdf
end

def parse_initialize #:nodoc:

:nodoc:
def parse_initialize #:nodoc:
    if @options[:force] == true
        @data.skip_until(/%PDF-/).nil?
        @data.pos = @data.pos - 5
    end
    pdf = PDF.new(self)
    info "...Reading header..."
    begin
        pdf.header = PDF::Header.parse(@data)
        @options[:callback].call(pdf.header)
    rescue InvalidHeaderError
        raise unless @options[:ignore_errors]
        warn "PDF header is invalid, ignoring..."
    end
    pdf
end