mspack_rb 0.2.3 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d95c98f63d77452303839ff28381a303748b7ef
4
- data.tar.gz: be1332875b3b16279ef38f24d729afd113221362
3
+ metadata.gz: 9daf394641a92e19fb3e05d7d07fc13b98607977
4
+ data.tar.gz: 2a3461a1586e09e9a779c665986352b545de8598
5
5
  SHA512:
6
- metadata.gz: de94dacd83888f31544b2343b091996730ce4326020a200630c37eb651727f12c9c8101cb4a1b3c6c394b3fd2deac4342eefbbcf6996a2990e4a6648d8d02fe8
7
- data.tar.gz: e768f570b844cd92a7793ba5d51f8a3e08a1181fa0e0d58a476cfcd57c1b337d3eeb643dbc2da873349c4a333a02b4abad0e5f8d8b82b66018e4fbe59a940c6d
6
+ metadata.gz: 45d9c9d9776f80b19ea98344154759bed54a5929041e62f9dfac6894aa2358a6cf4a16bc20bbfdc7a511b9b203672272ba79deabe62cb6a640045c93d4744c07
7
+ data.tar.gz: 722d93e39669963796218d09aa47eab15bfe3bb3ef677617d8061119f16c0b87d3a1c9555b1e87bd9481204059f9143bd566fde60e8c0ecc61ea97a44a8810e0
data/Rakefile CHANGED
@@ -8,4 +8,4 @@ Rake::ExtensionTask.new('mspack_native')
8
8
 
9
9
  RSpec::Core::RakeTask.new(:spec)
10
10
 
11
- task default: [ :compile, :spec ]
11
+ task default: [ :clean, :compile, :spec ]
@@ -1,7 +1,9 @@
1
1
  require "mspack_native"
2
2
 
3
- require "mspack/chm_decompressor"
4
- require "mspack/version"
3
+ require 'fileutils'
4
+
5
+ require 'mspack/chm_decompressor'
6
+ require 'mspack/version'
5
7
 
6
8
  module Mspack
7
9
 
@@ -2,19 +2,61 @@ require 'fileutils'
2
2
 
3
3
  module Mspack
4
4
  class ChmDecompressor
5
+ DEFAULT_BUFFER_SIZE = 4096.freeze
6
+
7
+ # Expects a ChmDecompressor::File and either a string, or a fixnum if a block
8
+ # is given.
9
+ #
10
+ # If no block is given, it calls Mspack.ensure_path and extracts file,
11
+ # returning the absolute file path.
12
+ #
13
+ # If a block is given, chunks of data are yielded with a maximum size given
14
+ # by the dir_or_buffer_size param. Buffer_size must be a multiple of
15
+ # DEFAULT_BUFFER_SIZE.
16
+ def extract(file, dir_or_buffer_size = DEFAULT_BUFFER_SIZE)
5
17
 
6
- # Expects a ChmDecompressor::File and a string.
7
- # Calls Mspack.ensure_path and extracts file.
8
- # Returns the absolute file path.
9
- def extract(file, dir = nil)
10
18
  if block_given?
11
- extract_to_path(file) { |data| yield data }
19
+ buffer_size = dir_or_buffer_size
20
+
21
+ if (buffer_size.fdiv(DEFAULT_BUFFER_SIZE).modulo(1) != 0)
22
+ raise ArgumentError,
23
+ 'Buffer_size must be a multiple of DEFAULT_BUFFER_SIZE'
24
+ end
25
+
26
+ a = 0
27
+ b = 0
28
+ did_yield = false
29
+ buffer = "\0" * buffer_size
30
+
31
+ extract_to_path(file) do |data|
32
+ did_yield = false
33
+ b = a + data.length - 1
34
+ buffer[a..b] = data
35
+
36
+ # full buffer - yield and reset
37
+ if b + 1 == buffer_size
38
+ yield buffer[0..b]
39
+ did_yield = true
40
+ a = 0
41
+
42
+ # last read if it's less than default size
43
+ elsif data.length > 0 && data.length < DEFAULT_BUFFER_SIZE
44
+ yield buffer[0..b]
45
+ did_yield = true
46
+
47
+ else
48
+ a = b + 1
49
+ end
50
+ end
51
+
52
+ # last read happened to be default size
53
+ yield buffer[0..b] if !did_yield && b > 0
54
+
12
55
  else
13
- path = Mspack.ensure_path(file.filename, dir)
56
+ path = Mspack.ensure_path(file.filename, dir_or_buffer_size)
14
57
  extract_to_path(file, path)
58
+ return path
15
59
  end
16
-
17
- path
18
60
  end
19
61
 
20
62
 
@@ -1,3 +1,3 @@
1
1
  module Mspack
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mspack_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-26 00:00:00.000000000 Z
11
+ date: 2017-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler