Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve signature ordering detection requirements #518

Merged
merged 2 commits into from Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/pull_request_template.md
Expand Up @@ -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.<extension>` 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 ```- [`<extension>`](URL) - Format name```, for example, ```- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) - Portable Network Graphics```
Expand Down
28 changes: 14 additions & 14 deletions core.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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])) {
Expand Down