aboutsummaryrefslogtreecommitdiff
path: root/src/shared/checksum.js
blob: 7e7e40fd327a8c42fb2c101abe3fa9ae0d425f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { readSync } from "node:fs";
import { open } from "node:fs/promises";
import { localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, pathDisplayName, shellEscapeLsName } from "./common.js";
import { UsageError, stdout } from "./diagnostics.js";

export const BASE_ENCODING_LONG_OPTIONS = ["decode", "ignore-garbage", "wrap", "help", "version"];

export function baseEncodingMetaOption(args) {
  return baseLikeMetaOption(args, BASE_ENCODING_LONG_OPTIONS, normalizeBaseEncodingLongOption);
}

export function baseLikeMetaOption(args, longOptions, normalizeLongOption) {
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") return null;
    if (arg.startsWith("--")) {
      const option = normalizeLongOption(arg);
      const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
      if (!longOptions.includes(name)) return null;
      if ((option === "--help" || option === "--version") && inlineValue == null) return option;
      if (name === "wrap") {
        const value = inlineValue ?? args[i + 1];
        if (value !== undefined) parseWrap(value);
        if (inlineValue == null) i++;
      }
      else if (inlineValue != null && name !== "wrap") return null;
      continue;
    }
    if (!arg.startsWith("-") || arg === "-") continue;
    for (let j = 1; j < arg.length; j++) {
      const ch = arg[j];
      if (!["d", "i", "w"].includes(ch)) return null;
      if (ch === "w") {
        const inlineValue = arg.slice(j + 1);
        const value = inlineValue === "" ? args[i + 1] : inlineValue;
        if (value !== undefined) parseWrap(value);
        if (inlineValue === "") i++;
        break;
      }
    }
  }
  return null;
}

export function baseEncodingDiagnosticName(file) {
  return shellEscapeLsName(pathDisplayName(file));
}

export function ensureSingleInputOperand(operands) {
  if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true);
}

