cyberchef 9.37.1 → 9.38.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 (34) hide show
  1. package/CHANGELOG.md +7 -2
  2. package/Gruntfile.js +2 -1
  3. package/package.json +2 -2
  4. package/src/core/Utils.mjs +2 -2
  5. package/src/core/config/Categories.json +1 -0
  6. package/src/core/config/OperationConfig.json +106 -3
  7. package/src/core/config/modules/Default.mjs +2 -0
  8. package/src/core/lib/Binary.mjs +9 -5
  9. package/src/core/lib/Extract.mjs +16 -11
  10. package/src/core/lib/FileSignatures.mjs +12 -12
  11. package/src/core/lib/Protocol.mjs +47 -0
  12. package/src/core/lib/Sort.mjs +105 -0
  13. package/src/core/lib/Stream.mjs +22 -12
  14. package/src/core/operations/ExtractDates.mjs +7 -1
  15. package/src/core/operations/ExtractDomains.mjs +29 -5
  16. package/src/core/operations/ExtractEmailAddresses.mjs +29 -5
  17. package/src/core/operations/ExtractFilePaths.mjs +38 -14
  18. package/src/core/operations/ExtractIPAddresses.mjs +44 -27
  19. package/src/core/operations/ExtractMACAddresses.mjs +28 -6
  20. package/src/core/operations/ExtractURLs.mjs +28 -5
  21. package/src/core/operations/ParseTCP.mjs +245 -0
  22. package/src/core/operations/ParseUDP.mjs +29 -24
  23. package/src/core/operations/Sort.mjs +5 -105
  24. package/src/core/operations/Strings.mjs +36 -14
  25. package/src/core/operations/ToBase45.mjs +1 -1
  26. package/src/core/operations/Unique.mjs +25 -5
  27. package/src/core/operations/index.mjs +2 -0
  28. package/src/node/index.mjs +5 -0
  29. package/src/web/waiters/OperationsWaiter.mjs +5 -1
  30. package/tests/node/tests/operations.mjs +3 -4
  31. package/tests/operations/index.mjs +1 -0
  32. package/tests/operations/tests/ExtractEmailAddresses.mjs +4 -4
  33. package/tests/operations/tests/ParseTCP.mjs +44 -0
  34. package/tests/operations/tests/ParseUDP.mjs +5 -18
package/CHANGELOG.md CHANGED
@@ -13,6 +13,9 @@ All major and minor version changes will be documented in this file. Details of
13
13
 
14
14
  ## Details
15
15
 
16
+ ### [9.38.0] - 2022-05-30
17
+ - Added 'Parse TCP' operation [@n1474335] | [a895d1d]
18
+
16
19
  ### [9.37.0] - 2022-03-29
17
20
  - 'SM4 Encrypt' and 'SM4 Decrypt' operations added [@swesven] | [#1189]
18
21
  - NoPadding options added for CBC and ECB modes in AES, DES and Triple DES Decrypt operations [@swesven] | [#1189]
@@ -135,7 +138,7 @@ All major and minor version changes will be documented in this file. Details of
135
138
 
136
139
  <details>
137
140
  <summary>Click to expand v8 minor versions</summary>
138
-
141
+
139
142
  ### [8.38.0] - 2019-07-03
140
143
  - 'Streebog' and 'GOST hash' operations added [@MShwed] [@n1474335] | [#530]
141
144
 
@@ -288,6 +291,7 @@ All major and minor version changes will be documented in this file. Details of
288
291
 
289
292
 
290
293
 
294
+ [9.38.0]: https://github.com/gchq/CyberChef/releases/tag/v9.38.0
291
295
  [9.37.0]: https://github.com/gchq/CyberChef/releases/tag/v9.37.0
292
296
  [9.36.0]: https://github.com/gchq/CyberChef/releases/tag/v9.36.0
293
297
  [9.35.0]: https://github.com/gchq/CyberChef/releases/tag/v9.35.0
@@ -416,6 +420,7 @@ All major and minor version changes will be documented in this file. Details of
416
420
  [289a417]: https://github.com/gchq/CyberChef/commit/289a417dfb5923de5e1694354ec42a08d9395bfe
417
421
  [e9ca4dc]: https://github.com/gchq/CyberChef/commit/e9ca4dc9caf98f33fd986431cd400c88082a42b8
418
422
  [dd18e52]: https://github.com/gchq/CyberChef/commit/dd18e529939078b89867297b181a584e8b2cc7da
423
+ [a895d1d]: https://github.com/gchq/CyberChef/commit/a895d1d82a2f92d440a0c5eca2bc7c898107b737
419
424
 
420
425
  [#95]: https://github.com/gchq/CyberChef/pull/299
421
426
  [#173]: https://github.com/gchq/CyberChef/pull/173
@@ -502,4 +507,4 @@ All major and minor version changes will be documented in this file. Details of
502
507
  [#1242]: https://github.com/gchq/CyberChef/pull/1242
503
508
  [#1244]: https://github.com/gchq/CyberChef/pull/1244
504
509
  [#1313]: https://github.com/gchq/CyberChef/pull/1313
505
- [#1326]: https://github.com/gchq/CyberChef/pull/1326
510
+ [#1326]: https://github.com/gchq/CyberChef/pull/1326
package/Gruntfile.js CHANGED
@@ -217,7 +217,8 @@ module.exports = function (grunt) {
217
217
  client: {
218
218
  logging: "error",
219
219
  overlay: true
220
- }
220
+ },
221
+ hot: "only"
221
222
  },
222
223
  plugins: [
223
224
  new webpack.DefinePlugin(BUILD_CONSTANTS),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cyberchef",
3
- "version": "9.37.1",
3
+ "version": "9.38.0",
4
4
  "description": "The Cyber Swiss Army Knife for encryption, encoding, compression and data analysis.",
5
5
  "author": "n1474335 <n1474335@gmail.com>",
6
6
  "homepage": "https://gchq.github.io/CyberChef",
@@ -48,7 +48,7 @@
48
48
  "autoprefixer": "^10.4.4",
49
49
  "babel-loader": "^8.2.4",
50
50
  "babel-plugin-dynamic-import-node": "^2.3.3",
51
- "chromedriver": "^99.0.0",
51
+ "chromedriver": "^101.0.0",
52
52
  "cli-progress": "^3.10.0",
53
53
  "colors": "^1.4.0",
54
54
  "copy-webpack-plugin": "^10.2.4",
@@ -723,8 +723,8 @@ class Utils {
723
723
  }
724
724
 
725
725
  if (removeScriptAndStyle) {
726
- htmlStr = recursiveRemove(/<script[^>]*>.*?<\/script>/gi, htmlStr);
727
- htmlStr = recursiveRemove(/<style[^>]*>.*?<\/style>/gi, htmlStr);
726
+ htmlStr = recursiveRemove(/<script[^>]*>.*?<\/script[^>]*>/gi, htmlStr);
727
+ htmlStr = recursiveRemove(/<style[^>]*>.*?<\/style[^>]*>/gi, htmlStr);
728
728
  }
729
729
  return htmlStr.replace(/<[^>]+>/g, "");
730
730
  }
@@ -190,6 +190,7 @@
190
190
  "Parse IP range",
191
191
  "Parse IPv6 address",
192
192
  "Parse IPv4 header",
193
+ "Parse TCP",
193
194
  "Parse UDP",
194
195
  "Parse SSH Host Key",
195
196
  "Parse URI",
@@ -4946,7 +4946,17 @@
4946
4946
  {
4947
4947
  "name": "Display total",
4948
4948
  "type": "boolean",
4949
- "value": true
4949
+ "value": false
4950
+ },
4951
+ {
4952
+ "name": "Sort",
4953
+ "type": "boolean",
4954
+ "value": false
4955
+ },
4956
+ {
4957
+ "name": "Unique",
4958
+ "type": "boolean",
4959
+ "value": false
4950
4960
  }
4951
4961
  ]
4952
4962
  },
@@ -4973,6 +4983,16 @@
4973
4983
  "name": "Display total",
4974
4984
  "type": "boolean",
4975
4985
  "value": false
4986
+ },
4987
+ {
4988
+ "name": "Sort",
4989
+ "type": "boolean",
4990
+ "value": false
4991
+ },
4992
+ {
4993
+ "name": "Unique",
4994
+ "type": "boolean",
4995
+ "value": false
4976
4996
  }
4977
4997
  ]
4978
4998
  },
@@ -4999,6 +5019,16 @@
4999
5019
  "name": "Display total",
5000
5020
  "type": "boolean",
5001
5021
  "value": false
5022
+ },
5023
+ {
5024
+ "name": "Sort",
5025
+ "type": "boolean",
5026
+ "value": false
5027
+ },
5028
+ {
5029
+ "name": "Unique",
5030
+ "type": "boolean",
5031
+ "value": false
5002
5032
  }
5003
5033
  ]
5004
5034
  },
@@ -5091,6 +5121,16 @@
5091
5121
  "name": "Display total",
5092
5122
  "type": "boolean",
5093
5123
  "value": false
5124
+ },
5125
+ {
5126
+ "name": "Sort",
5127
+ "type": "boolean",
5128
+ "value": false
5129
+ },
5130
+ {
5131
+ "name": "Unique",
5132
+ "type": "boolean",
5133
+ "value": false
5094
5134
  }
