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



33
34
35
# File 'lib/pdu_tools/helpers.rb', line 33

def dec2hexbyte dec
  "%02X" % dec
end

#decode16bit(data, length) ⇒ Object

Decodes 16bit UTF-16 string into UTF-8 string



118
119
120
121
122
123
124
# File 'lib/pdu_tools/helpers.rb', line 118

def decode16bit data, length
  data.split('').in_groups_of(4).collect()
  double_octets = data.split('').in_groups_of(4).map(&:join).map{|x| x.to_i(16) }[0, length / 2] # integer values
  double_octets.collect do |o|
    [o].pack('S')
  end.join.force_encoding('utf-16le').encode('utf-8')
end

#decode7bit(textdata, length = nil, offset = 1) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pdu_tools/helpers.rb', line 76

def decode7bit textdata, length = nil, offset = 1
  ret = ""
  bytes = []
  textdata.split('').each_slice(2) do |s|
    bytes << "%08b" % s.join.to_i(16)
  end

  cur_septet = ""
  next_septet = ""
  last_char = ""

  bytes.each_with_index do |byte, index|
    to_take = ([0,1].include? offset) ? 7 : 8-offset
    cur_septet.prepend byte[offset, to_take]
    next_septet = byte[0,offset] if offset > 0
    ret << cur_septet.to_i(2).chr

    if offset == 7
      offset = 1
      ret << next_septet.to_i(2).chr
      cur_septet = ""
    else
      offset += 1
      cur_septet = next_septet
    end
    next_septet = nil
  end
  if length
    ret[0..length - 1]
  else
    ret
  end
end

#decode8bit(data, length) ⇒ Object



110
111
112
113
114
115
# File 'lib/pdu_tools/helpers.rb', line 110

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pdu_tools/helpers.rb', line 37

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



61
62
63
64
65
# File 'lib/pdu_tools/helpers.rb', line 61

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

#gsm0338_to_utf8(string) ⇒ Object



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

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

#normal2swapped(string) ⇒ Object



67
68
69
70
# File 'lib/pdu_tools/helpers.rb', line 67

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

#swapped2normal(string) ⇒ Object



72
73
74
# File 'lib/pdu_tools/helpers.rb', line 72

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