lstd/bytes.ts

122 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-01-07 02:58:12 +11:00
import {
decodeBase64,
decodeHex,
encodeBase64,
encodeHex,
2025-01-07 03:53:58 +11:00
} from "jsr:@std/encoding@^1.0.6";
2025-01-07 02:58:12 +11:00
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export type BinaryLike = string | Uint8Array;
export function from_utf8(s: BinaryLike): string {
return typeof s === "string" ? s : decoder.decode(s);
}
export function to_utf8(s: BinaryLike): Uint8Array {
return typeof s === "string" ? encoder.encode(s) : s;
}
export function encode_utf8(s: BinaryLike, buf: Uint8Array): number {
if (typeof s === "string") return encoder.encodeInto(s, buf).written;
else return buf.set(s), s.length;
}
export function from_hex(s: BinaryLike): Uint8Array {
return decodeHex(from_utf8(s));
}
export function to_hex(b: BinaryLike): string {
return encodeHex(to_utf8(b));
}
export function from_base64(s: BinaryLike): Uint8Array {
return decodeBase64(from_utf8(s));
}
export function to_base64(b: BinaryLike): string {
return encodeBase64(to_utf8(b));
}
2025-01-08 02:03:09 +11:00
export function read_u8(b: Uint8Array, i = 0) {
return b[i];
2025-01-08 01:46:54 +11:00
}
2025-01-08 02:03:09 +11:00
export function write_u8(b: Uint8Array, n: number, i = 0) {
if (n < 0 || 0xff < n) throw new TypeError(`input out of range, expected u8`);
b[i] = n;
2025-01-08 01:46:54 +11:00
}
2025-01-08 02:03:09 +11:00
export function read_i8(b: Uint8Array, i = 0) {
const n = b[i];
return n | ((n & 0x80) * 0x1fffffe);
2025-01-08 01:46:54 +11:00
}
2025-01-08 02:03:09 +11:00
export function write_i8(b: Uint8Array, n: number, i = 0) {
if (n < -0x80 || 0x7f < n)
throw new TypeError(`input out of range, expected i8`);
b[i] = n;
}
export function read_u16_be(b: Uint8Array, i = 0) {
return (b[i++] << 8) + b[i];
2025-01-08 01:46:54 +11:00
}
export function write_u16_be(b: Uint8Array, n: number, i = 0) {
if (n < 0 || 0xffff < n)
throw new TypeError(`input out of range, expected u16`);
2025-01-08 02:03:09 +11:00
(b[i++] = n >>> 8), (b[i] = n);
}
export function read_i16_be(b: Uint8Array, i = 0) {
const n = (b[i++] << 8) + b[i];
return n | ((n & 0x8000) * 0x1fffe);
2025-01-08 01:46:54 +11:00
}
export function write_i16_be(b: Uint8Array, n: number, i = 0) {
if (n < -0x8000 || 0x7fff < n)
throw new TypeError(`input out of range, expected i16`);
2025-01-08 02:03:09 +11:00
(b[i++] = n >>> 8), (b[i] = n);
}
export function read_u32_be(b: Uint8Array, i = 0) {
return ((b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i]) >>> 0;
2025-01-08 01:46:54 +11:00
}
export function write_u32_be(b: Uint8Array, n: number, i = 0) {
if (n < 0 || 0xffffffff < n)
throw new TypeError(`input out of range, expected u32`);
2025-01-08 02:03:09 +11:00
(b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i] = n);
}
export function read_i32_be(b: Uint8Array, i = 0) {
return (b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i];
2025-01-08 01:46:54 +11:00
}
export function write_i32_be(b: Uint8Array, n: number, i = 0) {
if (n < -0x80000000 || 0x7fffffff < n)
throw new TypeError(`input out of range, expected i32`);
2025-01-08 02:03:09 +11:00
(b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i] = n);
2025-01-08 01:46:54 +11:00
}
2025-01-07 03:53:58 +11:00
export { equals as buf_eq, concat as buf_concat } from "jsr:@std/bytes@^1.0.4";
2025-01-07 02:58:12 +11:00
export function buf_concat_fast(a: Uint8Array, b: Uint8Array) {
const m = a.length;
const c = new Uint8Array(m + b.length);
return c.set(a, 0), c.set(b, m), c;
}
export function buf_resize(buf: Uint8Array, n: number) {
const res = new Uint8Array(n);
return res.set(buf.subarray(0, n)), res;
}
export function buf_xor(a: Uint8Array, b: Uint8Array) {
const n = Math.max(a.length, b.length);
const c = new Uint8Array(n);
for (let i = 0; i < n; i++) c[i] = a[i] ^ b[i];
return c;
}