|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 |
|
| 17 | +require 'fluent/plugin/extractor' |
17 | 18 | require 'stringio' |
18 | 19 | require 'zlib' |
19 | 20 | require 'zstd-ruby' |
20 | 21 |
|
21 | 22 | module Fluent |
22 | 23 | module Plugin |
23 | 24 | module Compressable |
| 25 | + DEFAULT_DECOMPRESSION_SIZE_LIMIT = 256 * 1024 * 1024 |
| 26 | + |
24 | 27 | def compress(data, type: :gzip, **kwargs) |
25 | 28 | output_io = kwargs[:output_io] |
26 | 29 | io = output_io || StringIO.new |
@@ -60,79 +63,21 @@ def decompress(compressed_data = nil, output_io: nil, input_io: nil, type: :gzip |
60 | 63 |
|
61 | 64 | private |
62 | 65 |
|
63 | | - def string_decompress_gzip(compressed_data) |
64 | | - io = StringIO.new(compressed_data) |
65 | | - out = '' |
66 | | - loop do |
67 | | - reader = Zlib::GzipReader.new(io) |
68 | | - out << reader.read |
69 | | - unused = reader.unused |
70 | | - reader.finish |
71 | | - unless unused.nil? |
72 | | - adjust = unused.length |
73 | | - io.pos -= adjust |
74 | | - end |
75 | | - break if io.eof? |
76 | | - end |
77 | | - out |
78 | | - end |
79 | | - |
80 | | - def string_decompress_zstd(compressed_data) |
81 | | - io = StringIO.new(compressed_data) |
82 | | - reader = Zstd::StreamReader.new(io) |
83 | | - out = '' |
84 | | - loop do |
85 | | - # Zstd::StreamReader needs to specify the size of the buffer |
86 | | - out << reader.read(1024) |
87 | | - # Zstd::StreamReader doesn't provide unused data, so we have to manually adjust the position |
88 | | - break if io.eof? |
89 | | - end |
90 | | - out |
91 | | - end |
92 | | - |
93 | 66 | def string_decompress(compressed_data, type = :gzip) |
94 | 67 | if type == :gzip |
95 | | - string_decompress_gzip(compressed_data) |
| 68 | + Extractor.decompress_gzip(compressed_data, limit: @decompression_size_limit || DEFAULT_DECOMPRESSION_SIZE_LIMIT) |
96 | 69 | elsif type == :zstd |
97 | | - string_decompress_zstd(compressed_data) |
| 70 | + Extractor.decompress_zstd(compressed_data, limit: @decompression_size_limit || DEFAULT_DECOMPRESSION_SIZE_LIMIT) |
98 | 71 | else |
99 | 72 | raise ArgumentError, "Unknown compression type: #{type}" |
100 | 73 | end |
101 | 74 | end |
102 | 75 |
|
103 | | - def io_decompress_gzip(input, output) |
104 | | - loop do |
105 | | - reader = Zlib::GzipReader.new(input) |
106 | | - v = reader.read |
107 | | - output.write(v) |
108 | | - unused = reader.unused |
109 | | - reader.finish |
110 | | - unless unused.nil? |
111 | | - adjust = unused.length |
112 | | - input.pos -= adjust |
113 | | - end |
114 | | - break if input.eof? |
115 | | - end |
116 | | - output |
117 | | - end |
118 | | - |
119 | | - def io_decompress_zstd(input, output) |
120 | | - reader = Zstd::StreamReader.new(input) |
121 | | - loop do |
122 | | - # Zstd::StreamReader needs to specify the size of the buffer |
123 | | - v = reader.read(1024) |
124 | | - output.write(v) |
125 | | - # Zstd::StreamReader doesn't provide unused data, so we have to manually adjust the position |
126 | | - break if input.eof? |
127 | | - end |
128 | | - output |
129 | | - end |
130 | | - |
131 | 76 | def io_decompress(input, output, type = :gzip) |
132 | 77 | if type == :gzip |
133 | | - io_decompress_gzip(input, output) |
| 78 | + Extractor.io_decompress_gzip(input, output) |
134 | 79 | elsif type == :zstd |
135 | | - io_decompress_zstd(input, output) |
| 80 | + Extractor.io_decompress_zstd(input, output) |
136 | 81 | else |
137 | 82 | raise ArgumentError, "Unknown compression type: #{type}" |
138 | 83 | end |
|
0 commit comments