xlsxrb 0.2.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +88 -0
- data/README.md +25 -3
- data/lib/xlsxrb/dsl_helpers.rb +65 -1
- data/lib/xlsxrb/elements/styles.rb +15 -6
- data/lib/xlsxrb/elements/worksheet.rb +2 -2
- data/lib/xlsxrb/number_formatter.rb +26 -8
- data/lib/xlsxrb/ooxml/cfb.rb +11 -3
- data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
- data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
- data/lib/xlsxrb/ooxml/crypto.rb +3 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
- data/lib/xlsxrb/ooxml/utils.rb +13 -0
- data/lib/xlsxrb/ooxml/workbook_writer.rb +43 -21
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +338 -259
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +109 -102
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
- data/lib/xlsxrb/ooxml/writer.rb +167 -160
- data/lib/xlsxrb/ooxml/xml_builder.rb +35 -7
- data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
- data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
- data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
- data/lib/xlsxrb/stream_row.rb +2 -2
- data/lib/xlsxrb/stream_sheet.rb +26 -11
- data/lib/xlsxrb/stream_writer.rb +212 -86
- data/lib/xlsxrb/style_builder.rb +339 -0
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +239 -88
- data/lib/xlsxrb/worksheet_builder.rb +847 -139
- data/lib/xlsxrb.rb +87 -21
- data/sig/generated/xlsxrb/dsl_helpers.rbs +37 -0
- data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
- data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/utils.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +53 -0
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +3 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +19 -2
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
- data/sig/generated/xlsxrb/stream_row.rbs +2 -2
- data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
- data/sig/generated/xlsxrb/stream_writer.rbs +46 -6
- data/sig/generated/xlsxrb/style_builder.rbs +261 -0
- data/sig/generated/xlsxrb/workbook_builder.rbs +42 -9
- data/sig/generated/xlsxrb/worksheet_builder.rbs +618 -4
- data/sig/generated/xlsxrb.rbs +53 -8
- metadata +9 -3
|
@@ -78,6 +78,10 @@ module Xlsxrb
|
|
|
78
78
|
rph_end_len = rph_end_tag.bytesize
|
|
79
79
|
has_rph = xml.include?(rph_start_pattern)
|
|
80
80
|
|
|
81
|
+
# Position of the next <t at or after the last position searched from. Searching
|
|
82
|
+
# again from every <si> that has no <t> would scan the rest of the part each time.
|
|
83
|
+
t_hint = nil
|
|
84
|
+
|
|
81
85
|
pos = sst_open_end + 1
|
|
82
86
|
while pos < sst_end
|
|
83
87
|
si_start = xml.index(si_start_pattern, pos)
|
|
@@ -97,15 +101,15 @@ module Xlsxrb
|
|
|
97
101
|
break unless si_end
|
|
98
102
|
|
|
99
103
|
# Fast path: check for simple single <t>...</t> inside <si>
|
|
100
|
-
first_t = xml
|
|
101
|
-
if first_t
|
|
104
|
+
first_t = t_hint = next_index(xml, t_start_pattern, si_open_end + 1, t_hint)
|
|
105
|
+
if first_t < si_end
|
|
102
106
|
tb = xml.getbyte(first_t + t_start_len)
|
|
103
107
|
if TAG_DELIM_BYTES.include?(tb)
|
|
104
108
|
t_open_end = xml.index(">", first_t + t_start_len)
|
|
105
109
|
if t_open_end && t_open_end < si_end
|
|
106
110
|
if xml.getbyte(t_open_end - 1) == 47 # <t/>
|
|
107
|
-
next_t = xml
|
|
108
|
-
str = if next_t
|
|
111
|
+
next_t = t_hint = next_index(xml, t_start_pattern, t_open_end + 1, t_hint)
|
|
112
|
+
str = if next_t < si_end
|
|
109
113
|
extract_multi_t(xml, si_open_end + 1, si_end, t_start_pattern, t_start_len, t_end_tag, t_end_len, rph_start_pattern, rph_end_tag, rph_end_len, has_rph: has_rph)
|
|
110
114
|
else
|
|
111
115
|
""
|
|
@@ -113,8 +117,8 @@ module Xlsxrb
|
|
|
113
117
|
else
|
|
114
118
|
t_end = xml.index(t_end_tag, t_open_end + 1)
|
|
115
119
|
if t_end && t_end <= si_end
|
|
116
|
-
next_t = xml
|
|
117
|
-
if next_t
|
|
120
|
+
next_t = t_hint = next_index(xml, t_start_pattern, t_end + t_end_len, t_hint)
|
|
121
|
+
if next_t < si_end
|
|
118
122
|
# Multiple <t> tags (rich text runs)
|
|
119
123
|
str = extract_multi_t(xml, si_open_end + 1, si_end, t_start_pattern, t_start_len, t_end_tag, t_end_len, rph_start_pattern, rph_end_tag, rph_end_len, has_rph: has_rph)
|
|
120
124
|
else
|
|
@@ -142,9 +146,23 @@ module Xlsxrb
|
|
|
142
146
|
|
|
143
147
|
private_class_method :fast_scan
|
|
144
148
|
|
|
149
|
+
# Returns the index of the first +pattern+ at or after +from+, or the size of +xml+ when
|
|
150
|
+
# there is none. +cached+ is the result of the previous call for the same pattern with a
|
|
151
|
+
# smaller +from+; while it is not behind +from+ it is still the answer, so a caller that
|
|
152
|
+
# moves forward searches each stretch of +xml+ once.
|
|
153
|
+
#: (String xml, String pattern, Integer from, Integer? cached) -> Integer
|
|
154
|
+
def self.next_index(xml, pattern, from, cached)
|
|
155
|
+
return cached if cached && cached >= from
|
|
156
|
+
|
|
157
|
+
xml.index(pattern, from) || xml.bytesize
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
private_class_method :next_index
|
|
161
|
+
|
|
145
162
|
def self.extract_multi_t(xml, from, to, t_start_pattern, t_start_len, t_end_tag, t_end_len, rph_start_pattern, rph_end_tag, rph_end_len, has_rph: false)
|
|
146
163
|
buf = +""
|
|
147
164
|
pos = from
|
|
165
|
+
rph_hint = nil
|
|
148
166
|
while pos < to
|
|
149
167
|
t_start = xml.index(t_start_pattern, pos)
|
|
150
168
|
break unless t_start && t_start < to
|
|
@@ -160,8 +178,8 @@ module Xlsxrb
|
|
|
160
178
|
|
|
161
179
|
if has_rph
|
|
162
180
|
# Phonetic runs (<rPh>) hold furigana readings and are not part of the string
|
|
163
|
-
rph_start = xml
|
|
164
|
-
if rph_start
|
|
181
|
+
rph_start = rph_hint = next_index(xml, rph_start_pattern, pos, rph_hint)
|
|
182
|
+
if rph_start < t_start
|
|
165
183
|
rph_end = xml.index(rph_end_tag, rph_start)
|
|
166
184
|
break unless rph_end && rph_end < to
|
|
167
185
|
|
data/lib/xlsxrb/ooxml/utils.rb
CHANGED
|
@@ -66,7 +66,20 @@ module Xlsxrb
|
|
|
66
66
|
# Default date-time format code used by Writer for Time cells.
|
|
67
67
|
DEFAULT_DATETIME_FORMAT = "yyyy\\-mm\\-dd\\ hh:mm:ss"
|
|
68
68
|
|
|
69
|
+
# First bytes of the four-byte UTF-8 sequences, which encode the characters outside
|
|
70
|
+
# the Basic Multilingual Plane: two UTF-16 code units each.
|
|
71
|
+
SUPPLEMENTARY_LEAD_BYTES = [0xF0, 0x2D, 0xF4].pack("C*").freeze # "\xF0-\xF4", a range for String#count
|
|
72
|
+
private_constant :SUPPLEMENTARY_LEAD_BYTES
|
|
73
|
+
|
|
69
74
|
class << self
|
|
75
|
+
# The length of UTF-8 text in UTF-16 code units, the unit Excel counts its text
|
|
76
|
+
# limits in (ADR 0011): one per character, and one more for each character outside
|
|
77
|
+
# the Basic Multilingual Plane, such as an emoji.
|
|
78
|
+
#: (String text) -> Integer
|
|
79
|
+
def utf16_length(text)
|
|
80
|
+
text.length + text.b.count(SUPPLEMENTARY_LEAD_BYTES)
|
|
81
|
+
end
|
|
82
|
+
|
|
70
83
|
# Checks whether a serial number is a valid Excel date within the supported range.
|
|
71
84
|
#: (untyped serial, ?date1904: bool) -> bool
|
|
72
85
|
def valid_excel_date?(serial, date1904: false)
|
|
@@ -218,19 +218,16 @@ module Xlsxrb
|
|
|
218
218
|
else
|
|
219
219
|
(bg_img.respond_to?(:data) ? bg_img.data : bg_img)
|
|
220
220
|
end
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
else
|
|
230
|
-
"png"
|
|
231
|
-
end
|
|
232
|
-
end
|
|
221
|
+
# The data is the image itself and is never read as a path (ADR 0012).
|
|
222
|
+
bg_ext = if bg_img.is_a?(Hash)
|
|
223
|
+
(bg_img[:ext] || (bg_img[:filename] && File.extname(bg_img[:filename]).delete_prefix(".")) || "png").to_s.downcase
|
|
224
|
+
elsif bg_img.respond_to?(:filename)
|
|
225
|
+
File.extname(bg_img.filename).delete_prefix(".").downcase
|
|
226
|
+
else
|
|
227
|
+
"png"
|
|
228
|
+
end
|
|
233
229
|
if bg_data && !bg_data.empty?
|
|
230
|
+
bg_ext = media_extension(bg_ext)
|
|
234
231
|
p_rid_num = sheet_rels.size + 1
|
|
235
232
|
picture_rid = "rId#{p_rid_num}"
|
|
236
233
|
sheet_rels << { id: picture_rid, type: "#{DOC_REL}/image", target: "../media/sheet#{idx + 1}_bg.#{bg_ext}" }
|
|
@@ -293,7 +290,7 @@ module Xlsxrb
|
|
|
293
290
|
sheet_images.each do |img|
|
|
294
291
|
# One media file per image: a sheet can hold several.
|
|
295
292
|
media_idx += 1
|
|
296
|
-
media_name = "image#{media_idx}.#{img[:ext]
|
|
293
|
+
media_name = "image#{media_idx}.#{media_extension(img[:ext])}"
|
|
297
294
|
zip.add_binary_entry("xl/media/#{media_name}", img[:file_data])
|
|
298
295
|
drawing_rels_data << { type: :image, target: "../media/#{media_name}" }
|
|
299
296
|
drawing_parts << { kind: :pic, img: img, rid_index: drawing_rels_data.size }
|
|
@@ -1250,6 +1247,20 @@ module Xlsxrb
|
|
|
1250
1247
|
ESCAPE_MAP = { "&" => "&", "<" => "<", ">" => ">", '"' => """, "'" => "'" }.freeze
|
|
1251
1248
|
private_constant :INVALID_XML_CHARS_RE, :ESCAPE_RE, :ESCAPE_MAP
|
|
1252
1249
|
|
|
1250
|
+
MEDIA_EXTENSION_RE = /\A[A-Za-z0-9]{1,10}\z/
|
|
1251
|
+
private_constant :MEDIA_EXTENSION_RE
|
|
1252
|
+
|
|
1253
|
+
# Returns the extension for a media part name. It becomes part of a ZIP entry name, a
|
|
1254
|
+
# relationship target, and a content type, so anything but letters and digits is
|
|
1255
|
+
# rejected.
|
|
1256
|
+
#: (untyped ext) -> String
|
|
1257
|
+
def media_extension(ext)
|
|
1258
|
+
str = (ext || "png").to_s
|
|
1259
|
+
raise ArgumentError, "Invalid image extension: #{str.inspect}. Use letters and digits only, such as \"png\"." unless str.match?(MEDIA_EXTENSION_RE)
|
|
1260
|
+
|
|
1261
|
+
str
|
|
1262
|
+
end
|
|
1263
|
+
|
|
1253
1264
|
def escape_xml(value)
|
|
1254
1265
|
str = value.to_s
|
|
1255
1266
|
str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
|
|
@@ -1257,6 +1268,17 @@ module Xlsxrb
|
|
|
1257
1268
|
str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
|
|
1258
1269
|
end
|
|
1259
1270
|
|
|
1271
|
+
# Escapes cell text: encodes it as ST_Xstring (XmlBuilder.encode_xstring), so that
|
|
1272
|
+
# text that looks like _xHHHH_ and characters XML 1.0 cannot hold are read back as
|
|
1273
|
+
# written, then escapes the XML special characters.
|
|
1274
|
+
def escape_text(value)
|
|
1275
|
+
str = value.to_s
|
|
1276
|
+
str = str.dup.force_encoding(Encoding::UTF_8) if str.encoding == Encoding::BINARY
|
|
1277
|
+
# Most text has neither; the checks cost less than encode_xstring's own.
|
|
1278
|
+
str = XmlBuilder.encode_xstring(str) if str.include?("_x") || str.match?(INVALID_XML_CHARS_RE)
|
|
1279
|
+
str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
|
|
1280
|
+
end
|
|
1281
|
+
|
|
1260
1282
|
def write_shared_strings_xml(zip)
|
|
1261
1283
|
return if @shared_strings.empty?
|
|
1262
1284
|
|
|
@@ -1282,7 +1304,7 @@ module Xlsxrb
|
|
|
1282
1304
|
buf << "<u/>"
|
|
1283
1305
|
else
|
|
1284
1306
|
mapped_u = StyleBuilder::UNDERLINE_MAP[font[:underline].to_sym] || font[:underline]
|
|
1285
|
-
buf << '<u val="' << mapped_u
|
|
1307
|
+
buf << '<u val="' << escape_xml(mapped_u) << '"/>'
|
|
1286
1308
|
end
|
|
1287
1309
|
end
|
|
1288
1310
|
va_val = font[:vert_align] || font[:vertical_align]
|
|
@@ -1290,27 +1312,27 @@ module Xlsxrb
|
|
|
1290
1312
|
va_val ||= "subscript" if font[:subscript]
|
|
1291
1313
|
if va_val
|
|
1292
1314
|
mapped_va = StyleBuilder::VERT_ALIGN_MAP[va_val.to_sym] || va_val.to_s
|
|
1293
|
-
buf << '<vertAlign val="' << mapped_va << '"/>'
|
|
1315
|
+
buf << '<vertAlign val="' << escape_xml(mapped_va) << '"/>'
|
|
1294
1316
|
end
|
|
1295
1317
|
font_sz = font[:sz] || font[:size]
|
|
1296
|
-
buf << '<sz val="' << font_sz
|
|
1318
|
+
buf << '<sz val="' << escape_xml(font_sz) << '"/>' if font_sz
|
|
1297
1319
|
color_attrs = color_tag_attrs(font[:color] || font)
|
|
1298
1320
|
if color_attrs
|
|
1299
|
-
attrs_str = color_attrs.map { |k, v| %(#{k}="#{v}") }.join(" ")
|
|
1321
|
+
attrs_str = color_attrs.map { |k, v| %(#{k}="#{escape_xml(v)}") }.join(" ")
|
|
1300
1322
|
buf << "<color #{attrs_str}/>"
|
|
1301
1323
|
end
|
|
1302
1324
|
buf << '<rFont val="' << escape_xml(font[:name]) << '"/>' if font[:name]
|
|
1303
|
-
buf << '<family val="' << font[:family]
|
|
1304
|
-
buf << '<scheme val="' << font[:scheme]
|
|
1325
|
+
buf << '<family val="' << escape_xml(font[:family]) << '"/>' if font[:family]
|
|
1326
|
+
buf << '<scheme val="' << escape_xml(font[:scheme]) << '"/>' if font[:scheme]
|
|
1305
1327
|
buf << "</rPr><t>"
|
|
1306
1328
|
else
|
|
1307
1329
|
buf << "<r><t>"
|
|
1308
1330
|
end
|
|
1309
|
-
buf <<
|
|
1331
|
+
buf << escape_text(text.to_s) << "</t></r>"
|
|
1310
1332
|
end
|
|
1311
1333
|
buf << "</si>"
|
|
1312
1334
|
else
|
|
1313
|
-
buf << "<si><t>" <<
|
|
1335
|
+
buf << "<si><t>" << escape_text(str.to_s) << "</t></si>"
|
|
1314
1336
|
end
|
|
1315
1337
|
|
|
1316
1338
|
if buf.bytesize >= 32_768
|
|
@@ -145,6 +145,13 @@ module Xlsxrb
|
|
|
145
145
|
hl_tag_end = xml.index(">", hl_start + 1)
|
|
146
146
|
break unless hl_tag_end
|
|
147
147
|
|
|
148
|
+
# "hyperlink" in text or a comment rather than in a tag: the tag found before it has
|
|
149
|
+
# already ended. Continue after the word, so that the position always advances.
|
|
150
|
+
if hl_tag_end < hl_start_term
|
|
151
|
+
pos = hl_start_term + 9
|
|
152
|
+
next
|
|
153
|
+
end
|
|
154
|
+
|
|
148
155
|
hl_tag = xml.byteslice(hl_start, hl_tag_end - hl_start)
|
|
149
156
|
ref = tag_attr(hl_tag, ' ref="')
|
|
150
157
|
rid = tag_attr(hl_tag, ' r:id="') || tag_attr(hl_tag, ' id="') || hl_tag[/(?:[a-zA-Z0-9_]+:)?id="([^"]+)"/, 1]
|
|
@@ -547,7 +554,7 @@ module Xlsxrb
|
|
|
547
554
|
def self.resolve_cell_value(type, raw_value, is_xml, shared_strings)
|
|
548
555
|
case type
|
|
549
556
|
when "s"
|
|
550
|
-
raw_value ? (shared_strings
|
|
557
|
+
raw_value ? shared_string_at(shared_strings, raw_value) : nil
|
|
551
558
|
when "b"
|
|
552
559
|
if raw_value
|
|
553
560
|
v = strip_ws(raw_value)
|
|
@@ -568,7 +575,7 @@ module Xlsxrb
|
|
|
568
575
|
text = strip_ws(raw_value)
|
|
569
576
|
return nil if text.empty?
|
|
570
577
|
|
|
571
|
-
if text.match?(STRICT_NUMERIC_RE)
|
|
578
|
+
if text.bytesize <= MAX_NUMERIC_TEXT_BYTES && text.match?(STRICT_NUMERIC_RE)
|
|
572
579
|
Integer(text, 10, exception: false) ||
|
|
573
580
|
Float(text, exception: false) ||
|
|
574
581
|
normalize_text(raw_value)
|
|
@@ -594,10 +601,30 @@ module Xlsxrb
|
|
|
594
601
|
text = strip_ws(raw)
|
|
595
602
|
return nil if text.empty?
|
|
596
603
|
|
|
597
|
-
raise(ParseError, "invalid numeric cell value: #{text.inspect}") unless text.match?(STRICT_NUMERIC_RE)
|
|
604
|
+
raise(ParseError, "invalid numeric cell value: #{text[0, 40].inspect}") unless text.bytesize <= MAX_NUMERIC_TEXT_BYTES && text.match?(STRICT_NUMERIC_RE)
|
|
598
605
|
|
|
599
606
|
Integer(text, 10, exception: false) || Float(text, exception: false) ||
|
|
600
|
-
raise(ParseError, "invalid numeric cell value: #{text.inspect}")
|
|
607
|
+
raise(ParseError, "invalid numeric cell value: #{text[0, 40].inspect}")
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
# Excel's last row. A larger row number is not a worksheet row, and an unbounded one
|
|
611
|
+
# would be built digit by digit into an arbitrarily large Integer.
|
|
612
|
+
MAX_ROW_NUMBER = 1_048_576
|
|
613
|
+
|
|
614
|
+
# Longer text is not read as a number. Excel writes numbers in at most 25 characters;
|
|
615
|
+
# converting millions of digits to an Integer takes time that grows faster than their
|
|
616
|
+
# number, and matching them takes memory in proportion.
|
|
617
|
+
MAX_NUMERIC_TEXT_BYTES = 400
|
|
618
|
+
private_constant :MAX_NUMERIC_TEXT_BYTES
|
|
619
|
+
|
|
620
|
+
# Looks up a shared string by the index in a cell's <v>. An index that is negative or
|
|
621
|
+
# too long to be one reads as an empty string, as an index past the end does.
|
|
622
|
+
#: (Array[untyped] shared_strings, String raw_index) -> untyped
|
|
623
|
+
def self.shared_string_at(shared_strings, raw_index)
|
|
624
|
+
return "" if raw_index.bytesize > 10
|
|
625
|
+
|
|
626
|
+
index = raw_index.to_i
|
|
627
|
+
index.negative? ? "" : (shared_strings[index] || "")
|
|
601
628
|
end
|
|
602
629
|
|
|
603
630
|
# SECURITY NOTE: Decodes only standard predefined XML entities in a single pass.
|
|
@@ -710,6 +737,8 @@ module Xlsxrb
|
|
|
710
737
|
break unless cb.between?(48, 57)
|
|
711
738
|
|
|
712
739
|
parsed_r = (parsed_r * 10) + (cb - 48)
|
|
740
|
+
raise ParseError, "row number exceeds #{MAX_ROW_NUMBER}" if parsed_r > MAX_ROW_NUMBER
|
|
741
|
+
|
|
713
742
|
ri += 1
|
|
714
743
|
end
|
|
715
744
|
row_index = parsed_r - 1
|
|
@@ -782,6 +811,10 @@ module Xlsxrb
|
|
|
782
811
|
|
|
783
812
|
private_class_method :fast_scan_rows_direct
|
|
784
813
|
|
|
814
|
+
# Bytes of one row, or of markup between rows, held while streaming a worksheet.
|
|
815
|
+
MAX_STREAM_BUFFER_BYTES = 50 * 1024 * 1024
|
|
816
|
+
private_constant :MAX_STREAM_BUFFER_BYTES
|
|
817
|
+
|
|
785
818
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
786
819
|
def self.scan_rows_stream(source, shared_strings, _part_name, styles = nil, date1904 = false, pad_empty_cells = false, &block)
|
|
787
820
|
# rubocop:enable Style/OptionalBooleanParameter
|
|
@@ -795,6 +828,11 @@ module Xlsxrb
|
|
|
795
828
|
row_end_len = 6
|
|
796
829
|
sd_end_tag = "</sheetData>"
|
|
797
830
|
last_row_index = -1
|
|
831
|
+
# SECURITY NOTE: The buffer is searched again each time a chunk is added. These hold
|
|
832
|
+
# how far the previous searches got without a match, so that a row or a stretch of
|
|
833
|
+
# other markup spanning many chunks is searched once, not once per chunk.
|
|
834
|
+
sd_end_scan_from = 0
|
|
835
|
+
row_end_scan_from = 0
|
|
798
836
|
|
|
799
837
|
push_chunk = lambda do |chunk|
|
|
800
838
|
return if in_sheet_data == :closed
|
|
@@ -837,7 +875,8 @@ module Xlsxrb
|
|
|
837
875
|
|
|
838
876
|
pos = 0
|
|
839
877
|
last_consumed_pos = 0
|
|
840
|
-
sd_end_pos = buffer.index(sd_end_tag)
|
|
878
|
+
sd_end_pos = buffer.index(sd_end_tag, sd_end_scan_from)
|
|
879
|
+
sd_end_scan_from = sd_end_pos || [buffer.bytesize - sd_end_tag.bytesize, 0].max
|
|
841
880
|
|
|
842
881
|
loop do
|
|
843
882
|
row_start = buffer.index(row_start_pattern, pos)
|
|
@@ -848,14 +887,24 @@ module Xlsxrb
|
|
|
848
887
|
return
|
|
849
888
|
end
|
|
850
889
|
|
|
851
|
-
|
|
890
|
+
if row_start.nil?
|
|
891
|
+
# No row starts here: keep only the end, which may hold the start of a tag.
|
|
892
|
+
keep_from = buffer.bytesize - [row_start_len, sd_end_tag.bytesize].max
|
|
893
|
+
last_consumed_pos = keep_from if keep_from > last_consumed_pos
|
|
894
|
+
break
|
|
895
|
+
end
|
|
852
896
|
|
|
853
897
|
next_pos = row_start + row_start_len
|
|
854
|
-
|
|
898
|
+
if next_pos >= buffer.bytesize
|
|
899
|
+
last_consumed_pos = row_start if row_start > last_consumed_pos
|
|
900
|
+
break
|
|
901
|
+
end
|
|
855
902
|
|
|
856
903
|
nb = buffer.getbyte(next_pos)
|
|
857
904
|
unless TAG_DELIM_BYTES.include?(nb)
|
|
905
|
+
# Another element whose name starts with "row": nothing before it is needed.
|
|
858
906
|
pos = next_pos
|
|
907
|
+
last_consumed_pos = pos
|
|
859
908
|
next
|
|
860
909
|
end
|
|
861
910
|
|
|
@@ -865,10 +914,14 @@ module Xlsxrb
|
|
|
865
914
|
is_self_closing = buffer.getbyte(tag_end - 1) == 47
|
|
866
915
|
|
|
867
916
|
unless is_self_closing
|
|
868
|
-
|
|
917
|
+
row_end_from = tag_end + 1
|
|
918
|
+
row_end_from = row_end_scan_from if row_end_scan_from > row_end_from
|
|
919
|
+
row_end_scan_from = 0
|
|
920
|
+
row_end = buffer.index(row_end_tag, row_end_from)
|
|
869
921
|
if row_end.nil?
|
|
870
|
-
|
|
871
|
-
|
|
922
|
+
# The row continues in the next chunk. It will then start where the consumed
|
|
923
|
+
# part of the buffer ends, so the position is kept relative to that.
|
|
924
|
+
row_end_scan_from = [buffer.bytesize - row_end_len - last_consumed_pos, 0].max
|
|
872
925
|
break
|
|
873
926
|
end
|
|
874
927
|
end
|
|
@@ -886,6 +939,8 @@ module Xlsxrb
|
|
|
886
939
|
break unless cb.between?(48, 57)
|
|
887
940
|
|
|
888
941
|
parsed_r = (parsed_r * 10) + (cb - 48)
|
|
942
|
+
raise ParseError, "row number exceeds #{MAX_ROW_NUMBER}" if parsed_r > MAX_ROW_NUMBER
|
|
943
|
+
|
|
889
944
|
ri += 1
|
|
890
945
|
end
|
|
891
946
|
row_index = parsed_r - 1
|
|
@@ -955,7 +1010,11 @@ module Xlsxrb
|
|
|
955
1010
|
last_consumed_pos = pos
|
|
956
1011
|
end
|
|
957
1012
|
|
|
958
|
-
|
|
1013
|
+
if last_consumed_pos.positive?
|
|
1014
|
+
buffer.slice!(0...last_consumed_pos)
|
|
1015
|
+
sd_end_scan_from = [sd_end_scan_from - last_consumed_pos, 0].max
|
|
1016
|
+
end
|
|
1017
|
+
raise ParseError, "malformed worksheet XML: a row or tag exceeds #{MAX_STREAM_BUFFER_BYTES} bytes" if buffer.bytesize > MAX_STREAM_BUFFER_BYTES
|
|
959
1018
|
end
|
|
960
1019
|
|
|
961
1020
|
catch(:done) do
|
|
@@ -1013,6 +1072,48 @@ module Xlsxrb
|
|
|
1013
1072
|
ATTR_T = /\bt="([a-zA-Z]+)"/
|
|
1014
1073
|
ATTR_S = /\bs="(\d+)"/
|
|
1015
1074
|
|
|
1075
|
+
CELL_CLOSE_RE = %r{</(?:[a-zA-Z0-9_]+:)?c>}
|
|
1076
|
+
UNCLOSED_CELL_RE = %r{<(?:[a-zA-Z0-9_]+:)?c(?:\s[^>]*)?(?<!/)>}
|
|
1077
|
+
INLINE_STRING_OPEN_RE = /<(?:[a-zA-Z0-9_]+:)?is[\s>]/
|
|
1078
|
+
INLINE_STRING_CLOSE_RE = %r{</(?:[a-zA-Z0-9_]+:)?is>}
|
|
1079
|
+
private_constant :CELL_CLOSE_RE, :UNCLOSED_CELL_RE, :INLINE_STRING_OPEN_RE, :INLINE_STRING_CLOSE_RE
|
|
1080
|
+
|
|
1081
|
+
# Raises ParseError for a row whose last <c> or <is> start tag has no end tag.
|
|
1082
|
+
#
|
|
1083
|
+
# SECURITY NOTE: CELL_FAST_RE looks ahead for </c> and </is>. For a start tag with no end
|
|
1084
|
+
# tag after it, that search runs to the end of the row and fails, once per such tag, so
|
|
1085
|
+
# a row of 40,000 unclosed <c> tags (a 3 KB file) took minutes. Every search made here
|
|
1086
|
+
# is a single pass over the row.
|
|
1087
|
+
#: (String chunk) -> void
|
|
1088
|
+
def self.verify_cells_closed!(chunk)
|
|
1089
|
+
inline = chunk.include?("<is") || chunk.include?(":is")
|
|
1090
|
+
return if !inline && chunk.end_with?("</c>")
|
|
1091
|
+
|
|
1092
|
+
last_close = chunk.byterindex(CELL_CLOSE_RE)
|
|
1093
|
+
tail_from = last_close ? last_close + 2 : 0
|
|
1094
|
+
raise ParseError, "malformed worksheet XML: <c> element is not closed" if chunk.byteindex(UNCLOSED_CELL_RE, tail_from)
|
|
1095
|
+
return unless inline
|
|
1096
|
+
|
|
1097
|
+
last_close = chunk.byterindex(INLINE_STRING_CLOSE_RE)
|
|
1098
|
+
tail_from = last_close ? last_close + 2 : 0
|
|
1099
|
+
raise ParseError, "malformed worksheet XML: <is> element is not closed" if chunk.byteindex(INLINE_STRING_OPEN_RE, tail_from)
|
|
1100
|
+
end
|
|
1101
|
+
|
|
1102
|
+
private_class_method :verify_cells_closed!
|
|
1103
|
+
|
|
1104
|
+
# Returns the bytes of a row with every sequence that is not UTF-8 replaced by U+FFFD,
|
|
1105
|
+
# so that the text, formulas, and attributes cut from it are valid strings. A row that
|
|
1106
|
+
# is valid, as nearly all are, is returned as is.
|
|
1107
|
+
#: (String chunk) -> String
|
|
1108
|
+
def self.scrub_row(chunk)
|
|
1109
|
+
encoding = chunk.encoding
|
|
1110
|
+
chunk.force_encoding(Encoding::UTF_8)
|
|
1111
|
+
chunk = chunk.scrub unless chunk.valid_encoding?
|
|
1112
|
+
chunk.force_encoding(encoding)
|
|
1113
|
+
end
|
|
1114
|
+
|
|
1115
|
+
private_class_method :scrub_row
|
|
1116
|
+
|
|
1016
1117
|
def self.fast_parse_cells_direct(xml, from, to, shared_strings, row_source, prefix = "")
|
|
1017
1118
|
cells = []
|
|
1018
1119
|
fast_scan_cells_direct(xml, from, to, shared_strings, row_source, prefix) { |c| cells << c }
|
|
@@ -1022,7 +1123,8 @@ module Xlsxrb
|
|
|
1022
1123
|
# rubocop:disable Style/OptionalBooleanParameter
|
|
1023
1124
|
def self.fast_scan_cells_direct(xml, from, to, shared_strings, row_source, _prefix = "", styles = nil, date1904 = false, &)
|
|
1024
1125
|
# rubocop:enable Style/OptionalBooleanParameter
|
|
1025
|
-
chunk = xml.byteslice(from, to - from)
|
|
1126
|
+
chunk = scrub_row(xml.byteslice(from, to - from))
|
|
1127
|
+
verify_cells_closed!(chunk)
|
|
1026
1128
|
row_idx = row_source.is_a?(Hash) ? row_source[:row] : row_source
|
|
1027
1129
|
col_idx = 0
|
|
1028
1130
|
matched_any = false
|
|
@@ -1118,6 +1220,8 @@ module Xlsxrb
|
|
|
1118
1220
|
chunk = xml.byteslice(from, to - from)
|
|
1119
1221
|
return [] if chunk.nil? || chunk.empty?
|
|
1120
1222
|
|
|
1223
|
+
chunk = scrub_row(chunk)
|
|
1224
|
+
verify_cells_closed!(chunk)
|
|
1121
1225
|
col_idx = 0
|
|
1122
1226
|
matched_any = false
|
|
1123
1227
|
row_values = []
|