♻ Move ByteReader class into separate file
This commit is contained in:
parent
523f306ab3
commit
8f5547222c
2 changed files with 36 additions and 36 deletions
|
@ -1,42 +1,7 @@
|
||||||
import * as zlib from 'zlib';
|
import * as zlib from 'zlib';
|
||||||
import { ISsnFileEntry } from '../interfaces/ISsnFileEntry';
|
import { ISsnFileEntry } from '../interfaces/ISsnFileEntry';
|
||||||
import decryptFile from './decryption/decryptFile';
|
import decryptFile from './decryption/decryptFile';
|
||||||
|
import ByteReader from './extractFileByteReader';
|
||||||
class ByteReader {
|
|
||||||
private dvArray: DataView[];
|
|
||||||
private dvIndex = 0;
|
|
||||||
private pos = 0;
|
|
||||||
constructor(dvArray: DataView[], startDvIndex: number, offset: number) {
|
|
||||||
this.dvArray = dvArray;
|
|
||||||
this.dvIndex = startDvIndex;
|
|
||||||
this.pos = offset;
|
|
||||||
}
|
|
||||||
public readByte() {
|
|
||||||
const curByte = this.dvArray[this.dvIndex].getUint8(this.pos);
|
|
||||||
this.pos += 1;
|
|
||||||
if (this.pos >= this.dvArray[this.dvIndex].byteLength) {
|
|
||||||
this.pos = 0;
|
|
||||||
this.dvIndex += 1;
|
|
||||||
if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); }
|
|
||||||
}
|
|
||||||
return curByte;
|
|
||||||
}
|
|
||||||
public seek(num: number) {
|
|
||||||
this.pos += num;
|
|
||||||
if (this.pos >= this.dvArray[this.dvIndex].byteLength) {
|
|
||||||
this.pos -= this.dvArray[this.dvIndex].byteLength;
|
|
||||||
this.dvIndex += 1;
|
|
||||||
if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public extractDv(length: number) {
|
|
||||||
const dv = new DataView(new ArrayBuffer(length));
|
|
||||||
for (let i = 0; i < length; i += 1) {
|
|
||||||
dv.setUint8(i, this.readByte()); //TODO: refactor this so it is more optimized
|
|
||||||
}
|
|
||||||
return dv;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Extracts the given file from the given DataView array and returns it as an ArrayBuffer.
|
/** Extracts the given file from the given DataView array and returns it as an ArrayBuffer.
|
||||||
* Will throw an error when end of final DataView is reached.
|
* Will throw an error when end of final DataView is reached.
|
||||||
|
|
35
src/ssn/extractFileByteReader.ts
Normal file
35
src/ssn/extractFileByteReader.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
export default class ByteReader {
|
||||||
|
private dvArray: DataView[];
|
||||||
|
private dvIndex = 0;
|
||||||
|
private pos = 0;
|
||||||
|
constructor(dvArray: DataView[], startDvIndex: number, offset: number) {
|
||||||
|
this.dvArray = dvArray;
|
||||||
|
this.dvIndex = startDvIndex;
|
||||||
|
this.pos = offset;
|
||||||
|
}
|
||||||
|
public readByte() {
|
||||||
|
const curByte = this.dvArray[this.dvIndex].getUint8(this.pos);
|
||||||
|
this.pos += 1;
|
||||||
|
if (this.pos >= this.dvArray[this.dvIndex].byteLength) {
|
||||||
|
this.pos = 0;
|
||||||
|
this.dvIndex += 1;
|
||||||
|
if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); }
|
||||||
|
}
|
||||||
|
return curByte;
|
||||||
|
}
|
||||||
|
public seek(num: number) {
|
||||||
|
this.pos += num;
|
||||||
|
if (this.pos >= this.dvArray[this.dvIndex].byteLength) {
|
||||||
|
this.pos -= this.dvArray[this.dvIndex].byteLength;
|
||||||
|
this.dvIndex += 1;
|
||||||
|
if (this.dvIndex >= this.dvArray.length) { throw new Error('Tried to read beyond DataView boundary in extractFile'); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public extractDv(length: number) {
|
||||||
|
const dv = new DataView(new ArrayBuffer(length));
|
||||||
|
for (let i = 0; i < length; i += 1) {
|
||||||
|
dv.setUint8(i, this.readByte()); //TODO: refactor this so it is more optimized
|
||||||
|
}
|
||||||
|
return dv;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue