Skip to content

Instantly share code, notes, and snippets.

@PROPHESSOR
Last active July 23, 2021 23:29
Show Gist options
  • Save PROPHESSOR/b01ce16b546614ee8e4d49d50f65c336 to your computer and use it in GitHub Desktop.
Save PROPHESSOR/b01ce16b546614ee8e4d49d50f65c336 to your computer and use it in GitHub Desktop.
PROPHESSOR's ByteTools for many programming languages

PROPHESSOR's ByteTools libraries specification

Languages

  • C++
  • Python 3
  • JavaScript (Node.js)
  • JavaScript (Browser)
  • TypeScript (Node.js)

TODO: Write this

Methods

  • tell - returns current offset
  • seek(offset, mode) - changes the offset {mode 'START' - from start of file, mode 'CUR' - from current offset, mode 'END'? - from end of file}
  • read... - returns the value and change the offset
  • seek... - returns the value but doesn't change the offset
  • skip... - changes the offset and returns it
  • readString(length=-1) - reads the string to null byte or to specified length

Data types

  • Int8 - signed byte
  • UInt8 - unsigned byte (char)
  • Int16LE - signed word in little endian (short)
  • UInt16LE - unsigned word in little endian (unsigned short)
  • Int16BE - signed word in big endian (short)
  • UInt16BE - unsigned word in big endian (unsigned short)
  • Int32LE - signed double word in little endian (int)
  • UInt32LE - unsigned double word in little endian (unsigned int)
  • Int32BE - signed double word in big endian (int)
  • UInt32BE - unsigned double word in big endian (unsigned int)
