gpxkit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GpxKit
4
+ module XML
5
+ # The IO that Nokogiri reads from. It
6
+ #
7
+ # - counts bytes for Limits#max_bytes,
8
+ # - checks the prolog (everything before the root element) and refuses a
9
+ # DTD internal subset, which is where entities are declared,
10
+ # - skips whitespace before the XML declaration (SPECIFICATION.md,
11
+ # section 7), keeping a byte order mark,
12
+ # - keeps the line count and the end of the last line, so the Parser can
13
+ # tell a file that was cut off from one that is malformed (#tail_after).
14
+ #
15
+ # Nokogiri swallows exceptions raised in #read, so a refusal is kept in
16
+ # #error and #read reports the end of input; the Parser raises it.
17
+ class Source
18
+ CHUNK_SIZE = 64 * 1024
19
+ TAIL_WINDOW = 4096
20
+ CONTINUATION_BYTES = "\x80-\xBF".b.freeze
21
+ BOM = "\xEF\xBB\xBF".b.freeze
22
+
23
+ attr_reader :error, :lines
24
+
25
+ def initialize(io, limits:, source_label:, clock:)
26
+ @io = io
27
+ @limits = limits
28
+ @source_label = source_label
29
+ @clock = clock
30
+ @bytes = 0
31
+ @pending = nil
32
+ @eof = false
33
+ @error = nil
34
+ @lines = 1
35
+ @last_line = +"".b
36
+ @last_line_chars = 0
37
+ @delivered = false
38
+ @markup = false
39
+ end
40
+
41
+ # True once the underlying IO has reported its end and everything read
42
+ # has been handed on.
43
+ def eof?
44
+ @eof && (@pending.nil? || @pending.empty?)
45
+ end
46
+
47
+ # The text handed on after a position libxml2 reported (1-based line,
48
+ # 1-based column in characters), or nil when the position is not on
49
+ # the last line or too far from its end to tell.
50
+ def tail_after(line, column)
51
+ return "" if line > @lines
52
+ return nil if line < @lines
53
+
54
+ length = @last_line_chars - (column - 1)
55
+ return "" if length <= 0
56
+
57
+ window = @last_line.dup.force_encoding(Encoding::UTF_8).scrub
58
+ length > window.length ? nil : window[-length..]
59
+ end
60
+
61
+ # True once any byte has been handed to the XML parser.
62
+ def delivered?
63
+ @delivered
64
+ end
65
+
66
+ # True when the input handed on starts like XML: "<", after an optional
67
+ # byte order mark.
68
+ def markup?
69
+ @markup
70
+ end
71
+
72
+ def read(length, _buffer = nil)
73
+ return nil if @error
74
+ return refuse(time_limit_error) if @clock.exceeded?
75
+
76
+ @pending ||= prefix
77
+ return nil if @error
78
+
79
+ data = @pending.empty? ? read_raw(length) : take(length)
80
+ return nil if data.nil? || @error
81
+
82
+ unless @delivered
83
+ @delivered = true
84
+ @markup = data.byteslice(data.start_with?(BOM) ? BOM.bytesize : 0, 1) == "<"
85
+ end
86
+ count_lines(data)
87
+ data
88
+ end
89
+
90
+ private
91
+
92
+ def read_raw(length)
93
+ return nil if @eof
94
+
95
+ data = @io.read(length)
96
+ if data.nil? || data.empty?
97
+ @eof = true
98
+ return nil
99
+ end
100
+
101
+ @bytes += data.bytesize
102
+ max = @limits.max_bytes
103
+ if max && @bytes > max
104
+ return refuse(LimitExceededError.new("The input is over the #{max}-byte limit",
105
+ limit: :max_bytes, source: @source_label))
106
+ end
107
+
108
+ data.b
109
+ end
110
+
111
+ # Reads until the prolog is complete and returns what was read, minus
112
+ # whitespace before the XML declaration.
113
+ def prefix
114
+ buffer = +"".b
115
+ loop do
116
+ chunk = read_raw(CHUNK_SIZE)
117
+ return +"".b if @error
118
+
119
+ buffer << chunk if chunk
120
+ result = Prolog.scan(buffer, complete: chunk.nil?)
121
+ if result == :unsafe
122
+ refuse(UnsafeContentError.new("The document declares a DTD internal subset", source: @source_label))
123
+ return +"".b
124
+ end
125
+
126
+ max = @limits.max_text_bytes
127
+ if max && (result == :more ? buffer.bytesize : result) > max
128
+ refuse(LimitExceededError.new("The prolog is over the #{max}-byte text limit",
129
+ limit: :max_text_bytes, source: @source_label))
130
+ return +"".b
131
+ end
132
+ return without_leading_whitespace(buffer) unless result == :more
133
+ end
134
+ end
135
+
136
+ def without_leading_whitespace(buffer)
137
+ bom = buffer.start_with?(BOM)
138
+ body = bom ? buffer.byteslice(BOM.bytesize..) : buffer
139
+ body = body.sub(/\A[ \t\r\n]+/n, "")
140
+ bom ? BOM + body : body
141
+ end
142
+
143
+ def take(length)
144
+ data = @pending.byteslice(0, length)
145
+ @pending = @pending.byteslice(length..) || +"".b
146
+ data
147
+ end
148
+
149
+ def count_lines(data)
150
+ newline = data.rindex("\n")
151
+ if newline
152
+ @lines += data.count("\n")
153
+ part = data.byteslice((newline + 1)..)
154
+ @last_line = +"".b
155
+ @last_line_chars = 0
156
+ else
157
+ part = data
158
+ end
159
+ @last_line << part
160
+ @last_line = @last_line.byteslice(-TAIL_WINDOW..) if @last_line.bytesize > TAIL_WINDOW
161
+ @last_line_chars += part.bytesize - part.count(CONTINUATION_BYTES)
162
+ end
163
+
164
+ def time_limit_error
165
+ LimitExceededError.new("Reading took more than #{@limits.max_seconds} s",
166
+ limit: :max_seconds, source: @source_label)
167
+ end
168
+
169
+ def refuse(error)
170
+ @error ||= error
171
+ nil
172
+ end
173
+ end
174
+
175
+ # Scans the prolog: an optional byte order mark, whitespace, the XML
176
+ # declaration, processing instructions, comments and a document type
177
+ # declaration, up to the root element's start tag.
178
+ module Prolog
179
+ WHITESPACE = [0x20, 0x09, 0x0D, 0x0A].freeze
180
+ PI_START = "<?".b.freeze
181
+ PI_END = "?>".b.freeze
182
+ COMMENT_START = "<!--".b.freeze
183
+ COMMENT_END = "-->".b.freeze
184
+ DOCTYPE = "<!DOCTYPE".b.freeze
185
+ MARKUP_STARTS = [PI_START, COMMENT_START, DOCTYPE].freeze
186
+
187
+ module_function
188
+
189
+ # The byte position where the prolog ends (at the root element, or at
190
+ # anything that is not prolog), :more when more input is needed to
191
+ # decide, or :unsafe for a DTD internal subset. With complete: true
192
+ # there is no more input, and anything unfinished is left for the XML
193
+ # parser to report.
194
+ def scan(buffer, complete:)
195
+ utf16 = utf16(buffer)
196
+ return scan(utf16, complete: complete) if utf16
197
+
198
+ position = buffer.start_with?(Source::BOM) ? Source::BOM.bytesize : 0
199
+ loop do
200
+ position += 1 while position < buffer.bytesize && WHITESPACE.include?(buffer.getbyte(position))
201
+ rest = buffer.byteslice(position, DOCTYPE.bytesize) || "".b
202
+ return complete ? position : :more if rest.empty?
203
+ return position unless rest.getbyte(0) == 0x3C # "<"
204
+
205
+ finish =
206
+ if rest.start_with?(PI_START) then after(buffer, PI_END, position + 2)
207
+ elsif rest.start_with?(COMMENT_START) then after(buffer, COMMENT_END, position + 4)
208
+ elsif rest.start_with?(DOCTYPE) then after_doctype(buffer, position + DOCTYPE.bytesize)
209
+ elsif !complete && MARKUP_STARTS.any? { |start| start.start_with?(rest) } then nil
210
+ else return position
211
+ end
212
+ return :unsafe if finish == :unsafe
213
+ return complete ? position : :more if finish.nil?
214
+
215
+ position = finish
216
+ end
217
+ end
218
+
219
+ UTF16_LE_BOM = "\xFF\xFE".b.freeze
220
+ UTF16_BE_BOM = "\xFE\xFF".b.freeze
221
+ UTF32_LE_BOM = "\xFF\xFE\x00\x00".b.freeze
222
+
223
+ # A UTF-8 copy of a UTF-16 buffer (by its byte order mark, or by "<"
224
+ # followed or preceded by a zero byte), so a DTD in a UTF-16 document
225
+ # is found too; nil for any other buffer. libxml2 still reads the
226
+ # original bytes.
227
+ def utf16(buffer)
228
+ encoding =
229
+ if buffer.start_with?(UTF32_LE_BOM) then nil
230
+ elsif buffer.start_with?(UTF16_LE_BOM) || buffer.start_with?("<\x00".b) then Encoding::UTF_16LE
231
+ elsif buffer.start_with?(UTF16_BE_BOM) || buffer.start_with?("\x00<".b) then Encoding::UTF_16BE
232
+ end
233
+ return unless encoding
234
+
235
+ units = buffer.byteslice(0, buffer.bytesize & ~1)
236
+ units = units.byteslice(2..) if units.start_with?(UTF16_LE_BOM) || units.start_with?(UTF16_BE_BOM)
237
+ units.dup.force_encoding(encoding).encode(Encoding::UTF_8, invalid: :replace, undef: :replace).b
238
+ end
239
+
240
+ def after(buffer, terminator, from)
241
+ found = buffer.index(terminator, from)
242
+ found && found + terminator.bytesize
243
+ end
244
+
245
+ # The position after the declaration's ">", nil if it is not complete,
246
+ # or :unsafe when it has an internal subset ("[" outside quotes).
247
+ def after_doctype(buffer, from)
248
+ quote = nil
249
+ (from...buffer.bytesize).each do |position|
250
+ byte = buffer.getbyte(position)
251
+ if quote
252
+ quote = nil if byte == quote
253
+ elsif byte == 0x22 || byte == 0x27 # " or '
254
+ quote = byte
255
+ elsif byte == 0x5B # [
256
+ return :unsafe
257
+ elsif byte == 0x3E # >
258
+ return position + 1
259
+ end
260
+ end
261
+ nil
262
+ end
263
+ end
264
+ end
265
+ end
data/lib/gpxkit.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ require_relative "gpxkit/version"
6
+ require_relative "gpxkit/error"
7
+ require_relative "gpxkit/limits"
8
+ require_relative "gpxkit/xml/clock"
9
+ require_relative "gpxkit/xml/element"
10
+ require_relative "gpxkit/xml/source"
11
+ require_relative "gpxkit/xml/parser"
12
+ require_relative "gpxkit/types"
13
+ require_relative "gpxkit/schema"
14
+ require_relative "gpxkit/field"
15
+ require_relative "gpxkit/message"
16
+ require_relative "gpxkit/message_builder"
17
+ require_relative "gpxkit/reader"
18
+ require_relative "gpxkit/activity"
19
+
20
+ # A read-only, streaming parser for GPX 1.1 and 1.0 files.
21
+ # See SPECIFICATION.md for the API and what each milestone delivers.
22
+ module GpxKit
23
+ NAMESPACE_1_1 = "http://www.topografix.com/GPX/1/1"
24
+ NAMESPACE_1_0 = "http://www.topografix.com/GPX/1/0"
25
+ NAMESPACES = { NAMESPACE_1_1 => "1.1", NAMESPACE_1_0 => "1.0" }.freeze
26
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gpxkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - The gpxkit contributors
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-11-23 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: nokogiri
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.17'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.17'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '7.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '5.0'
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '7.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '13.0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '13.0'
60
+ description: Reads GPX 1.1 and 1.0 files safely and lazily, keeping every value as
61
+ written, with an activity view shared with fitkit and tcxkit.
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - CHANGELOG.md
67
+ - LICENSE
68
+ - README.md
69
+ - SPECIFICATION.md
70
+ - lib/gpxkit.rb
71
+ - lib/gpxkit/activity.rb
72
+ - lib/gpxkit/error.rb
73
+ - lib/gpxkit/field.rb
74
+ - lib/gpxkit/limits.rb
75
+ - lib/gpxkit/message.rb
76
+ - lib/gpxkit/message_builder.rb
77
+ - lib/gpxkit/reader.rb
78
+ - lib/gpxkit/schema.rb
79
+ - lib/gpxkit/types.rb
80
+ - lib/gpxkit/version.rb
81
+ - lib/gpxkit/xml/clock.rb
82
+ - lib/gpxkit/xml/element.rb
83
+ - lib/gpxkit/xml/parser.rb
84
+ - lib/gpxkit/xml/source.rb
85
+ homepage: https://github.com/scarhand/gpxkit
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ source_code_uri: https://github.com/scarhand/gpxkit
90
+ changelog_uri: https://github.com/scarhand/gpxkit/blob/main/CHANGELOG.md
91
+ bug_tracker_uri: https://github.com/scarhand/gpxkit/issues
92
+ rubygems_mfa_required: 'true'
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '3.1'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.6.2
108
+ specification_version: 4
109
+ summary: A read-only, streaming Ruby parser for GPX files
110
+ test_files: []