Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes parser / regex errors while parsing IEC61162-450 / LWE sentences #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions maritime-modules/heuristic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ local proto_iec61162450_binary = require "proto.iec61162450binary"
local function nmea_0183_heuristic_checker(buffer, pinfo, tree)
local length = buffer:len()
if length < 12 then return false end

local pattern = "^[!%$]%u%w-%p.-%*[%d%u][%d%u]\r\n"
local nmea_sentence = string.find(buffer():string(), pattern)
if nmea_sentence == nil then
return false
else
local matches, matches_order = parser_nmea:find_nmea_0183(buffer, pinfo)
for _, beg_idx in pairs(matches_order) do
match = matches[beg_idx]
local sub_buff_beg = beg_idx - 1
local sub_buff_len = match:len()
local sub_buffer = buffer(sub_buff_beg, sub_buff_len):tvb()
proto_nmea0183.dissector(sub_buffer, pinfo, tree)
end
return true
end

local cnt = -1
repeat
cnt = cnt + 1

local pattern = "^[!%$]%u%w-%p.-%*[%d%u][%d%u]\r\n"
local nmea_sentence = string.find(buffer:raw(cnt), pattern)
if nmea_sentence ~= nil then
local matches, matches_order = parser_nmea:find_nmea_0183(buffer, pinfo)
for _, beg_idx in pairs(matches_order) do
match = matches[beg_idx]
local sub_buff_beg = beg_idx - 1
local sub_buff_len = match:len()
local sub_buffer = buffer(sub_buff_beg, sub_buff_len):tvb()
proto_nmea0183.dissector(sub_buffer, pinfo, tree)
end
return true
end
until(cnt >= length or cnt > 32)
return false
end

local function iec_61162_450_nmea_heuristic_checker(buffer, pinfo, tree)
Expand Down
4 changes: 2 additions & 2 deletions maritime-modules/parser/iec450.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function parser_iec450:find_tags(buffer)
local msg = buffer():string()
local pattern = "^UdPbC\0(\\%w%p.-%*[%d%u][%d%u]\\)[!%$]%u%w-%p.-%*[%d%u][%d%u]\r\n"
local tags = string.match(msg, pattern)
local tags_beg, tags_end = string.find(msg, utilities:pgsub(tags))
local tags_beg, tags_end = string.find(msg, tags, 1, true)
return utilities:offset_length(tags_beg, tags_end)
end

Expand Down Expand Up @@ -50,7 +50,7 @@ function parser_iec450:find_sentence_iec(buffer)
local msg = buffer():string()
local pattern = "^UdPbC\0\\%w%p.-%*[%d%u][%d%u]\\([!%$]%u%w-%p.-%*[%d%u][%d%u]\r\n)"
local sentence = string.match(msg, pattern)
local sentence_beg, sentence_end = string.find(msg, utilities:pgsub(sentence))
local sentence_beg, sentence_end = string.find(msg, sentence, 1, true)
return utilities:offset_length(sentence_beg, sentence_end)
end

Expand Down
26 changes: 13 additions & 13 deletions maritime-modules/parser/nmea.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,57 @@ local utilities = require "utilities"
local parser_nmea = {}

function parser_nmea:find_sentence(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "([!%$]%u%w-%p.-%*[%d%u][%d%u])\r\n"
local sentence = string.match(buffer(0, -1):string(), pattern)
local sentence_beg, sentence_end = string.find(msg, utilities:pgsub(sentence))
local sentence_beg, sentence_end = string.find(msg, sentence, 1, true)
return utilities:offset_length(sentence_beg, sentence_end)
end

function parser_nmea:find_sentence_load(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$](%u%w-%p.-)%*[%d%u][%d%u]\r\n"
local sentence = string.match(buffer(0, -1):string(), pattern)
local sentence_beg, sentence_end = string.find(msg, utilities:pgsub(sentence))
local sentence_beg, sentence_end = string.find(msg, sentence, 1, true)
return utilities:offset_length(sentence_beg, sentence_end)
end

function parser_nmea:find_talker_id(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$](%u%w-)%p.-%*[%d%u][%d%u]\r\n"
local talker_sentence_ids = string.match(buffer(0, -1):string(), pattern)
local talkerid = talker_sentence_ids:sub(1, -4)
local talk_id_beg, talk_id_end = string.find(msg, utilities:pgsub(talkerid))
local talk_id_beg, talk_id_end = string.find(msg, talkerid, 1, true)
return utilities:offset_length(talk_id_beg, talk_id_end)
end

function parser_nmea:find_sentence_id(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$](%u%w-)%p.-%*[%d%u][%d%u]\r\n"
local talker_sentence_id = string.match(buffer(0, -1):string(), pattern)
local sentenceid = talker_sentence_id:sub(-3, -1)
local sent_id_beg, sent_id_end = string.find(msg, utilities:pgsub(sentenceid))
local sent_id_beg, sent_id_end = string.find(msg, sentenceid, 1, true)
return utilities:offset_length(sent_id_beg, sent_id_end)
end

function parser_nmea:find_data(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$]%u%w-%p(.-)%*[%d%u][%d%u]\r\n"
local data = string.match(buffer(0, -1):string(), pattern)
local data_beg, data_end = string.find(msg, utilities:pgsub(data))
local data_beg, data_end = string.find(msg, data, 1, true)
return utilities:offset_length(data_beg, data_end)
end

function parser_nmea:find_checksum(buffer)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$]%u%w-%p.-%*([%d%u][%d%u])\r\n"
local chksm = string.match(buffer(0, -1):string(), pattern)
local chksm_beg, chksm_end = string.find(msg, utilities:pgsub(chksm))
local chksm_beg, chksm_end = string.find(msg, chksm, 1, true)
return utilities:offset_length(chksm_beg, chksm_end)
end

function parser_nmea:find_nmea_0183(buffer, pinfo)
local msg = buffer():string()
local msg = buffer():raw()
local pattern = "[!%$]%u%w-%p.-%*[%d%u][%d%u]\r\n"
local matches = {}
local matches_order = {}
Expand Down
5 changes: 0 additions & 5 deletions maritime-modules/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ if not _G['maritimedissector'] then return end

local utilities = {}

function utilities:pgsub(str)
local replaced = str:gsub("*", "%%*"):gsub("-", "%%-"):gsub(",", "%%p"):gsub("%.", "%%p"):gsub("?", "%%p"):gsub("+", "%%p")
return replaced
end

function utilities:offset_length(beg_idx, end_idx)
local offset = beg_idx - 1
local length = end_idx - offset
Expand Down