@netkitty/codec 2.0.0 → 2.1.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 (87) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/README.md +10 -6
  3. package/README.zh-CN.md +9 -6
  4. package/dist/Codec.js +4 -1
  5. package/dist/abstracts/BaseHeader.d.ts +26 -0
  6. package/dist/abstracts/BaseHeader.js +53 -0
  7. package/dist/headers/AH.js +3 -2
  8. package/dist/headers/BFD.d.ts +2 -1
  9. package/dist/headers/BFD.js +30 -4
  10. package/dist/headers/BGP.js +5 -1
  11. package/dist/headers/C37118.d.ts +3 -2
  12. package/dist/headers/C37118.js +34 -15
  13. package/dist/headers/CDP.d.ts +3 -3
  14. package/dist/headers/CDP.js +46 -10
  15. package/dist/headers/CMS.js +25 -10
  16. package/dist/headers/COTP.js +2 -3
  17. package/dist/headers/CoAP.js +20 -2
  18. package/dist/headers/DCCP.d.ts +3 -2
  19. package/dist/headers/DCCP.js +60 -8
  20. package/dist/headers/DNP3.d.ts +5 -3
  21. package/dist/headers/DNP3.js +62 -7
  22. package/dist/headers/EIGRP.d.ts +3 -2
  23. package/dist/headers/EIGRP.js +31 -5
  24. package/dist/headers/EthernetII.js +1 -0
  25. package/dist/headers/GELF.js +1 -0
  26. package/dist/headers/GENEVE.js +1 -0
  27. package/dist/headers/GRE.js +1 -0
  28. package/dist/headers/GTPv2C.js +1 -0
  29. package/dist/headers/Goose.js +4 -4
  30. package/dist/headers/HSR.js +1 -0
  31. package/dist/headers/HTTP2.js +1 -0
  32. package/dist/headers/ICMP.js +2 -1
  33. package/dist/headers/ICMPv6.js +10 -16
  34. package/dist/headers/IEC104_I_Frame.js +189 -160
  35. package/dist/headers/IEC104_S_Frame.js +19 -1
  36. package/dist/headers/IEC104_U_Frame.js +19 -1
  37. package/dist/headers/IEC61850SampledValues.js +4 -4
  38. package/dist/headers/IGMP.d.ts +8 -7
  39. package/dist/headers/IGMP.js +57 -19
  40. package/dist/headers/IPFIX.js +1 -1
  41. package/dist/headers/IPv4.js +26 -23
  42. package/dist/headers/IPv6.js +4 -1
  43. package/dist/headers/IPv6HopByHopOptions.js +3 -1
  44. package/dist/headers/ISAKMP.js +1 -0
  45. package/dist/headers/Kerberos.js +1 -0
  46. package/dist/headers/L2TP.js +1 -0
  47. package/dist/headers/LDAP.js +1 -1
  48. package/dist/headers/LLC.js +1 -0
  49. package/dist/headers/NHRP.d.ts +1 -1
  50. package/dist/headers/NHRP.js +29 -4
  51. package/dist/headers/OSPF.d.ts +3 -3
  52. package/dist/headers/OSPF.js +72 -7
  53. package/dist/headers/PIM.d.ts +3 -3
  54. package/dist/headers/PIM.js +55 -6
  55. package/dist/headers/PPPoEDiscovery.js +2 -0
  56. package/dist/headers/PTP.js +5 -1
  57. package/dist/headers/RDP.js +2 -0
  58. package/dist/headers/RGoose.js +2 -0
  59. package/dist/headers/RSVP.d.ts +3 -3
  60. package/dist/headers/RSVP.js +56 -42
  61. package/dist/headers/RTCP.js +1 -0
  62. package/dist/headers/SCTP.d.ts +2 -2
  63. package/dist/headers/SCTP.js +29 -5
  64. package/dist/headers/SNAP.js +2 -2
  65. package/dist/headers/SNMP.js +2 -2
  66. package/dist/headers/STUN.js +1 -1
  67. package/dist/headers/SercosIII.d.ts +3 -2
  68. package/dist/headers/SercosIII.js +41 -5
  69. package/dist/headers/TCP.js +13 -58
  70. package/dist/headers/TLS_Alert.js +20 -2
  71. package/dist/headers/TLS_ApplicationData.d.ts +3 -3
  72. package/dist/headers/TLS_ApplicationData.js +25 -6
  73. package/dist/headers/TLS_ChangeCipherSpec.d.ts +2 -2
  74. package/dist/headers/TLS_ChangeCipherSpec.js +24 -4
  75. package/dist/headers/TLS_Handshake.js +39 -6
  76. package/dist/headers/TLS_Heartbeat.d.ts +4 -3
  77. package/dist/headers/TLS_Heartbeat.js +40 -7
  78. package/dist/headers/TPKT.js +1 -1
  79. package/dist/headers/UDP.js +17 -61
  80. package/dist/headers/VLAN_802dot1Q.js +1 -0
  81. package/dist/headers/VRRP.d.ts +2 -1
  82. package/dist/headers/VRRP.js +45 -9
  83. package/dist/helper/Checksum.d.ts +15 -0
  84. package/dist/helper/Checksum.js +56 -0
  85. package/dist/helper/Crc.d.ts +18 -0
  86. package/dist/helper/Crc.js +58 -0
  87. package/package.json +2 -2
