@netkitty/codec 1.2.0 → 2.0.0

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.
Files changed (179) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/README.md +58 -10
  3. package/README.zh-CN.md +52 -9
  4. package/dist/Codec.d.ts +3 -2
  5. package/dist/Codec.js +106 -10
  6. package/dist/abstracts/BaseHeader.d.ts +28 -2
  7. package/dist/abstracts/BaseHeader.js +132 -43
  8. package/dist/errors/CodecSchemaValidateError.d.ts +34 -1
  9. package/dist/errors/CodecSchemaValidateError.js +26 -3
  10. package/dist/headers/ADSAMS.js +2 -0
  11. package/dist/headers/AH.js +1 -0
  12. package/dist/headers/AMQP.js +2 -1
  13. package/dist/headers/ARP.js +16 -10
  14. package/dist/headers/BACnetIP.js +1 -0
  15. package/dist/headers/BGP.js +1 -0
  16. package/dist/headers/Babel.js +2 -1
  17. package/dist/headers/BitTorrent.d.ts +2 -0
  18. package/dist/headers/BitTorrent.js +4 -2
  19. package/dist/headers/C37118.js +1 -0
  20. package/dist/headers/CAPWAP.js +1 -0
  21. package/dist/headers/CDP.js +1 -1
  22. package/dist/headers/CMS.js +2 -2
  23. package/dist/headers/COTP.d.ts +2 -0
  24. package/dist/headers/COTP.js +6 -1
  25. package/dist/headers/CassandraCQL.js +1 -0
  26. package/dist/headers/Codesys.js +1 -0
  27. package/dist/headers/Collectd.js +1 -1
  28. package/dist/headers/DCCP.js +1 -0
  29. package/dist/headers/DHCPFailover.js +1 -0
  30. package/dist/headers/DNS.js +3 -3
  31. package/dist/headers/DTLS.js +1 -0
  32. package/dist/headers/Diameter.js +3 -2
  33. package/dist/headers/EAPOL.js +1 -1
  34. package/dist/headers/EIGRP.js +1 -1
  35. package/dist/headers/ENIP.js +1 -0
  36. package/dist/headers/Elasticsearch.js +1 -0
  37. package/dist/headers/EtherCAT.js +1 -0
  38. package/dist/headers/EthernetII.js +6 -0
  39. package/dist/headers/FTP.js +6 -6
  40. package/dist/headers/Finger.js +3 -3
  41. package/dist/headers/FoundationFieldbusHSE.js +1 -0
  42. package/dist/headers/GELF.js +7 -1
  43. package/dist/headers/GENEVE.js +1 -1
  44. package/dist/headers/GLBP.js +1 -1
  45. package/dist/headers/GTPU.js +1 -0
  46. package/dist/headers/GTPv2C.js +1 -0
  47. package/dist/headers/GitProtocol.js +2 -2
  48. package/dist/headers/Goose.d.ts +32 -2
  49. package/dist/headers/Goose.js +116 -89
  50. package/dist/headers/Gopher.js +6 -6
  51. package/dist/headers/HARTIP.js +1 -0
  52. package/dist/headers/HSR.js +2 -0
  53. package/dist/headers/HTTP.js +6 -6
  54. package/dist/headers/HTTP2.js +1 -0
  55. package/dist/headers/ICMP.js +1 -0
  56. package/dist/headers/ICMPv6.js +1 -0
  57. package/dist/headers/IEC104_I_Frame.js +26 -3
  58. package/dist/headers/IEC104_S_Frame.js +7 -2
  59. package/dist/headers/IEC104_U_Frame.js +14 -3
  60. package/dist/headers/IEC61850SampledValues.js +2 -1
  61. package/dist/headers/IGMP.js +1 -0
  62. package/dist/headers/IMAP.js +5 -5
  63. package/dist/headers/IPFIX.js +2 -1
  64. package/dist/headers/IPv4.js +18 -1
  65. package/dist/headers/IPv6.js +1 -0
  66. package/dist/headers/IPv6HopByHopOptions.js +12 -11
  67. package/dist/headers/IRC.js +4 -4
  68. package/dist/headers/ISAKMP.js +2 -1
  69. package/dist/headers/ISCSI.js +2 -0
  70. package/dist/headers/ISIS.js +2 -1
  71. package/dist/headers/ISOSession.d.ts +2 -0
  72. package/dist/headers/ISOSession.js +14 -10
  73. package/dist/headers/Ident.js +8 -8
  74. package/dist/headers/KNXnetIP.js +1 -0
  75. package/dist/headers/Kafka.js +1 -0
  76. package/dist/headers/Kerberos.js +1 -0
  77. package/dist/headers/L2TP.js +2 -2
  78. package/dist/headers/LACP.js +1 -1
  79. package/dist/headers/LDAP.js +2 -3
  80. package/dist/headers/LDP.js +1 -0
  81. package/dist/headers/LLC.d.ts +2 -0
  82. package/dist/headers/LLC.js +2 -0
  83. package/dist/headers/LPD.js +3 -3
  84. package/dist/headers/MGCP.js +7 -7
  85. package/dist/headers/MMS.d.ts +2 -0
  86. package/dist/headers/MMS.js +10 -6
  87. package/dist/headers/MQTT.js +1 -0
  88. package/dist/headers/MQTTSN.js +1 -0
  89. package/dist/headers/MarkerProtocol.js +1 -1
  90. package/dist/headers/Megaco.js +3 -3
  91. package/dist/headers/Memcached.js +3 -2
  92. package/dist/headers/ModbusTCP.js +1 -0
  93. package/dist/headers/ModbusUDP.js +1 -0
  94. package/dist/headers/MongoDB.js +1 -0
  95. package/dist/headers/MySQL.js +1 -0
  96. package/dist/headers/NATS.js +2 -2
  97. package/dist/headers/NBDS.js +1 -0
  98. package/dist/headers/NBSS.js +1 -0
  99. package/dist/headers/NFS.js +1 -0
  100. package/dist/headers/NHRP.js +1 -0
  101. package/dist/headers/NNTP.js +6 -6
  102. package/dist/headers/NetFlowV5.js +1 -0
  103. package/dist/headers/OLSR.js +2 -1
  104. package/dist/headers/OPCUA.js +1 -0
  105. package/dist/headers/OpenFlow.js +1 -0
  106. package/dist/headers/OpenVPN.js +1 -0
  107. package/dist/headers/PCWorx.js +1 -0
  108. package/dist/headers/POP3.js +5 -5
  109. package/dist/headers/PPPoEDiscovery.js +2 -1
  110. package/dist/headers/PPPoESession.js +1 -0
  111. package/dist/headers/PPTP.js +1 -0
  112. package/dist/headers/PTP.js +1 -0
  113. package/dist/headers/PostgreSQL.js +2 -0
  114. package/dist/headers/QUIC.js +2 -0
  115. package/dist/headers/RADIUS.js +1 -0
  116. package/dist/headers/RDP.d.ts +2 -0
  117. package/dist/headers/RDP.js +4 -1
  118. package/dist/headers/RFB.js +4 -4
  119. package/dist/headers/RGoose.js +6 -1
  120. package/dist/headers/RSVP.js +3 -2
  121. package/dist/headers/RTCP.js +1 -0
  122. package/dist/headers/RTP.js +2 -1
  123. package/dist/headers/RTPS.d.ts +2 -0
  124. package/dist/headers/RTPS.js +4 -1
  125. package/dist/headers/RTSP.js +7 -7
  126. package/dist/headers/RawData.js +1 -1
  127. package/dist/headers/Redis.js +4 -4
  128. package/dist/headers/Rlogin.js +5 -5
  129. package/dist/headers/Rsync.js +2 -2
  130. package/dist/headers/S7comm.d.ts +2 -0
  131. package/dist/headers/S7comm.js +5 -1
  132. package/dist/headers/SCTP.js +2 -2
  133. package/dist/headers/SFlow.js +2 -1
  134. package/dist/headers/SIP.js +6 -6
  135. package/dist/headers/SLMP.js +1 -0
  136. package/dist/headers/SLP.js +2 -0
  137. package/dist/headers/SLPv1.js +1 -0
  138. package/dist/headers/SMB1.js +1 -0
  139. package/dist/headers/SMB2.js +1 -0
  140. package/dist/headers/SMPP.js +1 -0
  141. package/dist/headers/SMTP.js +6 -6
  142. package/dist/headers/SNAP.js +18 -5
  143. package/dist/headers/SNMP.js +4 -4
  144. package/dist/headers/SOCKS4.js +11 -5
  145. package/dist/headers/SSDP.js +7 -7
  146. package/dist/headers/SSH.js +5 -4
  147. package/dist/headers/STOMP.js +5 -5
  148. package/dist/headers/STUN.js +2 -0
  149. package/dist/headers/Skinny.js +1 -0
  150. package/dist/headers/StatsD.js +4 -4
  151. package/dist/headers/SunRPC.js +1 -0
  152. package/dist/headers/Syslog.js +2 -2
  153. package/dist/headers/TCP.js +18 -6
  154. package/dist/headers/TLS_Alert.js +23 -11
  155. package/dist/headers/TLS_ApplicationData.js +9 -4
  156. package/dist/headers/TLS_ChangeCipherSpec.js +10 -5
  157. package/dist/headers/TLS_Handshake.js +41 -26
  158. package/dist/headers/TLS_Heartbeat.js +10 -1
  159. package/dist/headers/TPKT.js +3 -1
  160. package/dist/headers/TacacsPlus.js +1 -0
  161. package/dist/headers/Telnet.js +2 -1
  162. package/dist/headers/Teredo.js +2 -2
  163. package/dist/headers/UDP.js +8 -0
  164. package/dist/headers/VLAN_802dot1Q.js +7 -0
  165. package/dist/headers/WCCP.js +1 -0
  166. package/dist/headers/WHOIS.js +3 -3
  167. package/dist/headers/WSDiscovery.js +2 -2
  168. package/dist/headers/XMPP.js +3 -3
  169. package/dist/headers/Zabbix.js +1 -0
  170. package/dist/index.d.ts +3 -3
  171. package/dist/lib/SchemaValidator.d.ts +31 -0
  172. package/dist/lib/SchemaValidator.js +137 -0
  173. package/dist/schema/ProtocolFieldJSONSchema.d.ts +4 -1
  174. package/dist/schema/ProtocolJSONSchema.d.ts +1 -1
  175. package/dist/types/CodecErrorInfo.d.ts +11 -0
  176. package/dist/types/LayerGraph.d.ts +14 -1
  177. package/package.json +2 -2
  178. package/dist/lib/GetBERIntegerLengthFromBuffer.d.ts +0 -6
  179. package/dist/lib/GetBERIntegerLengthFromBuffer.js +0 -16
