Tidied up Steganography operations. FileType and toBase64 functions now accept ArrayBuffers.

This commit is contained in:
n1474335 2019-09-04 13:54:59 +01:00
parent 5bc5c0df90
commit eb769c7fb4
28 changed files with 81 additions and 75 deletions

View file

@ -12,7 +12,7 @@ import Utils from "../Utils.mjs";
/**
* Base64's the input byte array using the given alphabet, returning a string.
*
* @param {byteArray|Uint8Array|string} data
* @param {byteArray|Uint8Array|ArrayBuffer|string} data
* @param {string} [alphabet="A-Za-z0-9+/="]
* @returns {string}
*
@ -25,6 +25,9 @@ import Utils from "../Utils.mjs";
*/
export function toBase64(data, alphabet="A-Za-z0-9+/=") {
if (!data) return "";
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data);
}
if (typeof data == "string") {
data = Utils.strToByteArray(data);
}

View file

@ -72,9 +72,9 @@ export const JOIN_DELIM_OPTIONS = [
{name: "Nothing (join chars)", value: ""}
];
/*
RGBA list delimiters.
*/
/**
* RGBA list delimiters.
*/
export const RGBA_DELIM_OPTIONS = [
{name: "Comma", value: ","},
{name: "Space", value: " "},

View file

@ -75,7 +75,7 @@ function bytesMatch(sig, buf, offset=0) {
* Given a buffer, detects magic byte sequences at specific positions and returns the
* extension and mime type.
*
* @param {Uint8Array} buf
* @param {Uint8Array|ArrayBuffer} buf
* @param {string[]} [categories=All] - Which categories of file to look for
* @returns {Object[]} types
* @returns {string} type.name - Name of file type
@ -84,6 +84,10 @@ function bytesMatch(sig, buf, offset=0) {
* @returns {string} [type.desc] - Description
*/
export function detectFileType(buf, categories=Object.keys(FILE_SIGNATURES)) {
if (buf instanceof ArrayBuffer) {
buf = new Uint8Array(buf);
}
if (!(buf && buf.length > 1)) {
return [];
}
@ -203,7 +207,7 @@ function locatePotentialSig(buf, sig, offset) {
* Detects whether the given buffer is a file of the type specified.
*
* @param {string|RegExp} type
* @param {Uint8Array} buf
* @param {Uint8Array|ArrayBuffer} buf
* @returns {string|false} The mime type or false if the type does not match
*/
export function isType(type, buf) {
@ -230,7 +234,7 @@ export function isType(type, buf) {
/**
* Detects whether the given buffer contains an image file.
*
* @param {Uint8Array} buf
* @param {Uint8Array|ArrayBuffer} buf
* @returns {string|false} The mime type or false if the type does not match
*/
export function isImage(buf) {