1
0
mirror of synced 2024-11-24 06:50:16 +01:00

Merge pull request #1762 from gchq/feature/floats

This commit is contained in:
a3957273 2024-03-30 18:34:07 +00:00 committed by GitHub
commit 75a28b558e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 327 additions and 0 deletions

1
package-lock.json generated
View File

@ -46,6 +46,7 @@
"flat": "^6.0.1", "flat": "^6.0.1",
"geodesy": "1.1.3", "geodesy": "1.1.3",
"highlight.js": "^11.9.0", "highlight.js": "^11.9.0",
"ieee754": "^1.1.13",
"jimp": "^0.16.13", "jimp": "^0.16.13",
"jquery": "3.7.1", "jquery": "3.7.1",
"js-crc": "^0.2.0", "js-crc": "^0.2.0",

View File

@ -122,6 +122,7 @@
"escodegen": "^2.1.0", "escodegen": "^2.1.0",
"esprima": "^4.0.1", "esprima": "^4.0.1",
"exif-parser": "^0.1.12", "exif-parser": "^0.1.12",
"ieee754": "^1.1.13",
"fernet": "^0.3.2", "fernet": "^0.3.2",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"flat": "^6.0.1", "flat": "^6.0.1",

View File

@ -14,6 +14,8 @@
"From Charcode", "From Charcode",
"To Decimal", "To Decimal",
"From Decimal", "From Decimal",
"To Float",
"From Float",
"To Binary", "To Binary",
"From Binary", "From Binary",
"To Octal", "To Octal",

View File