@@ -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";
@@ -58,6 +57,7 @@ class Goose extends BaseHeader_1.BaseHeader {
58
57
  type: 'integer',
59
58
  minimum: 0,
60
59
  maximum: 65535,
60
+ autoCalculated: true,
61
61
  label: 'Length',
62
62
  decode: () => {
63
63
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(2, 2)));
@@ -134,9 +134,10 @@ class Goose extends BaseHeader_1.BaseHeader {
134
134
  try {
135
135
  const buffer = this.readBytes(8, readTLVBufferLength, true);
136
136
  this.TLVInstance = node_tlv_1.default.parse(buffer);
137
- const tagWithLength = buffer.toString('hex').replace(this.TLVInstance.bValue.toString('hex'), '#').split('#');
138
- const tagWithLengthBytes = Math.max(...tagWithLength.map((value) => Math.ceil(value.length / 2)));
139
- const TLVBufferLength = this.TLVInstance.bValue.length + tagWithLengthBytes;
137
+ //Consume exactly the outer TLV's own span (tag + length + value). Anything after
138
+ //it (Ethernet padding, trailing bytes) is not part of the goosePdu and must be
139
+ //left for the next layer (raw) so it survives re-encode.
140
+ const TLVBufferLength = this.TLVInstance.bTag.length + this.TLVInstance.bLength.length + this.TLVInstance.bValue.length;
140
141
  this.TLVInstance = node_tlv_1.default.parse(this.readBytes(8, TLVBufferLength, false));
141
142
  }
142
143
  catch (e) {
@@ -189,7 +190,7 @@ class Goose extends BaseHeader_1.BaseHeader {
189
190
  properties: {
190
191
  gocbRef: {
191
192
  type: 'string',
192
- maxLength: 129,
193
+ //Not a hard maxLength: a decoded over-long value must re-encode; encode flags it instead.
193
194
  label: 'GoCBReference',
194
195
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.ASCII,
195
196
  decode: () => {
@@ -235,7 +236,7 @@ class Goose extends BaseHeader_1.BaseHeader {
235
236
  },
236
237
  datSet: {
237
238
  type: 'string',
238
- maxLength: 129,
239
+ //Not a hard maxLength: a decoded over-long value must re-encode; encode flags it instead.
239
240
  label: 'DatSet',
240
241
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.ASCII,
241
242
  decode: () => {
@@ -258,7 +259,7 @@ class Goose extends BaseHeader_1.BaseHeader {
258
259
  },
259
260
  goID: {
260
261
  type: 'string',
261
- maxLength: 65,
262
+ //Not a hard maxLength: a decoded over-long value must re-encode; encode flags it instead.
262
263
  label: 'GoID',
263
264
  contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.ASCII,
264
265
  decode: () => {
@@ -720,7 +721,7 @@ class Goose extends BaseHeader_1.BaseHeader {
720
721
  decode: () => {
721
722
  const allDataTLV = this.TLVChild.find((tlv) => tlv.getTag('number') === 0xAB);
722
723
  const dataTLVs = allDataTLV ? allDataTLV.getChild() : [];
723
- const allData = this.decodeDataTLVItem(dataTLVs);
724
+ const allData = this.decodeDataTLVItem(dataTLVs, this.instance.goosePdu.allData.getPath());
724
725
  if (allData.length)
725
726
  this.instance.goosePdu.allData.setValue(allData);
726
727
  },
@@ -772,9 +773,10 @@ class Goose extends BaseHeader_1.BaseHeader {
772
773
  * @param dataTLVs
773
774
  * @protected
774
775
  */
775
- decodeDataTLVItem(dataTLVs) {
776
+ decodeDataTLVItem(dataTLVs, path) {
776
777
  const dataItems = [];
777
- dataTLVs.forEach((dataTLV) => {
778
+ dataTLVs.forEach((dataTLV, index) => {
779
+ const itemPath = `${path}[${index}]`;
778
780
  const length = dataTLV.getLength('number');
779
781
  const value = dataTLV.getValue('buffer');
780
782
  switch (dataTLV.getTag('number')) {
@@ -788,84 +790,17 @@ class Goose extends BaseHeader_1.BaseHeader {
788
790
  break;
789
791
  case 0x84:
790
792
  {
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
- }
793
+ dataItems.push(this.decodeBitStringItem(value, itemPath));
809
794
  }
810
795
  break;
811
796
  case 0x85:
812
797
  {
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
- }
798
+ dataItems.push(this.decodeSignedIntegerItem(value, itemPath));
838
799
  }
839
800
  break;
840
801
  case 0x86:
841
802
  {
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
- }
803
+ dataItems.push(this.decodeUnsignedIntegerItem(value, itemPath));
869
804
  }
870
805
  break;
871
806
  case 0x87:
@@ -906,7 +841,7 @@ class Goose extends BaseHeader_1.BaseHeader {
906
841
  const subDataTLVs = node_tlv_1.default.parseList(value);
907
842
  dataItems.push({
908
843
  dataType: ItemDataType.Structure,
909
- value: this.decodeDataTLVItem(subDataTLVs)
844
+ value: this.decodeDataTLVItem(subDataTLVs, `${itemPath}.value`)
910
845
  });
911
846
  }
912
847
  break;
@@ -914,6 +849,79 @@ class Goose extends BaseHeader_1.BaseHeader {
914
849
  });
915
850
  return dataItems;
916
851
  }
852
+ /**
853
+ * Decode a BIT STRING (tag 0x84) data item
854
+ * CODED-ENUM is 2 octets and Quality 3 octets (the leading unused-bits octet included in the
855
+ * value). A 1-octet content is still a CODED-ENUM; longer bit strings are kept as a Quality bit
856
+ * string of the full content width so no bits are lost.
857
+ * @param value
858
+ * @param path
859
+ * @protected
860
+ */
861
+ decodeBitStringItem(value, path) {
862
+ if (!value.length) {
863
+ this.recordError(path, 'Empty BIT STRING content, decoded as 0');
864
+ return { dataType: ItemDataType.CODEDENUM, value: 0 };
865
+ }
866
+ if (value.length <= 2)
867
+ return { dataType: ItemDataType.CODEDENUM, value: parseInt(value.toString('hex'), 16) };
868
+ return {
869
+ dataType: ItemDataType.Quality,
870
+ //BigInt so bit strings wider than 53 bits keep every bit.
871
+ value: BigInt(`0x${value.toString('hex')}`).toString(2).padStart(value.length * 8, '0')
872
+ };
873
+ }
874
+ /**
875
+ * Decode a signed BER INTEGER (tag 0x85) data item
876
+ * The content is two's complement (X.690 8.3), so a leading 00 is the sign-extension byte of a
877
+ * positive value and must be kept (00 c8 = +200). The data type is inferred from the content
878
+ * length since the minimal BER encoding carries no declared width.
879
+ * @param value
880
+ * @param path
881
+ * @protected
882
+ */
883
+ decodeSignedIntegerItem(value, path) {
884
+ if (!value.length) {
885
+ this.recordError(path, 'Empty INTEGER content, decoded as 0');
886
+ return { dataType: ItemDataType.INT8, value: 0 };
887
+ }
888
+ let intValue = BigInt(`0x${value.toString('hex')}`);
889
+ if (value[0] & 0x80)
890
+ intValue -= 1n << BigInt(value.length * 8);
891
+ if (value.length === 1)
892
+ return { dataType: ItemDataType.INT8, value: Number(intValue) };
893
+ if (value.length === 2)
894
+ return { dataType: ItemDataType.INT16, value: Number(intValue) };
895
+ if (value.length <= 4)
896
+ return { dataType: ItemDataType.INT32, value: Number(intValue) };
897
+ if (value.length > 8)
898
+ this.recordError(path, 'INTEGER exceeds 64 bits');
899
+ return { dataType: ItemDataType.INT64, value: intValue.toString() };
900
+ }
901
+ /**
902
+ * Decode an unsigned BER INTEGER (tag 0x86) data item
903
+ * A leading 00 is dropped only when it is the mandatory positive-sign pad (next byte has its high
904
+ * bit set), so fixed-width encodings keep their declared width. The data type is then inferred
905
+ * from the remaining content length.
906
+ * @param value
907
+ * @param path
908
+ * @protected
909
+ */
910
+ decodeUnsignedIntegerItem(value, path) {
911
+ if (!value.length) {
912
+ this.recordError(path, 'Empty INTEGER content, decoded as 0');
913
+ return { dataType: ItemDataType.INT8U, value: 0 };
914
+ }
915
+ const content = value.length > 1 && value[0] === 0x00 && value[1] & 0x80 ? value.subarray(1) : value;
916
+ const uintValue = Number(BigInt(`0x${content.toString('hex')}`));
917
+ if (content.length === 1)
918
+ return { dataType: ItemDataType.INT8U, value: uintValue };
919
+ if (content.length === 2)
920
+ return { dataType: ItemDataType.INT16U, value: uintValue };
921
+ if (content.length > 4)
922
+ this.recordError(path, 'Unsigned INTEGER exceeds 32 bits');
923
+ return { dataType: ItemDataType.INT32U, value: uintValue };
924
+ }
917
925
  /**
918
926
  * Encode data TLV items
919
927
  * @param dataItems
@@ -956,7 +964,13 @@ class Goose extends BaseHeader_1.BaseHeader {
956
964
  return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int32ToBERHex)(intValue), 'hex'));
957
965
  }
958
966
  case ItemDataType.INT64: {
959
- const intValue = BigInt(dataItem.value);
967
+ let intValue = 0n;
968
+ try {
969
+ intValue = BigInt(dataItem.value ?? 0);
970
+ }
971
+ catch (e) {
972
+ this.recordError(errorNodePath, 'Invalid INT64 value');
973
+ }
960
974
  if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807'))
961
975
  this.recordError(errorNodePath, 'Invalid INT64 value');
962
976
  return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int64ToBERHex)(intValue), 'hex'));
@@ -1000,7 +1014,13 @@ class Goose extends BaseHeader_1.BaseHeader {
1000
1014
  if (isNaN(codedEnumValue))
1001
1015
  this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
1002
1016
  codedEnumValue = codedEnumValue ? codedEnumValue : 0;
1003
- return new node_tlv_1.default(0x84, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(codedEnumValue), 'hex'));
1017
+ if (codedEnumValue < 0 || codedEnumValue > 65535) {
1018
+ this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
1019
+ codedEnumValue = Math.min(Math.max(codedEnumValue, 0), 65535);
1020
+ }
1021
+ //A BIT STRING, not an INTEGER: always 2 octets and never sign-padded, so it
1022
+ //re-decodes as CODED-ENUM (a 1-octet or 00-padded 3-octet form did not).
1023
+ return new node_tlv_1.default(0x84, Buffer.from((0, NumberToHex_1.UInt16ToHex)(codedEnumValue), 'hex'));
1004
1024
  }
1005
1025
  case ItemDataType.OCTETSTRING: {
1006
1026
  const hexText = dataItem.value;
@@ -1040,15 +1060,22 @@ class Goose extends BaseHeader_1.BaseHeader {
1040
1060
  return new node_tlv_1.default(0x91, Buffer.from(timestamp.toString(16).padStart(8 * 2, '0'), 'hex'));
1041
1061
  }
1042
1062
  case ItemDataType.Quality: {
1043
- const bitString = dataItem.value;
1044
- let intValue = parseInt(bitString, 2);
1045
- if (isNaN(intValue))
1063
+ let bitString = `${dataItem.value}`;
1064
+ if (!/^[01]+$/.test(bitString)) {
1046
1065
  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'));
1066
+ bitString = '0';
1067
+ }
1068
+ //At least 3 octets; a wider bit string keeps its full width. BigInt + padStart
1069
+ //avoids the odd-length hex that Buffer.from(...,'hex') silently truncated.
1070
+ const octets = Math.max(3, Math.ceil(bitString.length / 8));
1071
+ return new node_tlv_1.default(0x84, Buffer.from(BigInt(`0b${bitString}`).toString(16).padStart(octets * 2, '0'), 'hex'));
1049
1072
  }
1050
1073
  case ItemDataType.Structure: {
1051
- return new node_tlv_1.default(0xa2, Buffer.concat(this.encodeDataTLVItem(dataItem.value).map((tlv) => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))));
1074
+ //A Structure without members (value omitted) is an empty SEQUENCE, not an error.
1075
+ const members = Array.isArray(dataItem.value) ? dataItem.value : [];
1076
+ if (dataItem.value !== undefined && !Array.isArray(dataItem.value))
1077
+ this.recordError(errorNodePath, 'Invalid Structure value, encoded as empty');
1078
+ return new node_tlv_1.default(0xa2, Buffer.concat(this.encodeDataTLVItem(members).map((tlv) => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))));
1052
1079
  }
1053
1080
  default: {
1054
1081
  this.recordError(errorNodePath, 'Invalid dataType, ignored');
@@ -133,13 +133,13 @@ class Gopher extends BaseHeader_1.BaseHeader {
133
133
  //Display-only metadata parsed on decode (no encode — populated by the `message` field
134
134
  //below, never read back). isRequest is decided by the TCP port direction; selector and
135
135
  //searchTerm apply to a request; itemCount to a response; hasTerminator flags the "." end.
136
- isRequest: { type: 'boolean', label: 'Is Request', default: false },
137
- selector: { type: 'string', label: 'Selector', default: '' },
138
- searchTerm: { type: 'string', label: 'Search Term', default: '' },
139
- itemCount: { type: 'integer', label: 'Item Count', minimum: 0, default: 0 },
140
- hasTerminator: { type: 'boolean', label: 'Has Terminator', default: false },
136
+ isRequest: { type: 'boolean', label: 'Is Request', readOnly: true, default: false },
137
+ selector: { type: 'string', label: 'Selector', readOnly: true, default: '' },
138
+ searchTerm: { type: 'string', label: 'Search Term', readOnly: true, default: '' },
139
+ itemCount: { type: 'integer', label: 'Item Count', readOnly: true, minimum: 0, default: 0 },
140
+ hasTerminator: { type: 'boolean', label: 'Has Terminator', readOnly: true, default: false },
141
141
  //Drives the one-line summary (request selector / response line count).
142
- summaryInfo: { type: 'string', label: 'Summary', hidden: true, default: '' },
142
+ summaryInfo: { type: 'string', label: 'Summary', readOnly: true, hidden: true, default: '' },
143
143
  //The whole raw payload is the single source of truth: decoded verbatim to hex and
144
144
  //re-emitted untouched (byte-perfect for any Gopher payload). The first line is parsed into
145
145
  //the display-only metadata above, which carry no codec of their own.
@@ -75,6 +75,7 @@ class HARTIP extends BaseHeader_1.BaseHeader {
75
75
  sequenceNumber: this.fieldUInt('sequenceNumber', 4, 2, 'Sequence Number'),
76
76
  byteCount: {
77
77
  type: 'integer',
78
+ autoCalculated: true,
78
79
  label: 'Byte Count',
79
80
  //minimum 0 (not 8): a crafted/corrupt message may carry a Byte Count below the 8-byte
80
81
  //header, and such a value must round-trip (honored verbatim) rather than being rejected
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HSR = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
+ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
5
6
  const NumberToHex_1 = require("../helper/NumberToHex");
6
7
  const BufferToHex_1 = require("../helper/BufferToHex");
7
8
  /**
@@ -67,6 +68,7 @@ class HSR extends BaseHeader_1.BaseHeader {
67
68
  minLength: 4,
68
69
  maxLength: 4,
69
70
  label: 'EtherType',
71
+ contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX,
70
72
  //The ORIGINAL EtherType of the carried frame. MUST be named `etherType` and stored as a
71
73
  //lowercase 4-hex string exactly like VLAN/Ethernet, because L2 children (GOOSE, SV,
72
74
  //VLAN, ARP) discriminate by reading prevCodecModule.instance.etherType — a different
@@ -119,12 +119,12 @@ class HTTP extends BaseHeader_1.BaseHeader {
119
119
  //Display-only metadata parsed from the start-line on decode (no encode — populated by the
120
120
  //message field above, never read back). isRequest distinguishes a Request-Line from a
121
121
  //Status-Line; method/requestUri are for requests, statusCode/reasonPhrase for responses.
122
- isRequest: { type: 'boolean', label: 'Is Request' },
123
- method: { type: 'string', label: 'Method' },
124
- requestUri: { type: 'string', label: 'Request URI' },
125
- version: { type: 'string', label: 'Version' },
126
- statusCode: { type: 'integer', label: 'Status Code', minimum: 0, maximum: 999 },
127
- reasonPhrase: { type: 'string', label: 'Reason Phrase' }
122
+ isRequest: { type: 'boolean', label: 'Is Request', readOnly: true },
123
+ method: { type: 'string', label: 'Method', readOnly: true },
124
+ requestUri: { type: 'string', label: 'Request URI', readOnly: true },
125
+ version: { type: 'string', label: 'Version', readOnly: true },
126
+ statusCode: { type: 'integer', label: 'Status Code', readOnly: true, minimum: 0, maximum: 999 },
127
+ reasonPhrase: { type: 'string', label: 'Reason Phrase', readOnly: true }
128
128
  }
129
129
  };
130
130
  }
@@ -76,6 +76,7 @@ class HTTP2 extends BaseHeader_1.BaseHeader {
76
76
  //frame may lie); else derived from the payload byte length.
77
77
  length: {
78
78
  type: 'integer',
79
+ autoCalculated: true,
79
80
  label: 'Length',
80
81
  minimum: 0,
81
82
  maximum: 16777215,
@@ -42,6 +42,7 @@ class ICMP extends BaseHeader_1.BaseHeader {
42
42
  },
43
43
  checksum: {
44
44
  type: 'integer',
45
+ autoCalculated: true,
45
46
  label: 'Checksum',
46
47
  minimum: 0,
47
48
  maximum: 65535,
@@ -43,6 +43,7 @@ class ICMPv6 extends BaseHeader_1.BaseHeader {
43
43
  },
44
44
  checksum: {
45
45
  type: 'integer',
46
+ autoCalculated: true,
46
47
  label: 'Checksum',
47
48
  minimum: 0,
48
49
  maximum: 65535,
@@ -52,6 +52,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
52
52
  //Exposed read-only for analysis; the authoritative bytes are in controlField above.
53
53
  txSequence: {
54
54
  type: 'integer',
55
+ readOnly: true,
55
56
  label: 'Tx Sequence Number N(S)',
56
57
  decode: () => {
57
58
  this.instance.txSequence.setValue(this.readBytes(2, 2).readUInt16LE() >> 1);
@@ -59,6 +60,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
59
60
  },
60
61
  rxSequence: {
61
62
  type: 'integer',
63
+ readOnly: true,
62
64
  label: 'Rx Sequence Number N(R)',
63
65
  decode: () => {
64
66
  this.instance.rxSequence.setValue(this.readBytes(4, 2).readUInt16LE() >> 1);
@@ -84,7 +86,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
84
86
  }
85
87
  },
86
88
  encode: () => {
87
- const controlType = this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
89
+ const controlType = this.instance.apciType.getValue('I-Format', (nodePath) => this.recordError(nodePath, 'Not Found'));
88
90
  switch (controlType) {
89
91
  case 'I-Format':
90
92
  {
@@ -92,7 +94,12 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
92
94
  }
93
95
  break;
94
96
  default: {
95
- this.writeBits(5, 1, 7, 1, this.instance.apciType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
97
+ //Not the label: the raw format bit decode stores for an illegal frame (0/1).
98
+ //It lives in the first control octet (offset 2), like the I-Format case.
99
+ const formatBit = /^[01]$/.test(`${controlType}`) ? Number(controlType) : NaN;
100
+ if (formatBit !== 0 && formatBit !== 1)
101
+ this.recordError(this.instance.apciType.getPath(), `Unknown APCI type '${controlType}', encoded as I-Format`, 'error');
102
+ this.writeBits(2, 1, 7, 1, formatBit === 1 ? 1 : 0);
96
103
  }
97
104
  }
98
105
  }
@@ -2018,7 +2025,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
2018
2025
  maximum: 1
2019
2026
  }
2020
2027
  },
2021
- required: ['address', 'SCS', 'QU', 'S_OR_E']
2028
+ required: ['address', 'DCS', 'QU', 'S_OR_E']
2022
2029
  },
2023
2030
  //步调节命令 C_RC_NA_1 = RCO
2024
2031
  {
@@ -4515,14 +4522,17 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4515
4522
  case 46:
4516
4523
  {
4517
4524
  const DCS = this.readBits(objectOffset + 3, 1, 6, 2);
4525
+ const QU = this.readBits(objectOffset + 3, 1, 1, 5);
4518
4526
  const S_OR_E = this.readBits(objectOffset + 3, 1, 0, 1);
4519
4527
  const C_DC_NA_1 = {
4520
4528
  address: 0,
4521
4529
  DCS: 0,
4530
+ QU: 0,
4522
4531
  S_OR_E: 0
4523
4532
  };
4524
4533
  C_DC_NA_1.address = buffer.readUintBE(0, 3);
4525
4534
  C_DC_NA_1.DCS = DCS;
4535
+ C_DC_NA_1.QU = QU;
4526
4536
  C_DC_NA_1.S_OR_E = S_OR_E;
4527
4537
  IOA_arr.push(C_DC_NA_1);
4528
4538
  }
@@ -4531,14 +4541,17 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4531
4541
  case 47:
4532
4542
  {
4533
4543
  const RCS = this.readBits(objectOffset + 3, 1, 6, 2);
4544
+ const QU = this.readBits(objectOffset + 3, 1, 1, 5);
4534
4545
  const S_OR_E = this.readBits(objectOffset + 3, 1, 0, 1);
4535
4546
  const C_RC_NA_1 = {
4536
4547
  address: 0,
4537
4548
  RCS: 0,
4549
+ QU: 0,
4538
4550
  S_OR_E: 0
4539
4551
  };
4540
4552
  C_RC_NA_1.address = buffer.readUintBE(0, 3);
4541
4553
  C_RC_NA_1.RCS = RCS;
4554
+ C_RC_NA_1.QU = QU;
4542
4555
  C_RC_NA_1.S_OR_E = S_OR_E;
4543
4556
  IOA_arr.push(C_RC_NA_1);
4544
4557
  }
@@ -4666,6 +4679,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4666
4679
  case 59:
4667
4680
  {
4668
4681
  const DCS = this.readBits(objectOffset + 3, 1, 6, 2);
4682
+ const QU = this.readBits(objectOffset + 3, 1, 1, 5);
4669
4683
  const S_OR_E = this.readBits(objectOffset + 3, 1, 0, 1);
4670
4684
  const Milliseconds = this.readBytes(objectOffset + 4, 2).readUInt16LE();
4671
4685
  const Minutes = this.readBits(objectOffset + 6, 1, 2, 6);
@@ -4680,6 +4694,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4680
4694
  const C_DC_TA_1 = {
4681
4695
  address: 0,
4682
4696
  DCS: 0,
4697
+ QU: 0,
4683
4698
  S_OR_E: 0,
4684
4699
  Milliseconds: 0,
4685
4700
  Minutes: 0,
@@ -4694,6 +4709,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4694
4709
  };
4695
4710
  C_DC_TA_1.address = buffer.readUintBE(0, 3);
4696
4711
  C_DC_TA_1.DCS = DCS;
4712
+ C_DC_TA_1.QU = QU;
4697
4713
  C_DC_TA_1.S_OR_E = S_OR_E;
4698
4714
  C_DC_TA_1.Milliseconds = Milliseconds;
4699
4715
  C_DC_TA_1.Minutes = Minutes;
@@ -4712,6 +4728,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4712
4728
  case 60:
4713
4729
  {
4714
4730
  const RCS = this.readBits(objectOffset + 3, 1, 6, 2);
4731
+ const QU = this.readBits(objectOffset + 3, 1, 1, 5);
4715
4732
  const S_OR_E = this.readBits(objectOffset + 3, 1, 0, 1);
4716
4733
  const Milliseconds = this.readBytes(objectOffset + 4, 2).readUInt16LE();
4717
4734
  const Minutes = this.readBits(objectOffset + 6, 1, 2, 6);
@@ -4726,6 +4743,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4726
4743
  const C_RC_TA_1 = {
4727
4744
  address: 0,
4728
4745
  RCS: 0,
4746
+ QU: 0,
4729
4747
  S_OR_E: 0,
4730
4748
  Milliseconds: 0,
4731
4749
  Minutes: 0,
@@ -4740,6 +4758,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
4740
4758
  };
4741
4759
  C_RC_TA_1.address = buffer.readUintBE(0, 3);
4742
4760
  C_RC_TA_1.RCS = RCS;
4761
+ C_RC_TA_1.QU = QU;
4743
4762
  C_RC_TA_1.S_OR_E = S_OR_E;
4744
4763
  C_RC_TA_1.Milliseconds = Milliseconds;
4745
4764
  C_RC_TA_1.Minutes = Minutes;
@@ -5897,6 +5916,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
5897
5916
  buf.writeUIntLE(address_LE, 0, 3);
5898
5917
  this.writeIOAAddress(objectOffset, buf, i);
5899
5918
  this.writeBits(objectOffset + 3, 1, 6, 2, IOA_message[i - 1].DCS);
5919
+ this.writeBits(objectOffset + 3, 1, 1, 5, IOA_message[i - 1].QU ?? 0);
5900
5920
  this.writeBits(objectOffset + 3, 1, 0, 1, IOA_message[i - 1].S_OR_E);
5901
5921
  }
5902
5922
  break;
@@ -5909,6 +5929,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
5909
5929
  buf.writeUIntLE(address_LE, 0, 3);
5910
5930
  this.writeIOAAddress(objectOffset, buf, i);
5911
5931
  this.writeBits(objectOffset + 3, 1, 6, 2, IOA_message[i - 1].RCS);
5932
+ this.writeBits(objectOffset + 3, 1, 1, 5, IOA_message[i - 1].QU ?? 0);
5912
5933
  this.writeBits(objectOffset + 3, 1, 0, 1, IOA_message[i - 1].S_OR_E);
5913
5934
  }
5914
5935
  break;
@@ -5997,6 +6018,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
5997
6018
  buf.writeUIntLE(address_LE, 0, 3);
5998
6019
  this.writeIOAAddress(objectOffset, buf, i);
5999
6020
  this.writeBits(objectOffset + 3, 1, 6, 2, IOA_message[i - 1].DCS);
6021
+ this.writeBits(objectOffset + 3, 1, 1, 5, IOA_message[i - 1].QU ?? 0);
6000
6022
  this.writeBits(objectOffset + 3, 1, 0, 1, IOA_message[i - 1].S_OR_E);
6001
6023
  const Milliseconds_buffer = (0, NumberToBuffer_1.UInt16ToBuffer)(IOA_message[i - 1].Milliseconds);
6002
6024
  this.writeBytes(objectOffset + 4, (0, NumberToBuffer_1.UInt16ToBuffer)(Milliseconds_buffer.readUInt16LE()));
@@ -6020,6 +6042,7 @@ class IEC104_I_Frame extends BaseHeader_1.BaseHeader {
6020
6042
  buf.writeUIntLE(address_LE, 0, 3);
6021
6043
  this.writeIOAAddress(objectOffset, buf, i);
6022
6044
  this.writeBits(objectOffset + 3, 1, 6, 2, IOA_message[i - 1].RCS);
6045
+ this.writeBits(objectOffset + 3, 1, 1, 5, IOA_message[i - 1].QU ?? 0);
6023
6046
  this.writeBits(objectOffset + 3, 1, 0, 1, IOA_message[i - 1].S_OR_E);
6024
6047
  const Milliseconds_buffer = (0, NumberToBuffer_1.UInt16ToBuffer)(IOA_message[i - 1].Milliseconds);
6025
6048
  this.writeBytes(objectOffset + 4, (0, NumberToBuffer_1.UInt16ToBuffer)(Milliseconds_buffer.readUInt16LE()));
@@ -66,6 +66,7 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
66
66
  //precisely to acknowledge this sequence number); controlField holds the authoritative bytes.
67
67
  rxSequence: {
68
68
  type: 'integer',
69
+ readOnly: true,
69
70
  label: 'Rx Sequence Number N(R)',
70
71
  decode: () => {
71
72
  this.instance.rxSequence.setValue(this.readBytes(4, 2).readUInt16LE() >> 1);
@@ -91,7 +92,7 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
91
92
  }
92
93
  },
93
94
  encode: () => {
94
- const controlType = this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
95
+ const controlType = this.instance.apciType.getValue('S-Format', (nodePath) => this.recordError(nodePath, 'Not Found'));
95
96
  switch (controlType) {
96
97
  case 'S-Format':
97
98
  {
@@ -99,7 +100,11 @@ class IEC104_S_Frame extends BaseHeader_1.BaseHeader {
99
100
  }
100
101
  break;
101
102
  default: {
102
- this.writeBits(2, 1, 7, 1, this.instance.apciType.getValue(0, (nodePath) => this.recordError(nodePath, 'Not Found')));
103
+ //Not the label: the raw format bit decode stores for an illegal frame (0/1).
104
+ const formatBit = /^[01]$/.test(`${controlType}`) ? Number(controlType) : NaN;
105
+ if (formatBit !== 0 && formatBit !== 1)
106
+ this.recordError(this.instance.apciType.getPath(), `Unknown APCI type '${controlType}', encoded as S-Format`, 'error');
107
+ this.writeBits(2, 1, 7, 1, formatBit === 0 ? 0 : 1);
103
108
  }
104
109
  }
105
110
  }
@@ -59,7 +59,7 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
59
59
  this.instance.controlField.setValue((0, BufferToHex_1.BufferToHex)(this.readBytes(2, 4)));
60
60
  },
61
61
  encode: () => {
62
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
62
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.controlField.getValue('00000000', (nodePath) => this.recordError(nodePath, 'Not Found'))));
63
63
  }
64
64
  },
65
65
  apciType: {
@@ -107,7 +107,10 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
107
107
  }
108
108
  },
109
109
  encode: () => {
110
- const controlType = this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'));
110
+ //Left out: controlField (encoded before this) is the verbatim control word; keep it.
111
+ if (this.instance.apciType.isUndefined())
112
+ return;
113
+ const controlType = this.instance.apciType.getValue('00000000');
111
114
  switch (controlType) {
112
115
  case 'U-Format Type:Test Frame Activation':
113
116
  {
@@ -140,7 +143,15 @@ class IEC104_U_Frame extends BaseHeader_1.BaseHeader {
140
143
  }
141
144
  break;
142
145
  default: {
143
- this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(this.instance.apciType.getValue('0', (nodePath) => this.recordError(nodePath, 'Not Found'))));
146
+ //Not a known label: the verbatim 4-octet control word decode stores for an
147
+ //unrecognized U-frame. Anything else cannot be encoded; keep the 4-octet framing.
148
+ if (/^[0-9a-fA-F]{8}$/.test(`${controlType}`)) {
149
+ this.writeBytes(2, (0, HexToBuffer_1.HexToBuffer)(controlType));
150
+ }
151
+ else {
152
+ this.recordError(this.instance.apciType.getPath(), `Unknown APCI type '${controlType}', control field encoded as 00000000`, 'error');
153
+ this.writeBytes(2, Buffer.alloc(4, 0));
154
+ }
144
155
  }
145
156
  }
146
157
  }