208 lines
5.7 KiB
Plaintext
208 lines
5.7 KiB
Plaintext
/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */
|
|
/** Shared helpers for drive-resident /bin scripts (prepended before each command). */
|
|
function bareStdin(ctx) {
|
|
return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : ''
|
|
}
|
|
|
|
/** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */
|
|
function bareFormatModeString(mode, type) {
|
|
const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-'
|
|
const perm = mode & 0o777
|
|
const r = (bit) => (perm & bit ? 'r' : '-')
|
|
const w = (bit) => (perm & bit ? 'w' : '-')
|
|
const x = (bit) => (perm & bit ? 'x' : '-')
|
|
return (
|
|
typeChar +
|
|
r(0o400) +
|
|
w(0o200) +
|
|
x(0o100) +
|
|
r(0o040) +
|
|
w(0o020) +
|
|
x(0o010) +
|
|
r(0o004) +
|
|
w(0o002) +
|
|
x(0o001)
|
|
)
|
|
}
|
|
|
|
/** @param {number} mtimeMs @param {number} [nowMs] */
|
|
function bareFormatLsMtime(mtimeMs, nowMs) {
|
|
const now = nowMs != null ? nowMs : Date.now()
|
|
const d = new Date(mtimeMs)
|
|
const months = [
|
|
'Jan',
|
|
'Feb',
|
|
'Mar',
|
|
'Apr',
|
|
'May',
|
|
'Jun',
|
|
'Jul',
|
|
'Aug',
|
|
'Sep',
|
|
'Oct',
|
|
'Nov',
|
|
'Dec'
|
|
]
|
|
const mon = months[d.getMonth()]
|
|
const day = String(d.getDate()).padStart(2, ' ')
|
|
const sixMo = 180 * 24 * 3600 * 1000
|
|
if (Math.abs(now - mtimeMs) > sixMo) {
|
|
const yr = String(d.getFullYear()).padStart(4, ' ')
|
|
return mon + ' ' + day + ' ' + yr
|
|
}
|
|
const hh = String(d.getHours()).padStart(2, '0')
|
|
const mm = String(d.getMinutes()).padStart(2, '0')
|
|
return mon + ' ' + day + ' ' + hh + ':' + mm
|
|
}
|
|
|
|
/** @param {number} size */
|
|
function barePosixBlocks(size) {
|
|
return Math.ceil(Number(size) / 512) || 0
|
|
}
|
|
|
|
/**
|
|
* Raw stdout for NUL/binary when **`process.stdout.write`** is missing.
|
|
* If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it.
|
|
* @param {Record<string, unknown>} ctx
|
|
* @param {string | Uint8Array} chunk
|
|
* @returns {boolean}
|
|
*/
|
|
function bareOsEmitRaw(ctx, chunk) {
|
|
if (typeof ctx.bareOsBinWrite === 'function') {
|
|
const b4 = ctx.b4a
|
|
const u8 =
|
|
typeof chunk === 'string'
|
|
? b4 && typeof b4.from === 'function'
|
|
? b4.from(chunk)
|
|
: new TextEncoder().encode(chunk)
|
|
: chunk
|
|
ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8))
|
|
return true
|
|
}
|
|
const w = globalThis.process?.stdout?.write
|
|
if (typeof w === 'function') {
|
|
w.call(globalThis.process.stdout, chunk)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
/** RFC 1321 MD5 — no Web Crypto; used by md5sum. */
|
|
function bareMd5DigestBytes(u8) {
|
|
const n = u8.length
|
|
const bitLen = (BigInt(n) * 8n) & 0xffffffffffffffffn
|
|
const padLen = (56 - ((n + 1) % 64) + 64) % 64
|
|
const total = n + 1 + padLen + 8
|
|
const buf = new Uint8Array(total)
|
|
buf.set(u8)
|
|
buf[n] = 0x80
|
|
const view = new DataView(buf.buffer)
|
|
view.setUint32(total - 8, Number(bitLen & 0xffffffffn), true)
|
|
view.setUint32(total - 4, Number((bitLen >> 32n) & 0xffffffffn), true)
|
|
|
|
let a0 = 0x67452301
|
|
let b0 = 0xefcdab89
|
|
let c0 = 0x98badcfe
|
|
let d0 = 0x10325476
|
|
const s = [
|
|
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9,
|
|
14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
|
4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
|
|
]
|
|
const K = new Uint32Array(64)
|
|
for (let i = 0; i < 64; i++) K[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 0x100000000) >>> 0
|
|
|
|
const leftRotate = (x, c) => ((x << c) | (x >>> (32 - c))) >>> 0
|
|
for (let off = 0; off < total; off += 64) {
|
|
const M = new Uint32Array(16)
|
|
for (let i = 0; i < 16; i++) {
|
|
M[i] = view.getUint32(off + i * 4, true)
|
|
}
|
|
let A = a0
|
|
let B = b0
|
|
let C = c0
|
|
let D = d0
|
|
for (let i = 0; i < 64; i++) {
|
|
let F, g
|
|
if (i < 16) {
|
|
F = (B & C) | (~B & D)
|
|
g = i
|
|
} else if (i < 32) {
|
|
F = (D & B) | (~D & C)
|
|
g = (5 * i + 1) % 16
|
|
} else if (i < 48) {
|
|
F = B ^ C ^ D
|
|
g = (3 * i + 5) % 16
|
|
} else {
|
|
F = C ^ (B | ~D)
|
|
g = (7 * i) % 16
|
|
}
|
|
F = (F + A + K[i] + M[g]) >>> 0
|
|
A = D
|
|
D = C
|
|
C = B
|
|
B = (B + leftRotate(F, s[i])) >>> 0
|
|
}
|
|
a0 = (a0 + A) >>> 0
|
|
b0 = (b0 + B) >>> 0
|
|
c0 = (c0 + C) >>> 0
|
|
d0 = (d0 + D) >>> 0
|
|
}
|
|
const out = new Uint8Array(16)
|
|
const dv = new DataView(out.buffer)
|
|
dv.setUint32(0, a0, true)
|
|
dv.setUint32(4, b0, true)
|
|
dv.setUint32(8, c0, true)
|
|
dv.setUint32(12, d0, true)
|
|
return [...out].map((b) => b.toString(16).padStart(2, '0')).join('')
|
|
}
|
|
|
|
async function run(ctx, argv) {
|
|
const paths = []
|
|
for (let i = 1; i < argv.length; i++) {
|
|
const a = argv[i]
|
|
if (a === '-h' || a === '--help') {
|
|
ctx.console.log(
|
|
'usage: md5sum [FILE]...\n' +
|
|
'With no FILE, or when FILE is -, read standard input. Uses bundled MD5 (no Web Crypto MD5).'
|
|
)
|
|
ctx.exitCode = 0
|
|
return
|
|
}
|
|
if (a.startsWith('-') && a !== '-') {
|
|
ctx.console.error('md5sum: unsupported option ' + a)
|
|
ctx.exitCode = 1
|
|
return
|
|
}
|
|
paths.push(a)
|
|
}
|
|
const b4 = ctx.b4a
|
|
function one(name, buf) {
|
|
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf)
|
|
try {
|
|
const hex = bareMd5DigestBytes(u8)
|
|
ctx.console.log(hex + ' ' + name)
|
|
} catch (e) {
|
|
ctx.console.error('md5sum: ' + (e.message || e))
|
|
ctx.exitCode = 1
|
|
}
|
|
}
|
|
if (!paths.length || (paths.length === 1 && paths[0] === '-')) {
|
|
one('-', b4.from(bareStdin(ctx)))
|
|
return
|
|
}
|
|
for (const p of paths) {
|
|
if (p === '-') {
|
|
one('-', b4.from(bareStdin(ctx)))
|
|
continue
|
|
}
|
|
const b = await ctx.vfs.readFile(p)
|
|
if (!b) {
|
|
ctx.console.error('md5sum: ' + p + ': No such file')
|
|
ctx.exitCode = 1
|
|
continue
|
|
}
|
|
one(p, b)
|
|
}
|
|
}
|