1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var fs = require('fs');
|
7 | var max_bytes = 512;
|
8 |
|
9 | module.exports = function(bytes, size) {
|
10 |
|
11 | if (size === undefined) {
|
12 | var file = bytes;
|
13 | try {
|
14 | if(!fs.statSync(file).isFile()) return false;
|
15 | } catch (err) {
|
16 |
|
17 | }
|
18 | var descriptor = fs.openSync(file, 'r');
|
19 | try {
|
20 | bytes = Buffer.alloc(max_bytes);
|
21 | size = fs.readSync(descriptor, bytes, 0, bytes.length, 0);
|
22 | } finally {
|
23 | fs.closeSync(descriptor);
|
24 | }
|
25 | }
|
26 |
|
27 | else if (typeof size === "function") {
|
28 | var file = bytes, callback = size;
|
29 | fs.stat(file, function(err, stat) {
|
30 | if (err || !stat.isFile()) return callback(null, false);
|
31 |
|
32 | fs.open(file, 'r', function(err, descriptor){
|
33 | if (err) return callback(err);
|
34 | var bytes = Buffer.alloc(max_bytes);
|
35 |
|
36 | fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){
|
37 | fs.close(descriptor, function(err2){
|
38 | if (err || err2)
|
39 | return callback(err || err2);
|
40 | return callback(null, isBinaryCheck(bytes, size));
|
41 | });
|
42 | });
|
43 | });
|
44 | });
|
45 | }
|
46 |
|
47 | return isBinaryCheck(bytes, size);
|
48 | }
|
49 |
|
50 | function isBinaryCheck(bytes, size) {
|
51 | if (size === 0)
|
52 | return false;
|
53 |
|
54 | var suspicious_bytes = 0;
|
55 | var total_bytes = Math.min(size, max_bytes);
|
56 |
|
57 | if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
|
58 |
|
59 | return false;
|
60 | }
|
61 |
|
62 | for (var i = 0; i < total_bytes; i++) {
|
63 | if (bytes[i] === 0) {
|
64 | return true;
|
65 | }
|
66 | else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {
|
67 |
|
68 | if (bytes[i] > 193 && bytes[i] < 224 && i + 1 < total_bytes) {
|
69 | i++;
|
70 | if (bytes[i] > 127 && bytes[i] < 192) {
|
71 | continue;
|
72 | }
|
73 | }
|
74 | else if (bytes[i] > 223 && bytes[i] < 240 && i + 2 < total_bytes) {
|
75 | i++;
|
76 | if (bytes[i] > 127 && bytes[i] < 192 && bytes[i + 1] > 127 && bytes[i + 1] < 192) {
|
77 | i++;
|
78 | continue;
|
79 | }
|
80 | }
|
81 | suspicious_bytes++;
|
82 |
|
83 | if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) {
|
84 | return true;
|
85 | }
|
86 | }
|
87 | }
|
88 |
|
89 | if ((suspicious_bytes * 100) / total_bytes > 10) {
|
90 | return true;
|
91 | }
|
92 |
|
93 | return false;
|
94 | }
|