You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2022/07/21 15:39:22 UTC
[plc4x] branch develop updated: feat(cbus): validate the checksum on read
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new cfffc808d feat(cbus): validate the checksum on read
cfffc808d is described below
commit cfffc808d294a3d4ca0766e970a967974c036be4
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Jul 21 17:39:15 2022 +0200
feat(cbus): validate the checksum on read
---
plc4go/protocols/cbus/readwrite/model/StaticHelper.go | 17 ++++++++++++-----
.../plc4x/java/cbus/readwrite/utils/StaticHelper.java | 17 ++++++++++++-----
2 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/plc4go/protocols/cbus/readwrite/model/StaticHelper.go b/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
index 0810292d8..740bc90f8 100644
--- a/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
+++ b/plc4go/protocols/cbus/readwrite/model/StaticHelper.go
@@ -123,16 +123,23 @@ func readBytesFromHex(logicalName string, readBuffer utils.ReadBuffer, payloadLe
readBuffer.Reset(readBuffer.GetPos() - 1)
hexBytes = hexBytes[:len(hexBytes)-1]
}
- if srchk {
- // We need to reset the last to hex bytes
- readBuffer.Reset(readBuffer.GetPos() - 2)
- hexBytes = hexBytes[:len(hexBytes)-2]
- }
rawBytes := make([]byte, hex.DecodedLen(len(hexBytes)))
n, err := hex.Decode(rawBytes, hexBytes)
if err != nil {
return nil, err
}
+ if srchk {
+ checksum := byte(0x0)
+ for _, aByte := range rawBytes {
+ checksum += aByte
+ }
+ if checksum != 0x0 {
+ return nil, errors.New("Checksum validation failed")
+ }
+ // We need to reset the last to hex bytes
+ readBuffer.Reset(readBuffer.GetPos() - 2)
+ rawBytes = rawBytes[:len(rawBytes)-1]
+ }
log.Debug().Msgf("%d bytes decoded", n)
return rawBytes, nil
}
diff --git a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
index 6f58d1aac..034eac9a9 100644
--- a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
+++ b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/readwrite/utils/StaticHelper.java
@@ -102,17 +102,24 @@ public class StaticHelper {
readBuffer.reset(readBuffer.getPos() - 1);
hexBytes = Arrays.copyOf(hexBytes, hexBytes.length - 1);
}
- if (srchk) {
- // We need to reset the last to hex bytes
- readBuffer.reset(readBuffer.getPos() - 2);
- hexBytes = Arrays.copyOf(hexBytes, hexBytes.length - 2);
- }
byte[] rawBytes;
try {
rawBytes = Hex.decodeHex(new String(hexBytes));
} catch (DecoderException e) {
throw new ParseException("error getting hex", e);
}
+ if (srchk) {
+ byte checksum = 0x0;
+ for (byte aByte : rawBytes) {
+ checksum+=aByte;
+ }
+ if (checksum != 0x0) {
+ throw new ParseException("Checksum validation failed");
+ }
+ // We need to reset the last to hex bytes
+ readBuffer.reset(readBuffer.getPos() - 2);
+ rawBytes = Arrays.copyOf(rawBytes, rawBytes.length - 1);
+ }
return rawBytes;
}