TrinetraLabs/Placebo_AI
0
1"use strict";2var utils = require("../utils");3 4function DataReader(data) {5 this.data = data; // type : see implementation6 this.length = data.length;7 this.index = 0;8 this.zero = 0;9}10DataReader.prototype = {11 /**12 * Check that the offset will not go too far.13 * @param {string} offset the additional offset to check.14 * @throws {Error} an Error if the offset is out of bounds.15 */16 checkOffset: function(offset) {17 this.checkIndex(this.index + offset);18 },19 /**20 * Check that the specified index will not be too far.21 * @param {string} newIndex the index to check.22 * @throws {Error} an Error if the index is out of bounds.23 */24 checkIndex: function(newIndex) {25 if (this.length < this.zero + newIndex || newIndex < 0) {26 throw new Error("End of data reached (data length = " + this.length + ", asked index = " + (newIndex) + "). Corrupted zip ?");27 }28 },29 /**30 * Change the index.31 * @param {number} newIndex The new index.32 * @throws {Error} if the new index is out of the data.33 */34 setIndex: function(newIndex) {35 this.checkIndex(newIndex);36 this.index = newIndex;37 },38 /**39 * Skip the next n bytes.40 * @param {number} n the number of bytes to skip.41 * @throws {Error} if the new index is out of the data.42 */43 skip: function(n) {44 this.setIndex(this.index + n);45 },46 /**47 * Get the byte at the specified index.48 * @param {number} i the index to use.49 * @return {number} a byte.50 */51 byteAt: function() {52 // see implementations53 },54 /**55 * Get the next number with a given byte size.56 * @param {number} size the number of bytes to read.57 * @return {number} the corresponding number.58 */59 readInt: function(size) {60 var result = 0,61 i;62 this.checkOffset(size);63 for (i = this.index + size - 1; i >= this.index; i--) {64 result = (result << 8) + this.byteAt(i);65 }66 this.index += size;67 return result;68 },69 /**70 * Get the next string with a given byte size.71 * @param {number} size the number of bytes to read.72 * @return {string} the corresponding string.73 */74 readString: function(size) {75 return utils.transformTo("string", this.readData(size));76 },77 /**78 * Get raw data without conversion, <size> bytes.79 * @param {number} size the number of bytes to read.80 * @return {Object} the raw data, implementation specific.81 */82 readData: function() {83 // see implementations84 },85 /**86 * Find the last occurrence of a zip signature (4 bytes).87 * @param {string} sig the signature to find.88 * @return {number} the index of the last occurrence, -1 if not found.89 */90 lastIndexOfSignature: function() {91 // see implementations92 },93 /**94 * Read the signature (4 bytes) at the current position and compare it with sig.95 * @param {string} sig the expected signature96 * @return {boolean} true if the signature matches, false otherwise.97 */98 readAndCheckSignature: function() {99 // see implementations100 },101 /**102 * Get the next date.103 * @return {Date} the date.104 */105 readDate: function() {106 var dostime = this.readInt(4);107 return new Date(Date.UTC(108 ((dostime >> 25) & 0x7f) + 1980, // year109 ((dostime >> 21) & 0x0f) - 1, // month110 (dostime >> 16) & 0x1f, // day111 (dostime >> 11) & 0x1f, // hour112 (dostime >> 5) & 0x3f, // minute113 (dostime & 0x1f) << 1)); // second114 }115};116module.exports = DataReader;117 