@ -0,0 +1,78 @@
/**
* @author tcode2k16 [tcode2k16@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import ieee754 from "ieee754";
import {DELIM_OPTIONS} from "../lib/Delim.mjs";
/**
* From Float operation
*/
class FromFloat extends Operation {
/**
* FromFloat constructor
*/
constructor() {
super();
this.name = "From Float";
this.module = "Default";
this.description = "Convert from EEE754 Floating Point Numbers";
this.infoURL = "https://en.wikipedia.org/wiki/IEEE_754";
this.inputType = "string";
this.outputType = "byteArray";
this.args = [
{
"name": "Endianness",
"type": "option",
"value": [
"Big Endian",
"Little Endian"
]
},
{
"name": "Size",
"type": "option",
"value": [
"Float (4 bytes)",
"Double (8 bytes)"
]
},
{
"name": "Delimiter",
"type": "option",
"value": DELIM_OPTIONS
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
if (input.length === 0) return [];
const [endianness, size, delimiterName] = args;
const delim = Utils.charRep(delimiterName || "Space");
const byteSize = size === "Double (8 bytes)" ? 8 : 4;
const isLE = endianness === "Little Endian";
const mLen = byteSize === 4 ? 23 : 52;
const floats = input.split(delim);
const output = new Array(floats.length*byteSize);
for (let i = 0; i < floats.length; i++) {
ieee754.write(output, parseFloat(floats[i]), i*byteSize, isLE, mLen, byteSize);
}
return output;
}
}
export default FromFloat;

View File

@ -0,0 +1,80 @@
/**
* @author tcode2k16 [tcode2k16@gmail.com]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import Utils from "../Utils.mjs";
import ieee754 from "ieee754";
import {DELIM_OPTIONS} from "../lib/Delim.mjs";
/**
* To Float operation
*/
class ToFloat extends Operation {
/**
* ToFloat constructor
*/
constructor() {
super();
this.name = "To Float";
this.module = "Default";
this.description = "Convert to EEE754 Floating Point Numbers";
this.infoURL = "https://en.wikipedia.org/wiki/IEEE_754";
this.inputType = "byteArray";
this.outputType = "string";
this.args = [
{
"name": "Endianness",
"type": "option",
"value": [
"Big Endian",
"Little Endian"
]
},
{
"name": "Size",
"type": "option",
"value": [
"Float (4 bytes)",
"Double (8 bytes)"
]
},
{
"name": "Delimiter",
"type": "option",
"value": DELIM_OPTIONS
}
];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [endianness, size, delimiterName] = args;
const delim = Utils.charRep(delimiterName || "Space");
const byteSize = size === "Double (8 bytes)" ? 8 : 4;
const isLE = endianness === "Little Endian";
const mLen = byteSize === 4 ? 23 : 52;
if (input.length % byteSize !== 0) {
throw new OperationError(`Input is not a multiple of ${byteSize}`);
}
const output = [];
for (let i = 0; i < input.length; i+=byteSize) {
output.push(ieee754.read(input, i, isLE, mLen, byteSize));
}
return output.join(delim);
}
}
export default ToFloat;

View File

@ -62,6 +62,7 @@ import "./tests/DefangIP.mjs";
import "./tests/ELFInfo.mjs"; import "./tests/ELFInfo.mjs";
import "./tests/Enigma.mjs"; import "./tests/Enigma.mjs";
import "./tests/ExtractEmailAddresses.mjs"; import "./tests/ExtractEmailAddresses.mjs";
import "./tests/Float.mjs";
import "./tests/FileTree.mjs"; import "./tests/FileTree.mjs";
import "./tests/FletcherChecksum.mjs"; import "./tests/FletcherChecksum.mjs";
import "./tests/Fork.mjs"; import "./tests/Fork.mjs";

View File

@ -0,0 +1,164 @@
/**
* Float tests.
*
* @author tcode2k16 [tcode2k16@gmail.com]
*
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
name: "To Float: nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "From Hex",
args: ["Auto"]
},
{
op: "To Float",
args: ["Big Endian", "Float (4 bytes)", "Space"]
}
],
},
{
name: "To Float (Big Endian, 4 bytes): 0.5",
input: "3f0000003f000000",
expectedOutput: "0.5 0.5",
recipeConfig: [
{
op: "From Hex",
args: ["Auto"]
},
{
op: "To Float",
args: ["Big Endian", "Float (4 bytes)", "Space"]
}
]
},
{
name: "To Float (Little Endian, 4 bytes): 0.5",
input: "0000003f0000003f",
expectedOutput: "0.5 0.5",
recipeConfig: [
{
op: "From Hex",
args: ["Auto"]
},
{
op: "To Float",
args: ["Little Endian", "Float (4 bytes)", "Space"]
}
]
},
{
name: "To Float (Big Endian, 8 bytes): 0.5",
input: "3fe00000000000003fe0000000000000",
expectedOutput: "0.5 0.5",
recipeConfig: [
{
op: "From Hex",
args: ["Auto"]
},
{
op: "To Float",
args: ["Big Endian", "Double (8 bytes)", "Space"]
}
]
},
{
name: "To Float (Little Endian, 8 bytes): 0.5",
input: "000000000000e03f000000000000e03f",
expectedOutput: "0.5 0.5",
recipeConfig: [
{
op: "From Hex",
args: ["Auto"]
},
{
op: "To Float",
args: ["Little Endian", "Double (8 bytes)", "Space"]
}
]
},
{
name: "From Float: nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "From Float",
args: ["Big Endian", "Float (4 bytes)", "Space"]
},
{
op: "To Hex",
args: ["None"]
}
]
},
{
name: "From Float (Big Endian, 4 bytes): 0.5",
input: "0.5 0.5",
expectedOutput: "3f0000003f000000",
recipeConfig: [
{
op: "From Float",
args: ["Big Endian", "Float (4 bytes)", "Space"]
},
{
op: "To Hex",
args: ["None"]
}
]
},
{
name: "From Float (Little Endian, 4 bytes): 0.5",
input: "0.5 0.5",
expectedOutput: "0000003f0000003f",
recipeConfig: [
{
op: "From Float",
args: ["Little Endian", "Float (4 bytes)", "Space"]
},
{
op: "To Hex",
args: ["None"]
}
]
},
{
name: "From Float (Big Endian, 8 bytes): 0.5",
input: "0.5 0.5",
expectedOutput: "3fe00000000000003fe0000000000000",
recipeConfig: [
{
op: "From Float",
args: ["Big Endian", "Double (8 bytes)", "Space"]
},
{
op: "To Hex",
args: ["None"]
}
]
},
{
name: "From Float (Little Endian, 8 bytes): 0.5",
input: "0.5 0.5",
expectedOutput: "000000000000e03f000000000000e03f",
recipeConfig: [
{
op: "From Float",
args: ["Little Endian", "Double (8 bytes)", "Space"]
},
{
op: "To Hex",
args: ["None"]
}
]
}
]);