Add sanitize cc
This commit is contained in:
parent
db56a97e8c
commit
0f4646e011
|
@ -91,14 +91,24 @@ class Unpacker
|
||||||
letters.join
|
letters.join
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def str_sanitize_cc(str)
|
||||||
|
letters = str.chars
|
||||||
|
letters.map! do |c|
|
||||||
|
c.ord < 32 ? ' ' : c
|
||||||
|
end
|
||||||
|
letters.join
|
||||||
|
end
|
||||||
|
|
||||||
def get_string(sanitize = SANITIZE)
|
def get_string(sanitize = SANITIZE)
|
||||||
return nil if @data.nil?
|
return nil if @data.nil?
|
||||||
|
|
||||||
str = ''
|
str = ''
|
||||||
|
p @data
|
||||||
@data.each_with_index do |byte, index|
|
@data.each_with_index do |byte, index|
|
||||||
if byte.zero?
|
if byte.zero?
|
||||||
@data = index == @data.length - 1 ? nil : @data[(index + 1)..]
|
@data = index == @data.length - 1 ? nil : @data[(index + 1)..]
|
||||||
str = str_sanitize(str) unless (sanitize & SANITIZE).zero?
|
str = str_sanitize(str) unless (sanitize & SANITIZE).zero?
|
||||||
|
str = str_sanitize_cc(str) unless (sanitize & SANITIZE_CC).zero?
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
str += byte.chr
|
str += byte.chr
|
||||||
|
|
|
@ -19,6 +19,27 @@ describe 'Unpacker', :unpacker do
|
||||||
expect(u.get_string).to eq(' BB')
|
expect(u.get_string).to eq(' BB')
|
||||||
expect(u.get_string).to eq(nil)
|
expect(u.get_string).to eq(nil)
|
||||||
end
|
end
|
||||||
|
it 'Should keep \r, \n and \t' do
|
||||||
|
u = Unpacker.new(["\r".ord, 0x41, 0x41, 0x00, "\n".ord, 0x42, "\t".ord, 0x42, 0x00])
|
||||||
|
expect(u.get_string).to eq("\rAA")
|
||||||
|
expect(u.get_string).to eq("\nB\tB")
|
||||||
|
expect(u.get_string).to eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'Unpack strings sanitize cc' do
|
||||||
|
it 'Should replace bytes lower than 32 with spaces' do
|
||||||
|
u = Unpacker.new([0x02, 0x41, 0x41, 0x00, 0x1F, 0x42, 0x42, 0x00])
|
||||||
|
expect(u.get_string).to eq(' AA')
|
||||||
|
expect(u.get_string).to eq(' BB')
|
||||||
|
expect(u.get_string).to eq(nil)
|
||||||
|
end
|
||||||
|
it 'Should NOT keep \r, \n and \t' do
|
||||||
|
u = Unpacker.new(["\r".ord, 0x41, 0x41, 0x00, "\n".ord, 0x42, "\t".ord, 0x42, 0x00])
|
||||||
|
expect(u.get_string(SANITIZE_CC)).to eq(' AA')
|
||||||
|
expect(u.get_string(SANITIZE_CC)).to eq(' B B')
|
||||||
|
expect(u.get_string(SANITIZE_CC)).to eq(nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'Unpack single byte integers' do
|
context 'Unpack single byte integers' do
|
||||||
|
|
Loading…
Reference in a new issue