crc32.njs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env node
  2. /* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
  3. /* eslint-env node */
  4. /* vim: set ts=2 ft=javascript: */
  5. /*jshint node:true */
  6. var X/*:CRC32Module*/;
  7. try { X = require('../'); } catch(e) { X = require('crc-32'); }
  8. function help()/*:number*/ {
  9. [
  10. "usage: crc32 [options] [filename]",
  11. "",
  12. "Options:",
  13. " -h, --help output usage information",
  14. " -V, --version output the version number",
  15. " -S, --seed=<n> use integer seed as starting value (rolling CRC)",
  16. " -H, --hex-seed=<h> use hex seed as starting value (rolling CRC)",
  17. " -d, --signed print result with format `%d` (default)",
  18. " -u, --unsigned print result with format `%u`",
  19. " -x, --hex print result with format `%0.8x`",
  20. " -X, --HEX print result with format `%0.8X`",
  21. " -c, --crc32c use CRC32C (Castagnoli)",
  22. " -F, --format=<s> use specified printf format",
  23. "",
  24. "Set filename = '-' or pipe data into crc32 to read from stdin",
  25. "Default output mode is signed (-d)",
  26. ""
  27. ].forEach(function(l) { console.log(l); });
  28. return 0;
  29. }
  30. function version()/*:number*/ { console.log(X.version); return 0; }
  31. var fs = require('fs');
  32. try { require('exit-on-epipe'); } catch(e) {}
  33. function die(msg/*:string*/, ec/*:?number*/)/*:void*/ { console.error(msg); process.exit(ec || 0); }
  34. var args/*:Array<string>*/ = process.argv.slice(2);
  35. var filename/*:string*/ = "";
  36. var fmt/*:string*/ = "";
  37. var seed = 0, r = 10;
  38. for(var i = 0; i < args.length; ++i) {
  39. var arg = args[i];
  40. if(arg.charCodeAt(0) != 45) { if(filename === "") filename = arg; continue; }
  41. var m = arg.indexOf("=") == -1 ? arg : arg.substr(0, arg.indexOf("="));
  42. switch(m) {
  43. case "-": filename = "-"; break;
  44. case "--help": case "-h": process.exit(help()); break;
  45. case "--version": case "-V": process.exit(version()); break;
  46. case "--crc32c": case "-c": try { X = require('../crc32c'); } catch(e) { X = require('crc-32/crc32c'); } break;
  47. case "--signed": case "-d": fmt = "%d"; break;
  48. case "--unsigned": case "-u": fmt = "%u"; break;
  49. case "--hex": case "-x": fmt = "%0.8x"; break;
  50. case "--HEX": case "-X": fmt = "%0.8X"; break;
  51. case "--format": case "-F":
  52. try {
  53. require("printj");
  54. fmt = ((m!=arg) ? arg.substr(m.length+1) : args[++i])||"";
  55. } catch(e) {
  56. console.error("The `crc-32` module removed the `printj` dependency for formatting");
  57. console.error("Use the `crc32-cli` module instead:");
  58. console.error(" $ npx crc32-cli [options] [filename]");
  59. } break;
  60. case "--hex-seed": case "-H": r = 16;
  61. /* falls through */
  62. case "--seed": case "-S":
  63. seed=parseInt((m!=arg) ? arg.substr(m.length+1) : args[++i], r)||0; break;
  64. default: die("crc32: unrecognized option `" + arg + "'", 22);
  65. }
  66. }
  67. if(!process.stdin.isTTY) filename = filename || "-";
  68. if(filename.length===0) die("crc32: must specify a filename ('-' for stdin)",1);
  69. var crc32 = seed;
  70. // $FlowIgnore -- Writable is callable but type sig disagrees
  71. var writable = require('stream').Writable();
  72. writable._write = function(chunk, e, cb) { crc32 = X.buf(chunk, crc32); cb(); };
  73. writable._writev = function(chunks, cb) {
  74. chunks.forEach(function(c) { crc32 = X.buf(c.chunk, crc32);});
  75. cb();
  76. };
  77. writable.on('finish', function() {
  78. if(fmt === "") console.log(crc32);
  79. else try { console.log(require("printj").sprintf(fmt, crc32)); } catch(e) {
  80. switch(fmt) {
  81. case "%d": console.log(crc32); break;
  82. case "%u": console.log(crc32 >>> 0); break;
  83. case "%0.8x": console.log((crc32 >>> 0).toString(16).padStart(8, "0").toLowerCase()); break;
  84. case "%0.8X": console.log((crc32 >>> 0).toString(16).padStart(8, "0").toUpperCase()); break;
  85. }
  86. }
  87. });
  88. if(filename === "-") process.stdin.pipe(writable);
  89. else if(fs.existsSync(filename)) fs.createReadStream(filename).pipe(writable);
  90. else die("crc32: " + filename + ": No such file or directory", 2);