-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_encode.sf
57 lines (44 loc) · 1.41 KB
/
log_encode.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/ruby
# Author: Trizen
# Date: 02 April 2022
# https://github.com/trizen
# Encode data, using logarithms of integers.
# Example:
# Let's say we want to encode the bytes "abc" == [97, 98, 99]
# We try to find an integer n such that log(n) contains '097098099' after the decimal point.
# One such integer is 1453293358, which gives log(1453293358) = 21.097098099...
# Usage:
# sidef script.sf [input_file]
# The script generates a valid Sidef script as output, such that, when executed, it will produce the input data.
define (
PERL_CODE = false # true to generate Perl code
SLICE_SIZE = 1 # how many bytes to encode into an integer
)
func log_encode(bytes) {
var v = bytes.map { '%03d' % _ }.join
for n in (10 .. 99) {
var t = Num("#{n}.#{v}").exp.round
if (t.log.to_s.substr(3).begins_with(v)) {
return t
}
}
return nil
}
var text = File(ARGV[0] \\ __FILE__).read
var values = []
text.bytes.each_slice(SLICE_SIZE, {|*slice|
while (slice.len != SLICE_SIZE) {
slice += ' '.ord
}
values << log_encode(slice)
})
if (PERL_CODE) {
print %Q'print pack("C#{SLICE_SIZE}", unpack("x3(a3)#{SLICE_SIZE}", log)) for qw('
print values.join(' ')
say ')'
}
else {
print 'STDOUT.binmode(":raw");%n['
print values.join(' ')
say %Q'].each { pack("C#{SLICE_SIZE}", unpack("x3(a3)#{SLICE_SIZE}", .log)).print }'
}