export function parseWrap(value) {
  const text = String(value);
  if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid wrap size: ${localeQuotedEscapedDiagnostic(text)}`);
  const parsed = BigInt(text);
  if (parsed > 9223372036854775807n) return 0;
  const wrap = parsed > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(parsed);
  return wrap;
}

export function formatEncodedOutput(encoded, wrap) {
  if (encoded === "") return "";
  if (wrap === 0) return encoded;
  if (wrap >= encoded.length) return `${encoded}\n`;
  return `${encoded.match(new RegExp(`.{1,${wrap}}`, "g"))?.join("\n") ?? ""}\n`;
}

export function normalizeBaseEncodingLongOptions(args) {
  return normalizeLongOptionsByPrefix(args, BASE_ENCODING_LONG_OPTIONS);
}

export function normalizeBaseEncodingLongOption(arg) {
  return normalizeLongOptionByPrefix(arg, BASE_ENCODING_LONG_OPTIONS);
}

export function decodeBase64(text, ignoreGarbage = false, url = false) {
  let cleaned = text.replace(/\s+/g, "");
  const alphabet = url ? "A-Za-z0-9_\\-=" : "A-Za-z0-9+/=";
  if (ignoreGarbage) cleaned = cleaned.replace(new RegExp(`[^${alphabet}]`, "g"), "");
  else {
    const invalid = cleaned.search(new RegExp(`[^${alphabet}]`));
    if (invalid !== -1) {
      const prefix = decodeBase64Clean(cleaned.slice(0, invalid), url).bytes;
      if (prefix.length) stdout(prefix);
      throw new UsageError("invalid input");
    }
  }
  const decoded = decodeBase64Clean(cleaned, url);
  if (!decoded.valid) {
    if (decoded.bytes.length) stdout(decoded.bytes);
    throw new UsageError("invalid input");
  }
  return decoded.bytes;
}

export function base64Alphabet(text, url = false) {
  return url ? text.replaceAll("-", "+").replaceAll("_", "/") : text;
}

export function decodeBase64Clean(text, url = false) {
  const chunks = [];
  let valid = true;
  let i = 0;
  while (i < text.length) {
    let start = i;
    while (i < text.length && text[i] !== "=") i++;
    let base = text.slice(start, i);
    let pads = "";
    while (i < text.length && text[i] === "=") pads += text[i++];
    if (base.length === 0 && pads.length > 0) {
      valid = false;
      break;
    }
    if (base.length === 0) continue;
    const segment = decodeBase64Segment(base, pads, url);
    if (segment.bytes.length) chunks.push(segment.bytes);
    if (!segment.valid) {
      valid = false;
      break;
    }
  }
  return { bytes: Buffer.concat(chunks), valid };
}

export function decodeBase64Segment(base, pads, url = false) {
  const needPads = (4 - (base.length % 4)) % 4;
  const bytes = base.length % 4 === 1
    ? Buffer.from(base64Alphabet(base.slice(0, base.length - 1), url), "base64")
    : Buffer.from(base64Alphabet(base + "=".repeat(needPads), url), "base64");
  let valid = base.length % 4 !== 1;
  if (pads.length) {
    valid &&= pads.length <= 2;
    valid &&= (base.length + pads.length) % 4 === 0;
    if (pads.length === 1) valid &&= base.length % 4 === 3 && (base64DigitValue(base.at(-1), url) & 0b11) === 0;
    else if (pads.length === 2) valid &&= base.length % 4 === 2 && (base64DigitValue(base.at(-1), url) & 0b1111) === 0;
    else valid = false;
  } else if (base.length % 4 === 2) {
    valid &&= (base64DigitValue(base.at(-1), url) & 0b1111) === 0;
  } else if (base.length % 4 === 3) {
    valid &&= (base64DigitValue(base.at(-1), url) & 0b11) === 0;
  }
  return { bytes, valid };
}

export function base64DigitValue(ch, url = false) {
  const alphabet = url ? "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  return alphabet.indexOf(ch);
}

export async function forEachInputChunk(file, callback) {
  const buffer = Buffer.allocUnsafe(256 * 1024);
  if (file === "-") {
    while (true) {
      const n = readSync(0, buffer, 0, buffer.length, null);
      if (n === 0) break;
      callback(buffer.subarray(0, n));
    }
    return;
  }
  const handle = await open(file, "r");
  try {
    while (true) {
      const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
      if (bytesRead === 0) break;
      callback(buffer.subarray(0, bytesRead));
    }
  } finally {
    await handle.close();
  }
}

export async function sumForFile(file, sysv = false) {
  let length = 0;
  let sum = 0;
  await forEachInputChunk(file, (chunk) => {
    length += chunk.length;
    for (const byte of chunk) {
      if (sysv) sum = (sum + byte) % 0x100000000;
      else sum = ((sum >> 1) + ((sum & 1) << 15) + byte) & 0xffff;
    }
  });
  if (sysv) {
    sum = (sum & 0xffff) + Math.floor(sum / 0x10000);
    sum = (sum & 0xffff) + Math.floor(sum / 0x10000);
  }
  return { sum, blocks: Math.ceil(length / (sysv ? 512 : 1024)) };
}

export function base32Encode(data) {
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  let bits = 0;
  let value = 0;
  let out = "";
  for (const byte of data) {
    value = (value << 8) | byte;
    bits += 8;
    while (bits >= 5) {
      out += alphabet[(value >>> (bits - 5)) & 31];
      bits -= 5;
    }
  }
  if (bits > 0) out += alphabet[(value << (5 - bits)) & 31];
  while (out.length % 8) out += "=";
  return out;
}

export function base32Decode(text) {
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  const decoded = decodeBase32Clean(text, alphabet);
  if (!decoded.valid) {
    if (decoded.bytes.length) stdout(decoded.bytes);
    throw new UsageError("invalid input");
  }
  return decoded.bytes;
}

export function decodeBase32Clean(text, alphabet) {
  const chunks = [];
  let valid = true;
  let i = 0;
  while (i < text.length) {
    let start = i;
    while (i < text.length && text[i] !== "=") i++;
    const base = text.slice(start, i);
    let pads = "";
    while (i < text.length && text[i] === "=") pads += text[i++];
    if (base.length === 0 && pads.length > 0) {
      valid = false;
      break;
    }
    if (base.length === 0) continue;
    const segment = decodeBase32Segment(base, pads, alphabet);
    if (segment.bytes.length) chunks.push(segment.bytes);
    if (!segment.valid) {
      valid = false;
      break;
    }
  }
  return { bytes: Buffer.concat(chunks), valid };
}

export function decodeBase32Segment(base, pads, alphabet) {
  let bits = 0;
  let value = 0;
  const out = [];
  for (const ch of base) {
    const idx = alphabet.indexOf(ch);
    if (idx === -1) {
      return { bytes: Uint8Array.from(out), valid: false };
    }
    value = (value << 5) | idx;
    bits += 5;
    if (bits >= 8) {
      out.push((value >>> (bits - 8)) & 255);
      bits -= 8;
    }
  }
  const padByRemainder = new Map([[0, 0], [2, 6], [4, 4], [5, 3], [7, 1]]);
  const expectedPads = padByRemainder.get(base.length % 8);
  let valid = expectedPads !== undefined;
  if (pads.length) valid &&= pads.length === expectedPads && (base.length + pads.length) % 8 === 0;
  valid &&= bits === 0 || (value & ((1 << bits) - 1)) === 0;
  return { bytes: Uint8Array.from(out), valid };
}