import { decodeAscii85, decodeBase32, decodeBase58, decodeBase64, decodeBase64Url, decodeHex, encodeAscii85, encodeBase32, encodeBase58, encodeBase64, encodeBase64Url, encodeHex, } from "jsr:@std/encoding@^1.0.6"; 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, offset = 0) { if (typeof s === "string") { if (offset !== 0) buf = buf.subarray(offset); return encoder.encodeInto(s, buf).written; } else { return buf.set(s, offset), 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_base32(s: BinaryLike): Uint8Array { return decodeBase32(from_utf8(s)); } export function to_base32(b: BinaryLike): string { return encodeBase32(to_utf8(b)); } export function from_base58(s: BinaryLike): Uint8Array { return decodeBase58(from_utf8(s)); } export function to_base58(b: BinaryLike): string { return encodeBase58(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)); } export function from_base64url(s: BinaryLike): Uint8Array { return decodeBase64Url(from_utf8(s)); } export function to_base64url(b: BinaryLike): string { return encodeBase64Url(to_utf8(b)); } export function from_ascii85(s: BinaryLike): Uint8Array { return decodeAscii85(from_utf8(s)); } export function to_ascii85(b: BinaryLike): string { return encodeAscii85(to_utf8(b)); } export function read_u8(b: Uint8Array, i = 0) { return b[i]; } 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; } export function read_i8(b: Uint8Array, i = 0) { const n = b[i]; return n | ((n & 0x80) * 0x1fffffe); } 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]; } 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`); (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); } 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`); (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; } 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`); (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]; } 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`); (b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i] = n); } export { equals as buf_eq, concat as buf_concat } from "jsr:@std/bytes@^1.0.4"; 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; }