diff options
author | LinuxWizard42 <computerwizard@linuxmail.org> | 2022-10-12 22:54:37 +0300 |
---|---|---|
committer | LinuxWizard42 <computerwizard@linuxmail.org> | 2022-10-12 22:54:37 +0300 |
commit | 703e03aba33f234712206769f57717ba7d92d23d (patch) | |
tree | 0041f04ccb75bd5379c764e9fe42249fffe75fc3 /node_modules/isbinaryfile | |
parent | ab6e257e6e9d9a483d7e86f220d8b209a2cd7753 (diff) | |
download | FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.gz FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.zst |
Added export_allowed file to make repository visible in cgit
Diffstat (limited to 'node_modules/isbinaryfile')
-rw-r--r-- | node_modules/isbinaryfile/LICENSE.txt | 22 | ||||
-rw-r--r-- | node_modules/isbinaryfile/README.md | 78 | ||||
-rw-r--r-- | node_modules/isbinaryfile/index.js | 129 | ||||
-rw-r--r-- | node_modules/isbinaryfile/package.json | 36 |
4 files changed, 265 insertions, 0 deletions
diff --git a/node_modules/isbinaryfile/LICENSE.txt b/node_modules/isbinaryfile/LICENSE.txt new file mode 100644 index 0000000..4226d72 --- /dev/null +++ b/node_modules/isbinaryfile/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2017 Garen J. Torikian + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/isbinaryfile/README.md b/node_modules/isbinaryfile/README.md new file mode 100644 index 0000000..1f3aad9 --- /dev/null +++ b/node_modules/isbinaryfile/README.md @@ -0,0 +1,78 @@ +# isBinaryFile + +Detects if a file is binary in Node.js. Similar to [Perl's `-B` switch](http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary), in that: +- it reads the first few thousand bytes of a file +- checks for a `null` byte; if it's found, it's binary +- flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary + +Much of the logic is pretty much ported from [ag](https://github.com/ggreer/the_silver_searcher). + +Note: if the file doesn't exist, is a directory, or is empty, the function returns `false`. + +## Installation + +``` +npm install isbinaryfile +``` + +## Usage + +### isBinaryFile(filepath, callback) + +* `filepath`, a `string` indicating the path to the file. +* `callback`, a `function` for the callback. It has two arguments: + - `err`, the typical Node.js error argument + - `result`, a `boolean` of `true` or `false`, depending on if the file is binary + + +### isBinaryFile(bytes, size, callback) + +* `bytes`, a `Buffer` of the file's contents. +* `size`, an optional `number` indicating the file size. +* `callback`, a `function` for the callback. It has two arguments: + - `err`, the typical Node.js error argument + - `result`, a `boolean` of `true` or `false`, depending on if the file is binary + + +### isBinaryFile.sync(filepath) + +* `filepath`, a `string` indicating the path to the file. + + +### isBinaryFile.sync(bytes, size) + +* `bytes`, a `Buffer` of the file's contents. +* `size`, an `number` indicating the file size. + + +Returns a `boolean` of `true` or `false`, depending on if the file is binary. + +### Examples + +```javascript +var isBinaryFile = require("isbinaryfile"); + +fs.readFile("some_file", function(err, data) { + fs.lstat("some_file", function(err, stat) { + isBinaryFile(data, stat.size, function (err, result) { + if (!err) { + if (result) { + console.log("It is!") + } + else { + console.log("No.") + } + } + }); + }); +}); + +isBinaryFile.sync("some_file"); // true or false +var bytes = fs.readFileSync(("some_file")); +var size = fs.lstatSync(("some_file").size; +isBinaryFile.sync(bytes, size); // true or false +``` + +## Testing + +Run `npm install` to install `mocha`, then run `npm test`. diff --git a/node_modules/isbinaryfile/index.js b/node_modules/isbinaryfile/index.js new file mode 100644 index 0000000..7c3f06a --- /dev/null +++ b/node_modules/isbinaryfile/index.js @@ -0,0 +1,129 @@ +var fs = require('fs'); +var path = require('path'); +var alloc = require('buffer-alloc'); +var MAX_BYTES = 512; + +module.exports = function(bytes, size, cb) { + // Only two args + if (cb === undefined) { + var file = bytes; + cb = size; + + fs.stat(file, function(err, stat) { + if (err || !stat.isFile()) return cb(err, false); + + fs.open(file, 'r', function(r_err, descriptor){ + if (r_err) return cb(r_err); + bytes = alloc(MAX_BYTES); + // Read the file with no encoding for raw buffer access. + fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){ + fs.close(descriptor, function(c_err){ + if (c_err) return cb(c_err, false); + return cb(null, isBinaryCheck(bytes, size)); + }); + }); + }); + }); + } + else + return cb(null, isBinaryCheck(bytes, size)); +}; + +function isBinaryCheck(bytes, size) { + if (size === 0) + return false; + + var suspicious_bytes = 0; + var total_bytes = Math.min(size, MAX_BYTES); + + // UTF-8 BOM + if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { + return false; + } + + // UTF-32 BOM + if (size >= 4 && bytes[0] === 0x00 && bytes[1] === 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) { + return false; + } + + // UTF-32 LE BOM + if (size >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] === 0x00 && bytes[3] === 0x00) { + return false; + } + + // GB BOM + if (size >= 4 && bytes[0] == 0x84 && bytes[1] == 0x31 && bytes[2] == 0x95 && bytes[3] == 0x33) { + return false; + } + + if (total_bytes >= 5 && bytes.slice(0, 5) == "%PDF-") { + /* PDF. This is binary. */ + return true; + } + + // UTF-16 BE BOM + if (size >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) { + return false; + } + + // UTF-16 LE BOM + if (size >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) { + return false; + } + + for (var i = 0; i < total_bytes; i++) { + if (bytes[i] === 0) { // NULL byte--it's binary! + return true; + } + else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) { + // UTF-8 detection + if (bytes[i] > 193 && bytes[i] < 224 && i + 1 < total_bytes) { + i++; + if (bytes[i] > 127 && bytes[i] < 192) { + continue; + } + } + else if (bytes[i] > 223 && bytes[i] < 240 && i + 2 < total_bytes) { + i++; + if (bytes[i] > 127 && bytes[i] < 192 && bytes[i + 1] > 127 && bytes[i + 1] < 192) { + i++; + continue; + } + } + suspicious_bytes++; + // Read at least 32 bytes before making a decision + if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) { + return true; + } + } + } + + if ((suspicious_bytes * 100) / total_bytes > 10) { + return true; + } + + return false; +} + +module.exports.sync = function(bytes, size) { + // Only one arg + if (size === undefined) { + var file = bytes; + try { + if(!fs.statSync(file).isFile()) return false; + } catch (err) { + // otherwise continue on + } + var descriptor = fs.openSync(file, 'r'); + try { + // Read the file with no encoding for raw buffer access. + bytes = alloc(MAX_BYTES); + size = fs.readSync(descriptor, bytes, 0, bytes.length, 0); + } finally { + fs.closeSync(descriptor); + } + return isBinaryCheck(bytes, size); + } + else + return isBinaryCheck(bytes, size); +} diff --git a/node_modules/isbinaryfile/package.json b/node_modules/isbinaryfile/package.json new file mode 100644 index 0000000..97d9a10 --- /dev/null +++ b/node_modules/isbinaryfile/package.json @@ -0,0 +1,36 @@ +{ + "name": "isbinaryfile", + "description": "Detects if a file is binary in Node.js. Similar to Perl's -B.", + "version": "3.0.3", + "dependencies": { + "buffer-alloc": "^1.2.0" + }, + "devDependencies": { + "mocha": "^2.2.4", + "grunt": "~0.4.1", + "grunt-release": "~0.6.0", + "grunt-exec": "0.4.3", + "grunt-cli": "~0.1.13" + }, + "engines": { + "node": ">=0.6.0" + }, + "files": [ + "index.js" + ], + "license": "MIT", + "main": "./index.js", + "maintainers": [ + { + "name": "Garen J. Torikian", + "email": "gjtorikian@gmail.com" + } + ], + "repository": { + "type": "git", + "url": "https://github.com/gjtorikian/isBinaryFile" + }, + "scripts": { + "test": "mocha" + } +} |