Class: Origami::PPKLite

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/extensions/ppklite.rb,
lib/origami/parsers/ppklite.rb

Overview

Class representing an Adobe Reader certificate store.

Defined Under Namespace

Modules: Descriptor Classes: AddressList, Catalog, Certificate, Error, Header, PPK, Parser, Revision, User, UserList

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser = nil) ⇒ PPKLite

:nodoc:



205
206
207
208
209
210
211
212
# File 'lib/origami/extensions/ppklite.rb', line 205

def initialize(parser = nil) #:nodoc:
    @header = PPKLite::Header.new
    @revisions = [ Revision.new(self) ]
    @revisions.first.trailer = Trailer.new
    @parser = parser

    init if parser.nil?
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



203
204
205
# File 'lib/origami/extensions/ppklite.rb', line 203

def header
  @header
end

#revisionsObject

Returns the value of attribute revisions.



203
204
205
# File 'lib/origami/extensions/ppklite.rb', line 203

def revisions
  @revisions
end

Class Method Details

.read(path, options = {}) ⇒ Object



45
46
47
48
49
# File 'lib/origami/extensions/ppklite.rb', line 45

def self.read(path, options = {})
    path = File.expand_path(path) if path.is_a?(::String)

    PPKLite::Parser.new(options).parse(path)
end

Instance Method Details

#<<(object) ⇒ Object Also known as: insert



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/origami/extensions/ppklite.rb', line 244

def <<(object)
    object.set_indirect(true)
    object.set_document(self)

    if object.no.zero?
        maxno = 1
        maxno = maxno.succ while get_object(maxno)

        object.generation = 0
        object.no = maxno
    end

    @revisions.first.body[object.reference] = object

    object.reference
end

#add_certificate(certfile, attributes, viewable: false, editable: false) ⇒ Object

Add a certificate into the address book



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/origami/extensions/ppklite.rb', line 346

def add_certificate(certfile, attributes, viewable: false, editable: false)
    if certfile.is_a?(OpenSSL::X509::Certificate)
        x509 = certfile
    else
        x509 = OpenSSL::X509::Certificate.new(certfile)
    end

    address_book = get_address_book

    cert = Certificate.new
    cert.Cert = x509.to_der
    cert.ID = address_book.NextID
    address_book.NextID += 1

    cert.Trust = attributes
    cert.Viewable = viewable
    cert.Editable = editable

    address_book.Entries.push(self << cert)
end

#cast_object(reference, type) ⇒ Object

:nodoc:



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/origami/extensions/ppklite.rb', line 219

def cast_object(reference, type) #:nodoc:
    @revisions.each do |rev|
        if rev.body.include?(reference) and type < rev.body[reference].class
            rev.body[reference] = rev.body[reference].cast_to(type, @parser)

            rev.body[reference]
        else
            nil
        end
    end
end

#CatalogObject



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

def Catalog
    get_object(@revisions.first.trailer.Root)
end

#certificatesObject



339
340
341
# File 'lib/origami/extensions/ppklite.rb', line 339

def certificates
    self.each_certificate.to_a
end

#each_certificate(&b) ⇒ Object



331
332
333
# File 'lib/origami/extensions/ppklite.rb', line 331

def each_certificate(&b)
    each_entry(Descriptor::CERTIFICATE, &b)
end

#each_user(&b) ⇒ Object



319
320
321
# File 'lib/origami/extensions/ppklite.rb', line 319

def each_user(&b)
    each_entry(Descriptor::USER, &b)
end

#get_certificate(id) ⇒ Object



335
336
337
# File 'lib/origami/extensions/ppklite.rb', line 335

def get_certificate(id)
    self.each_certificate.find {|cert| cert.ID == id }
end

#get_object(no, generation = 0) ⇒ Object

:nodoc:



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/origami/extensions/ppklite.rb', line 231

def get_object(no, generation = 0) #:nodoc:
    case no
    when Reference
      target = no
    when ::Integer
      target = Reference.new(no, generation)
    when Origami::Object
      return no
    end

    @revisions.first.body[target]
end

#get_user(id) ⇒ Object



323
324
325
# File 'lib/origami/extensions/ppklite.rb', line 323

def get_user(id)
    self.each_user.find {|user| user.ID == id }
end

#indirect_objectsObject Also known as: root_objects



214
215
216
# File 'lib/origami/extensions/ppklite.rb', line 214

def indirect_objects
    @revisions.inject([]) do |set, rev| set.concat(rev.objects) end
end

#save(path) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/origami/extensions/ppklite.rb', line 266

def save(path)
    bin = "".b
    bin << @header.to_s

    lastno, brange = 0, 0

    xrefs = [ XRef.new(0, XRef::FIRSTFREE, XRef::FREE) ]
    xrefsection = XRef::Section.new

    @revisions.first.body.values.sort.each { |obj|
        if (obj.no - lastno).abs > 1
            xrefsection << XRef::Subsection.new(brange, xrefs)
            brange = obj.no
            xrefs.clear
        end

        xrefs << XRef.new(bin.size, obj.generation, XRef::USED)
        lastno = obj.no

        obj.pre_build

        bin << obj.to_s

        obj.post_build
    }

    xrefsection << XRef::Subsection.new(brange, xrefs)

    @xreftable = xrefsection
    @trailer ||= Trailer.new
    @trailer.Size = @revisions.first.body.size + 1
    @trailer.startxref = bin.size

    bin << @xreftable.to_s
    bin << @trailer.to_s

    if path.respond_to?(:write)
        io = path
    else
        path = File.expand_path(path)
        io = File.open(path, "wb", encoding: 'binary')
        close = true
    end

    begin
        io.write(bin)
    ensure
        io.close if close
    end

    self
end

#usersObject



327
328
329
# File 'lib/origami/extensions/ppklite.rb', line 327

def users
    self.each_user.to_a
end