mainHeader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var Utils = require("../util"),
  2. Constants = Utils.Constants;
  3. /* The entries in the end of central directory */
  4. module.exports = function () {
  5. var _volumeEntries = 0,
  6. _totalEntries = 0,
  7. _size = 0,
  8. _offset = 0,
  9. _commentLength = 0;
  10. return {
  11. get diskEntries() {
  12. return _volumeEntries;
  13. },
  14. set diskEntries(/*Number*/ val) {
  15. _volumeEntries = _totalEntries = val;
  16. },
  17. get totalEntries() {
  18. return _totalEntries;
  19. },
  20. set totalEntries(/*Number*/ val) {
  21. _totalEntries = _volumeEntries = val;
  22. },
  23. get size() {
  24. return _size;
  25. },
  26. set size(/*Number*/ val) {
  27. _size = val;
  28. },
  29. get offset() {
  30. return _offset;
  31. },
  32. set offset(/*Number*/ val) {
  33. _offset = val;
  34. },
  35. get commentLength() {
  36. return _commentLength;
  37. },
  38. set commentLength(/*Number*/ val) {
  39. _commentLength = val;
  40. },
  41. get mainHeaderSize() {
  42. return Constants.ENDHDR + _commentLength;
  43. },
  44. loadFromBinary: function (/*Buffer*/ data) {
  45. // data should be 22 bytes and start with "PK 05 06"
  46. // or be 56+ bytes and start with "PK 06 06" for Zip64
  47. if (
  48. (data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG) &&
  49. (data.length < Constants.ZIP64HDR || data.readUInt32LE(0) !== Constants.ZIP64SIG)
  50. ) {
  51. throw new Error(Utils.Errors.INVALID_END);
  52. }
  53. if (data.readUInt32LE(0) === Constants.ENDSIG) {
  54. // number of entries on this volume
  55. _volumeEntries = data.readUInt16LE(Constants.ENDSUB);
  56. // total number of entries
  57. _totalEntries = data.readUInt16LE(Constants.ENDTOT);
  58. // central directory size in bytes
  59. _size = data.readUInt32LE(Constants.ENDSIZ);
  60. // offset of first CEN header
  61. _offset = data.readUInt32LE(Constants.ENDOFF);
  62. // zip file comment length
  63. _commentLength = data.readUInt16LE(Constants.ENDCOM);
  64. } else {
  65. // number of entries on this volume
  66. _volumeEntries = Utils.readBigUInt64LE(data, Constants.ZIP64SUB);
  67. // total number of entries
  68. _totalEntries = Utils.readBigUInt64LE(data, Constants.ZIP64TOT);
  69. // central directory size in bytes
  70. _size = Utils.readBigUInt64LE(data, Constants.ZIP64SIZE);
  71. // offset of first CEN header
  72. _offset = Utils.readBigUInt64LE(data, Constants.ZIP64OFF);
  73. _commentLength = 0;
  74. }
  75. },
  76. toBinary: function () {
  77. var b = Buffer.alloc(Constants.ENDHDR + _commentLength);
  78. // "PK 05 06" signature
  79. b.writeUInt32LE(Constants.ENDSIG, 0);
  80. b.writeUInt32LE(0, 4);
  81. // number of entries on this volume
  82. b.writeUInt16LE(_volumeEntries, Constants.ENDSUB);
  83. // total number of entries
  84. b.writeUInt16LE(_totalEntries, Constants.ENDTOT);
  85. // central directory size in bytes
  86. b.writeUInt32LE(_size, Constants.ENDSIZ);
  87. // offset of first CEN header
  88. b.writeUInt32LE(_offset, Constants.ENDOFF);
  89. // zip file comment length
  90. b.writeUInt16LE(_commentLength, Constants.ENDCOM);
  91. // fill comment memory with spaces so no garbage is left there
  92. b.fill(" ", Constants.ENDHDR);
  93. return b;
  94. },
  95. toJSON: function () {
  96. // creates 0x0000 style output
  97. const offset = function (nr, len) {
  98. let offs = nr.toString(16).toUpperCase();
  99. while (offs.length < len) offs = "0" + offs;
  100. return "0x" + offs;
  101. };
  102. return {
  103. diskEntries: _volumeEntries,
  104. totalEntries: _totalEntries,
  105. size: _size + " bytes",
  106. offset: offset(_offset, 4),
  107. commentLength: _commentLength
  108. };
  109. },
  110. toString: function () {
  111. return JSON.stringify(this.toJSON(), null, "\t");
  112. }
  113. };
  114. };
  115. // Misspelled