From 8ce8aaddf9603eba9d7894b2ed41a332a7f44b28 Mon Sep 17 00:00:00 2001 From: Denny Sheirer Date: Fri, 6 Jan 2023 04:17:57 -0500 Subject: [PATCH] #1399 Resolves device ID parsing issue for Motorola ARS Device Registration. (#1400) Co-authored-by: Dennis Sheirer --- .../github/dsheirer/bits/BinaryMessage.java | 77 ++++++++++++++----- .../decode/ip/ars/DeviceRegistration.java | 28 +++---- .../decode/ip/ars/UserRegistration.java | 29 +++---- .../decode/ip/ars/identifier/ARSDevice.java | 12 +-- 4 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/main/java/io/github/dsheirer/bits/BinaryMessage.java b/src/main/java/io/github/dsheirer/bits/BinaryMessage.java index 13aae11de..1853eb1f6 100644 --- a/src/main/java/io/github/dsheirer/bits/BinaryMessage.java +++ b/src/main/java/io/github/dsheirer/bits/BinaryMessage.java @@ -1,32 +1,28 @@ /* + * ***************************************************************************** + * Copyright (C) 2014-2023 Dennis Sheirer * - * * ****************************************************************************** - * * Copyright (C) 2014-2019 Dennis Sheirer - * * - * * This program is free software: you can redistribute it and/or modify - * * it under the terms of the GNU General Public License as published by - * * the Free Software Foundation, either version 3 of the License, or - * * (at your option) any later version. - * * - * * This program is distributed in the hope that it will be useful, - * * but WITHOUT ANY WARRANTY; without even the implied warranty of - * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * * GNU General Public License for more details. - * * - * * You should have received a copy of the GNU General Public License - * * along with this program. If not, see - * * ***************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** */ package io.github.dsheirer.bits; import io.github.dsheirer.edac.CRC; +import java.util.BitSet; import org.apache.commons.lang3.Validate; import org.apache.commons.math3.util.FastMath; -import java.util.BitSet; - public class BinaryMessage extends BitSet { private static final long serialVersionUID = 1L; @@ -1038,6 +1034,51 @@ public static BinaryMessage load(String message) return buffer; } + /** + * Loads the hexadecimal text into a binary message + * @param hex text to load from + * @return loaded binary message + */ + public static BinaryMessage loadHex(String hex) + { + if(!hex.matches("[0-9A-F]*")) + { + throw new IllegalArgumentException("Message must contain only 0-9 and A-F hexadecimal characters"); + } + + int length = hex.length() / 2; + + if(length * 2 < hex.length()) + { + length++; + } + + BinaryMessage buffer = new BinaryMessage(length * 8); + + int bufferOffset = 0; + for(int x = 0; x < hex.length(); x += 2) + { + int endIndex = x + 2; + if(endIndex > hex.length()) + { + endIndex--; + } + + String rawHex = hex.substring(x, endIndex); + + if(rawHex.length() == 1) + { + rawHex = rawHex + "0"; + } + + int value = Integer.parseInt(rawHex, 16); + buffer.setByte(bufferOffset, (byte)(value & 0xFF)); + bufferOffset += 8; + } + + return buffer; + } + /** * Left rotates the bits between start and end indices, number of places. */ diff --git a/src/main/java/io/github/dsheirer/module/decode/ip/ars/DeviceRegistration.java b/src/main/java/io/github/dsheirer/module/decode/ip/ars/DeviceRegistration.java index e219f9c2e..61b4db8df 100644 --- a/src/main/java/io/github/dsheirer/module/decode/ip/ars/DeviceRegistration.java +++ b/src/main/java/io/github/dsheirer/module/decode/ip/ars/DeviceRegistration.java @@ -1,7 +1,6 @@ /* - * ****************************************************************************** - * sdrtrunk - * Copyright (C) 2014-2018 Dennis Sheirer + * ***************************************************************************** + * Copyright (C) 2014-2023 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see - * ***************************************************************************** + * **************************************************************************** */ package io.github.dsheirer.module.decode.ip.ars; @@ -25,11 +24,10 @@ import io.github.dsheirer.module.decode.ip.ars.identifier.ARSDevice; import io.github.dsheirer.module.decode.ip.ars.identifier.ARSPassword; import io.github.dsheirer.module.decode.ip.ars.identifier.ARSUser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Automatic Registration Service - Device Registration @@ -176,7 +174,7 @@ private void parsePayload() pointer += 8; - if(deviceIdentifierSize > 0) + if(deviceIdentifierSize > 0 && deviceIdentifierSize < 8) { StringBuilder sb = new StringBuilder(); @@ -186,21 +184,13 @@ private void parsePayload() pointer += 8; } - try - { - int device = Integer.parseInt(sb.toString()); - mDevice = ARSDevice.createFrom(device); - } - catch(Exception e) - { - mLog.error("Error parsing ARS device identifier from value [" + sb.toString() + "]"); - } + mDevice = ARSDevice.createFrom(sb.toString()); } int userIdentifierSize = getMessage().getInt(BYTE_VALUE, pointer); pointer += 8; - if(userIdentifierSize > 0) + if(userIdentifierSize > 0 && userIdentifierSize < 8) { StringBuilder sb = new StringBuilder(); @@ -216,7 +206,7 @@ private void parsePayload() int passwordSize = getMessage().getInt(BYTE_VALUE, pointer); pointer += 8; - if(passwordSize > 0) + if(passwordSize > 0 && passwordSize < 8) { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/io/github/dsheirer/module/decode/ip/ars/UserRegistration.java b/src/main/java/io/github/dsheirer/module/decode/ip/ars/UserRegistration.java index b42a86b80..fdf550264 100644 --- a/src/main/java/io/github/dsheirer/module/decode/ip/ars/UserRegistration.java +++ b/src/main/java/io/github/dsheirer/module/decode/ip/ars/UserRegistration.java @@ -1,7 +1,6 @@ /* - * ****************************************************************************** - * sdrtrunk - * Copyright (C) 2014-2018 Dennis Sheirer + * ***************************************************************************** + * Copyright (C) 2014-2023 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see - * ***************************************************************************** + * **************************************************************************** */ package io.github.dsheirer.module.decode.ip.ars; @@ -25,11 +24,10 @@ import io.github.dsheirer.module.decode.ip.ars.identifier.ARSDevice; import io.github.dsheirer.module.decode.ip.ars.identifier.ARSPassword; import io.github.dsheirer.module.decode.ip.ars.identifier.ARSUser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class UserRegistration extends ARSHeader { @@ -108,7 +106,7 @@ private void parsePayload() pointer += 8; - if(deviceIdentifierSize > 0) + if(deviceIdentifierSize > 0 && deviceIdentifierSize < 8) { StringBuilder sb = new StringBuilder(); @@ -118,22 +116,13 @@ private void parsePayload() pointer += 8; } - try - { - int device = Integer.parseInt(sb.toString()); - mDevice = ARSDevice.createFrom(device); - } - catch(Exception e) - { - //Abort processing - return; - } + mDevice = ARSDevice.createFrom(sb.toString()); } int userIdentifierSize = getMessage().getInt(BYTE_VALUE, pointer); pointer += 8; - if(userIdentifierSize > 0) + if(userIdentifierSize > 0 && deviceIdentifierSize < 8) { StringBuilder sb = new StringBuilder(); @@ -149,7 +138,7 @@ private void parsePayload() int passwordSize = getMessage().getInt(BYTE_VALUE, pointer); pointer += 8; - if(passwordSize > 0) + if(passwordSize > 0 && passwordSize < 8) { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/io/github/dsheirer/module/decode/ip/ars/identifier/ARSDevice.java b/src/main/java/io/github/dsheirer/module/decode/ip/ars/identifier/ARSDevice.java index 7909c7cc2..6547d7df0 100644 --- a/src/main/java/io/github/dsheirer/module/decode/ip/ars/identifier/ARSDevice.java +++ b/src/main/java/io/github/dsheirer/module/decode/ip/ars/identifier/ARSDevice.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2020 Dennis Sheirer + * Copyright (C) 2014-2023 Dennis Sheirer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,20 +22,20 @@ import io.github.dsheirer.identifier.Form; import io.github.dsheirer.identifier.IdentifierClass; import io.github.dsheirer.identifier.Role; -import io.github.dsheirer.identifier.integer.IntegerIdentifier; +import io.github.dsheirer.identifier.string.StringIdentifier; import io.github.dsheirer.protocol.Protocol; /** * Automatic Registration Service - Device Identifier */ -public class ARSDevice extends IntegerIdentifier +public class ARSDevice extends StringIdentifier { /** * Constructs an instance * @param value of the device identifier * @param role of the device (TO/FROM) */ - public ARSDevice(int value, Role role) + public ARSDevice(String value, Role role) { super(value, IdentifierClass.USER, Form.ARS_DEVICE, role); } @@ -49,7 +49,7 @@ public Protocol getProtocol() /** * Creates an ARS device with a FROM role */ - public static ARSDevice createFrom(int value) + public static ARSDevice createFrom(String value) { return new ARSDevice(value, Role.FROM); } @@ -57,7 +57,7 @@ public static ARSDevice createFrom(int value) /** * Creates an ARS device with a TO role */ - public static ARSDevice createTo(int value) + public static ARSDevice createTo(String value) { return new ARSDevice(value, Role.TO); }