mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-08 07:21:02 -04:00
Merge 2a2d8e9e67
into 2f5b0533d8
This commit is contained in:
commit
bd3f73f87a
8 changed files with 234 additions and 1 deletions
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -2419,6 +2419,11 @@
|
|||
"cssom": "0.3.2"
|
||||
}
|
||||
},
|
||||
"csv-string": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/csv-string/-/csv-string-3.1.2.tgz",
|
||||
"integrity": "sha512-JzjYuRSU/5y6H9BqwaJTNsF8dYlFql14n1GIOgu2QHFCoU20U2DaeMShyIZuvU7WS0n7u+AMhgUOuqRZ9eFhog=="
|
||||
},
|
||||
"ctph.js": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/ctph.js/-/ctph.js-0.0.5.tgz",
|
||||
|
|
|
@ -76,6 +76,7 @@
|
|||
"bootstrap-switch": "^3.3.4",
|
||||
"crypto-api": "^0.8.0",
|
||||
"crypto-js": "^3.1.9-1",
|
||||
"csv-string": "^3.1.2",
|
||||
"ctph.js": "0.0.5",
|
||||
"diff": "^3.4.0",
|
||||
"escodegen": "^1.9.1",
|
||||
|
|
|
@ -331,6 +331,8 @@ const Categories = [
|
|||
"Extract EXIF",
|
||||
"Numberwang",
|
||||
"XKCD Random Number",
|
||||
"Parse CSV to string",
|
||||
"Parse string to CSV"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -4018,6 +4018,37 @@ const OperationConfig = {
|
|||
inputType: "string",
|
||||
outputType: "number",
|
||||
args: []
|
||||
},
|
||||
"CSV To String": {
|
||||
module: "CSVParser",
|
||||
description: "Function used to parse CSV string to a regular string.",
|
||||
inputType: "string",
|
||||
outputType: "string",
|
||||
args: [
|
||||
{
|
||||
name: "delimeter",
|
||||
type: "string",
|
||||
value: ","
|
||||
},
|
||||
{
|
||||
name: "quotes",
|
||||
type: "string",
|
||||
value: '"'
|
||||
}
|
||||
]
|
||||
},
|
||||
"String to CSV": {
|
||||
module: "CSVParser",
|
||||
description: "Function used to parse a string array to a CSV string.",
|
||||
inputType: "string",
|
||||
outputType: "string",
|
||||
args: [
|
||||
{
|
||||
name: "delimeter",
|
||||
type: "string",
|
||||
value: ","
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
|
|
21
src/core/config/modules/CSVParser.js
Normal file
21
src/core/config/modules/CSVParser.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
import CSVParser from "../../operations/CSVParser.js";
|
||||
|
||||
|
||||
/**
|
||||
* CSVParser module.
|
||||
*
|
||||
* Libraries:
|
||||
* - csv-string
|
||||
*
|
||||
* @author VimalRaghubir [vraghubir0418@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
|
||||
|
||||
OpModules.CSVParser = {
|
||||
"Parse from CSV": CSVParser.csvToString,
|
||||
"Parse to CSV": CSVParser.stringToCSV,
|
||||
};
|
||||
|
||||
export default OpModules;
|
|
@ -21,6 +21,7 @@ import PublicKeyModule from "./PublicKey.js";
|
|||
import RegexModule from "./Regex.js";
|
||||
import ShellcodeModule from "./Shellcode.js";
|
||||
import URLModule from "./URL.js";
|
||||
import CSVModule from "./CSVParser.js";
|
||||
|
||||
Object.assign(
|
||||
OpModules,
|
||||
|
@ -37,7 +38,8 @@ Object.assign(
|
|||
PublicKeyModule,
|
||||
RegexModule,
|
||||
ShellcodeModule,
|
||||
URLModule
|
||||
URLModule,
|
||||
CSVModule
|
||||
);
|
||||
|
||||
export default OpModules;
|
||||
|
|
48
src/core/operations/CSVParser.js
Normal file
48
src/core/operations/CSVParser.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { CSV } from "csv-string";
|
||||
|
||||
/**
|
||||
* @author VimalRaghubir [vraghubir0418@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
* @namespace
|
||||
*/
|
||||
|
||||
|
||||
const CSVParser = {
|
||||
/**
|
||||
* Parse from CSV
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
csvToString: function(input, args) {
|
||||
let array = "";
|
||||
if (input) {
|
||||
let detectedDelimeter = CSV.detect(input);
|
||||
if (detectedDelimeter !== args[0]) {
|
||||
args[0] = detectedDelimeter;
|
||||
}
|
||||
array = CSV.parse(input, args[0], args[1]);
|
||||
} else {
|
||||
array = "The passed in data is not a csv string. Please pass in a csv string.";
|
||||
}
|
||||
return array;
|
||||
},
|
||||
/**
|
||||
* Parse to CSV
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
stringToCSV: function(input, args) {
|
||||
let string = "";
|
||||
if (input) {
|
||||
string = CSV.stringify(input, args[0]);
|
||||
} else {
|
||||
string = "The passed in data is not a string that can be converted to a CSV.";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
};
|
||||
|
||||
export default CSVParser;
|
123
test/tests/operations/CSVParser.js
Normal file
123
test/tests/operations/CSVParser.js
Normal file
|
@ -0,0 +1,123 @@
|
|||
/**
|
||||
* CSVParser tests.
|
||||
*
|
||||
* @author Vimal Raghubir
|
||||
*
|
||||
* @copyright Crown Copyright 2017
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import TestRegister from "../../TestRegister.js";
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "Testing hello parsed to CSV",
|
||||
input: "hello",
|
||||
expectedOutput: "hello",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "String to CSV",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing hello world parsed to CSV",
|
||||
input: "['hello', 'world']",
|
||||
expectedOutput: "hello;world",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "String to CSV",
|
||||
args: [";"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing false parsed to CSV",
|
||||
input: false,
|
||||
expectedOutput: "The passed in data is not a string that can be converted to a CSV.",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "String to CSV",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing ||| parsed to CSV",
|
||||
input: "|||",
|
||||
expectedOutput: "|||||",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "String to CSV",
|
||||
args: ["|"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing 0 parsed to CSV",
|
||||
input: 0,
|
||||
expectedOutput: "The passed in data is not a string that can be converted to a CSV.",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "String to CSV",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing 1,2,3,\n1,2, parsed to String",
|
||||
input: "1,2,3,\n,1,2,",
|
||||
expectedOutput: "[[1,2,3],[1,2]]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "CSV to String",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing \n\n\n parsed to String",
|
||||
input: "\n\n\n",
|
||||
expectedOutput: "[[],[],[]]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "CSV to String",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing 1,2,3,4,5 parsed to String",
|
||||
input: "1,2,3,4,5",
|
||||
expectedOutput: "[[1,2,3,4,5]]",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "CSV to String",
|
||||
args: ["|"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing 0 parsed to CSV",
|
||||
input: 0,
|
||||
expectedOutput: "The passed in data is not a string that can be converted to a CSV.",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "CSV to String",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "Testing false parsed to CSV",
|
||||
input: false,
|
||||
expectedOutput: "The passed in data is not a string that can be converted to a CSV.",
|
||||
recipeConfig: [
|
||||
{
|
||||
op: "CSV to String",
|
||||
args: [","],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
Loading…
Add table
Add a link
Reference in a new issue