idz: Add utilities for binary I/O

This commit is contained in:
Tau 2020-03-11 22:22:56 -04:00 committed by Tau
parent 587f73518d
commit 6fdfd7e847

62
src/idz/util/bin.ts Normal file
View File

@ -0,0 +1,62 @@
import iconv from "iconv-lite";
const ASCII = "ascii";
const SHIFT_JIS = "shift_jis";
function readStr(buf: Buffer, begin: number, end: number, encoding: string) {
end = Math.min(buf.indexOf(0, begin), end);
return iconv.decode(buf.slice(begin, end), encoding);
}
export function readAsciiStr(buf: Buffer, begin: number, end: number) {
return readStr(buf, begin, end, ASCII);
}
export function readSjisStr(buf: Buffer, begin: number, end: number) {
return readStr(buf, begin, end, SHIFT_JIS);
}
// WARNING: This is sent little-endian.
export function writeIpAddr(buf: Buffer, str: string, pos: number) {
const bits = str.split(".");
for (let i = 0; i < 4; i++) {
buf.writeUInt8(parseInt(bits[3 - i]), pos + i);
}
}
function writeStr(
buf: Buffer,
begin: number,
end: number,
str: string,
encoding: string
) {
const tmp = iconv.encode(str + "\0", encoding);
if (tmp.length >= end - begin) {
throw new Error("String field overflow");
}
tmp.copy(buf, begin);
buf.writeUInt8(0, begin + tmp.length);
}
export function writeAsciiStr(
buf: Buffer,
begin: number,
end: number,
str: string
) {
writeStr(buf, begin, end, str, ASCII);
}
export function writeSjisStr(
buf: Buffer,
begin: number,
end: number,
str: string
) {
writeStr(buf, begin, end, str, SHIFT_JIS);
}