Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZEUS-2397: use all uppercase characters in QRs #2398

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions utils/BbqrUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ export const QR_DATA_CAPACITY = {
export const ENCODING_NAMES = {
H: 'HEX',
Z: 'Zlib compressed',
'2': 'Base32'
'2': 'Base32',
B: 'BC-UR'
} as const;

export type FileType = keyof typeof FILETYPE_NAMES;
Expand Down Expand Up @@ -352,11 +353,19 @@ export function encodeData(raw: Uint8Array, encoding?: Encoding) {
raw = compressed;
}
}
if (encoding === 'B') {
return {
encoding,
encoded:
'BCUR-' + base32.encode(raw).replace(/=*$/, '').toUpperCase(),
splitMod: 8
};
}

return {
encoding,
// base32 without padding
encoded: base32.encode(raw).replace(/=*$/, ''),
encoded: base32.encode(raw).replace(/=*$/, '').toUpperCase(),
splitMod: 8
};
}
Expand Down Expand Up @@ -468,10 +477,12 @@ export function splitQRs(
n++, offset += perEach
) {
parts.push(
`B$${actualEncoding}${fileType}` +
(
`B$${actualEncoding}${fileType}` +
intToBase36(count) +
intToBase36(n) +
encoded.slice(offset, offset + perEach)
).toUpperCase()
);
}

Expand Down Expand Up @@ -506,20 +517,23 @@ export function decodeData(parts: string[], encoding: Encoding) {
// decode the parts back into a Uint8Array

if (encoding === 'H') {
return joinByteParts(parts.map((p) => hexToBytes(p)));
return joinByteParts(parts.map((p) => hexToBytes(p.toUpperCase())));
}

const bytes = joinByteParts(
parts.map((p) => {
const padding = (8 - (p.length % 8)) % 8;

return base32.decode(p + '='.repeat(padding));
return base32.decode(p.toUpperCase() + '='.repeat(padding));
})
);

if (encoding === 'Z') {
return pako.inflate(bytes, { windowBits: -10 });
}
if (encoding === 'B') {
return base32.decode(parts.join(''));
}

return bytes;
}
Expand Down
Loading