5095
5135
  ]
5096
5136
  },
@@ -5174,6 +5214,16 @@
5174
5214
  "name": "Display total",
5175
5215
  "type": "boolean",
5176
5216
  "value": false
5217
+ },
5218
+ {
5219
+ "name": "Sort",
5220
+ "type": "boolean",
5221
+ "value": false
5222
+ },
5223
+ {
5224
+ "name": "Unique",
5225
+ "type": "boolean",
5226
+ "value": false
5177
5227
  }
5178
5228
  ]
5179
5229
  },
@@ -5228,6 +5278,16 @@
5228
5278
  "name": "Display total",
5229
5279
  "type": "boolean",
5230
5280
  "value": false
5281
+ },
5282
+ {
5283
+ "name": "Sort",
5284
+ "type": "boolean",
5285
+ "value": false
5286
+ },
5287
+ {
5288
+ "name": "Unique",
5289
+ "type": "boolean",
5290
+ "value": false
5231
5291
  }
5232
5292
  ]
5233
5293
  },
@@ -9458,6 +9518,25 @@
9458
9518
  }
9459
9519
  ]
9460
9520
  },
9521
+ "Parse TCP": {
9522
+ "module": "Default",
9523
+ "description": "Parses a TCP header and payload (if present).",
9524
+ "infoURL": "https://wikipedia.org/wiki/Transmission_Control_Protocol",
9525
+ "inputType": "string",
9526
+ "outputType": "html",
9527
+ "flowControl": false,
9528
+ "manualBake": false,
9529
+ "args": [
9530
+ {
9531
+ "name": "Input format",
9532
+ "type": "option",
9533
+ "value": [
9534
+ "Hex",
9535
+ "Raw"
9536
+ ]
9537
+ }
9538
+ ]
9539
+ },
9461
9540
  "Parse TLV": {
9462
9541
  "module": "Default",
9463
9542
  "description": "Converts a Type-Length-Value (TLV) encoded string into a JSON object. Can optionally include a <code>Key</code> / <code>Type</code> entry. <br><br>Tags: Key-Length-Value, KLV, Length-Value, LV",
@@ -9488,11 +9567,20 @@
9488
9567
  "module": "Default",
9489
9568
  "description": "Parses a UDP header and payload (if present).",
9490
9569
  "infoURL": "https://wikipedia.org/wiki/User_Datagram_Protocol",
9491
- "inputType": "ArrayBuffer",
9570
+ "inputType": "string",
9492
9571
  "outputType": "html",
9493
9572
  "flowControl": false,
9494
9573
  "manualBake": false,
9495
- "args": []
9574
+ "args": [
9575
+ {
9576
+ "name": "Input format",
9577
+ "type": "option",
9578
+ "value": [
9579
+ "Hex",
9580
+ "Raw"
9581
+ ]
9582
+ }
9583
+ ]
9496
9584
  },
9497
9585
  "Parse UNIX file permissions": {
9498
9586
  "module": "Default",
@@ -12712,6 +12800,16 @@
12712
12800
  "name": "Display total",
12713
12801
  "type": "boolean",
12714
12802
  "value": false
12803
+ },
12804
+ {
12805
+ "name": "Sort",
12806
+ "type": "boolean",
12807
+ "value": false
12808
+ },
12809
+ {
12810
+ "name": "Unique",
12811
+ "type": "boolean",
12812
+ "value": false
12715
12813
  }
12716
12814
  ]
12717
12815
  },
@@ -16039,6 +16137,11 @@
16039
16137
  "Colon",
16040
16138
  "Nothing (separate chars)"
16041
16139
  ]
16140
+ },
16141
+ {
16142
+ "name": "Display count",
16143
+ "type": "boolean",
16144
+ "value": false
16042
16145
  }
