LCOV - code coverage report
Current view: top level - pkg/lib/cockpit/_internal - base64.js Coverage Total Hit
Test: cockpit Lines: 0.0 % 53 0
Test Date: 2026-07-02 14:11:36

            Line data    Source code
       1              : // SPDX-License-Identifier: LGPL-2.1-or-later
       2              : /*
       3              :  * These are the polyfills from Mozilla. It's pretty nasty that
       4              :  * these weren't in the typed array standardization.
       5              :  *
       6              :  * https://developer.mozilla.org/en-US/docs/Glossary/Base64
       7              :  */
       8              : 
       9            0 : function uint6_to_b64 (x) {
      10            0 :     return x < 26 ? x + 65 : x < 52 ? x + 71 : x < 62 ? x - 4 : x === 62 ? 43 : x === 63 ? 47 : 65;
      11            0 : }
      12              : 
      13            0 : export function base64_encode(data) {
      14            0 :     if (typeof data === "string")
      15            0 :         return window.btoa(data);
      16              :     /* For when the caller has chosen to use ArrayBuffer */
      17            0 :     if (data instanceof window.ArrayBuffer)
      18            0 :         data = new window.Uint8Array(data);
      19            0 :     const length = data.length;
      20            0 :     let mod3 = 2;
      21            0 :     let str = "";
      22            0 :     for (let uint24 = 0, i = 0; i < length; i++) {
      23            0 :         mod3 = i % 3;
      24            0 :         uint24 |= data[i] << (16 >>> mod3 & 24);
      25            0 :         if (mod3 === 2 || length - i === 1) {
      26            0 :             str += String.fromCharCode(uint6_to_b64(uint24 >>> 18 & 63),
      27            0 :                                        uint6_to_b64(uint24 >>> 12 & 63),
      28            0 :                                        uint6_to_b64(uint24 >>> 6 & 63),
      29            0 :                                        uint6_to_b64(uint24 & 63));
      30            0 :             uint24 = 0;
      31            0 :         }
      32            0 :     }
      33              : 
      34            0 :     return str.substring(0, str.length - 2 + mod3) + (mod3 === 2 ? '' : mod3 === 1 ? '=' : '==');
      35            0 : }
      36              : 
      37            0 : function b64_to_uint6 (x) {
      38            0 :     return x > 64 && x < 91
      39            0 :         ? x - 65
      40            0 :         : x > 96 && x < 123
      41            0 :             ? x - 71
      42            0 :             : x > 47 && x < 58 ? x + 4 : x === 43 ? 62 : x === 47 ? 63 : 0;
      43            0 : }
      44              : 
      45            0 : export function base64_decode(str, constructor) {
      46            0 :     if (constructor === String)
      47            0 :         return window.atob(str);
      48            0 :     const ilen = str.length;
      49            0 :     let eq;
      50            0 :     for (eq = 0; eq < 3; eq++) {
      51            0 :         if (str[ilen - (eq + 1)] != '=')
      52            0 :             break;
      53            0 :     }
      54            0 :     const olen = (ilen * 3 + 1 >> 2) - eq;
      55            0 :     const data = new (constructor || Array)(olen);
      56            0 :     for (let mod3, mod4, uint24 = 0, oi = 0, ii = 0; ii < ilen; ii++) {
      57            0 :         mod4 = ii & 3;
      58            0 :         uint24 |= b64_to_uint6(str.charCodeAt(ii)) << 18 - 6 * mod4;
      59            0 :         if (mod4 === 3 || ilen - ii === 1) {
      60            0 :             for (mod3 = 0; mod3 < 3 && oi < olen; mod3++, oi++)
      61            0 :                 data[oi] = uint24 >>> (16 >>> mod3 & 24) & 255;
      62            0 :             uint24 = 0;
      63            0 :         }
      64            0 :     }
      65            0 :     return data;
      66            0 : }
        

Generated by: LCOV version 2.0-1