@netkitty/codec 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.2.1](https://github.com/myq1991/netkitty/compare/@netkitty/codec@1.2.0...@netkitty/codec@1.2.1) (2026-09-28)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **codec:** decode GOOSE allData INTEGER and BIT STRING items without data loss ([eeca0a5](https://github.com/myq1991/netkitty/commit/eeca0a54a942ed84917d37b5df10d77c83d5ad06))
12
+
13
+
14
+
15
+
16
+
6
17
  # [1.2.0](https://github.com/myq1991/netkitty/compare/@netkitty/codec@1.1.0...@netkitty/codec@1.2.0) (2026-07-23)
7
18
 
8
19
 
@@ -36,7 +36,7 @@ type INT32DataItem = {
36
36
  };
37
37
  type INT64DataItem = {
38
38
  dataType: ItemDataType.INT64;
39
- value: bigint;
39
+ value: string;
40
40
  };
41
41
  type INT8UDataItem = {
42
42
  dataType: ItemDataType.INT8U;
@@ -99,7 +99,37 @@ export declare class Goose extends BaseHeader {
99
99
  * @param dataTLVs
100
100
  * @protected
101
101
  */
102
- protected decodeDataTLVItem(dataTLVs: TLV[]): DataItem[];
102
+ protected decodeDataTLVItem(dataTLVs: TLV[], path: string): DataItem[];
103
+ /**
104
+ * Decode a BIT STRING (tag 0x84) data item
105
+ * CODED-ENUM is 2 octets and Quality 3 octets (the leading unused-bits octet included in the
106
+ * value). A 1-octet content is still a CODED-ENUM; longer bit strings are kept as a Quality bit
107
+ * string of the full content width so no bits are lost.
108
+ * @param value
109
+ * @param path
110
+ * @protected
111
+ */
112
+ protected decodeBitStringItem(value: Buffer, path: string): DataItem;
113
+ /**
114
+ * Decode a signed BER INTEGER (tag 0x85) data item
115
+ * The content is two's complement (X.690 8.3), so a leading 00 is the sign-extension byte of a
116
+ * positive value and must be kept (00 c8 = +200). The data type is inferred from the content
117
+ * length since the minimal BER encoding carries no declared width.
118
+ * @param value
119
+ * @param path
120
+ * @protected
121
+ */
122
+ protected decodeSignedIntegerItem(value: Buffer, path: string): DataItem;
123
+ /**
124
+ * Decode an unsigned BER INTEGER (tag 0x86) data item
125
+ * A leading 00 is dropped only when it is the mandatory positive-sign pad (next byte has its high
126
+ * bit set), so fixed-width encodings keep their declared width. The data type is then inferred
127
+ * from the remaining content length.
128
+ * @param value
129
+ * @param path
130
+ * @protected
131
+ */
132
+ protected decodeUnsignedIntegerItem(value: Buffer, path: string): DataItem;
103
133
  /**
104
134
  * Encode data TLV items
105
135
  * @param dataItems
@@ -14,7 +14,6 @@ const NumberToBERBuffer_1 = require("../helper/NumberToBERBuffer");
14
14
  const BufferToNumber_1 = require("../helper/BufferToNumber");
15
15
  const BufferToHex_1 = require("../helper/BufferToHex");
16
16
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
17
- const GetBERIntegerLengthFromBuffer_1 = require("../lib/GetBERIntegerLengthFromBuffer");
18
17
  var ItemDataType;
19
18
  (function (ItemDataType) {
20
19
  ItemDataType["Boolean"] = "Boolean";
@@ -720,7 +719,7 @@ class Goose extends BaseHeader_1.BaseHeader {
720
719
  decode: () => {
721
720
  const allDataTLV = this.TLVChild.find((tlv) => tlv.getTag('number') === 0xAB);
722
721
  const dataTLVs = allDataTLV ? allDataTLV.getChild() : [];
723
- const allData = this.decodeDataTLVItem(dataTLVs);
722
+ const allData = this.decodeDataTLVItem(dataTLVs, this.instance.goosePdu.allData.getPath());
724
723
  if (allData.length)
725
724
  this.instance.goosePdu.allData.setValue(allData);
726
725
  },
@@ -772,9 +771,10 @@ class Goose extends BaseHeader_1.BaseHeader {
772
771
  * @param dataTLVs
773
772
  * @protected
774
773
  */
775
- decodeDataTLVItem(dataTLVs) {
774
+ decodeDataTLVItem(dataTLVs, path) {
776
775
  const dataItems = [];
777
- dataTLVs.forEach((dataTLV) => {
776
+ dataTLVs.forEach((dataTLV, index) => {
777
+ const itemPath = `${path}[${index}]`;
778
778
  const length = dataTLV.getLength('number');
779
779
  const value = dataTLV.getValue('buffer');
780
780
  switch (dataTLV.getTag('number')) {
@@ -788,84 +788,17 @@ class Goose extends BaseHeader_1.BaseHeader {
788
788
  break;
789
789
  case 0x84:
790
790
  {
791
- switch (length) {
792
- case 2:
793
- {
794
- dataItems.push({
795
- dataType: ItemDataType.CODEDENUM,
796
- value: parseInt(value.toString('hex'), 16)
797
- });
798
- }
799
- break;
800
- case 3:
801
- {
802
- dataItems.push({
803
- dataType: ItemDataType.Quality,
804
- value: parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0')
805
- });
806
- }
807
- break;
808
- }
791
+ dataItems.push(this.decodeBitStringItem(value, itemPath));
809
792
  }
810
793
  break;
811
794
  case 0x85:
812
795
  {
813
- const integerRealLength = (0, GetBERIntegerLengthFromBuffer_1.GetBERIntegerLengthFromBuffer)(value);
814
- if (integerRealLength === 1) {
815
- dataItems.push({
816
- dataType: ItemDataType.INT8,
817
- value: (0, HexToNumber_1.HexToInt8)(value.toString('hex'))
818
- });
819
- }
820
- else if (integerRealLength === 2) {
821
- dataItems.push({
822
- dataType: ItemDataType.INT16,
823
- value: (0, HexToNumber_1.HexToInt16)(value.toString('hex'))
824
- });
825
- }
826
- else if (integerRealLength === 4) {
827
- dataItems.push({
828
- dataType: ItemDataType.INT32,
829
- value: (0, HexToNumber_1.HexToInt32)(value.toString('hex'))
830
- });
831
- }
832
- else if (integerRealLength === 8) {
833
- dataItems.push({
834
- dataType: ItemDataType.INT64,
835
- value: (0, HexToNumber_1.HexToInt64)(value.toString('hex'))
836
- });
837
- }
796
+ dataItems.push(this.decodeSignedIntegerItem(value, itemPath));
838
797
  }
839
798
  break;
840
799
  case 0x86:
841
800
  {
842
- const unsignedIntegerRealLength = (0, GetBERIntegerLengthFromBuffer_1.GetBERIntegerLengthFromBuffer)(value);
843
- switch (unsignedIntegerRealLength) {
844
- case 1:
845
- {
846
- dataItems.push({
847
- dataType: ItemDataType.INT8U,
848
- value: (0, HexToNumber_1.HexToUInt8)(value.toString('hex'))
849
- });
850
- }
851
- break;
852
- case 2:
853
- {
854
- dataItems.push({
855
- dataType: ItemDataType.INT16U,
856
- value: (0, HexToNumber_1.HexToUInt16)(value.toString('hex'))
857
- });
858
- }
859
- break;
860
- case 4:
861
- {
862
- dataItems.push({
863
- dataType: ItemDataType.INT32U,
864
- value: (0, HexToNumber_1.HexToUInt32)(value.toString('hex'))
865
- });
866
- }
867
- break;
868
- }
801
+ dataItems.push(this.decodeUnsignedIntegerItem(value, itemPath));
869
802
  }
870
803
  break;
871
804
  case 0x87:
@@ -906,7 +839,7 @@ class Goose extends BaseHeader_1.BaseHeader {
906
839
  const subDataTLVs = node_tlv_1.default.parseList(value);
907
840
  dataItems.push({
908
841
  dataType: ItemDataType.Structure,
909
- value: this.decodeDataTLVItem(subDataTLVs)
842
+ value: this.decodeDataTLVItem(subDataTLVs, `${itemPath}.value`)
910
843
  });
911
844
  }
912
845
  break;
@@ -914,6 +847,79 @@ class Goose extends BaseHeader_1.BaseHeader {
914
847
  });
915
848
  return dataItems;
916
849
  }
850
+ /**
851
+ * Decode a BIT STRING (tag 0x84) data item
852
+ * CODED-ENUM is 2 octets and Quality 3 octets (the leading unused-bits octet included in the
853
+ * value). A 1-octet content is still a CODED-ENUM; longer bit strings are kept as a Quality bit
854
+ * string of the full content width so no bits are lost.
855
+ * @param value
856
+ * @param path
857
+ * @protected
858
+ */
859
+ decodeBitStringItem(value, path) {
860
+ if (!value.length) {
861
+ this.recordError(path, 'Empty BIT STRING content, decoded as 0');
862
+ return { dataType: ItemDataType.CODEDENUM, value: 0 };
863
+ }
864
+ if (value.length <= 2)
865
+ return { dataType: ItemDataType.CODEDENUM, value: parseInt(value.toString('hex'), 16) };
866
+ return {
867
+ dataType: ItemDataType.Quality,
868
+ //BigInt so bit strings wider than 53 bits keep every bit.
869
+ value: BigInt(`0x${value.toString('hex')}`).toString(2).padStart(value.length * 8, '0')
870
+ };
871
+ }
872
+ /**
873
+ * Decode a signed BER INTEGER (tag 0x85) data item
874
+ * The content is two's complement (X.690 8.3), so a leading 00 is the sign-extension byte of a
875
+ * positive value and must be kept (00 c8 = +200). The data type is inferred from the content
876
+ * length since the minimal BER encoding carries no declared width.
877
+ * @param value
878
+ * @param path
879
+ * @protected
880
+ */
881
+ decodeSignedIntegerItem(value, path) {
882
+ if (!value.length) {
883
+ this.recordError(path, 'Empty INTEGER content, decoded as 0');
884
+ return { dataType: ItemDataType.INT8, value: 0 };
885
+ }
886
+ let intValue = BigInt(`0x${value.toString('hex')}`);
887
+ if (value[0] & 0x80)
888
+ intValue -= 1n << BigInt(value.length * 8);
889
+ if (value.length === 1)
890
+ return { dataType: ItemDataType.INT8, value: Number(intValue) };
891
+ if (value.length === 2)
892
+ return { dataType: ItemDataType.INT16, value: Number(intValue) };
893
+ if (value.length <= 4)
894
+ return { dataType: ItemDataType.INT32, value: Number(intValue) };
895
+ if (value.length > 8)
896
+ this.recordError(path, 'INTEGER exceeds 64 bits');
897
+ return { dataType: ItemDataType.INT64, value: intValue.toString() };
898
+ }
899
+ /**
900
+ * Decode an unsigned BER INTEGER (tag 0x86) data item
901
+ * A leading 00 is dropped only when it is the mandatory positive-sign pad (next byte has its high
902
+ * bit set), so fixed-width encodings keep their declared width. The data type is then inferred
903
+ * from the remaining content length.
904
+ * @param value
905
+ * @param path
906
+ * @protected
907
+ */
908
+ decodeUnsignedIntegerItem(value, path) {
909
+ if (!value.length) {
910
+ this.recordError(path, 'Empty INTEGER content, decoded as 0');
911
+ return { dataType: ItemDataType.INT8U, value: 0 };
912
+ }
913
+ const content = value.length > 1 && value[0] === 0x00 && value[1] & 0x80 ? value.subarray(1) : value;
914
+ const uintValue = Number(BigInt(`0x${content.toString('hex')}`));
915
+ if (content.length === 1)
916
+ return { dataType: ItemDataType.INT8U, value: uintValue };
917
+ if (content.length === 2)
918
+ return { dataType: ItemDataType.INT16U, value: uintValue };
919
+ if (content.length > 4)
920
+ this.recordError(path, 'Unsigned INTEGER exceeds 32 bits');
921
+ return { dataType: ItemDataType.INT32U, value: uintValue };
922
+ }
917
923
  /**
918
924
  * Encode data TLV items
919
925
  * @param dataItems
@@ -1000,7 +1006,13 @@ class Goose extends BaseHeader_1.BaseHeader {
1000
1006
  if (isNaN(codedEnumValue))
1001
1007
  this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
1002
1008
  codedEnumValue = codedEnumValue ? codedEnumValue : 0;
1003
- return new node_tlv_1.default(0x84, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(codedEnumValue), 'hex'));
1009
+ if (codedEnumValue < 0 || codedEnumValue > 65535) {
1010
+ this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
1011
+ codedEnumValue = Math.min(Math.max(codedEnumValue, 0), 65535);
1012
+ }
1013
+ //A BIT STRING, not an INTEGER: always 2 octets and never sign-padded, so it
1014
+ //re-decodes as CODED-ENUM (a 1-octet or 00-padded 3-octet form did not).
1015
+ return new node_tlv_1.default(0x84, Buffer.from((0, NumberToHex_1.UInt16ToHex)(codedEnumValue), 'hex'));
1004
1016
  }
1005
1017
  case ItemDataType.OCTETSTRING: {
1006
1018
  const hexText = dataItem.value;
@@ -1040,12 +1052,15 @@ class Goose extends BaseHeader_1.BaseHeader {
1040
1052
  return new node_tlv_1.default(0x91, Buffer.from(timestamp.toString(16).padStart(8 * 2, '0'), 'hex'));
1041
1053
  }
1042
1054
  case ItemDataType.Quality: {
1043
- const bitString = dataItem.value;
1044
- let intValue = parseInt(bitString, 2);
1045
- if (isNaN(intValue))
1055
+ let bitString = `${dataItem.value}`;
1056
+ if (!/^[01]+$/.test(bitString)) {
1046
1057
  this.recordError(errorNodePath, 'Invalid Quality value');
1047
- intValue = intValue ? intValue : 0;
1048
- return new node_tlv_1.default(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'));
1058
+ bitString = '0';
1059
+ }
1060
+ //At least 3 octets; a wider bit string keeps its full width. BigInt + padStart
1061
+ //avoids the odd-length hex that Buffer.from(...,'hex') silently truncated.
1062
+ const octets = Math.max(3, Math.ceil(bitString.length / 8));
1063
+ return new node_tlv_1.default(0x84, Buffer.from(BigInt(`0b${bitString}`).toString(16).padStart(octets * 2, '0'), 'hex'));
1049
1064
  }
1050
1065
  case ItemDataType.Structure: {
1051
1066
  return new node_tlv_1.default(0xa2, Buffer.concat(this.encodeDataTLVItem(dataItem.value).map((tlv) => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netkitty/codec",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Schema-driven packet codec: encode/decode 180+ network and industrial (OT/ICS/SCADA) protocol headers — TCP/IP, TLS, HTTP/2, DNS, MQTT, plus Modbus, DNP3, IEC 61850 (GOOSE/SV/MMS), IEC 104, S7comm, OPC UA, PROFINET, EtherCAT, BACnet and more.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -138,5 +138,5 @@
138
138
  },
139
139
  "homepage": "https://github.com/myq1991/netkitty/tree/main/packages/codec#readme",
140
140
  "bugs": "https://github.com/myq1991/netkitty/issues",
141
- "gitHead": "52958b705354ac379a1a6d90a3955773c190d65a"
141
+ "gitHead": "519fdefcb5c76f69fade408cc36e0c2fa69ccad4"
142
142
  }
@@ -1,6 +0,0 @@
1
- /**
2
- * Get BER encoded integer real length from buffer
3
- * @param buffer
4
- * @constructor
5
- */
6
- export declare function GetBERIntegerLengthFromBuffer(buffer: Buffer): number;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GetBERIntegerLengthFromBuffer = GetBERIntegerLengthFromBuffer;
4
- /**
5
- * Get BER encoded integer real length from buffer
6
- * @param buffer
7
- * @constructor
8
- */
9
- function GetBERIntegerLengthFromBuffer(buffer) {
10
- if (buffer[0] === 0) {
11
- return buffer.length - 1;
12
- }
13
- else {
14
- return buffer.length;
15
- }
16
- }