Module: PDUTools::Helpers

Included in:
Decoder, Encoder
Defined in:
lib/pdu_tools/helpers.rb

Constant Summary collapse

GSM_03_38_ESCAPES =
{
    "@" => "\x00",
    "$" => "\x02",
    "_" => "\x11",
    "^" => "\x1B\x14",
    "{" => "\x1B\x28",
    "}" => "\x1B\x29",
    "\\" => "\x1B\x2F",
    "[" => "\x1B\x3C",
    "~" => "\x1B\x3D",
    "]" => "\x1B\x3E",
    "|" => "\x1B\x40"
    # "\x80" => "\x1B\x65"
}

Instance Method Summary collapse

Instance Method Details

#dec2hexbyte(dec) ⇒ Object



25
26
27
# File 'lib/pdu_tools/helpers.rb', line 25

def dec2hexbyte dec
  "%02X" % dec
end

#decode16bit(data, length) ⇒ Object



104
105
106
107
108
109
# File 'lib/pdu_tools/helpers.rb', line 104

def decode16bit data, length
  dobule_octets = data.split('').in_groups_of(4).collect(&:join)[0, length/2]
  dobule_octets.collect do |o|
    [o.to_i(16)].pack("U")
  end.join
end

#decode7bit(textdata, length) ⇒ Object

def decode7bit data, length

septets = data.to_i(16).to_s(2).split('').in_groups_of(7).collect(&:join)[0,length]
septets.collect do |s|
  s.to_i(2).chr
end.join

end



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pdu_tools/helpers.rb', line 75

def decode7bit textdata, length
  bytes = []
  textdata.split('').each_slice(2) do |s|
    bytes << "%08b" % s.join.to_i(16)
  end
  bit = (bytes.size % 7)
  bytes.reverse!
  bytes.each_with_index do |byte, index|
    if bit == 0 or index == 0
      bytes.insert(index, "")
      bit = 7 if bit == 0
      next
    else
      bytes[index-1] = "#{bytes[index-1]}#{(bytes[index]||"")[0,bit]}"
      bytes[index] = bytes[index][bit..-1] if bytes[index]
      bit -= 1
    end
  end
  bytes = bytes.reverse.collect{|b| "0#{b}".to_i(2) }.collect{|b| b.zero? ? nil : b }.compact
  bytes.collect{|b| b.chr }.join
end

#decode8bit(data, length) ⇒ Object



97
98
99
100
101
102
# File 'lib/pdu_tools/helpers.rb', line 97

def decode8bit data, length
  octets = data.split('').in_groups_of(2).collect(&:join)[0, length]
  octets.collect do |o|
    o.to_i(16).chr
  end.join
end

#encode7bit(string, padding = 0) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pdu_tools/helpers.rb', line 29

def encode7bit string, padding=0
  current_byte = 0
  offset = padding
  packed = []
  string.chars.to_a.each_with_index do |char, i|
    # cap off any excess bytes
    septet = char.ord & 0x7F
    # append the septet and then cap off excess bytes
    current_byte |= (septet << offset) & 0xFF
    offset += 7
    if offset > 7
      # the current byte is full, add it to the encoded data.
      packed << current_byte
      # shift left and append the left shifted septet to the current byte
      septet = septet >> (7 - (offset - 8 ))
      current_byte = septet
      # update offset
      offset -= 8
    end
  end
  packed << current_byte if offset > 0 # append the last byte
  packed.collect{|c| "%02X" % c }.join
end

#encode8bit(string) ⇒ Object



53
54
55
56
57
# File 'lib/pdu_tools/helpers.rb', line 53

def encode8bit string
  string.chars.to_a.collect do |char|
    "%04X" % char.ord
  end.join
end

#normal2swapped(string) ⇒ Object



59
60
61
62
# File 'lib/pdu_tools/helpers.rb', line 59

def normal2swapped string
  string << "F" if string.length.odd?
  string.scan(/../).collect(&:reverse).join
end

#swapped2normal(string) ⇒ Object



64
65
66
# File 'lib/pdu_tools/helpers.rb', line 64

def swapped2normal string
  string.scan(/../).collect(&:reverse).join.gsub(/F$/,'')
end

#utf8_to_gsm0338(string) ⇒ Object



18
19
20
21
22
23
# File 'lib/pdu_tools/helpers.rb', line 18

def utf8_to_gsm0338 string
  GSM_03_38_ESCAPES.each do |find, replace|
    string.gsub! find, replace
  end
  string
end