sixword 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sixword/lib.rb +40 -3
- data/lib/sixword/version.rb +1 -1
- data/spec/sixword/lib_spec.rb +11 -1
- data/spec/sixword_spec.rb +18 -0
- metadata +4 -4
data/lib/sixword/lib.rb
CHANGED
@@ -65,7 +65,7 @@ module Sixword
|
|
65
65
|
# omit padding bits, if any
|
66
66
|
int >>= padding * 8
|
67
67
|
|
68
|
-
int
|
68
|
+
[int, 8 - padding]
|
69
69
|
end
|
70
70
|
|
71
71
|
# Extract the padding from a word, e.g. 'WORD3' => 'WORD', 3
|
@@ -78,7 +78,8 @@ module Sixword
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def self.decode_6_words_to_bstring(word_array, padding_ok)
|
81
|
-
int_to_byte_array(decode_6_words(word_array, padding_ok)).
|
81
|
+
int_to_byte_array(*decode_6_words(word_array, padding_ok)).
|
82
|
+
map(&:chr).join
|
82
83
|
end
|
83
84
|
|
84
85
|
def self.word_to_bits(word)
|
@@ -119,6 +120,17 @@ module Sixword
|
|
119
120
|
parity & 0b11
|
120
121
|
end
|
121
122
|
|
123
|
+
# Given an array of bytes, pack them into a single Integer.
|
124
|
+
#
|
125
|
+
# For example:
|
126
|
+
#
|
127
|
+
# >> byte_array_to_int([1, 2])
|
128
|
+
# => 258
|
129
|
+
#
|
130
|
+
# @param byte_array [Array<Fixnum>]
|
131
|
+
#
|
132
|
+
# @return Integer
|
133
|
+
#
|
122
134
|
def self.byte_array_to_int(byte_array)
|
123
135
|
int = 0
|
124
136
|
byte_array.each do |byte|
|
@@ -128,7 +140,23 @@ module Sixword
|
|
128
140
|
int
|
129
141
|
end
|
130
142
|
|
131
|
-
|
143
|
+
# Given an Integer, unpack it into an array of bytes.
|
144
|
+
#
|
145
|
+
# For example:
|
146
|
+
#
|
147
|
+
# >> int_to_byte_array(258)
|
148
|
+
# => [1, 2]
|
149
|
+
#
|
150
|
+
# >> int_to_byte_array(258, 3)
|
151
|
+
# => [0, 1, 2]
|
152
|
+
#
|
153
|
+
# @param int [Integer]
|
154
|
+
# @param length [Integer] (nil) Left zero padded size of byte array to
|
155
|
+
# return. If not provided, no leading zeroes will be added.
|
156
|
+
#
|
157
|
+
# @return Array<Fixnum>
|
158
|
+
#
|
159
|
+
def self.int_to_byte_array(int, length=nil)
|
132
160
|
unless int >= 0
|
133
161
|
raise ArgumentError.new("Not sure what to do with negative numbers")
|
134
162
|
end
|
@@ -140,6 +168,15 @@ module Sixword
|
|
140
168
|
int >>= 8
|
141
169
|
end
|
142
170
|
|
171
|
+
# pad to appropriate length with leading zeroes
|
172
|
+
if length
|
173
|
+
raise ArgumentError.new("Cannot pad to length < 0") if length < 0
|
174
|
+
|
175
|
+
while arr.length < length
|
176
|
+
arr << 0
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
143
180
|
arr.reverse!
|
144
181
|
|
145
182
|
arr
|
data/lib/sixword/version.rb
CHANGED
data/spec/sixword/lib_spec.rb
CHANGED
@@ -16,7 +16,17 @@ describe Sixword::Lib do
|
|
16
16
|
%w{ROME MUG FRED SCAN LIVE LACE} => 0xD1854218EBBB0B51,
|
17
17
|
}.each do |words, int|
|
18
18
|
debug_puts "Decoding 6-word array: #{words.inspect}"
|
19
|
-
Sixword::Lib.decode_6_words(words, false).should == int
|
19
|
+
Sixword::Lib.decode_6_words(words, false).should == [int, 8]
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
|
24
|
+
it 'should convert byte arrays to ints' do
|
25
|
+
Sixword::Lib.byte_array_to_int([1, 2]).should == 258
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should convert ints to byte arrays' do
|
29
|
+
Sixword::Lib.int_to_byte_array(258).should == [1, 2]
|
30
|
+
Sixword::Lib.int_to_byte_array(258, 3).should == [0, 1, 2]
|
31
|
+
end
|
22
32
|
end
|
data/spec/sixword_spec.rb
CHANGED
@@ -56,6 +56,24 @@ describe Sixword do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
it 'should handle all null bytes correctly' do
|
60
|
+
binary = "\0" * 8
|
61
|
+
encoded = ['A'] * 6
|
62
|
+
Sixword.encode(binary).should == encoded
|
63
|
+
Sixword.decode(encoded).should == binary
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should handle padded null bytes correctly' do
|
67
|
+
{
|
68
|
+
"\0\0\0foo" => ["A", "A", "HAY", "SLEW", "TROT", "A2"],
|
69
|
+
"\0\0\0foo\0\0" => ["A", "A", "HAY", "SLEW", "TROT", "A"],
|
70
|
+
"foo\0\0" => ["CHUB", "EMIL", "MUDD", "A", "A", "A3"],
|
71
|
+
}.each do |binary, encoded|
|
72
|
+
Sixword.pad_encode(binary).should == encoded
|
73
|
+
Sixword.pad_decode(encoded).should == binary
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
59
77
|
it 'should convert hex strings to byte strings' do
|
60
78
|
{"03e755bf6982fa55" => "\x03\xe7\x55\xbf\x69\x82\xfa\x55",
|
61
79
|
"19dd19a502ca2d60" => "\x19\xdd\x19\xa5\x02\xca\x2d\x60",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sixword
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
segments:
|
108
108
|
- 0
|
109
|
-
hash: -
|
109
|
+
hash: -2360590348015650263
|
110
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
111
|
none: false
|
112
112
|
requirements:
|
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
115
|
version: '0'
|
116
116
|
segments:
|
117
117
|
- 0
|
118
|
-
hash: -
|
118
|
+
hash: -2360590348015650263
|
119
119
|
requirements: []
|
120
120
|
rubyforge_project:
|
121
121
|
rubygems_version: 1.8.23
|