Module: Format

Defined in:
lib/ec2/amitools/format.rb

Class Method Summary collapse

Class Method Details

.bin2hex(data) ⇒ Object

Convert the binary data ((|data|)) to an ASCII string of hexadecimal digits that represents it.

E.g. if ((|data|)) is the string of bytes 0x1, 0x1A, 0xFF, it is converted to the string “011AFF”.



24
25
26
27
28
# File 'lib/ec2/amitools/format.rb', line 24

def Format.bin2hex(data)
  hex = StringIO.new
  data.unpack('H*').each {|digit| hex.write(digit)}
  hex.string
end

.block(data, blocksize) ⇒ Object

Breaks ((|data|)) into blocks of size ((|blocksize|)). The last block maybe less than ((|blocksize||).



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ec2/amitools/format.rb', line 36

def Format.block(data, blocksize)
  blocks = Array.new
  read = 0
  while read < data.size
    left = data.size - read
    blocks << data[read, (left < blocksize) ? left : blocksize]
    read += (left < blocksize ? left : blocksize)
  end
  
  blocks
end

.hex2bin(hex) ⇒ Object

Convert ASCII string of hexadecimal digits ((|hex|)) into the binary data it represents. If there are an odd number of hexedecimal digits in ((|hex|)) it is left padded with a leading ‘0’ character.

E.g. if ((|hex|)) is “11AFF”, it is converted to the string of bytes 0x1, 0x1A, 0xFF.



58
59
60
61
62
63
# File 'lib/ec2/amitools/format.rb', line 58

def Format.hex2bin(hex)
  hex = '0' + hex unless (hex.size % 2) == 0
  data = StringIO.new
  [[hex].pack('H*')].each {|digit| data.write(digit)}
  data.string
end

.int2byte(int) ⇒ Object

Return a single character string containing ((|int|)) converted to a single byte unsigned integer. The operand must be less than 256.

Raises:

  • (ArgumentError)


71
72
73
74
# File 'lib/ec2/amitools/format.rb', line 71

def Format.int2byte int
  raise ArgumentError.new('argument greater than 255') unless int < 256
  int.chr
end

.int2int16(i) ⇒ Object

Convert integer i to an unsigned 16 bit int packed into two bytes in big endian order.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
# File 'lib/ec2/amitools/format.rb', line 80

def Format::int2int16( i )
  raise ArgumentError.new( 'argument greater than 65535' ) unless i < 65536
  hi_byte = ( i >> 8 ).chr
  lo_byte = ( i & 0xFF).chr
  return [ hi_byte, lo_byte ]
end

.pad_pkcs7(data, blocksize) ⇒ Object

Pad data string ((|data|)) according to the PKCS #7 padding scheme.

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ec2/amitools/format.rb', line 92

def Format.pad_pkcs7(data, blocksize)
  raise ArgumentError.new("invalid data: #{data.to_s}") unless data and data.kind_of? String
  raise ArgumentError.new("illegal blocksize: #{blocksize}") unless blocksize > 0x0 and blocksize < 0xFF
  
  # Determine the number of padding characters required. If the data size is
  # divisible by the blocksize, a block of padding is required.
  nr_padchars = blocksize - (data.size % blocksize)
  nr_padchars = blocksize unless nr_padchars != 0
  
  # Create padding, where the padding byte is the number of padding bytes.
  padchar = nr_padchars.chr
  padding = padchar * nr_padchars
  
  data + padding
end

.unpad_pkcs7(data, blocksize) ⇒ Object

Pad ((|data|)) according to the PKCS #7 padding scheme.

Raises:

  • (ArgumentError)


113
114
115
116
117
118
119
120
121
122
# File 'lib/ec2/amitools/format.rb', line 113

def Format.unpad_pkcs7(data, blocksize)
  raise ArgumentError.new("illegal blocksize: #{blocksize}") unless blocksize > 0x0 and blocksize < 0xFF
  raise ArgumentError.new("invalid data: #{data.to_s}") unless data and data.kind_of? String 
  raise ArgumentError.new("invalid data size: #{data.size}") unless data.size > 0 and (data.size % blocksize) == 0
  
  nr_padchars = data[data.size - 1]
  raise ArgumentError.new("data padding character invalid: #{nr_padchars}") unless (nr_padchars > 0 and nr_padchars <= blocksize)
  
  data[0, data.size - nr_padchars]
end