CyberChef/src/core/operations/Image.js

124 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-04-29 21:44:39 +02:00
import * as ExifParser from "exif-parser";
import removeEXIF from "../lib/remove-exif.js";
2017-04-29 21:44:39 +02:00
import Utils from "../Utils.js";
2017-05-19 13:15:48 +02:00
import FileType from "./FileType.js";
2017-04-29 21:44:39 +02:00
/**
* Image operations.
*
* @author tlwr [toby@toby.codes]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*
* @namespace
*/
const Image = {
2017-05-13 17:08:14 +02:00
/**
* Extract EXIF operation.
*
* Extracts EXIF data from a byteArray, representing a JPG or a TIFF image.
*
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
2017-06-08 13:09:31 +02:00
runExtractEXIF(input, args) {
2017-04-29 21:44:39 +02:00
try {
const parser = ExifParser.create(input);
2017-05-08 18:49:13 +02:00
const result = parser.parse();
2017-04-29 21:44:39 +02:00
let lines = [];
for (let tagName in result.tags) {
let value = result.tags[tagName];
lines.push(`${tagName}: ${value}`);
}
2017-05-08 18:49:13 +02:00
const numTags = lines.length;
2017-04-29 21:44:39 +02:00
lines.unshift(`Found ${numTags} tags.\n`);
return lines.join("\n");
} catch (err) {
throw "Could not extract EXIF data from image: " + err;
2017-04-29 21:44:39 +02:00
}
},
2017-05-13 17:09:32 +02:00
2017-06-08 13:09:31 +02:00
2017-06-04 18:08:39 +02:00
/**
* Remove EXIF operation.
*
* Removes EXIF data from a byteArray, representing a JPG.
*
* @author David Moodie [davidmoodie12@gmail.com]
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
2017-06-04 18:08:39 +02:00
*/
2017-06-08 13:09:31 +02:00
runRemoveEXIF(input, args) {
// Do nothing if input is empty
if (input.length === 0) return input;
2017-06-04 18:08:39 +02:00
try {
return removeEXIF(input);
2017-06-04 18:08:39 +02:00
} catch (err) {
// Simply return input if no EXIF data is found
2017-06-04 18:23:53 +02:00
if (err === "Exif not found.") return input;
2017-06-04 18:08:39 +02:00
throw "Could not remove EXIF data from image: " + err;
}
},
2017-05-19 13:15:48 +02:00
2017-06-08 13:09:31 +02:00
2017-05-19 13:15:48 +02:00
/**
* @constant
* @default
*/
INPUT_FORMAT: ["Raw", "Base64", "Hex"],
/**
* Render Image operation.
*
* @author n1474335 [n1474335@gmail.com]
* @param {string} input
* @param {Object[]} args
* @returns {html}
*/
runRenderImage(input, args) {
const inputFormat = args[0];
let dataURI = "data:";
if (!input.length) return "";
// Convert input to raw bytes
switch (inputFormat) {
case "Hex":
input = Utils.fromHex(input);
break;
case "Base64":
// Don't trust the Base64 entered by the user.
// Unwrap it first, then re-encode later.
input = Utils.fromBase64(input, null, "byteArray");
break;
case "Raw":
default:
input = Utils.strToByteArray(input);
break;
}
// Determine file type
const type = FileType.magicType(input);
if (type && type.mime.indexOf("image") === 0) {
dataURI += type.mime + ";";
} else {
throw "Invalid file type";
}
// Add image data to URI
dataURI += "base64," + Utils.toBase64(input);
return "<img src='" + dataURI + "'>";
},
2017-04-29 21:44:39 +02:00
};
export default Image;