From be7e3ef81728aa5b87cd9fb1fdd95940e8d31502 Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 28 Dec 2021 12:48:49 +0100 Subject: [PATCH 1/2] Improve signature ordering detection requirements Fix order of `.gif` detection. Make 'FLIF' last test in 4-byte signature group --- .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..893d22a8 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])) { From f84f0f8034d98339fef46809fb498e891a384392 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 28 Dec 2021 18:27:08 +0100 Subject: [PATCH 2/2] Update pull_request_template.md --- .github/pull_request_template.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 893d22a8..f4dd07e9 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -6,9 +6,9 @@ If you're adding support for a new file type, please follow the below steps: - Add the file's MIME type to the `types` array in `supported.js`. - 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) + - 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```