-
Notifications
You must be signed in to change notification settings - Fork 7
/
piggypack.rb
executable file
·57 lines (49 loc) · 1.31 KB
/
piggypack.rb
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/env ruby
# piggypack does the following things:
#
# 1. Takes a file, called the "payload" file
payload_name = "astley.3gp"
overlay_name = "astley.ppm"
out = File.open("binary.pgm", "w")
handle = File.open(payload_name, "r")
overlay_handle = File.open(overlay_name, "r")
3.times do out << overlay_handle.readline + "\n"; end
bytes = handle.read
# The length of the file name (8B)
# the payload itself
# The length of the file (8B)
# the file itself
numbers = [
payload_name.length,
payload_name.unpack("C*"),
[bytes.length].pack('q*').unpack('C*'),
bytes.unpack("C*")
].flatten
len = bytes.length
offset = 0
loop {
begin
line = overlay_handle.readline
rescue
break
end
break unless line
line = line.split(' ').map{ | x | x.to_i }
0.upto(line.length / 3 - 1) { | x |
if offset < numbers.length
byte = numbers[offset]
pixel = x * 3
# xxx0 0000 Red channel gets msb top 3 bits as the lsb
line[pixel] = line[pixel] & 0xF8 | ((byte >> 5) & 0x7)
# 000x x000 Green gets the next 2
line[pixel + 1] = line[pixel + 1] & 0xFC | ((byte >> 3) & 0x3)
# 0000 0xxx Blue gets lsb 3
line[pixel + 2] = line[pixel + 2] & 0xF8 | (byte & 0x7)
offset += 1
end
}
out << line.join(' ') + "\n"
}
out << "\n"
out.close
`convert binary.pgm binary.png`