pack.jsView |
---|
65 | 65 | |
66 | 66 | var ended |
67 | 67 | var numObjects = -1 |
68 | 68 | var checksum = createHash('sha1') |
69 | | - var b = buffered(checksum(read)) |
70 | | - var readByte = b.chunks(1) |
71 | | - var readWord = b.chunks(4) |
| 69 | + var b = buffered(read) |
| 70 | + |
| 71 | + var readByte = checksum(b.chunks(1)) |
| 72 | + var readWord = checksum(b.chunks(4)) |
72 | 73 | var readChecksum = b.chunks(20) |
73 | | - var expectChecksum = false |
| 74 | + var expectChecksum = true |
74 | 75 | var opts = { |
75 | 76 | verbosity: 2 |
76 | 77 | } |
77 | 78 | |
113 | 114 | if (ended = end) return cb(end) |
114 | 115 | var firstByte = buf[0] |
115 | 116 | type = objectTypes[(firstByte >> 4) & 7] |
116 | 117 | value = firstByte & 15 |
117 | | - console.error('byte1', firstByte, firstByte.toString(2), value, value.toString(2)) |
| 118 | + |
118 | 119 | shift = 4 |
119 | 120 | checkByte(firstByte) |
120 | 121 | }) |
121 | 122 | |
130 | 131 | if (ended = end) return cb(end) |
131 | 132 | var byte = buf[0] |
132 | 133 | value += (byte & 0x7f) << shift |
133 | 134 | shift += 7 |
134 | | - console.error('byte', byte, byte.toString(2), value, value.toString(2)) |
| 135 | + |
135 | 136 | checkByte(byte) |
136 | 137 | } |
137 | 138 | } |
138 | 139 | |
139 | 140 | function getObject(cb) { |
140 | 141 | readVarInt(function (end, type, length) { |
141 | | - console.error('read var int', end, type, length) |
| 142 | + if (opts.verbosity >= 2) |
| 143 | + console.error('read var int', end, type, length) |
| 144 | + numObjects-- |
142 | 145 | if (end === true && expectChecksum) |
143 | 146 | onEnd(new Error('Missing checksum')) |
144 | 147 | if (ended = end) return cb(end) |
145 | | - numObjects-- |
146 | 148 | |
147 | 149 | cb(null, type, inflateBytes(readByte)) |
148 | 150 | }) |
149 | 151 | } |
150 | 152 | |
151 | 153 | function readTrailer(cb) { |
| 154 | + |
| 155 | + var expected = checksum.digest() |
152 | 156 | readChecksum(null, function (end, value) { |
153 | 157 | cb(true) |
154 | | - var actual = checksum.digest() |
155 | | - if (!value.equals(actual)) |
| 158 | + if (end === true && expectChecksum) |
| 159 | + onEnd(new Error('Missing checksum')) |
| 160 | + if (!value.equals(expected)) { |
156 | 161 | onEnd(new Error('Checksum mismatch: ' + |
157 | | - actual.hexSlice() + ' != ' + value.hexSlice())) |
158 | | - else |
| 162 | + expected.hexSlice() + ' != ' + value.hexSlice())) |
| 163 | + } else { |
| 164 | + if (opts.verbosity >= 2) |
| 165 | + console.error('checksum ok', expected.hexSlice()) |
159 | 166 | onEnd(null) |
| 167 | + } |
160 | 168 | }) |
161 | 169 | } |
162 | 170 | |
163 | 171 | function readObject(abort, cb) { |