16043
16146
  ]
16044
16147
  },
@@ -97,6 +97,7 @@ import ParseIPRange from "../../operations/ParseIPRange.mjs";
97
97
  import ParseIPv4Header from "../../operations/ParseIPv4Header.mjs";
98
98
  import ParseIPv6Address from "../../operations/ParseIPv6Address.mjs";
99
99
  import ParseSSHHostKey from "../../operations/ParseSSHHostKey.mjs";
100
+ import ParseTCP from "../../operations/ParseTCP.mjs";
100
101
  import ParseTLV from "../../operations/ParseTLV.mjs";
101
102
  import ParseUDP from "../../operations/ParseUDP.mjs";
102
103
  import ParseUNIXFilePermissions from "../../operations/ParseUNIXFilePermissions.mjs";
@@ -263,6 +264,7 @@ OpModules.Default = {
263
264
  "Parse IPv4 header": ParseIPv4Header,
264
265
  "Parse IPv6 address": ParseIPv6Address,
265
266
  "Parse SSH Host Key": ParseSSHHostKey,
267
+ "Parse TCP": ParseTCP,
266
268
  "Parse TLV": ParseTLV,
267
269
  "Parse UDP": ParseUDP,
268
270
  "Parse UNIX file permissions": ParseUNIXFilePermissions,
@@ -13,7 +13,7 @@ import OperationError from "../errors/OperationError.mjs";
13
13
  /**
14
14
  * Convert a byte array into a binary string.
15
15
  *
16
- * @param {Uint8Array|byteArray} data
16
+ * @param {Uint8Array|byteArray|number} data
17
17
  * @param {string} [delim="Space"]
18
18
  * @param {number} [padding=8]
19
19
  * @returns {string}
@@ -26,13 +26,17 @@ import OperationError from "../errors/OperationError.mjs";
26
26
  * toBinary([10,20,30], ":");
27
27
  */
28
28
  export function toBinary(data, delim="Space", padding=8) {
29
- if (!data) return "";
30
-
31
29
  delim = Utils.charRep(delim);
32
30
  let output = "";
33
31
 
34
- for (let i = 0; i < data.length; i++) {
35
- output += data[i].toString(2).padStart(padding, "0") + delim;
32
+ if (data.length) { // array
33
+ for (let i = 0; i < data.length; i++) {
34
+ output += data[i].toString(2).padStart(padding, "0") + delim;
35
+ }
36
+ } else if (typeof data === "number") { // Single value
37
+ return data.toString(2).padStart(padding, "0");
38
+ } else {
39
+ return "";
36
40
  }
37
41
 
38
42
  if (delim.length) {
@@ -12,15 +12,15 @@
12
12
  *
13
13
  * @param {string} input
14
14
  * @param {RegExp} searchRegex
15
- * @param {RegExp} removeRegex - A regular expression defining results to remove from the
15
+ * @param {RegExp} [removeRegex=null] - A regular expression defining results to remove from the
16
16
  * final list
17
- * @param {boolean} includeTotal - Whether or not to include the total number of results
17
+ * @param {Function} [sortBy=null] - The sorting comparison function to apply
18
+ * @param {boolean} [unique=false] - Whether to unique the results
18
19
  * @returns {string}
19
20
  */
20
- export function search (input, searchRegex, removeRegex, includeTotal) {
21
- let output = "",
22
- total = 0,
23
- match;
21
+ export function search(input, searchRegex, removeRegex=null, sortBy=null, unique=false) {
22
+ let results = [];
23
+ let match;
24
24
 
25
25
  while ((match = searchRegex.exec(input))) {
26
26
  // Moves pointer when an empty string is matched (prevents infinite loop)
@@ -30,14 +30,19 @@ export function search (input, searchRegex, removeRegex, includeTotal) {
30
30
 
31
31
  if (removeRegex && removeRegex.test(match[0]))
32
32
  continue;
33
- total++;
34
- output += match[0] + "\n";
33
+
34
+ results.push(match[0]);
35
+ }
36
+
37
+ if (sortBy) {
38
+ results = results.sort(sortBy);
35
39
  }
36
40
 
37
- if (includeTotal)
38
- output = "Total found: " + total + "\n\n" + output;
41
+ if (unique) {
42
+ results = results.unique();
43
+ }
39
44
 
40
- return output;
45
+ return results;
41
46
  }
42
47
 
43
48
 
@@ -3778,8 +3778,8 @@ function parseDEFLATE(stream) {
3778
3778
 
3779
3779
  while (!finalBlock) {
3780
3780
  // Read header
3781
- finalBlock = stream.readBits(1);
3782
- const blockType = stream.readBits(2);
3781
+ finalBlock = stream.readBits(1, "le");
3782
+ const blockType = stream.readBits(2, "le");
3783
3783
 
3784
3784
  if (blockType === 0) {
3785
3785
  /* No compression */
@@ -3798,16 +3798,16 @@ function parseDEFLATE(stream) {
3798
3798
  /* Dynamic Huffman */
3799
3799
 
3800
3800
  // Read the number of liternal and length codes
3801
- const hlit = stream.readBits(5) + 257;
3801
+ const hlit = stream.readBits(5, "le") + 257;
3802
3802
  // Read the number of distance codes
3803
- const hdist = stream.readBits(5) + 1;
3803
+ const hdist = stream.readBits(5, "le") + 1;
3804
3804
  // Read the number of code lengths
3805
- const hclen = stream.readBits(4) + 4;
3805
+ const hclen = stream.readBits(4, "le") + 4;
3806
3806
 
3807
3807
  // Parse code lengths
3808
3808
  const codeLengths = new Uint8Array(huffmanOrder.length);
3809
3809
  for (let i = 0; i < hclen; i++) {
3810
- codeLengths[huffmanOrder[i]] = stream.readBits(3);
3810
+ codeLengths[huffmanOrder[i]] = stream.readBits(3, "le");
3811
3811
  }
3812
3812
 
3813
3813
  // Parse length table
@@ -3819,16 +3819,16 @@ function parseDEFLATE(stream) {
3819
3819
  code = readHuffmanCode(stream, codeLengthsTable);
3820
3820
  switch (code) {
3821
3821
  case 16:
3822
- repeat = 3 + stream.readBits(2);
3822
+ repeat = 3 + stream.readBits(2, "le");
3823
3823
  while (repeat--) lengthTable[i++] = prev;
3824
3824
  break;
3825
3825
  case 17:
3826
- repeat = 3 + stream.readBits(3);
3826
+ repeat = 3 + stream.readBits(3, "le");
3827
3827
  while (repeat--) lengthTable[i++] = 0;
3828
3828
  prev = 0;
3829
3829
  break;
3830
3830
  case 18:
3831
- repeat = 11 + stream.readBits(7);
3831
+ repeat = 11 + stream.readBits(7, "le");
3832
3832
  while (repeat--) lengthTable[i++] = 0;
3833
3833
  prev = 0;
3834
3834
  break;
@@ -3886,11 +3886,11 @@ function parseHuffmanBlock(stream, litTab, distTab) {
3886
3886
  if (code < 256) continue;
3887
3887
 
3888
3888
  // Length code
3889
- stream.readBits(lengthExtraTable[code - 257]);
3889
+ stream.readBits(lengthExtraTable[code - 257], "le");
3890
3890
 
3891
3891
  // Dist code
3892
3892
  code = readHuffmanCode(stream, distTab);
3893
- stream.readBits(distanceExtraTable[code]);
3893
+ stream.readBits(distanceExtraTable[code], "le");
3894
3894
  }
3895
3895
  }
3896
3896
 
@@ -3948,7 +3948,7 @@ function readHuffmanCode(stream, table) {
3948
3948
  const [codeTable, maxCodeLength] = table;
3949
3949
 
3950
3950
  // Read max length
3951
- const bitsBuf = stream.readBits(maxCodeLength);
3951
+ const bitsBuf = stream.readBits(maxCodeLength, "le");
3952
3952
  const codeWithLength = codeTable[bitsBuf & ((1 << maxCodeLength) - 1)];
3953
3953
  const codeLength = codeWithLength >>> 16;
3954
3954
 
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Protocol parsing functions.
3
+ *
4
+ * @author n1474335 [n1474335@gmail.com]
5
+ * @copyright Crown Copyright 2022
6
+ * @license Apache-2.0
7
+ */
8
+
9
+ import BigNumber from "bignumber.js";
10
+ import {toHexFast} from "../lib/Hex.mjs";
11
+
12
+ /**
13
+ * Recursively displays a JSON object as an HTML table
14
+ *
15
+ * @param {Object} obj
16
+ * @returns string
17
+ */
18
+ export function objToTable(obj, nested=false) {
19
+ let html = `<table
20
+ class='table table-sm table-nonfluid ${nested ? "mb-0 table-borderless" : "table-bordered"}'
21
+ style='table-layout: fixed; ${nested ? "margin: -1px !important;" : ""}'>`;
22
+ if (!nested)
23
+ html += `<tr>
24
+ <th>Field</th>
25
+ <th>Value</th>
26
+ </tr>`;
27
+
28
+ for (const key in obj) {
29
+ html += `<tr><td style='word-wrap: break-word'>${key}</td>`;
30
+ if (typeof obj[key] === "object")
31
+ html += `<td style='padding: 0'>${objToTable(obj[key], true)}</td>`;
32
+ else
33
+ html += `<td>${obj[key]}</td>`;
34
+ html += "</tr>";
35
+ }
36
+ html += "</table>";
37
+ return html;
38
+ }
39
+
40
+ /**
41
+ * Converts bytes into a BigNumber string
42
+ * @param {Uint8Array} bs
43
+ * @returns {string}
44
+ */
45
+ export function bytesToLargeNumber(bs) {
46
+ return BigNumber(toHexFast(bs), 16).toString();
47
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Sorting functions
3
+ *
4
+ * @author n1474335 [n1474335@gmail.com]
5
+ * @copyright Crown Copyright 2022
6
+ * @license Apache-2.0
7
+ *
8
+ */
9
+
10
+ /**
11
+ * Comparison operation for sorting of strings ignoring case.
12
+ *
13
+ * @param {string} a
14
+ * @param {string} b
15
+ * @returns {number}
16
+ */
17
+ export function caseInsensitiveSort(a, b) {
18
+ return a.toLowerCase().localeCompare(b.toLowerCase());
19
+ }
20
+
21
+
22
+ /**
23
+ * Comparison operation for sorting of IPv4 addresses.
24
+ *
25
+ * @param {string} a
26
+ * @param {string} b
27
+ * @returns {number}
28
+ */
29
+ export function ipSort(a, b) {
30
+ let a_ = a.split("."),
31
+ b_ = b.split(".");
32
+
33
+ a_ = a_[0] * 0x1000000 + a_[1] * 0x10000 + a_[2] * 0x100 + a_[3] * 1;
34
+ b_ = b_[0] * 0x1000000 + b_[1] * 0x10000 + b_[2] * 0x100 + b_[3] * 1;
35
+
36
+ if (isNaN(a_) && !isNaN(b_)) return 1;
37
+ if (!isNaN(a_) && isNaN(b_)) return -1;
38
+ if (isNaN(a_) && isNaN(b_)) return a.localeCompare(b);
39
+
40
+ return a_ - b_;
41
+ }
42
+
43
+ /**
44
+ * Comparison operation for sorting of numeric values.
45
+ *
46
+ * @author Chris van Marle
47
+ * @param {string} a
48
+ * @param {string} b
49
+ * @returns {number}
50
+ */
51
+ export function numericSort(a, b) {
52
+ const a_ = a.split(/([^\d]+)/),
53
+ b_ = b.split(/([^\d]+)/);
54
+
55
+ for (let i = 0; i < a_.length && i < b.length; ++i) {
56
+ if (isNaN(a_[i]) && !isNaN(b_[i])) return 1; // Numbers after non-numbers
57
+ if (!isNaN(a_[i]) && isNaN(b_[i])) return -1;
58
+ if (isNaN(a_[i]) && isNaN(b_[i])) {
59
+ const ret = a_[i].localeCompare(b_[i]); // Compare strings
60
+ if (ret !== 0) return ret;
61
+ }
62
+ if (!isNaN(a_[i]) && !isNaN(b_[i])) { // Compare numbers
63
+ if (a_[i] - b_[i] !== 0) return a_[i] - b_[i];
64
+ }
65
+ }
66
+
67
+ return a.localeCompare(b);
68
+ }
69
+
70
+ /**
71
+ * Comparison operation for sorting of hexadecimal values.
72
+ *
73
+ * @author Chris van Marle
74
+ * @param {string} a
75
+ * @param {string} b
76
+ * @returns {number}
77
+ */
78
+ export function hexadecimalSort(a, b) {
79
+ let a_ = a.split(/([^\da-f]+)/i),
80
+ b_ = b.split(/([^\da-f]+)/i);
81
+
82
+ a_ = a_.map(v => {
83
+ const t = parseInt(v, 16);
84
+ return isNaN(t) ? v : t;
85
+ });
86
+
87
+ b_ = b_.map(v => {
88
+ const t = parseInt(v, 16);
89
+ return isNaN(t) ? v : t;
90
+ });
91
+
92
+ for (let i = 0; i < a_.length && i < b.length; ++i) {
93
+ if (isNaN(a_[i]) && !isNaN(b_[i])) return 1; // Numbers after non-numbers
94
+ if (!isNaN(a_[i]) && isNaN(b_[i])) return -1;
95
+ if (isNaN(a_[i]) && isNaN(b_[i])) {
96
+ const ret = a_[i].localeCompare(b_[i]); // Compare strings
97
+ if (ret !== 0) return ret;
98
+ }
99
+ if (!isNaN(a_[i]) && !isNaN(b_[i])) { // Compare numbers
100
+ if (a_[i] - b_[i] !== 0) return a_[i] - b_[i];
101
+ }
102
+ }
103
+
104
+ return a.localeCompare(b);
105
+ }
@@ -27,15 +27,17 @@ export default class Stream {
27
27
  }
28
28
 
29
29
  /**
30
- * Get a number of bytes from the current position.
30
+ * Get a number of bytes from the current position, or all remaining bytes.
31
31
  *
32
- * @param {number} numBytes
32
+ * @param {number} [numBytes=null]
33
33
  * @returns {Uint8Array}
34
34
  */
35
- getBytes(numBytes) {
35
+ getBytes(numBytes=null) {
36
36
  if (this.position > this.length) return undefined;
37
37
 
38
- const newPosition = this.position + numBytes;
38
+ const newPosition = numBytes !== null ?
39
+ this.position + numBytes :
40
+ this.length;
39
41
  const bytes = this.bytes.slice(this.position, newPosition);
40
42
  this.position = newPosition;
41
43
  this.bitPos = 0;
@@ -91,34 +93,40 @@ export default class Stream {
91
93
  }
92
94
 
93
95
  /**
94
- * Reads a number of bits from the buffer.
95
- *
96
- * @TODO Add endianness
96
+ * Reads a number of bits from the buffer in big or little endian.
97
97
  *
98
98
  * @param {number} numBits
99
+ * @param {string} [endianness="be"]
99
100
  * @returns {number}
100
101
  */
101
- readBits(numBits) {
102
+ readBits(numBits, endianness="be") {
102
103
  if (this.position > this.length) return undefined;
103
104
 
104
105
  let bitBuf = 0,
105
106
  bitBufLen = 0;
106
107
 
107
108
  // Add remaining bits from current byte
108
- bitBuf = (this.bytes[this.position++] & bitMask(this.bitPos)) >>> this.bitPos;
109
+ bitBuf = this.bytes[this.position++] & bitMask(this.bitPos);
110
+ if (endianness !== "be") bitBuf >>>= this.bitPos;
109
111
  bitBufLen = 8 - this.bitPos;
110
112
  this.bitPos = 0;
111
113
 
112
114
  // Not enough bits yet
113
115
  while (bitBufLen < numBits) {
114
- bitBuf |= this.bytes[this.position++] << bitBufLen;
116
+ if (endianness === "be")
117
+ bitBuf = (bitBuf << bitBufLen) | this.bytes[this.position++];
118
+ else
119
+ bitBuf |= this.bytes[this.position++] << bitBufLen;
115
120
  bitBufLen += 8;
116
121
  }
117
122
 
118
123
  // Reverse back to numBits
119
124
  if (bitBufLen > numBits) {
120
125
  const excess = bitBufLen - numBits;
121
- bitBuf &= (1 << numBits) - 1;
126
+ if (endianness === "be")
127
+ bitBuf >>>= excess;
128
+ else
129
+ bitBuf &= (1 << numBits) - 1;
122
130
  bitBufLen -= excess;
123
131
  this.position--;
124
132
  this.bitPos = 8 - excess;
@@ -133,7 +141,9 @@ export default class Stream {
133
141
  * @returns {number} The bit mask
134
142
  */
135
143
  function bitMask(bitPos) {
136
- return 256 - (1 << bitPos);
144
+ return endianness === "be" ?
145
+ (1 << (8 - bitPos)) - 1 :
146
+ 256 - (1 << bitPos);
137
147
  }
138
148
  }
139
149