mirror of
https://github.com/transatoshi-mw/grin-web-wallet.git
synced 2025-10-07 00:02:47 +00:00
contents of zip
This commit is contained in:
110
scripts/bit_writer.js
Executable file
110
scripts/bit_writer.js
Executable file
@@ -0,0 +1,110 @@
|
||||
// Use strict
|
||||
"use strict";
|
||||
|
||||
|
||||
// Classes
|
||||
|
||||
// Bit writer class
|
||||
class BitWriter {
|
||||
|
||||
// Public
|
||||
|
||||
// Constructor
|
||||
constructor() {
|
||||
|
||||
// Set data
|
||||
this.data = new Uint8Array([]);
|
||||
|
||||
// Initialize byte index
|
||||
this.byteIndex = 0;
|
||||
|
||||
// Initialize bit index
|
||||
this.bitIndex = 0;
|
||||
}
|
||||
|
||||
// Set bits
|
||||
setBits(value, numberOfBits) {
|
||||
|
||||
// Go through all bytes past one byte
|
||||
while(numberOfBits > Common.BITS_IN_A_BYTE) {
|
||||
|
||||
// Set byte
|
||||
this.setBits(value >>> (Common.BITS_IN_A_BYTE * (Math.floor(numberOfBits / Common.BITS_IN_A_BYTE) - 1) + numberOfBits % Common.BITS_IN_A_BYTE), Math.min(numberOfBits, Common.BITS_IN_A_BYTE));
|
||||
|
||||
// Update number of bits
|
||||
numberOfBits -= Common.BITS_IN_A_BYTE;
|
||||
}
|
||||
|
||||
// Check if no bits requested
|
||||
if(numberOfBits === 0) {
|
||||
|
||||
// Return
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if more data is needed
|
||||
if(this.bitIndex === 0 || this.bitIndex + numberOfBits > Common.BITS_IN_A_BYTE) {
|
||||
|
||||
// Increase data's size by one
|
||||
var temp = new Uint8Array(this.data["length"] + 1);
|
||||
|
||||
temp.set(this.data);
|
||||
|
||||
this.data = temp;
|
||||
}
|
||||
|
||||
// Check if value will overflow into the next byte
|
||||
if(this.bitIndex + numberOfBits > Common.BITS_IN_A_BYTE) {
|
||||
|
||||
// Include data in value at byte index
|
||||
this.data[this.byteIndex] |= value >>> ((this.bitIndex + numberOfBits) - Common.BITS_IN_A_BYTE);
|
||||
|
||||
// Include data in value at next byte index
|
||||
this.data[this.byteIndex + 1] |= value << (Common.BITS_IN_A_BYTE * 2 - (this.bitIndex + numberOfBits));
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
else {
|
||||
|
||||
// Include data in value at byte index
|
||||
this.data[this.byteIndex] |= value << (Common.BITS_IN_A_BYTE - (this.bitIndex + numberOfBits));
|
||||
}
|
||||
|
||||
// Update bit index
|
||||
this.bitIndex += numberOfBits;
|
||||
|
||||
// Check if bit index overflowed into the next byte
|
||||
if(this.bitIndex >= Common.BITS_IN_A_BYTE) {
|
||||
|
||||
// Increment byte index
|
||||
++this.byteIndex;
|
||||
|
||||
// Correct bit index
|
||||
this.bitIndex %= Common.BITS_IN_A_BYTE;
|
||||
}
|
||||
}
|
||||
|
||||
// Set bytes
|
||||
setBytes(bytes) {
|
||||
|
||||
// Go through all bytes
|
||||
for(var i = 0; i < bytes["length"]; ++i) {
|
||||
|
||||
// Set byte in the data
|
||||
this.setBits(bytes[i], Common.BITS_IN_A_BYTE);
|
||||
}
|
||||
}
|
||||
|
||||
// Get bytes
|
||||
getBytes() {
|
||||
|
||||
// Return data
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Main function
|
||||
|
||||
// Set global object's bit writer
|
||||
globalThis["BitWriter"] = BitWriter;
|
Reference in New Issue
Block a user