2 var utils = require('./utils');
4 function DataReader(data) {
5 this.data = null; // type : see implementation
9 DataReader.prototype = {
11 * Check that the offset will not go too far.
12 * @param {string} offset the additional offset to check.
13 * @throws {Error} an Error if the offset is out of bounds.
15 checkOffset: function(offset) {
16 this.checkIndex(this.index + offset);
19 * Check that the specifed index will not be too far.
20 * @param {string} newIndex the index to check.
21 * @throws {Error} an Error if the index is out of bounds.
23 checkIndex: function(newIndex) {
24 if (this.length < newIndex || newIndex < 0) {
25 throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?");
30 * @param {number} newIndex The new index.
31 * @throws {Error} if the new index is out of the data.
33 setIndex: function(newIndex) {
34 this.checkIndex(newIndex);
35 this.index = newIndex;
38 * Skip the next n bytes.
39 * @param {number} n the number of bytes to skip.
40 * @throws {Error} if the new index is out of the data.
43 this.setIndex(this.index + n);
46 * Get the byte at the specified index.
47 * @param {number} i the index to use.
48 * @return {number} a byte.
51 // see implementations
54 * Get the next number with a given byte size.
55 * @param {number} size the number of bytes to read.
56 * @return {number} the corresponding number.
58 readInt: function(size) {
61 this.checkOffset(size);
62 for (i = this.index + size - 1; i >= this.index; i--) {
63 result = (result << 8) + this.byteAt(i);
69 * Get the next string with a given byte size.
70 * @param {number} size the number of bytes to read.
71 * @return {string} the corresponding string.
73 readString: function(size) {
74 return utils.transformTo("string", this.readData(size));
77 * Get raw data without conversion, <size> bytes.
78 * @param {number} size the number of bytes to read.
79 * @return {Object} the raw data, implementation specific.
81 readData: function(size) {
82 // see implementations
85 * Find the last occurence of a zip signature (4 bytes).
86 * @param {string} sig the signature to find.
87 * @return {number} the index of the last occurence, -1 if not found.
89 lastIndexOfSignature: function(sig) {
90 // see implementations
94 * @return {Date} the date.
96 readDate: function() {
97 var dostime = this.readInt(4);
99 ((dostime >> 25) & 0x7f) + 1980, // year
100 ((dostime >> 21) & 0x0f) - 1, // month
101 (dostime >> 16) & 0x1f, // day
102 (dostime >> 11) & 0x1f, // hour
103 (dostime >> 5) & 0x3f, // minute
104 (dostime & 0x1f) << 1); // second
107 module.exports = DataReader;