/**
* ByteTools (C++) v1.1.1
*
* Copyright (c) PROPHESSOR 2019
*/
#pragma once
#include <iostream>
#include <cstdio>
#include <stdint.h>
#define SEEK_START SEEK_SET
class ByteTools {
public:
FILE *m_buffer;
ByteTools() {
std::cout << "ByteTools (C++) v1.0.0 // Copyright (c) PROPHESSOR 2019" << std::endl;
}
ByteTools(const char* filename) {
ByteTools();
open(filename);
}
void open(const char* filename) {
m_buffer = fopen(filename, "rb");
}
size_t tell(void) {
return ftell(m_buffer);
}
void seek(long int offset, int mode) {
fseek(m_buffer, offset, mode);
}
// read
// int8
int8_t readInt8() {
int8_t tmp;
fread(&tmp, sizeof(int8_t), 1, m_buffer);
return tmp;
}
uint8_t readUInt8() {
uint8_t tmp;
fread(&tmp, sizeof(uint8_t), 1, m_buffer);
return tmp;
}
// int16
int16_t readInt16() {
int16_t tmp;
fread(&tmp, sizeof(int16_t), 1, m_buffer);
return tmp;
}
uint16_t readUInt16() {
uint16_t tmp;
fread(&tmp, sizeof(uint16_t), 1, m_buffer);
return tmp;
}
int16_t readInt16LE() {
return readInt16();
}
uint16_t readUInt16LE() {
return readUInt16();
}
// int32
int32_t readInt32() {
int32_t tmp;
fread(&tmp, sizeof(int32_t), 1, m_buffer);
return tmp;
}
uint32_t readUInt32() {
uint32_t tmp;
fread(&tmp, sizeof(uint32_t), 1, m_buffer);
return tmp;
}
int32_t readInt32LE() {
return readInt32();
}
uint32_t readUInt32LE() {
return readUInt32();
}
// seek
// int8
int8_t seekInt8() {
int8_t tmp;
fread(&tmp, sizeof(int8_t), 1, m_buffer);
seek(-sizeof(int8_t), SEEK_CUR);
return tmp;
}
uint8_t seekUInt8() {
uint8_t tmp;
fread(&tmp, sizeof(uint8_t), 1, m_buffer);
seek(-sizeof(uint8_t), SEEK_CUR);
return tmp;
}
// int16
int16_t seekInt16() {
int16_t tmp;
fread(&tmp, sizeof(int16_t), 1, m_buffer);
seek(-sizeof(int16_t), SEEK_CUR);
return tmp;
}
uint16_t seekUInt16() {
uint16_t tmp;
fread(&tmp, sizeof(uint16_t), 1, m_buffer);
seek(-sizeof(uint16_t), SEEK_CUR);
return tmp;
}
int16_t seekInt16LE() {
return seekInt16();
}
uint16_t seekUInt16LE() {
return seekUInt16();
}
// int32
int32_t seekInt32() {
int32_t tmp;
fread(&tmp, sizeof(int32_t), 1, m_buffer);
seek(-sizeof(int32_t), SEEK_CUR);
return tmp;
}
uint32_t seekUInt32() {
uint32_t tmp;
fread(&tmp, sizeof(uint32_t), 1, m_buffer);
seek(-sizeof(uint32_t), SEEK_CUR);
return tmp;
}
int32_t seekInt32LE() {
return seekInt32();
}
uint32_t seekUInt32LE() {
return seekUInt32();
}
// skip
// int8
void skipInt8() {
seek(sizeof(int8_t), SEEK_CUR);
}
void skipUInt8() {
seek(sizeof(uint8_t), SEEK_CUR);
}
// int16
void skipInt16() {
seek(sizeof(int16_t), SEEK_CUR);
}
void skipUInt16() {
seek(sizeof(uint16_t), SEEK_CUR);
}
void skipInt16LE() {
seek(sizeof(int16_t), SEEK_CUR);
}
void skipUInt16LE() {
seek(sizeof(uint16_t), SEEK_CUR);
}
// int32
void skipInt32() {
seek(sizeof(int32_t), SEEK_CUR);
}
void skipUInt32() {
seek(sizeof(uint32_t), SEEK_CUR);
}
void skipInt32LE() {
seek(sizeof(int32_t), SEEK_CUR);
}
void skipUInt32LE() {
seek(sizeof(uint32_t), SEEK_CUR);
}
};
# Copyright (c) PROPHESSOR 2019-2020
class ByteTools():
def __init__(self, stream):
self.stream = stream
self.order = 'little'
def byteorder(self, order=None):
if not order:
return self.order
self.order = order
def parseASCIIString(self, length):
return self.stream.read(length).decode('ascii')
def parseUnicodeString(self, length):
return self.stream.read(length).decode('utf-8')
def parseString(self, length): return self.parseUnicodeString(length)
def getBytes(self, x):
bytes = self.stream.read(x)
if not bytes: raise IOError("Unexpected end of file")
return bytes
def parseUInt(self, x):
return int.from_bytes(self.getBytes(x), byteorder=self.order, signed=False)
def parseUInt8(self): return self.parseUInt(1)
def parseUInt16(self): return self.parseUInt(2)
def parseUInt32(self): return self.parseUInt(4)
def parseInt(self, x):
return int.from_bytes(self.getBytes(x), byteorder=self.order, signed=True)
def parseInt8(self): return self.parseInt(1)
def parseInt16(self): return self.parseInt(2)
def parseInt32(self): return self.parseInt(4)
def parseInt64(self): return self.parseInt(8)
/**
* ByteTools.js: Browser
* Copyright (c) 2020 PROPHESSOR
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
export default class ByteTools {
/**
*
* @param {DataView} view - DataView (new DataView(ArrayBuffer))
*/
constructor(view) {
this.buffer = view;
this.offset = 0;
}
// basics
tell() {
return this.offset;
}
/**
*
* @param {number} offset
* @param {'START'|'CUR'} mode
*/
seek(offset, mode = 'START') {
switch (mode) {
case 'CUR':
return this.offset += offset;
case 'START':
default:
return this.offset = offset;
}
}
// read
readString(length = Infinity) {
let string = '';
for (let i = 0; i < length; i++) {
const char = this.readUInt8();
if (length === Infinity && char === 0) return string;
string += String.fromCharCode(char);
}
return string;
}
// int8
readInt8() {
const tmp = this.buffer.getInt8(this.offset);
this.offset += 1;
return tmp;
}
readUInt8() {
const tmp = this.buffer.getUint8(this.offset);
this.offset += 1;
return tmp;
}
// int16
readInt16() {
const tmp = this.buffer.getInt16(this.offset, true);
this.offset += 2;
return tmp;
}
readUInt16() {
const tmp = this.buffer.getUint16(this.offset, true);
this.offset += 2;
return tmp;
}
// int32
readInt32() {
const tmp = this.buffer.getInt32(this.offset, true);
this.offset += 4;
return tmp;
}
readUInt32() {
const tmp = this.buffer.getUint32(this.offset, true);
this.offset += 4;
return tmp;
}
// seek
// int8
seekInt8() {
return this.buffer.getInt8(this.offset);
}
seekUInt8() {
return this.buffer.getUint8(this.offset);
}
// int16
seekInt16() {
return this.buffer.getInt16(this.offset, true);
}
seekUInt16() {
return this.buffer.getUint16(this.offset, true);
}
// int32
seekInt32() {
return this.buffer.getInt32(this.offset, true);
}
seekUInt32() {
return this.buffer.getUint32(this.offset, true);
}
// skip
// int8
skipInt8() {
return this.offset += 1;
}
// int16
skipInt16() {
return this.offset += 2;
}
// int32
skipInt32() {
return this.offset += 4;
}
}
/**
* Copyright (c) 2018 DRRP-Team (Created by PROPHESSOR)
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
"use strict";
const fs = require("fs");
function demoUsage() { // This function just for demonstration. You can remove it.
const _fs = require("fs");
const file = new ByteTools(_fs.readFileSync("filename.ext"));
console.log(file.readUInt8());
console.log(file.readUInt8());
console.log(file.readInt16LE());
console.log(file.tell());
console.log(file.seekInt8());
console.log(file.seek(32, "CUR"));
console.log(file.tell());
}
module.exports = class ByteTools {
constructor(buffer = Buffer.from(), offset = 0) {
this.buffer = buffer;
this.offset = offset;
}
// basics
tell() {
return this.offset;
}
seek(offset, mode = "START") {
switch (mode) {
case "CUR":
return this.offset += offset;
case "START":
default:
return this.offset = offset;
}
}
// read
// int8
readInt8() {
const tmp = this.buffer.readInt8(this.offset);
this.offset += 1;
return tmp;
}
readUInt8() {
const tmp = this.buffer.readUInt8(this.offset);
this.offset += 1;
return tmp;
}
// int16
readInt16LE() {
const tmp = this.buffer.readInt16LE(this.offset);
this.offset += 2;
return tmp;
}
readUInt16LE() {
const tmp = this.buffer.readUInt16LE(this.offset);
this.offset += 2;
return tmp;
}
readInt16BE() {
const tmp = this.buffer.readInt16BE(this.offset);
this.offset += 2;
return tmp;
}
readUInt16BE() {
const tmp = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return tmp;
}
// int32
readInt32LE() {
const tmp = this.buffer.readInt32LE(this.offset);
this.offset += 4;
return tmp;
}
readUInt32LE() {
const tmp = this.buffer.readUInt32LE(this.offset);
this.offset += 4;
return tmp;
}
readInt32BE() {
const tmp = this.buffer.readInt32BE(this.offset);
this.offset += 4;
return tmp;
}
readUInt32BE() {
const tmp = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return tmp;
}
readCharString(length=-1) {
let string = '';
if (length < 0) {
while (true) {
const code = this.readUInt8();
if (code === 0) break;
string += String.fromCharCode(code);
}
} else {
for (let i = 0; i < length; i++) {
string += String.fromCharCode(this.readUInt8());
}
}
return string;
}
// seek
// int8
seekInt8() {
return this.buffer.readInt8(this.offset);
}
seekUInt8() {
return this.buffer.readUInt8(this.offset);
}
// int16
seekInt16LE() {
return this.buffer.readInt16LE(this.offset);
}
seekUInt16LE() {
return this.buffer.readUInt16LE(this.offset);
}
seekInt16BE() {
return this.buffer.readInt16BE(this.offset);
}
seekUInt16BE() {
return this.buffer.readUInt16BE(this.offset);
}
// int32
seekInt32LE() {
return this.buffer.readInt32LE(this.offset);
}
seekUInt32LE() {
return this.buffer.readUInt32LE(this.offset);
}
seekInt32BE() {
return this.buffer.readInt32BE(this.offset);
}
seekUInt32BE() {
return this.buffer.readUInt32BE(this.offset);
}
// skip
// int8
skipInt8() {
return this.offset += 1;
}
skipUInt8() {
return this.offset += 1;
}
// int16
skipInt16LE() {
return this.offset += 2;
}
skipUInt16LE() {
return this.offset += 2;
}
skipInt16BE() {
return this.offset += 2;
}
skipUInt16BE() {
return this.offset += 2;
}
// int32
skipInt32LE() {
return this.offset += 4;
}
skipUInt32LE() {
return this.offset += 4;
}
skipInt32BE() {
return this.offset += 4;
}
skipUInt32BE() {
return this.offset += 4;
}
};
/**
* Copyright (c) PROPHESSOR 2021
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import fs from 'fs';
function demoUsage() { // This function just for demonstration. You can remove it.
const file = new ByteTools(fs.readFileSync("filename.ext"));
console.log(file.readUInt8());
console.log(file.readUInt8());
console.log(file.readInt16LE());
console.log(file.tell());
console.log(file.seekInt8());
console.log(file.seek(32, "CUR"));
console.log(file.tell());
}
type TSeekMode = 'START' | 'CUR';
export default class ByteTools {
buffer: Buffer;
offset: number;
constructor(buffer = Buffer.from([]), offset = 0) {
this.buffer = buffer;
this.offset = offset;
}
// basics
tell() {
return this.offset;
}
seek(offset: number, mode: TSeekMode = "START") {
switch (mode) {
case "CUR":
return this.offset += offset;
case "START":
default:
return this.offset = offset;
}
}
// read
// int8
readInt8() {
const tmp = this.buffer.readInt8(this.offset);
this.offset += 1;
return tmp;
}
readUInt8() {
const tmp = this.buffer.readUInt8(this.offset);
this.offset += 1;
return tmp;
}
// int16
readInt16LE() {
const tmp = this.buffer.readInt16LE(this.offset);
this.offset += 2;
return tmp;
}
readUInt16LE() {
const tmp = this.buffer.readUInt16LE(this.offset);
this.offset += 2;
return tmp;
}
readInt16BE() {
const tmp = this.buffer.readInt16BE(this.offset);
this.offset += 2;
return tmp;
}
readUInt16BE() {
const tmp = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return tmp;
}
// int32
readInt32LE() {
const tmp = this.buffer.readInt32LE(this.offset);
this.offset += 4;
return tmp;
}
readUInt32LE() {
const tmp = this.buffer.readUInt32LE(this.offset);
this.offset += 4;
return tmp;
}
readInt32BE() {
const tmp = this.buffer.readInt32BE(this.offset);
this.offset += 4;
return tmp;
}
readUInt32BE() {
const tmp = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return tmp;
}
readCharString(length = -1) {
let string = '';
if (length < 0) {
while (true) {
const code = this.readUInt8();
if (code === 0) break;
string += String.fromCharCode(code);
}
} else {
for (let i = 0; i < length; i++) {
string += String.fromCharCode(this.readUInt8());
}
}
return string;
}
// seek
// int8
seekInt8() {
return this.buffer.readInt8(this.offset);
}
seekUInt8() {
return this.buffer.readUInt8(this.offset);
}
// int16
seekInt16LE() {
return this.buffer.readInt16LE(this.offset);
}
seekUInt16LE() {
return this.buffer.readUInt16LE(this.offset);
}
seekInt16BE() {
return this.buffer.readInt16BE(this.offset);
}
seekUInt16BE() {
return this.buffer.readUInt16BE(this.offset);
}
// int32
seekInt32LE() {
return this.buffer.readInt32LE(this.offset);
}
seekUInt32LE() {
return this.buffer.readUInt32LE(this.offset);
}
seekInt32BE() {
return this.buffer.readInt32BE(this.offset);
}
seekUInt32BE() {
return this.buffer.readUInt32BE(this.offset);
}
// skip
// int8
skipInt8() {
return this.offset += 1;
}
skipUInt8() {
return this.offset += 1;
}
// int16
skipInt16LE() {
return this.offset += 2;
}
skipUInt16LE() {
return this.offset += 2;
}
skipInt16BE() {
return this.offset += 2;
}
skipUInt16BE() {
return this.offset += 2;
}
// int32
skipInt32LE() {
return this.offset += 4;
}
skipUInt32LE() {
return this.offset += 4;
}
skipInt32BE() {
return this.offset += 4;
}
skipUInt32BE() {
return this.offset += 4;
}
};
@PROPHESSOR
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment