extbrotli 0.0.1.PROTOTYPE

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +28 -0
  3. data/README.md +67 -0
  4. data/Rakefile +158 -0
  5. data/contrib/brotli/LICENSE +202 -0
  6. data/contrib/brotli/README.md +18 -0
  7. data/contrib/brotli/dec/bit_reader.c +55 -0
  8. data/contrib/brotli/dec/bit_reader.h +256 -0
  9. data/contrib/brotli/dec/context.h +260 -0
  10. data/contrib/brotli/dec/decode.c +1573 -0
  11. data/contrib/brotli/dec/decode.h +160 -0
  12. data/contrib/brotli/dec/dictionary.h +9494 -0
  13. data/contrib/brotli/dec/huffman.c +325 -0
  14. data/contrib/brotli/dec/huffman.h +77 -0
  15. data/contrib/brotli/dec/port.h +148 -0
  16. data/contrib/brotli/dec/prefix.h +756 -0
  17. data/contrib/brotli/dec/state.c +149 -0
  18. data/contrib/brotli/dec/state.h +185 -0
  19. data/contrib/brotli/dec/streams.c +99 -0
  20. data/contrib/brotli/dec/streams.h +100 -0
  21. data/contrib/brotli/dec/transform.h +315 -0
  22. data/contrib/brotli/dec/types.h +36 -0
  23. data/contrib/brotli/enc/backward_references.cc +769 -0
  24. data/contrib/brotli/enc/backward_references.h +50 -0
  25. data/contrib/brotli/enc/bit_cost.h +147 -0
  26. data/contrib/brotli/enc/block_splitter.cc +418 -0
  27. data/contrib/brotli/enc/block_splitter.h +78 -0
  28. data/contrib/brotli/enc/brotli_bit_stream.cc +884 -0
  29. data/contrib/brotli/enc/brotli_bit_stream.h +149 -0
  30. data/contrib/brotli/enc/cluster.h +290 -0
  31. data/contrib/brotli/enc/command.h +140 -0
  32. data/contrib/brotli/enc/context.h +185 -0
  33. data/contrib/brotli/enc/dictionary.h +9485 -0
  34. data/contrib/brotli/enc/dictionary_hash.h +4125 -0
  35. data/contrib/brotli/enc/encode.cc +715 -0
  36. data/contrib/brotli/enc/encode.h +196 -0
  37. data/contrib/brotli/enc/encode_parallel.cc +354 -0
  38. data/contrib/brotli/enc/encode_parallel.h +37 -0
  39. data/contrib/brotli/enc/entropy_encode.cc +492 -0
  40. data/contrib/brotli/enc/entropy_encode.h +88 -0
  41. data/contrib/brotli/enc/fast_log.h +179 -0
  42. data/contrib/brotli/enc/find_match_length.h +87 -0
  43. data/contrib/brotli/enc/hash.h +686 -0
  44. data/contrib/brotli/enc/histogram.cc +76 -0
  45. data/contrib/brotli/enc/histogram.h +100 -0
  46. data/contrib/brotli/enc/literal_cost.cc +172 -0
  47. data/contrib/brotli/enc/literal_cost.h +38 -0
  48. data/contrib/brotli/enc/metablock.cc +544 -0
  49. data/contrib/brotli/enc/metablock.h +88 -0
  50. data/contrib/brotli/enc/port.h +151 -0
  51. data/contrib/brotli/enc/prefix.h +85 -0
  52. data/contrib/brotli/enc/ringbuffer.h +108 -0
  53. data/contrib/brotli/enc/static_dict.cc +441 -0
  54. data/contrib/brotli/enc/static_dict.h +40 -0
  55. data/contrib/brotli/enc/static_dict_lut.h +12063 -0
  56. data/contrib/brotli/enc/streams.cc +127 -0
  57. data/contrib/brotli/enc/streams.h +129 -0
  58. data/contrib/brotli/enc/transform.h +250 -0
  59. data/contrib/brotli/enc/write_bits.h +91 -0
  60. data/ext/extbrotli.cc +24 -0
  61. data/ext/extbrotli.h +73 -0
  62. data/ext/extconf.rb +35 -0
  63. data/ext/lldecoder.c +220 -0
  64. data/ext/llencoder.cc +433 -0
  65. data/gemstub.rb +21 -0
  66. data/lib/extbrotli.rb +243 -0
  67. data/lib/extbrotli/version.rb +3 -0
  68. metadata +140 -0
data/lib/extbrotli.rb ADDED
@@ -0,0 +1,243 @@
1
+ #vim: set fileencoding:utf-8
2
+
3
+ ver = RUBY_VERSION.slice(/\d+\.\d+/)
4
+ soname = File.basename(__FILE__, ".rb") << ".so"
5
+ lib = File.join(File.dirname(__FILE__), ver, soname)
6
+ case
7
+ when File.file?(lib)
8
+ require_relative File.join(ver, soname)
9
+ when File.file?(File.join(File.dirname(__FILE__), ver))
10
+ require_relative soname
11
+ else
12
+ require soname
13
+ end
14
+
15
+ require "stringio"
16
+ require_relative "extbrotli/version"
17
+
18
+ module Brotli
19
+ Brotli = self
20
+
21
+ #
22
+ # call-seq:
23
+ # encode(**opts) -> brotli encoder
24
+ # encode(**opts) { |encoder| ... } -> outport
25
+ # encode(string, **opts) -> brotli'd string data
26
+ # encode(outport, **opts) -> brotli encoder
27
+ # encode(outport, **opts) { |encoder| ... } -> outport
28
+ #
29
+ def self.encode(outport = nil, **opts)
30
+ if outport.kind_of?(String)
31
+ return LowLevelEncoder.encode(outport, **opts)
32
+ end
33
+
34
+ e = Encoder.new(outport || "".b, **opts)
35
+ return e unless block_given?
36
+
37
+ begin
38
+ yield(e)
39
+ e.outport
40
+ ensure
41
+ e.close
42
+ end
43
+ end
44
+
45
+ #
46
+ # call-seq:
47
+ # decode(brotli_string) -> decoded string data
48
+ # decode(inport) -> brotli decoder
49
+ # decode(inport) { |decoder| ... } -> yield return
50
+ #
51
+ def self.decode(inport)
52
+ if inport.kind_of?(String)
53
+ # NOTE: LowLevelDecoder.decode を使わないのは、destsize の計算のための
54
+ # NOTE: BrotliDecompressedSize() が全体としてのバイト数を返さないため
55
+ return decode(StringIO.new(inport)) { |d| d.read }
56
+ end
57
+
58
+ d = Decoder.new(inport)
59
+ return d unless block_given?
60
+
61
+ begin
62
+ yield(d)
63
+ ensure
64
+ d.close
65
+ end
66
+ end
67
+
68
+ class Encoder
69
+ attr :encoder, :outport, :writebuf, :outbuf, :status
70
+
71
+ def initialize(outport, **opts)
72
+ @encoder = encoder = LowLevelEncoder.new(**opts)
73
+ @outport = outport
74
+ @writebuf = writebuf = StringIO.new("".b)
75
+ @outbuf = outbuf = "".b
76
+ @status = status = [:ready] # for finalizer
77
+
78
+ enc = self
79
+ self.class.class_eval do
80
+ set_finalizer(enc, encoder, outport, writebuf, outbuf, status)
81
+ end
82
+ end
83
+
84
+ def eof
85
+ status[0] == :ready ? false : true
86
+ end
87
+
88
+ alias eof? eof
89
+
90
+ def write(buf)
91
+ raise "closed stream" unless status
92
+
93
+ writebuf << buf
94
+ w = ""
95
+ blocksize = encoder.blocksize
96
+ blocks = writebuf.size / blocksize
97
+ if blocks > 0
98
+ writebuf.rewind
99
+ blocks.times do |i|
100
+ writebuf.read(blocksize, w)
101
+ encoder.encode_metablock(w, outbuf, LowLevelEncoder.compress_bound(blocksize), false);
102
+ outport << outbuf unless outbuf.empty?
103
+ end
104
+ writebuf.read(blocksize, w)
105
+ writebuf.truncate(0)
106
+ writebuf.rewind
107
+ writebuf << w
108
+ end
109
+
110
+ self
111
+ end
112
+
113
+ alias << write
114
+
115
+ def close
116
+ raise "closed stream" unless status
117
+
118
+ blocksize = encoder.blocksize
119
+
120
+ if writebuf.size > 0
121
+ encoder.encode_metablock(writebuf.string, outbuf, LowLevelEncoder.compress_bound(blocksize), true)
122
+ else
123
+ encoder.finish(outbuf, LowLevelEncoder.compress_bound(blocksize))
124
+ end
125
+
126
+ outport << outbuf unless outbuf.empty?
127
+
128
+ status[0] = nil
129
+
130
+ nil
131
+ end
132
+
133
+ class << self
134
+ private
135
+ def set_finalizer(obj, encoder, outport, writebuf, outbuf, status)
136
+ block = make_finalizer_block(encoder, outport, writebuf, outbuf, status)
137
+ ObjectSpace.define_finalizer(obj, block)
138
+ nil
139
+ end
140
+
141
+ private
142
+ def make_finalizer_block(encoder, outport, writebuf, outbuf, status)
143
+ proc do
144
+ if status[0] == :ready
145
+ TODO writebuf を flush する
146
+ TODO encoder.finish する
147
+
148
+ encoder.finish(outbuf, 1024 * 1024) # FIXME: retry to up scale size
149
+ outport << outbuf
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ class Decoder
157
+ attr :decoder, :inport, :readbuf, :destbuf, :status
158
+
159
+ BLOCKSIZE = 256 * 1024
160
+
161
+ def initialize(inport)
162
+ @decoder = LowLevelDecoder.new
163
+ @inport = inport
164
+ @readbuf = "".b
165
+ @destbuf = StringIO.new("".b)
166
+ @status = true # true, nil
167
+ end
168
+
169
+ def read(size = nil, buf = nil)
170
+ buf ||= "".b
171
+ buf.clear
172
+ return buf if size == 0
173
+ return nil unless status
174
+ w = "".b
175
+ while size.nil? || size > 0
176
+ if destbuf.eof? && !fetch
177
+ @status = nil
178
+ break
179
+ end
180
+
181
+ if size
182
+ s = destbuf.size - destbuf.pos
183
+ s = size if s > size
184
+ break unless destbuf.read(s, w)
185
+ buf << w
186
+ size -= w.bytesize
187
+ else
188
+ break unless destbuf.read(destbuf.size, w)
189
+ buf << w
190
+ end
191
+ end
192
+
193
+ if buf.empty?
194
+ nil
195
+ else
196
+ buf
197
+ end
198
+ end
199
+
200
+ def close
201
+ @status = nil
202
+ end
203
+
204
+ def eof
205
+ !status
206
+ end
207
+
208
+ alias eof? eof
209
+
210
+ private
211
+ def fetch
212
+ return nil if eof? || status == :done
213
+ return self unless destbuf.eof?
214
+ destbuf.string.clear
215
+ destbuf.rewind
216
+ d = "".b
217
+ s = 0
218
+ until destbuf.size > 0
219
+ if readbuf.empty?
220
+ if !inport.eof && t = inport.read(BLOCKSIZE)
221
+ readbuf << t
222
+ else
223
+ finish = true
224
+ end
225
+ end
226
+ (s, r, d) = decoder.decode(readbuf, d, BLOCKSIZE, finish ? 1 : 0)
227
+ if r
228
+ @readbuf = r
229
+ else
230
+ readbuf.clear
231
+ end
232
+ destbuf << d if d
233
+ case s
234
+ when 1
235
+ @status = :done
236
+ break
237
+ end
238
+ end
239
+ destbuf.rewind
240
+ self
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,3 @@
1
+ module Brotli
2
+ VERSION = "0.0.1.PROTOTYPE"
3
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extbrotli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.PROTOTYPE
5
+ platform: ruby
6
+ authors:
7
+ - dearblue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ description: |
28
+ unofficial ruby bindings for brotli <https://github.com/google/brotli> the compression library.
29
+ email: dearblue@users.osdn.me
30
+ executables: []
31
+ extensions:
32
+ - ext/extconf.rb
33
+ extra_rdoc_files:
34
+ - LICENSE
35
+ - README.md
36
+ - contrib/brotli/LICENSE
37
+ - contrib/brotli/README.md
38
+ - ext/extbrotli.cc
39
+ - ext/extbrotli.h
40
+ - ext/lldecoder.c
41
+ - ext/llencoder.cc
42
+ - lib/extbrotli.rb
43
+ - lib/extbrotli/version.rb
44
+ files:
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - contrib/brotli/LICENSE
49
+ - contrib/brotli/README.md
50
+ - contrib/brotli/dec/bit_reader.c
51
+ - contrib/brotli/dec/bit_reader.h
52
+ - contrib/brotli/dec/context.h
53
+ - contrib/brotli/dec/decode.c
54
+ - contrib/brotli/dec/decode.h
55
+ - contrib/brotli/dec/dictionary.h
56
+ - contrib/brotli/dec/huffman.c
57
+ - contrib/brotli/dec/huffman.h
58
+ - contrib/brotli/dec/port.h
59
+ - contrib/brotli/dec/prefix.h
60
+ - contrib/brotli/dec/state.c
61
+ - contrib/brotli/dec/state.h
62
+ - contrib/brotli/dec/streams.c
63
+ - contrib/brotli/dec/streams.h
64
+ - contrib/brotli/dec/transform.h
65
+ - contrib/brotli/dec/types.h
66
+ - contrib/brotli/enc/backward_references.cc
67
+ - contrib/brotli/enc/backward_references.h
68
+ - contrib/brotli/enc/bit_cost.h
69
+ - contrib/brotli/enc/block_splitter.cc
70
+ - contrib/brotli/enc/block_splitter.h
71
+ - contrib/brotli/enc/brotli_bit_stream.cc
72
+ - contrib/brotli/enc/brotli_bit_stream.h
73
+ - contrib/brotli/enc/cluster.h
74
+ - contrib/brotli/enc/command.h
75
+ - contrib/brotli/enc/context.h
76
+ - contrib/brotli/enc/dictionary.h
77
+ - contrib/brotli/enc/dictionary_hash.h
78
+ - contrib/brotli/enc/encode.cc
79
+ - contrib/brotli/enc/encode.h
80
+ - contrib/brotli/enc/encode_parallel.cc
81
+ - contrib/brotli/enc/encode_parallel.h
82
+ - contrib/brotli/enc/entropy_encode.cc
83
+ - contrib/brotli/enc/entropy_encode.h
84
+ - contrib/brotli/enc/fast_log.h
85
+ - contrib/brotli/enc/find_match_length.h
86
+ - contrib/brotli/enc/hash.h
87
+ - contrib/brotli/enc/histogram.cc
88
+ - contrib/brotli/enc/histogram.h
89
+ - contrib/brotli/enc/literal_cost.cc
90
+ - contrib/brotli/enc/literal_cost.h
91
+ - contrib/brotli/enc/metablock.cc
92
+ - contrib/brotli/enc/metablock.h
93
+ - contrib/brotli/enc/port.h
94
+ - contrib/brotli/enc/prefix.h
95
+ - contrib/brotli/enc/ringbuffer.h
96
+ - contrib/brotli/enc/static_dict.cc
97
+ - contrib/brotli/enc/static_dict.h
98
+ - contrib/brotli/enc/static_dict_lut.h
99
+ - contrib/brotli/enc/streams.cc
100
+ - contrib/brotli/enc/streams.h
101
+ - contrib/brotli/enc/transform.h
102
+ - contrib/brotli/enc/write_bits.h
103
+ - ext/extbrotli.cc
104
+ - ext/extbrotli.h
105
+ - ext/extconf.rb
106
+ - ext/lldecoder.c
107
+ - ext/llencoder.cc
108
+ - gemstub.rb
109
+ - lib/extbrotli.rb
110
+ - lib/extbrotli/version.rb
111
+ homepage: https://osdn.jp/projects/rutsubo/
112
+ licenses:
113
+ - 2-clause BSD License
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options:
117
+ - "--charset"
118
+ - UTF-8
119
+ - "-m"
120
+ - README.md
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '2.0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">"
131
+ - !ruby/object:Gem::Version
132
+ version: 1.3.1
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.8
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: ruby bindings for brotli
139
+ test_files: []
140
+ has_rdoc: