Read the source and its declared rule
The original contract permits canonical standard-alphabet base 64 with required padding. Q!UI= includes an unsupported symbol but the permissive Buffer path still yields AB. The correction checks grammar and re-encoding equality before accepting this bounded source.
| Record | Input JSON |
|---|---|
| Base input | {"encoded":"Q!UI="} |
Compare the actual paths
Good output holds noncanonical Q!UI=. Decoded bytes alone cannot show that the source representation satisfied its agreed transfer contract.
| Path | Output JSON |
|---|---|
| Misleading path | {"accepted":true,"bytes":[65,66]} |
| Corrected path | {"accepted":false,"error":"noncanonical-base64","original":"Q!UI="} |
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
const bytes=Buffer.from(i.encoded,'base64');
if(corrected&&(!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(i.encoded)||bytes.toString('base64')!==i.encoded))return {accepted:false,error:'noncanonical-base64',original:i.encoded};
return {accepted:true,bytes:[...bytes]};
};
const cases=[{"label":"misleading base","input":{"encoded":"Q!UI="},"expected":{"accepted":true,"bytes":[65,66]},"corrected":false},{"label":"corrected base","input":{"encoded":"Q!UI="},"expected":{"accepted":false,"error":"noncanonical-base64","original":"Q!UI="},"corrected":true},{"label":"canonical two-byte encoding","input":{"encoded":"QUI="},"expected":{"accepted":true,"bytes":[65,66]},"corrected":true},{"label":"canonical empty bytes","input":{"encoded":""},"expected":{"accepted":true,"bytes":[]},"corrected":true}];
for(const test of cases){
const actual=run(test.input,test.corrected);
assert.deepEqual(actual,test.expected,test.label);
console.log(test.label,actual);
}
Check boundaries before the next load
QUI= yields [65,66]. The canonical empty encoding yields an empty byte array under this optional-payload rule.
| Case | Input JSON | Expected output JSON |
|---|---|---|
| Canonical two-byte encoding | {"encoded":"QUI="} | {"accepted":true,"bytes":[65,66]} |
| Canonical empty encoding | {"encoded":""} | {"accepted":true,"bytes":[]} |
Does producing AB make Q!UI= acceptable?
Show the worked answer
No. It violates the declared grammar. Hold the malformed value; canonical QUI= is accepted.