UNPKG

7.2 kBJavaScriptView Raw
1var Utils = require("../util"),
2 Constants = Utils.Constants;
3
4/* The central directory file header */
5module.exports = function () {
6 var _verMade = 0x0A,
7 _version = 10,
8 _flags = 0,
9 _method = 0,
10 _time = 0,
11 _crc = 0,
12 _compressedSize = 0,
13 _size = 0,
14 _fnameLen = 0,
15 _extraLen = 0,
16 _comLen = 0,
17 _diskStart = 0,
18 _inattr = 438,
19 _attr = 438,
20 _offset = 0;
21
22 return {
23 get made () { return _verMade; },
24 set made (val) { _verMade = val; },
25
26 get version () { return _version; },
27 set version (val) { _version = val },
28
29 get flags () { return _flags },
30 set flags (val) { _flags = val; },
31
32 get method () { return _method; },
33 set method (val) { _method = val; },
34
35 get time () { return new Date(
36 ((_time >> 25) & 0x7f) + 1980,
37 ((_time >> 21) & 0x0f) - 1,
38 (_time >> 16) & 0x1f,
39 (_time >> 11) & 0x1f,
40 (_time >> 5) & 0x3f,
41 (_time & 0x1f) << 1
42 );
43 },
44 set time (val) { val = new Date(val);
45 _time = (val.getFullYear() - 1980 & 0x7f) << 25
46 | (val.getMonth() + 1) << 21
47 | val.getDay() << 16
48 | val.getHours() << 11
49 | val.getMinutes() << 5
50 | val.getSeconds() >> 1;
51 },
52
53 get crc () { return _crc; },
54 set crc (val) { _crc = val; },
55
56 get compressedSize () { return _compressedSize; },
57 set compressedSize (val) { _compressedSize = val; },
58
59 get size () { return _size; },
60 set size (val) { _size = val; },
61
62 get fileNameLength () { return _fnameLen; },
63 set fileNameLength (val) { _fnameLen = val; },
64
65 get extraLength () { return _extraLen },
66 set extraLength (val) { _extraLen = val; },
67
68 get commentLength () { return _comLen },
69 set commentLength (val) { _comLen = val },
70
71 get diskNumStart () { return _diskStart },
72 set diskNumStart (val) { _diskStart = val },
73
74 get inAttr () { return _inattr },
75 set inAttr (val) { _inattr = val },
76
77 get attr () { return _attr },
78 set attr (val) { _attr = val },
79
80 get offset () { return _offset },
81 set offset (val) { _offset = val },
82
83 get encripted () { return (_flags & 1) == 1 },
84
85 get entryHeaderSize () {
86 return Constants.CENHDR + _fnameLen + _extraLen + _comLen;
87 },
88
89 loadFromBinary : function(/*Buffer*/data) {
90 // data should be 46 bytes and start with "PK 01 02"
91 if (data.length != Constants.CENHDR || data.readUInt32LE(0) != Constants.CENSIG) {
92 throw Utils.Errors.INVALID_CEN;
93 }
94 // version made by
95 _verMade = data.readUInt16LE(Constants.CENVEM);
96 // version needed to extract
97 _version = data.readUInt16LE(Constants.CENVER);
98 // encrypt, decrypt flags
99 _flags = data.readUInt16LE(Constants.CENFLG);
100 // compression method
101 _method = data.readUInt16LE(Constants.CENHOW);
102 // modification time (2 bytes time, 2 bytes date)
103 _time = data.readUInt32LE(Constants.CENTIM);
104 // uncompressed file crc-32 value
105 _crc = data.readUInt32LE(Constants.CENCRC);
106 // compressed size
107 _compressedSize = data.readUInt32LE(Constants.CENSIZ);
108 // uncompressed size
109 _size = data.readUInt32LE(Constants.CENLEN);
110 // filename length
111 _fnameLen = data.readUInt16LE(Constants.CENNAM);
112 // extra field length
113 _extraLen = data.readUInt16LE(Constants.CENEXT);
114 // file comment length
115 _comLen = data.readUInt16LE(Constants.CENCOM);
116 // volume number start
117 _diskStart = data.readUInt16LE(Constants.CENDSK);
118 // internal file attributes
119 _inattr = data.readUInt16LE(Constants.CENATT);
120 // external file attributes
121 _attr = data.readUInt32LE(Constants.CENATX);
122 // LOC header offset
123 _offset = data.readUInt32LE(Constants.CENOFF);
124 },
125
126 toBinary : function() {
127 // CEN header size (46 bytes)
128 var data = new Buffer(Constants.CENHDR + _fnameLen + _extraLen + _comLen);
129 // "PK\001\002"
130 data.writeUInt32LE(Constants.CENSIG, 0);
131 // version made by
132 data.writeUInt16LE(_verMade, Constants.CENVEM);
133 // version needed to extract
134 data.writeUInt16LE(_version, Constants.CENVER);
135 // encrypt, decrypt flags
136 data.writeUInt16LE(_flags, Constants.CENFLG);
137 // compression method
138 data.writeUInt16LE(_method, Constants.CENHOW);
139 // modification time (2 bytes time, 2 bytes date)
140 data.writeUInt32LE(_time, Constants.CENTIM);
141 // uncompressed file crc-32 value
142 data.writeInt32LE(_crc, Constants.CENCRC, true);
143 // compressed size
144 data.writeUInt32LE(_compressedSize, Constants.CENSIZ);
145 // uncompressed size
146 data.writeUInt32LE(_size, Constants.CENLEN);
147 // filename length
148 data.writeUInt16LE(_fnameLen, Constants.CENNAM);
149 // extra field length
150 data.writeUInt16LE(_extraLen, Constants.CENEXT);
151 // file comment length
152 data.writeUInt16LE(_comLen, Constants.CENCOM);
153 // volume number start
154 data.writeUInt16LE(_diskStart, Constants.CENDSK);
155 // internal file attributes
156 data.writeUInt16LE(_inattr, Constants.CENATT);
157 // external file attributes
158 data.writeUInt32LE(_attr, Constants.CENATX);
159 // LOC header offset
160 data.writeUInt32LE(_offset, Constants.CENOFF);
161 // fill all with
162 data.fill(0x00, Constants.CENHDR);
163 return data;
164 },
165
166 toString : function() {
167 return '{\n' +
168 '\t"made" : ' + _verMade + ",\n" +
169 '\t"version" : ' + _version + ",\n" +
170 '\t"flags" : ' + _flags + ",\n" +
171 '\t"method" : ' + Utils.methodToString(_method) + ",\n" +
172 '\t"time" : ' + _time + ",\n" +
173 '\t"crc" : 0x' + _crc.toString(16).toUpperCase() + ",\n" +
174 '\t"compressedSize" : ' + _compressedSize + " bytes,\n" +
175 '\t"size" : ' + _size + " bytes,\n" +
176 '\t"fileNameLength" : ' + _fnameLen + ",\n" +
177 '\t"extraLength" : ' + _extraLen + " bytes,\n" +
178 '\t"commentLength" : ' + _comLen + " bytes,\n" +
179 '\t"diskNumStart" : ' + _diskStart + ",\n" +
180 '\t"inAttr" : ' + _inattr + ",\n" +
181 '\t"attr" : ' + _attr + ",\n" +
182 '\t"offset" : ' + _offset + ",\n" +
183 '\t"entryHeaderSize" : ' + (Constants.CENHDR + _fnameLen + _extraLen + _comLen) + " bytes\n" +
184 '}';
185 }
186 }
187};
\No newline at end of file