Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 63 lines (60 sloc) 1.189 kB
cc7c1a0 @Voker57 Added 2.0 version
authored
1 #!/usr/bin/env ruby
2 require 'digest'
3 require 'zlib'
4
36492ea @Voker57 + HC
authored
5 if ARGV.include? '-hc'
6 ARGV.delete '-hc'
7 hc = true
8 dict = {}
9 else
10 hc = false
11 end
12
13
41199d6 @Voker57 Added astral files
authored
14 if ARGV.size < 1
cc7c1a0 @Voker57 Added 2.0 version
authored
15 data = STDIN.read
16 # 2 is magic number
475901e @Voker57 Now env.var BS controls block size
authored
17 blocksize = ENV['BS'] ? ENV['BS'].to_i : 2
cc7c1a0 @Voker57 Added 2.0 version
authored
18 result = []
19 i = 0
20 while i < data.size
36492ea @Voker57 + HC
authored
21 if data.size - i < blocksize
22 hc = false
23 end
24 dat = data[i..i+blocksize-1]
25 if hc && dict[dat]
0884b79 @Voker57 Fixed error
authored
26 hash = dict[dat]
36492ea @Voker57 + HC
authored
27 else
28 hash = Zlib.crc32(dat).to_s(16)
29 dict [dat] = hash if hc
30 end
31 result << hash
cc7c1a0 @Voker57 Added 2.0 version
authored
32 i += blocksize
33 end
475901e @Voker57 Now env.var BS controls block size
authored
34 puts "#{result.join('-')} #{data.size}"
cc7c1a0 @Voker57 Added 2.0 version
authored
35 else
36 urandom = File.open('/dev/urandom', 'r')
475901e @Voker57 Now env.var BS controls block size
authored
37 blocksize = ENV['BS'] ? ENV['BS'].to_i : 2
cc7c1a0 @Voker57 Added 2.0 version
authored
38 ssize = 0
41199d6 @Voker57 Added astral files
authored
39 if(ARGV[0] =~ /^-f=(.*)$/)
40 key, length = File.read($1).split(/\s+/)
41 else
42 key, length = ARGV
43 end
cc7c1a0 @Voker57 Added 2.0 version
authored
44 # 8 too
41199d6 @Voker57 Added astral files
authored
45 key.split('-').each do |hash|
46 if length.to_i - ssize < blocksize
47 blocksize = length.to_i - ssize
36492ea @Voker57 + HC
authored
48 hc = false
49 end
50 if hc && dict[hash]
51 rdata = dict[hash]
52 else
53 while (rhash = Zlib.crc32(rdata = urandom.read(blocksize)).to_s(16)) != hash
54 dict[rhash] = rdata if hc
55 end
56 dict[rhash] = rdata if hc
cc7c1a0 @Voker57 Added 2.0 version
authored
57 end
58 STDOUT.write(rdata)
36492ea @Voker57 + HC
authored
59 # puts dict.inspect
cc7c1a0 @Voker57 Added 2.0 version
authored
60 ssize += rdata.size
61 STDOUT.flush
62 end
63 end
Something went wrong with that request. Please try again.