All files / src/Ice Base64.js

88.99% Statements 194/218
85.45% Branches 47/55
100% Functions 5/5
88.99% Lines 194/218

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 21941x 41x 41x 41x 41x 41x 41x 41x 20x 20x 1x 1x 19x 19x 19x 19x                     20x 41x 272x 272x 153x 153x 119x 272x 91x 91x 28x 272x 9x 9x 19x 268x     19x 19x 272x 41x 41x 41x 41x 7x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x     5x 5x 5x 5x 5x 5x 7x         5x 5x 5x 5x 7x 41x 41x 14x 14x 14x 14x 14x 269x 269x 269x 269x 269x 14x 14x     14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 65x 65x 68x 68x 68x 68x 68x 68x 68x 68x 68x 63x 63x 68x 68x 59x 59x 68x 14x 14x 14x 41x 41x 540x 300x 300x 240x 540x 183x 183x 57x 540x 18x 18x 39x 532x     39x 532x 10x 10x 29x 532x 28x 28x 1x 1x 540x 41x  
// Copyright (c) ZeroC, Inc.
 
import { Buffer } from "./Buffer.js";
 
const _codeA = "A".charCodeAt(0);
const _codea = "a".charCodeAt(0);
const _code0 = "0".charCodeAt(0);
 
function encodeChar(uc) {
    if (uc < 26) {
        return String.fromCharCode(_codeA + uc);
    }
 
    if (uc < 52) {
        return String.fromCharCode(_codea + (uc - 26));
    }

    if (uc < 62) {
        return String.fromCharCode(_code0 + (uc - 52));
    }

    if (uc == 62) {
        return "+";
    }

    return "/";
}
 
function decodeChar(c) {
    if (c >= "A" && c <= "Z") {
        return c.charCodeAt(0) - _codeA;
    }
 
    if (c >= "a" && c <= "z") {
        return c.charCodeAt(0) - _codea + 26;
    }
 
    if (c >= "0" && c <= "9") {
        return c.charCodeAt(0) - _code0 + 52;
    }
 
    if (c == "+") {
        return 62;
    }
 
    return 63;
}
 
export class Base64 {
    // Expects native Buffer
    static encode(buf) {
        if (buf === null || buf.length === 0) {
            return "";
        }
 
        const v = [];
 
        let by1;
        let by2;
        let by3;
        let by4;
        let by5;
        let by6;
        let by7;
 
        for (let i = 0; i < buf.length; i += 3) {
            by1 = buf[i] & 0xff;
            by2 = 0;
            by3 = 0;
 
            if (i + 1 < buf.length) {
                by2 = buf[i + 1] & 0xff;
            }
 
            if (i + 2 < buf.length) {
                by3 = buf[i + 2] & 0xff;
            }
 
            by4 = (by1 >> 2) & 0xff;
            by5 = (((by1 & 0x3) << 4) | (by2 >> 4)) & 0xff;
            by6 = (((by2 & 0xf) << 2) | (by3 >> 6)) & 0xff;
            by7 = by3 & 0x3f;
 
            v.push(encodeChar(by4));
            v.push(encodeChar(by5));
 
            if (i + 1 < buf.length) {
                v.push(encodeChar(by6));
            } else {
                v.push("=");
            }
 
            if (i + 2 < buf.length) {
                v.push(encodeChar(by7));
            } else {
                v.push("=");
            }
        }
 
        const returnValue = v.join("");
        const outString = [];
        let iter = 0;
 
        while (returnValue.length - iter > 76) {
            outString.push(returnValue.substring(iter, iter + 76));
            outString.push("\r\n");
            iter += 76;
        }
 
        outString.push(returnValue.substring(iter));
 
        return outString.join("");
    }
 
    static decode(
        str, // Returns native Buffer
    ) {
        const newStr = [];
 
        for (let j = 0; j < str.length; j++) {
            const c = str.charAt(j);
            if (Base64.isBase64(c)) {
                newStr.push(c);
            }
        }
 
        if (newStr.length === 0) {
            return null;
        }
 
        // Note: This is how we were previously computing the size of the return
        //       sequence.  The method below is more efficient (and correct).
        // size_t lines = str.size() / 78;
        // size_t totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4);
 
        // Figure out how long the final sequence is going to be.
        const totalBytes = (newStr.length * 3) / 4 + 1;
 
        const returnValue = new Buffer();
        returnValue.resize(totalBytes);
 
        let by1;
        let by2;
        let by3;
        let by4;
 
        let c1;
        let c2;
        let c3;
        let c4;
 
        for (let i = 0; i < newStr.length; i += 4) {
            c1 = "A";
            c2 = "A";
            c3 = "A";
            c4 = "A";
 
            c1 = newStr[i];
 
            if (i + 1 < newStr.length) {
                c2 = newStr[i + 1];
            }
 
            if (i + 2 < newStr.length) {
                c3 = newStr[i + 2];
            }
 
            if (i + 3 < newStr.length) {
                c4 = newStr[i + 3];
            }
 
            by1 = decodeChar(c1) & 0xff;
            by2 = decodeChar(c2) & 0xff;
            by3 = decodeChar(c3) & 0xff;
            by4 = decodeChar(c4) & 0xff;
 
            returnValue.put((by1 << 2) | (by2 >> 4));
 
            if (c3 != "=") {
                returnValue.put(((by2 & 0xf) << 4) | (by3 >> 2));
            }
 
            if (c4 != "=") {
                returnValue.put(((by3 & 0x3) << 6) | by4);
            }
        }
 
        return returnValue.remaining > 0 ? returnValue.getArrayAt(0, returnValue.position) : returnValue.getArrayAt(0);
    }
 
    static isBase64(c) {
        if (c >= "A" && c <= "Z") {
            return true;
        }
 
        if (c >= "a" && c <= "z") {
            return true;
        }
 
        if (c >= "0" && c <= "9") {
            return true;
        }
 
        if (c == "+") {
            return true;
        }
 
        if (c == "/") {
            return true;
        }
 
        if (c == "=") {
            return true;
        }
 
        return false;
    }
}