From d89a4a18deb39d14570d1be901ec305a8fe4aa80 Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 28 Dec 2021 18:27:35 +0100 Subject: [PATCH] Improve signature ordering detection requirements (#518) Co-authored-by: Sindre Sorhus --- .github/pull_request_template.md | 6 +++++- core.js | 28 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 1e907b9b..f4dd07e9 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,7 +4,11 @@ If you're adding support for a new file type, please follow the below steps: - Add a fixture file named `fixture.` to the `fixture` directory. - Add the file extension to the `extensions` array in `supported.js`. - Add the file's MIME type to the `types` array in `supported.js`. -- Add the file type detection logic to the `core.js` file. +- Add the file type detection logic to the `core.js` file +- Respect the sequence: + - Signature with shorter sample size (counted from offset 0 until the last required byte position) will be executed first. + - Only the initial determination for the file type counts for the sequence. + - Existing signatures requiring same sample length (same *signature group*) will be tested prior to your new detections. Yours will be last. (rational: common formats first). - Add the file extension to the `FileType` type in `core.d.ts`. - Add the file's MIME type to the `MimeType` type in `core.d.ts`. - Add the file extension to the `Supported file types` section in the readme, in the format ```- [``](URL) - Format name```, for example, ```- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) - Portable Network Graphics``` diff --git a/core.js b/core.js index c3685b63..4092840e 100644 --- a/core.js +++ b/core.js @@ -151,6 +151,13 @@ class FileTypeParser { // -- 3-byte signatures -- + if (this.check([0x47, 0x49, 0x46])) { + return { + ext: 'gif', + mime: 'image/gif', + }; + } + if (this.check([0xFF, 0xD8, 0xFF])) { return { ext: 'jpg', @@ -214,20 +221,6 @@ class FileTypeParser { // -- 4-byte signatures -- - if (this.check([0x7F, 0x45, 0x4C, 0x46])) { - return { - ext: 'elf', - mime: 'application/x-elf', - }; - } - - if (this.check([0x47, 0x49, 0x46])) { - return { - ext: 'gif', - mime: 'image/gif', - }; - } - if (this.checkString('FLIF')) { return { ext: 'flif', @@ -798,6 +791,13 @@ class FileTypeParser { }; } + if (this.check([0x7F, 0x45, 0x4C, 0x46])) { + return { + ext: 'elf', + mime: 'application/x-elf', + }; + } + // -- 5-byte signatures -- if (this.check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {