Add length parameter to GSM7Bit decode
parent
84fbedc46b
commit
cf655b2b3c
|
@ -31,17 +31,18 @@ module Biju
|
||||||
user_data_length: string[52..53],
|
user_data_length: string[52..53],
|
||||||
}
|
}
|
||||||
res[:sender_number] = string[22..(22 + res[:address_length])]
|
res[:sender_number] = string[22..(22 + res[:address_length])]
|
||||||
res[:user_data] = PDU.decode_user_data(
|
res[:user_data] = PDU.decode_user_data(string[54..-1],
|
||||||
string[54..-1], res[:data_coding_scheme])
|
encoding: res[:data_coding_scheme],
|
||||||
|
length: res[:user_data_length].hex)
|
||||||
|
|
||||||
res
|
res
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.decode_user_data(message, encoding = '00')
|
def self.decode_user_data(message, encoding: '00', length: 0)
|
||||||
encoding = data_coding_scheme(encoding) unless encoding.is_a?(Symbol)
|
encoding = data_coding_scheme(encoding) unless encoding.is_a?(Symbol)
|
||||||
|
|
||||||
raise ArgumentError, "Unknown encoding" unless ENCODING.has_key?(:gsm7bit)
|
raise ArgumentError, "Unknown encoding" unless ENCODING.has_key?(:gsm7bit)
|
||||||
ENCODING[:gsm7bit].decode(message)
|
ENCODING[:gsm7bit].decode(message, length: length)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.data_coding_scheme(dcs)
|
def self.data_coding_scheme(dcs)
|
||||||
|
|
|
@ -1,81 +1,83 @@
|
||||||
module Biju::PDU
|
module Biju
|
||||||
class GSM7Bit
|
module PDU
|
||||||
BASIC_7BIT_CHARACTER_SET = [
|
class GSM7Bit
|
||||||
'@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', "\n", 'Ø', 'ø', "\r", 'Å', 'å',
|
BASIC_7BIT_CHARACTER_SET = [
|
||||||
"\u0394", '_', "\u03a6", "\u0393", "\u039b", "\u03a9", "\u03a0","\u03a8", "\u03a3", "\u0398", "\u039e", "\e", 'Æ', 'æ', 'ß', 'É',
|
'@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', "\n", 'Ø', 'ø', "\r", 'Å', 'å',
|
||||||
' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')','*', '+', ',', '-', '.', '/',
|
"\u0394", '_', "\u03a6", "\u0393", "\u039b", "\u03a9", "\u03a0","\u03a8", "\u03a3", "\u0398", "\u039e", "\e", 'Æ', 'æ', 'ß', 'É',
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7','8', '9', ':', ';', '<', '=', '>', '?',
|
' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')','*', '+', ',', '-', '.', '/',
|
||||||
'¡', 'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
'0', '1', '2', '3', '4', '5', '6', '7','8', '9', ':', ';', '<', '=', '>', '?',
|
||||||
'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§',
|
'¡', 'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
||||||
'¿', 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§',
|
||||||
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ','ü', 'à'
|
'¿', 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
|
||||||
]
|
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ','ü', 'à'
|
||||||
|
]
|
||||||
|
|
||||||
BASIC_7BIT_CHARACTER_SET_EXTENSION = {
|
BASIC_7BIT_CHARACTER_SET_EXTENSION = {
|
||||||
0x0A => "\n",
|
0x0A => "\n",
|
||||||
0x0D => '',
|
0x0D => '',
|
||||||
0x14 => '^',
|
0x14 => '^',
|
||||||
0x1B => '',
|
0x1B => '',
|
||||||
0x28 => '{',
|
0x28 => '{',
|
||||||
0x29 => '}',
|
0x29 => '}',
|
||||||
0x2F => '\\',
|
0x2F => '\\',
|
||||||
0x3C => '[',
|
0x3C => '[',
|
||||||
0x3D => '~',
|
0x3D => '~',
|
||||||
0x3E => ']',
|
0x3E => ']',
|
||||||
0x40 => '|',
|
0x40 => '|',
|
||||||
0x65 => '€',
|
0x65 => '€',
|
||||||
}
|
}
|
||||||
|
|
||||||
def self.decode(string)
|
def self.decode(string, length: 0)
|
||||||
res = ''
|
res = ''
|
||||||
next_char = 0
|
next_char = 0
|
||||||
|
|
||||||
string.scan(/../).map(&:hex).each_with_index do |octet, i|
|
string.scan(/../).map(&:hex).each_with_index do |octet, i|
|
||||||
index = i % 7
|
index = i % 7
|
||||||
current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char
|
current = ((octet & (2 ** (7 - index) - 1)) << index) | next_char
|
||||||
|
|
||||||
res = add_char(res, current)
|
res = add_char(res, current)
|
||||||
|
|
||||||
next_char = octet >> (7 - index)
|
next_char = octet >> (7 - index)
|
||||||
if index == 6
|
if index == 6
|
||||||
res = add_char(res, next_char)
|
res = add_char(res, next_char)
|
||||||
next_char = 0
|
next_char = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
res[0..(length - 1)]
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.encode(string)
|
||||||
|
res = ''
|
||||||
|
string.chars.each do |char|
|
||||||
|
if get_septet(char)
|
||||||
|
res << get_septet(char).reverse
|
||||||
|
elsif get_septet(char, escape: true)
|
||||||
|
res << get_septet("\e").reverse
|
||||||
|
res << get_septet(char, escape: true).reverse
|
||||||
|
end
|
||||||
|
end
|
||||||
|
res << ("0" * (8 - (res.length % 8))) unless res.length % 8 == 0
|
||||||
|
|
||||||
|
res.scan(/.{8}/).map { |octet| "%02x" % octet.reverse.to_i(2) }.join
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.add_char(string, char)
|
||||||
|
if string[-1] == "\e"
|
||||||
|
string.chop << BASIC_7BIT_CHARACTER_SET_EXTENSION[char]
|
||||||
|
else
|
||||||
|
string << BASIC_7BIT_CHARACTER_SET[char]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
res
|
def self.get_septet(char, escape: false)
|
||||||
end
|
char = (!escape ? BASIC_7BIT_CHARACTER_SET.index(char) : BASIC_7BIT_CHARACTER_SET_EXTENSION.key(char))
|
||||||
|
|
||||||
def self.encode(string)
|
return nil unless char
|
||||||
res = ''
|
"%07b" % char
|
||||||
string.chars.each do |char|
|
|
||||||
if get_septet(char)
|
|
||||||
res << get_septet(char).reverse
|
|
||||||
elsif get_septet(char, escape: true)
|
|
||||||
res << get_septet("\e").reverse
|
|
||||||
res << get_septet(char, escape: true).reverse
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
res << ("0" * (8 - (res.length % 8))) unless res.length % 8 == 0
|
|
||||||
|
|
||||||
res.scan(/.{8}/).map { |octet| "%02x" % octet.reverse.to_i(2) }.join
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def self.add_char(string, char)
|
|
||||||
if string[-1] == "\e"
|
|
||||||
string.chop << BASIC_7BIT_CHARACTER_SET_EXTENSION[char]
|
|
||||||
else
|
|
||||||
string << BASIC_7BIT_CHARACTER_SET[char]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.get_septet(char, escape: false)
|
|
||||||
char = (!escape ? BASIC_7BIT_CHARACTER_SET.index(char) : BASIC_7BIT_CHARACTER_SET_EXTENSION.key(char))
|
|
||||||
|
|
||||||
return nil unless char
|
|
||||||
"%07b" % char
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue