Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit a3cef92d authored by Simon Chan's avatar Simon Chan
Browse files

fix(struct): parse int32 and uint32

parent 076d67e2
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -181,7 +181,8 @@
            "ignoreMissingScript": true,
            "allowWarningsInSuccessfulBuild": true,
            "enableParallelism": true,
            "incremental": true
            "incremental": true,
            "safeForSimultaneousRushProcesses": true
        },
        {
            "commandKind": "bulk",
+51 −41
Original line number Diff line number Diff line
// Prepare maps for O(1) searching
const charToIndex: Record<string, number> = {};
// Array is faster than object literal or `Map`
const charToIndex: number[] = [];
const indexToChar: number[] = [];
const paddingChar = '='.charCodeAt(0);
const paddingChar = "=".charCodeAt(0);

function addRange(start: string, end: string) {
    const charCodeStart = start.charCodeAt(0);
    const charCodeEnd = end.charCodeAt(0);

    for (let charCode = charCodeStart; charCode <= charCodeEnd; charCode += 1) {
        charToIndex[String.fromCharCode(charCode)] = indexToChar.length;
        charToIndex[charCode] = indexToChar.length;
        indexToChar.push(charCode);
    }
}

addRange('A', 'Z');
addRange('a', 'z');
addRange('0', '9');
addRange('+', '+');
addRange('/', '/');
addRange("A", "Z");
addRange("a", "z");
addRange("0", "9");
addRange("+", "+");
addRange("/", "/");

/**
 * Calculate the required length of the output buffer for the given input length.
@@ -25,10 +25,12 @@ addRange('/', '/');
 * @param inputLength Length of the input in bytes
 * @returns Length of the output in bytes
 */
export function calculateBase64EncodedLength(inputLength: number): [outputLength: number, paddingLength: number] {
export function calculateBase64EncodedLength(
    inputLength: number
): [outputLength: number, paddingLength: number] {
    const remainder = inputLength % 3;
    const paddingLength = remainder !== 0 ? 3 - remainder : 0;
    return [(inputLength + paddingLength) / 3 * 4, paddingLength];
    return [((inputLength + paddingLength) / 3) * 4, paddingLength];
}

/**
@@ -37,9 +39,7 @@ export function calculateBase64EncodedLength(inputLength: number): [outputLength
 * @param input The input buffer
 * @returns The encoded output buffer
 */
export function encodeBase64(
    input: Uint8Array,
): Uint8Array;
export function encodeBase64(input: Uint8Array): Uint8Array;
/**
 * Encode the given input into base64 and write it to the output buffer.
 *
@@ -51,15 +51,14 @@ export function encodeBase64(
 * @param output The output buffer
 * @returns The number of bytes written to the output buffer
 */
export function encodeBase64(input: Uint8Array, output: Uint8Array): number;
export function encodeBase64(
    input: Uint8Array,
    output: Uint8Array,
): number;
export function encodeBase64(
    input: Uint8Array,
    output?: Uint8Array,
    output?: Uint8Array
): Uint8Array | number {
    const [outputLength, paddingLength] = calculateBase64EncodedLength(input.length);
    const [outputLength, paddingLength] = calculateBase64EncodedLength(
        input.length
    );

    if (!output) {
        output = new Uint8Array(outputLength);
@@ -67,7 +66,7 @@ export function encodeBase64(
        return output;
    } else {
        if (output.length < outputLength) {
            throw new Error('output buffer is too small');
            throw new Error("output buffer is too small");
        }

        output = output.subarray(0, outputLength);
@@ -76,7 +75,10 @@ export function encodeBase64(
        // we check if it's possible to encode in-place.
        if (input.buffer !== output.buffer) {
            encodeForward(input, output, paddingLength);
        } else if (output.byteOffset + output.length - (paddingLength + 1) <= input.byteOffset + input.length) {
        } else if (
            output.byteOffset + output.length - (paddingLength + 1) <=
            input.byteOffset + input.length
        ) {
            // Output ends before input ends
            // So output won't catch up with input.

@@ -111,14 +113,18 @@ export function encodeBase64(
        } else {
            // Input is in the middle of output,
            // not possible to read neither first or last three bytes,
            throw new Error('input and output cannot overlap');
            throw new Error("input and output cannot overlap");
        }

        return outputLength;
    }
}

function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
function encodeForward(
    input: Uint8Array,
    output: Uint8Array,
    paddingLength: number
) {
    let inputIndex = 0;
    let outputIndex = 0;

@@ -160,7 +166,7 @@ function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: num
        output[outputIndex] = indexToChar[x >> 2]!;
        outputIndex += 1;

        output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
        output[outputIndex] = indexToChar[(x & 0b11) << 4]!;
        outputIndex += 1;

        output[outputIndex] = paddingChar;
@@ -184,14 +190,18 @@ function encodeForward(input: Uint8Array, output: Uint8Array, paddingLength: num
        output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
        outputIndex += 1;

        output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
        output[outputIndex] = indexToChar[(y & 0b1111) << 2]!;
        outputIndex += 1;

        output[outputIndex] = paddingChar;
    }
}

function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: number) {
function encodeBackward(
    input: Uint8Array,
    output: Uint8Array,
    paddingLength: number
) {
    let inputIndex = input.length - 1;
    let outputIndex = output.length - 1;

@@ -207,7 +217,7 @@ function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: nu
        output[outputIndex] = paddingChar;
        outputIndex -= 1;

        output[outputIndex] = indexToChar[((x & 0b11) << 4)]!;
        output[outputIndex] = indexToChar[(x & 0b11) << 4]!;
        outputIndex -= 1;

        output[outputIndex] = indexToChar[x >> 2]!;
@@ -226,7 +236,7 @@ function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: nu
        output[outputIndex] = paddingChar;
        outputIndex -= 1;

        output[outputIndex] = indexToChar[((y & 0b1111) << 2)]!;
        output[outputIndex] = indexToChar[(y & 0b1111) << 2]!;
        outputIndex -= 1;

        output[outputIndex] = indexToChar[((x & 0b11) << 4) | (y >> 4)]!;
@@ -268,29 +278,29 @@ function encodeBackward(input: Uint8Array, output: Uint8Array, paddingLength: nu

export function decodeBase64(input: string): Uint8Array {
    let padding: number;
    if (input[input.length - 2] === '=') {
    if (input[input.length - 2] === "=") {
        padding = 2;
    } else if (input[input.length - 1] === '=') {
    } else if (input[input.length - 1] === "=") {
        padding = 1;
    } else {
        padding = 0;
    }

    const result = new Uint8Array(input.length / 4 * 3 - padding);
    const result = new Uint8Array((input.length / 4) * 3 - padding);
    let sIndex = 0;
    let dIndex = 0;

    while (sIndex < input.length - (padding !== 0 ? 4 : 0)) {
        const a = charToIndex[input[sIndex]!]!;
        const a = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const b = charToIndex[input[sIndex]!]!;
        const b = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const c = charToIndex[input[sIndex]!]!;
        const c = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const d = charToIndex[input[sIndex]!]!;
        const d = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
@@ -304,23 +314,23 @@ export function decodeBase64(input: string): Uint8Array {
    }

    if (padding === 1) {
        const a = charToIndex[input[sIndex]!]!;
        const a = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const b = charToIndex[input[sIndex]!]!;
        const b = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const c = charToIndex[input[sIndex]!]!;
        const c = charToIndex[input.charCodeAt(sIndex)]!;

        result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
        dIndex += 1;

        result[dIndex] = ((b & 0b1111) << 4) | ((c & 0b11_1100) >> 2);
    } else if (padding === 2) {
        const a = charToIndex[input[sIndex]!]!;
        const a = charToIndex[input.charCodeAt(sIndex)]!;
        sIndex += 1;

        const b = charToIndex[input[sIndex]!]!;
        const b = charToIndex[input.charCodeAt(sIndex)]!;

        result[dIndex] = (a << 2) | ((b & 0b11_0000) >> 4);
    }
+1 −2
Original line number Diff line number Diff line
{
    "extends": "./tsconfig.build.json",
    "compilerOptions": {
        "types": [
        ],
        "types": [],
    },
    "exclude": []
}
+5 −5
Original line number Diff line number Diff line
@@ -19,10 +19,10 @@ interface AbortControllerConstructor {
    new (): AbortController;
}

declare global {
    // eslint-disable-next-line no-var
    var AbortController: AbortControllerConstructor;
interface GlobalExtension {
    AbortController: AbortControllerConstructor;
}

export const AbortController: AbortControllerConstructor =
    globalThis.AbortController;
export const AbortController: AbortControllerConstructor = (
    globalThis as unknown as GlobalExtension
).AbortController;
+1 −2
Original line number Diff line number Diff line
{
    "extends": "./tsconfig.build.json",
    "compilerOptions": {
        "types": [
        ],
        "types": [],
    },
    "exclude": []
}
Loading