@@ -4,7 +4,7 @@ exports.UDP = void 0;
4
4
  const BaseHeader_1 = require("../abstracts/BaseHeader");
5
5
  const BufferToNumber_1 = require("../helper/BufferToNumber");
6
6
  const NumberToBuffer_1 = require("../helper/NumberToBuffer");
7
- const IPToBuffer_1 = require("../helper/IPToBuffer");
7
+ const Checksum_1 = require("../helper/Checksum");
8
8
  class UDP extends BaseHeader_1.BaseHeader {
9
9
  constructor() {
10
10
  super(...arguments);
@@ -24,61 +24,17 @@ class UDP extends BaseHeader_1.BaseHeader {
24
24
  * @param udpHeaderBuffer
25
25
  */
26
26
  calculateUDPChecksum(udpHeaderBuffer) {
27
- const udpHeaderLength = udpHeaderBuffer.length;
28
- //Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack, so record
29
- //an error and leave the checksum 0 instead of dereferencing a missing previous layer.
30
- if (!this.prevCodecModule) {
27
+ //The pseudo-header comes from the nearest IPv4/IPv6 layer beneath, past AH or IPv6 extension
28
+ //headers. Same contract as TCP: a UDP layer with no IP beneath is a legitimate crafted stack,
29
+ //so record it and leave the checksum 0 instead of dereferencing a missing layer.
30
+ const pseudoHeader = this.ipPseudoHeader(17, udpHeaderBuffer.length);
31
+ if (!pseudoHeader) {
31
32
  this.recordError(this.instance.checksum.getPath(), 'Cannot compute UDP checksum: no IP layer beneath UDP');
32
33
  return 0;
33
34
  }
34
- const ipVersion = this.prevCodecModule.instance.version.getValue();
35
- let pseudoHeaderBuffer = Buffer.from([]);
36
- const sourceIp = this.prevCodecModule.instance.sip.getValue();
37
- const destinationIp = this.prevCodecModule.instance.dip.getValue();
38
- if (ipVersion === 4) {
39
- //4 Bytes
40
- const sourceIPv4Buffer = Buffer.from(sourceIp.split('.').map((value) => parseInt(value)));
41
- const destinationIPv4Buffer = Buffer.from(destinationIp.split('.').map((value) => parseInt(value)));
42
- //IPv4 Pseudo header
43
- pseudoHeaderBuffer = Buffer.concat([
44
- sourceIPv4Buffer,
45
- destinationIPv4Buffer,
46
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
47
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
48
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
49
- Buffer.from([udpHeaderLength & 0xFF])
50
- ]);
51
- }
52
- else if (ipVersion === 6) {
53
- //16 Bytes
54
- const sourceIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(sourceIp);
55
- const destinationIPv6Buffer = (0, IPToBuffer_1.IPv6ToBuffer)(destinationIp);
56
- //IPv6 Pseudo header
57
- pseudoHeaderBuffer = Buffer.concat([
58
- sourceIPv6Buffer,
59
- destinationIPv6Buffer,
60
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
61
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
62
- (0, NumberToBuffer_1.UInt8ToBuffer)(0), //Reserved field
63
- (0, NumberToBuffer_1.UInt8ToBuffer)(17), //Protocol type (UDP = 17)
64
- Buffer.from([(udpHeaderLength >> 8) & 0xFF]),
65
- Buffer.from([udpHeaderLength & 0xFF])
66
- ]);
67
- }
68
- else {
69
- return 0;
70
- }
71
- let dataBuffer = Buffer.concat([pseudoHeaderBuffer, udpHeaderBuffer]);
72
- if (dataBuffer.length % 2)
73
- dataBuffer = Buffer.concat([dataBuffer, Buffer.from('00', 'hex')]);
74
- const data = Uint8Array.from(dataBuffer);
75
- let sum = 0;
76
- for (let i = 0; i < data.length; i += 2)
77
- sum += (data[i] << 8) + (data[i + 1] || 0);
78
- while (sum > 0xFFFF)
79
- sum = (sum & 0xFFFF) + (sum >>> 16);
80
- const _cs = (~sum) & 0xFFFF;
81
- return _cs === 0 ? 0xFFFF : _cs;
35
+ //A computed 0x0000 is sent as 0xFFFF: 0 means "no checksum" (RFC 768).
36
+ const checksum = (0, Checksum_1.InternetChecksum)(pseudoHeader, udpHeaderBuffer);
37
+ return checksum === 0 ? 0xFFFF : checksum;
82
38
  }
83
39
  static #schemaCache;
84
40
  //Class-cached SCHEMA (④): field closures are functions taking dynamic `this` via .call(this).
@@ -104,14 +60,14 @@ class UDP extends BaseHeader_1.BaseHeader {
104
60
  this.instance.length.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(4, 2)));
105
61
  },
106
62
  encode: function () {
107
- const length = this.instance.length.getValue(0);
108
- if (length) {
63
+ if (!this.isLeftOut(this.instance.length)) {
64
+ const length = this.instance.length.getValue(0);
109
65
  this.instance.length.setValue(length);
110
66
  this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
111
67
  }
112
68
  else {
113
- this.instance.length.setValue(length);
114
- this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(length));
69
+ this.instance.length.setValue(0);
70
+ this.writeBytes(4, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
115
71
  this.addPostPacketEncodeHandler(() => {
116
72
  let startCount = false;
117
73
  let udpLength = 0;
@@ -137,14 +93,14 @@ class UDP extends BaseHeader_1.BaseHeader {
137
93
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
138
94
  },
139
95
  encode: function () {
140
- const checksum = this.instance.checksum.getValue(0);
141
- if (checksum) {
96
+ if (!this.isLeftOut(this.instance.checksum)) {
97
+ const checksum = this.instance.checksum.getValue(0);
142
98
  this.instance.checksum.setValue(checksum);
143
99
  this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
144
100
  }
145
101
  else {
146
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
147
- this.instance.checksum.setValue(checksum);
102
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
103
+ this.instance.checksum.setValue(0);
148
104
  this.addPostPacketEncodeHandler(() => {
149
105
  let startCount = false;
150
106
  let udpHeaderWithDataLength = 0;
@@ -65,6 +65,7 @@ class VLAN_802dot1Q extends BaseHeader_1.BaseHeader {
65
65
  }
66
66
  },
67
67
  etherType: {
68
+ autoCalculated: true,
68
69
  type: 'string',
69
70
  minLength: 4,
70
71
  maxLength: 4,
@@ -12,7 +12,8 @@ import { DemuxProducer } from '../types/DemuxProducer';
12
12
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
13
13
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
14
14
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
15
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
15
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
16
+ * written as given otherwise.
16
17
  */
17
18
  export declare class VRRP extends BaseHeader {
18
19
  #private;
@@ -9,6 +9,7 @@ const BufferToIP_1 = require("../helper/BufferToIP");
9
9
  const IPToBuffer_1 = require("../helper/IPToBuffer");
10
10
  const BufferToHex_1 = require("../helper/BufferToHex");
11
11
  const HexToBuffer_1 = require("../helper/HexToBuffer");
12
+ const Checksum_1 = require("../helper/Checksum");
12
13
  /**
13
14
  * VRRP — Virtual Router Redundancy Protocol (v2 RFC 3768, v3 RFC 5798), carried directly over IP as
14
15
  * protocol 112. A fixed 8-byte header — Version (high nibble of byte 0) + Type (low nibble), Virtual
@@ -20,7 +21,8 @@ const HexToBuffer_1 = require("../helper/HexToBuffer");
20
21
  * The version is the high nibble of byte 0 and selects how bytes 4-5 and the trailer are interpreted;
21
22
  * version-specific fields no-op when the decoded version does not match, so on encode each octet is
22
23
  * written by exactly one field — a well-formed advertisement round-trips byte-for-byte. The checksum is
23
- * honored verbatim (not recomputed — it differs by version and, in v3, covers an IP pseudo-header).
24
+ * computed when left out (over the message for v2, over an IP pseudo-header plus the message for v3) and
25
+ * written as given otherwise.
24
26
  */
25
27
  class VRRP extends BaseHeader_1.BaseHeader {
26
28
  constructor() {
@@ -54,6 +56,27 @@ class VRRP extends BaseHeader_1.BaseHeader {
54
56
  }
55
57
  return this.packet.length - this.startPos;
56
58
  }
59
+ /**
60
+ * The VRRP checksum: the Internet checksum of the whole VRRP message (v2, RFC 3768 §5.3.8, and v3
61
+ * over IPv4); v3 over IPv6 prepends the IPv6 pseudo-header (protocol 112, upper-layer length = the
62
+ * message length). RFC 5798 was ambiguous for v3 over IPv4 and some implementations added an IPv4
63
+ * pseudo-header; RFC 9568, which obsoletes it, says there is none, as Wireshark checks by default.
64
+ * Called once every field is written, with the checksum still zero.
65
+ * @private
66
+ */
67
+ #calculateChecksum() {
68
+ const message = this.packet.subarray(this.startPos, this.endPos);
69
+ const version = this.instance.version.getValue(2);
70
+ const ip = this.nearestIPModule();
71
+ if (version !== 3 || (ip && ip.id === 'ipv4'))
72
+ return (0, Checksum_1.InternetChecksum)(message);
73
+ const pseudoHeader = this.ipPseudoHeader(112, message.length);
74
+ if (!pseudoHeader) {
75
+ this.recordError(this.instance.checksum.getPath(), 'Cannot compute VRRP v3 checksum: no IP layer beneath VRRP');
76
+ return 0;
77
+ }
78
+ return (0, Checksum_1.InternetChecksum)(pseudoHeader, message);
79
+ }
57
80
  static #schemaCache;
58
81
  //Class-cached SCHEMA: field closures are plain functions taking dynamic `this` via .call(this), so
59
82
  //the structure is shareable and never rebuilt per packet. VRRP carries both v2 (RFC 3768) and v3
@@ -73,6 +96,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
73
96
  label: 'Version',
74
97
  minimum: 0,
75
98
  maximum: 15,
99
+ default: 2,
76
100
  decode: function () {
77
101
  this.instance.version.setValue(this.readBits(0, 1, 0, 4));
78
102
  const version = this.instance.version.getValue(0);
@@ -92,6 +116,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
92
116
  label: 'Type',
93
117
  minimum: 0,
94
118
  maximum: 15,
119
+ default: 1,
95
120
  decode: function () {
96
121
  this.instance.type.setValue(this.readBits(0, 1, 4, 4));
97
122
  },
@@ -131,6 +156,7 @@ class VRRP extends BaseHeader_1.BaseHeader {
131
156
  label: 'Adver Int (seconds)',
132
157
  minimum: 0,
133
158
  maximum: 255,
159
+ default: 1,
134
160
  //v2 only: byte 5, advertisement interval in whole seconds (RFC 3768 §5.3.7).
135
161
  decode: function () {
136
162
  const version = this.instance.version.getValue(0);
@@ -215,21 +241,31 @@ class VRRP extends BaseHeader_1.BaseHeader {
215
241
  },
216
242
  checksum: {
217
243
  type: 'integer',
244
+ autoCalculated: true,
218
245
  label: 'Checksum',
219
246
  minimum: 0,
220
247
  maximum: 65535,
221
- //Honoured verbatim (faithful executor): decode reads it, encode writes the same value
222
- //back untouched. VRRP checksums differ by version (v2 over the message only, v3 over
223
- //an IP pseudo-header too), so recomputation is left to the caller.
248
+ //Left out, computed once every field is written (#calculateChecksum: v2 over the
249
+ //message only, v3 over an IP pseudo-header too); a given value is written as given.
224
250
  decode: function () {
225
251
  this.instance.checksum.setValue((0, BufferToNumber_1.BufferToUInt16)(this.readBytes(6, 2)));
226
252
  },
227
253
  encode: function () {
228
- let checksum = this.instance.checksum.getValue(0);
229
- checksum = checksum > 65535 ? 65535 : checksum;
230
- checksum = checksum < 0 ? 0 : checksum;
231
- this.instance.checksum.setValue(checksum);
232
- this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
254
+ if (!this.isLeftOut(this.instance.checksum)) {
255
+ let checksum = this.instance.checksum.getValue(0);
256
+ checksum = checksum > 65535 ? 65535 : checksum;
257
+ checksum = checksum < 0 ? 0 : checksum;
258
+ this.instance.checksum.setValue(checksum);
259
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
260
+ return;
261
+ }
262
+ this.instance.checksum.setValue(0);
263
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(0));
264
+ this.addPostSelfEncodeHandler(() => {
265
+ const checksum = this.#calculateChecksum();
266
+ this.instance.checksum.setValue(checksum);
267
+ this.writeBytes(6, (0, NumberToBuffer_1.UInt16ToBuffer)(checksum));
268
+ });
233
269
  }
234
270
  },
235
271
  addresses: {
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
3
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
4
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
5
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
6
+ * @param buffers
7
+ */
8
+ export declare function InternetChecksum(...buffers: Buffer[]): number;
9
+ /**
10
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
11
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
12
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
13
+ * @param buffers
14
+ */
15
+ export declare function CRC32c(...buffers: Buffer[]): number;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternetChecksum = InternetChecksum;
4
+ exports.CRC32c = CRC32c;
5
+ /**
6
+ * The Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the data as
7
+ * big-endian 16-bit words, an odd trailing octet padded with a zero octet. Pass the pseudo-header and
8
+ * the covered bytes as separate buffers; they are summed as one stream, so every buffer but the last
9
+ * should have an even length. The checksum field itself must be zero (or excluded) in the input.
10
+ * @param buffers
11
+ */
12
+ function InternetChecksum(...buffers) {
13
+ let sum = 0;
14
+ let odd = -1;
15
+ for (const buffer of buffers) {
16
+ for (let i = 0; i < buffer.length; i++) {
17
+ if (odd < 0) {
18
+ odd = buffer[i];
19
+ }
20
+ else {
21
+ sum += (odd << 8) | buffer[i];
22
+ odd = -1;
23
+ }
24
+ }
25
+ }
26
+ if (odd >= 0)
27
+ sum += odd << 8;
28
+ while (sum > 0xFFFF)
29
+ sum = (sum & 0xFFFF) + (sum >>> 16);
30
+ return (~sum) & 0xFFFF;
31
+ }
32
+ /** CRC-32C (Castagnoli) lookup table for the reflected polynomial 0x82F63B78, built once. */
33
+ const CRC32C_TABLE = (() => {
34
+ const table = new Uint32Array(256);
35
+ for (let n = 0; n < 256; n++) {
36
+ let crc = n;
37
+ for (let k = 0; k < 8; k++)
38
+ crc = (crc & 1) ? (crc >>> 1) ^ 0x82F63B78 : crc >>> 1;
39
+ table[n] = crc >>> 0;
40
+ }
41
+ return table;
42
+ })();
43
+ /**
44
+ * CRC-32C (Castagnoli, RFC 9260 appendix A): reflected, initial value and final XOR 0xFFFFFFFF.
45
+ * Returns the CRC as an unsigned 32-bit number; SCTP puts it on the wire least significant byte
46
+ * first. The buffers are processed as one stream; the checksum field itself must be zero in the input.
47
+ * @param buffers
48
+ */
49
+ function CRC32c(...buffers) {
50
+ let crc = 0xFFFFFFFF;
51
+ for (const buffer of buffers) {
52
+ for (let i = 0; i < buffer.length; i++)
53
+ crc = CRC32C_TABLE[(crc ^ buffer[i]) & 0xFF] ^ (crc >>> 8);
54
+ }
55
+ return (crc ^ 0xFFFFFFFF) >>> 0;
56
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * CRC-32 as used by IEEE 802.3 / zlib (CRC-32/ISO-HDLC): polynomial 0x04C11DB7 processed reflected,
3
+ * initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF. Check value: Crc32('123456789') = 0xCBF43926.
4
+ * @param buffer
5
+ */
6
+ export declare function Crc32(buffer: Buffer): number;
7
+ /**
8
+ * CRC-16/DNP (IEEE 1815 data link layer): polynomial 0x3D65 processed reflected, initial value 0,
9
+ * final XOR 0xFFFF. DNP3 sends it low octet first. Check value: Crc16Dnp('123456789') = 0xEA82.
10
+ * @param buffer
11
+ */
12
+ export declare function Crc16Dnp(buffer: Buffer): number;
13
+ /**
14
+ * CRC-16/CCITT-FALSE (the IEEE C37.118.2 CHK): polynomial 0x1021 processed MSB-first, initial value
15
+ * 0xFFFF, no final XOR. Check value: Crc16CcittFalse('123456789') = 0x29B1.
16
+ * @param buffer
17
+ */
18
+ export declare function Crc16CcittFalse(buffer: Buffer): number;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Crc32 = Crc32;
4
+ exports.Crc16Dnp = Crc16Dnp;
5
+ exports.Crc16CcittFalse = Crc16CcittFalse;
6
+ /**
7
+ * Lookup table of a reflected (LSB-first) CRC: entry n is the register after shifting byte n through
8
+ * the reversed polynomial eight times.
9
+ * @param reversedPolynomial
10
+ */
11
+ function ReflectedTable(reversedPolynomial) {
12
+ const table = new Uint32Array(256);
13
+ for (let n = 0; n < 256; n++) {
14
+ let register = n;
15
+ for (let bit = 0; bit < 8; bit++)
16
+ register = register & 1 ? (register >>> 1) ^ reversedPolynomial : register >>> 1;
17
+ table[n] = register >>> 0;
18
+ }
19
+ return table;
20
+ }
21
+ const CRC32_TABLE = ReflectedTable(0xEDB88320);
22
+ const CRC16_DNP_TABLE = ReflectedTable(0xA6BC);
23
+ /**
24
+ * CRC-32 as used by IEEE 802.3 / zlib (CRC-32/ISO-HDLC): polynomial 0x04C11DB7 processed reflected,
25
+ * initial value 0xFFFFFFFF, final XOR 0xFFFFFFFF. Check value: Crc32('123456789') = 0xCBF43926.
26
+ * @param buffer
27
+ */
28
+ function Crc32(buffer) {
29
+ let register = 0xFFFFFFFF;
30
+ for (let i = 0; i < buffer.length; i++)
31
+ register = CRC32_TABLE[(register ^ buffer[i]) & 0xFF] ^ (register >>> 8);
32
+ return (register ^ 0xFFFFFFFF) >>> 0;
33
+ }
34
+ /**
35
+ * CRC-16/DNP (IEEE 1815 data link layer): polynomial 0x3D65 processed reflected, initial value 0,
36
+ * final XOR 0xFFFF. DNP3 sends it low octet first. Check value: Crc16Dnp('123456789') = 0xEA82.
37
+ * @param buffer
38
+ */
39
+ function Crc16Dnp(buffer) {
40
+ let register = 0;
41
+ for (let i = 0; i < buffer.length; i++)
42
+ register = CRC16_DNP_TABLE[(register ^ buffer[i]) & 0xFF] ^ (register >>> 8);
43
+ return (register ^ 0xFFFF) & 0xFFFF;
44
+ }
45
+ /**
46
+ * CRC-16/CCITT-FALSE (the IEEE C37.118.2 CHK): polynomial 0x1021 processed MSB-first, initial value
47
+ * 0xFFFF, no final XOR. Check value: Crc16CcittFalse('123456789') = 0x29B1.
48
+ * @param buffer
49
+ */
50
+ function Crc16CcittFalse(buffer) {
51
+ let register = 0xFFFF;
52
+ for (let i = 0; i < buffer.length; i++) {
53
+ register ^= buffer[i] << 8;
54
+ for (let bit = 0; bit < 8; bit++)
55
+ register = register & 0x8000 ? ((register << 1) ^ 0x1021) & 0xFFFF : (register << 1) & 0xFFFF;
56
+ }
57
+ return register;
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netkitty/codec",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
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": "a0b16ffec11eb830dd5846bd942dd9a63ae04d5e"
141
+ "gitHead": "c392c19e0fbf6de6d2ce5213ea775c6033ff93cd"
142
142
  }