isbinaryfile.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright 2013-2022 the PM2 project authors. All rights reserved.
  3. * Use of this source code is governed by a license that
  4. * can be found in the LICENSE file.
  5. */
  6. var fs = require('fs');
  7. var max_bytes = 512;
  8. module.exports = function(bytes, size) {
  9. // Read the file with no encoding for raw buffer access.
  10. if (size === undefined) {
  11. var file = bytes;
  12. try {
  13. if(!fs.statSync(file).isFile()) return false;
  14. } catch (err) {
  15. // otherwise continue on
  16. }
  17. var descriptor = fs.openSync(file, 'r');
  18. try {
  19. bytes = Buffer.alloc(max_bytes);
  20. size = fs.readSync(descriptor, bytes, 0, bytes.length, 0);
  21. } finally {
  22. fs.closeSync(descriptor);
  23. }
  24. }
  25. // async version has a function instead of a `size`
  26. else if (typeof size === "function") {
  27. var file = bytes, callback = size;
  28. fs.stat(file, function(err, stat) {
  29. if (err || !stat.isFile()) return callback(null, false);
  30. fs.open(file, 'r', function(err, descriptor){
  31. if (err) return callback(err);
  32. var bytes = Buffer.alloc(max_bytes);
  33. // Read the file with no encoding for raw buffer access.
  34. fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){
  35. fs.close(descriptor, function(err2){
  36. if (err || err2)
  37. return callback(err || err2);
  38. return callback(null, isBinaryCheck(bytes, size));
  39. });
  40. });
  41. });
  42. });
  43. }
  44. return isBinaryCheck(bytes, size);
  45. }
  46. function isBinaryCheck(bytes, size) {
  47. if (size === 0)
  48. return false;
  49. var suspicious_bytes = 0;
  50. var total_bytes = Math.min(size, max_bytes);
  51. if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
  52. // UTF-8 BOM. This isn't binary.
  53. return false;
  54. }
  55. for (var i = 0; i < total_bytes; i++) {
  56. if (bytes[i] === 0) { // NULL byte--it's binary!
  57. return true;
  58. }
  59. else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {
  60. // UTF-8 detection
  61. if (bytes[i] > 193 && bytes[i] < 224 && i + 1 < total_bytes) {
  62. i++;
  63. if (bytes[i] > 127 && bytes[i] < 192) {
  64. continue;
  65. }
  66. }
  67. else if (bytes[i] > 223 && bytes[i] < 240 && i + 2 < total_bytes) {
  68. i++;
  69. if (bytes[i] > 127 && bytes[i] < 192 && bytes[i + 1] > 127 && bytes[i + 1] < 192) {
  70. i++;
  71. continue;
  72. }
  73. }
  74. suspicious_bytes++;
  75. // Read at least 32 bytes before making a decision
  76. if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) {
  77. return true;
  78. }
  79. }
  80. }
  81. if ((suspicious_bytes * 100) / total_bytes > 10) {
  82. return true;
  83. }
  84. return false;
  85. }