File: parser.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (281 lines) | stat: -rw-r--r-- 9,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# coding: utf-8
# typed: strict
# frozen_string_literal: true

################################################################################
#
# Copyright (C) 2006 Peter J Jones (pjones@pmade.com)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
################################################################################

class PDF::Reader
  ################################################################################
  # An internal PDF::Reader class that reads objects from the PDF file and converts
  # them into useable ruby objects (hash's, arrays, true, false, etc)
  class Parser

    TOKEN_STRATEGY = proc { |parser, token| Token.new(token) } #: Proc

    STRATEGIES = {
      "/"  => proc { |parser, token| parser.send(:pdf_name) },
      "<<" => proc { |parser, token| parser.send(:dictionary) },
      "["  => proc { |parser, token| parser.send(:array) },
      "("  => proc { |parser, token| parser.send(:string) },
      "<"  => proc { |parser, token| parser.send(:hex_string) },

      nil     => proc { nil },
      "true"  => proc { true },
      "false" => proc { false },
      "null"  => proc { nil },

      "obj"       => TOKEN_STRATEGY,
      "endobj"    => TOKEN_STRATEGY,
      "stream"    => TOKEN_STRATEGY,
      "endstream" => TOKEN_STRATEGY,
      ">>"        => TOKEN_STRATEGY,
      "]"         => TOKEN_STRATEGY,
      ">"         => TOKEN_STRATEGY,
      ")"         => TOKEN_STRATEGY
    } #: Hash[String?, Proc]

    ################################################################################
    # Create a new parser around a PDF::Reader::Buffer object
    #
    # buffer - a PDF::Reader::Buffer object that contains PDF data
    # objects  - a PDF::Reader::ObjectHash object that can return objects from the PDF file
    #: (PDF::Reader::Buffer, ?PDF::Reader::ObjectHash?) -> void
    def initialize(buffer, objects=nil)
      @buffer = buffer
      @objects  = objects
    end
    ################################################################################
    # Reads the next token from the underlying buffer and convets it to an appropriate
    # object
    #
    # operators - a hash of supported operators to read from the underlying buffer.
    #: (?Hash[String | PDF::Reader::Token, Symbol]) -> (
    #|   PDF::Reader::Reference |
    #|   PDF::Reader::Token |
    #|   Numeric |
    #|   String |
    #|   Symbol |
    #|   Array[untyped] |
    #|   Hash[untyped, untyped] |
    #|   nil
    #| )
    def parse_token(operators={})
      token = @buffer.token

      if token.nil?
        nil
      elsif token.is_a?(String) && STRATEGIES.has_key?(token)
        proc = STRATEGIES[token]
        proc.call(self, token) if proc
      elsif token.is_a? PDF::Reader::Reference
        token
      elsif operators.has_key? token
        Token.new(token)
      elsif token.frozen?
        token
      elsif token =~ /\d*\.\d/
        token.to_f
      else
        token.to_i
      end
    end
    ################################################################################
    # Reads an entire PDF object from the buffer and returns it as a Ruby String.
    # If the object is a content stream, returns both the stream and the dictionary
    # that describes it
    #
    # id  - the object ID to return
    # gen - the object revision number to return
    #: (Integer, Integer) -> (
    #|   PDF::Reader::Reference |
    #|   PDF::Reader::Token |
    #|   PDF::Reader::Stream |
    #|   Numeric |
    #|   String |
    #|   Symbol |
    #|   Array[untyped] |
    #|   Hash[untyped, untyped] |
    #|   nil
    #| )
    def object(id, gen)
      idCheck = parse_token

      # Sometimes the xref table is corrupt and points to an offset slightly too early in the file.
      # check the next token, maybe we can find the start of the object we're looking for
      if idCheck != id
        Error.assert_equal(parse_token, id)
      end
      Error.assert_equal(parse_token, gen)
      Error.str_assert(parse_token, "obj")

      obj = parse_token
      post_obj = parse_token

      if obj.is_a?(Hash) && post_obj == "stream"
        stream(obj)
      else
        obj
      end
    end

    private

    ################################################################################
    # reads a PDF dict from the buffer and converts it to a Ruby Hash.
    #: () -> Hash[Symbol, untyped]
    def dictionary
      dict = {}

      loop do
        key = parse_token
        break if key.kind_of?(Token) and key == ">>"
        raise MalformedPDFError, "unterminated dict" if @buffer.empty?
        PDF::Reader::Error.validate_type_as_malformed(key, "Dictionary key", Symbol)

        value = parse_token
        value.kind_of?(Token) and Error.str_assert_not(value, ">>")
        dict[key] = value
      end

      dict
    end
    ################################################################################
    # reads a PDF name from the buffer and converts it to a Ruby Symbol
    #: () -> Symbol
    def pdf_name
      tok = @buffer.token

      if tok.is_a?(String)
        tok = tok.dup.gsub(/#([A-Fa-f0-9]{2})/) do |match|
          res = match[1, 2]
          res ? res.hex.chr : ""
        end
        tok.to_sym
      elsif tok.is_a?(PDF::Reader::Reference)
        raise MalformedPDFError, "unexpected reference"
      else
        raise MalformedPDFError, "unexpected nil PDF Name"
      end
    end
    ################################################################################
    # reads a PDF array from the buffer and converts it to a Ruby Array.
    #: () -> Array[untyped]
    def array
      a = []

      loop do
        item = parse_token
        break if item.kind_of?(Token) and item == "]"
        raise MalformedPDFError, "unterminated array" if @buffer.empty?
        a << item
      end

      a
    end
    ################################################################################
    # Reads a PDF hex string from the buffer and converts it to a Ruby String
    #: () -> String
    def hex_string
      str = "".dup

      loop do
        token = @buffer.token
        break if token == ">"
        raise MalformedPDFError, "unterminated hex string" if @buffer.empty?
        str << token
      end

      # add a missing digit if required, as required by the spec
      str << "0" unless str.size % 2 == 0
      [str].pack('H*')
    end
    ################################################################################
    # Reads a PDF String from the buffer and converts it to a Ruby String
    #: () -> String
    def string
      str = @buffer.token
      raise MalformedPDFError, "unexpected reference" if str.is_a?(PDF::Reader::Reference)
      raise MalformedPDFError, "unexpected nil PDF String" if str.nil?
      return "".dup.force_encoding("binary") if str == ")"
      Error.assert_equal(parse_token, ")")

      str.gsub!(/\\(\r\n|[nrtbf()\\\n\r]|([0-7]{1,3}))?|\r\n?/m) do |match|
        if $2.nil? # not octal digits
          MAPPING[match] || "".dup
        else # must be octal digits
          ($2.oct & 0xff).chr # ignore high level overflow
        end
      end
      str.force_encoding("binary")
    end

    MAPPING = {
      "\r"   => "\n",
      "\r\n" => "\n",
      "\\n"  => "\n",
      "\\r"  => "\r",
      "\\t"  => "\t",
      "\\b"  => "\b",
      "\\f"  => "\f",
      "\\("  => "(",
      "\\)"  => ")",
      "\\\\" => "\\",
      "\\\n" => "",
      "\\\r" => "",
      "\\\r\n" => "",
    } #: Hash[String, String]

    ################################################################################
    # Decodes the contents of a PDF Stream and returns it as a Ruby String.
    #: (Hash[Symbol, untyped]) -> PDF::Reader::Stream
    def stream(dict)
      raise MalformedPDFError, "PDF malformed, missing stream length" unless dict.has_key?(:Length)
      if @objects
        length = @objects.deref_integer(dict[:Length])
        if dict[:Filter]
          dict[:Filter] = @objects.deref_name_or_array(dict[:Filter])
        end
      else
        length = dict[:Length] || 0
      end

      PDF::Reader::Error.validate_type_as_malformed(length, "length", Numeric)

      data = @buffer.read(length, :skip_eol => true)

      Error.str_assert(parse_token, "endstream")

      # We used to assert that the stream had the correct closing token, but it doesn't *really*
      # matter if it's missing, and other readers seems to handle its absence just fine
      # Error.str_assert(parse_token, "endobj")

      PDF::Reader::Stream.new(dict, data || "")
    end
    ################################################################################
  end
  ################################################################################
end
################################################################################