SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
19 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
13.3 KB · 363 lines javascript
Raw Blame History
1"use strict";23Object.defineProperty(exports, "__esModule", {4  value: true5});6exports["default"] = void 0;7var Log = _interopRequireWildcard(require("../util/logging.js"));8var _inflator = _interopRequireDefault(require("../inflator.js"));9function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }10function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }11function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }12function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }13function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }14function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }15function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }16function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }17function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*18 * noVNC: HTML5 VNC client19 * Copyright (C) 2019 The noVNC authors20 * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca)21 * Licensed under MPL 2.0 (see LICENSE.txt)22 *23 * See README.md for usage and integration instructions.24 *25 */26var TightDecoder = exports["default"] = /*#__PURE__*/function () {27  function TightDecoder() {28    _classCallCheck(this, TightDecoder);29    this._ctl = null;30    this._filter = null;31    this._numColors = 0;32    this._palette = new Uint8Array(1024); // 256 * 4 (max palette size * max bytes-per-pixel)33    this._len = 0;34    this._zlibs = [];35    for (var i = 0; i < 4; i++) {36      this._zlibs[i] = new _inflator["default"]();37    }38  }39  return _createClass(TightDecoder, [{40    key: "decodeRect",41    value: function decodeRect(x, y, width, height, sock, display, depth) {42      if (this._ctl === null) {43        if (sock.rQwait("TIGHT compression-control", 1)) {44          return false;45        }46        this._ctl = sock.rQshift8();4748        // Reset streams if the server requests it49        for (var i = 0; i < 4; i++) {50          if (this._ctl >> i & 1) {51            this._zlibs[i].reset();52            Log.Info("Reset zlib stream " + i);53          }54        }5556        // Figure out filter57        this._ctl = this._ctl >> 4;58      }59      var ret;60      if (this._ctl === 0x08) {61        ret = this._fillRect(x, y, width, height, sock, display, depth);62      } else if (this._ctl === 0x09) {63        ret = this._jpegRect(x, y, width, height, sock, display, depth);64      } else if (this._ctl === 0x0A) {65        ret = this._pngRect(x, y, width, height, sock, display, depth);66      } else if ((this._ctl & 0x08) == 0) {67        ret = this._basicRect(this._ctl, x, y, width, height, sock, display, depth);68      } else {69        throw new Error("Illegal tight compression received (ctl: " + this._ctl + ")");70      }71      if (ret) {72        this._ctl = null;73      }74      return ret;75    }76  }, {77    key: "_fillRect",78    value: function _fillRect(x, y, width, height, sock, display, depth) {79      if (sock.rQwait("TIGHT", 3)) {80        return false;81      }82      var pixel = sock.rQshiftBytes(3);83      display.fillRect(x, y, width, height, pixel, false);84      return true;85    }86  }, {87    key: "_jpegRect",88    value: function _jpegRect(x, y, width, height, sock, display, depth) {89      var data = this._readData(sock);90      if (data === null) {91        return false;92      }93      display.imageRect(x, y, width, height, "image/jpeg", data);94      return true;95    }96  }, {97    key: "_pngRect",98    value: function _pngRect(x, y, width, height, sock, display, depth) {99      throw new Error("PNG received in standard Tight rect");100    }101  }, {102    key: "_basicRect",103    value: function _basicRect(ctl, x, y, width, height, sock, display, depth) {104      if (this._filter === null) {105        if (ctl & 0x4) {106          if (sock.rQwait("TIGHT", 1)) {107            return false;108          }109          this._filter = sock.rQshift8();110        } else {111          // Implicit CopyFilter112          this._filter = 0;113        }114      }115      var streamId = ctl & 0x3;116      var ret;117      switch (this._filter) {118        case 0:119          // CopyFilter120          ret = this._copyFilter(streamId, x, y, width, height, sock, display, depth);121          break;122        case 1:123          // PaletteFilter124          ret = this._paletteFilter(streamId, x, y, width, height, sock, display, depth);125          break;126        case 2:127          // GradientFilter128          ret = this._gradientFilter(streamId, x, y, width, height, sock, display, depth);129          break;130        default:131          throw new Error("Illegal tight filter received (ctl: " + this._filter + ")");132      }133      if (ret) {134        this._filter = null;135      }136      return ret;137    }138  }, {139    key: "_copyFilter",140    value: function _copyFilter(streamId, x, y, width, height, sock, display, depth) {141      var uncompressedSize = width * height * 3;142      var data;143      if (uncompressedSize === 0) {144        return true;145      }146      if (uncompressedSize < 12) {147        if (sock.rQwait("TIGHT", uncompressedSize)) {148          return false;149        }150        data = sock.rQshiftBytes(uncompressedSize);151      } else {152        data = this._readData(sock);153        if (data === null) {154          return false;155        }156        this._zlibs[streamId].setInput(data);157        data = this._zlibs[streamId].inflate(uncompressedSize);158        this._zlibs[streamId].setInput(null);159      }160      var rgbx = new Uint8Array(width * height * 4);161      for (var i = 0, j = 0; i < width * height * 4; i += 4, j += 3) {162        rgbx[i] = data[j];163        rgbx[i + 1] = data[j + 1];164        rgbx[i + 2] = data[j + 2];165        rgbx[i + 3] = 255; // Alpha166      }167      display.blitImage(x, y, width, height, rgbx, 0, false);168      return true;169    }170  }, {171    key: "_paletteFilter",172    value: function _paletteFilter(streamId, x, y, width, height, sock, display, depth) {173      if (this._numColors === 0) {174        if (sock.rQwait("TIGHT palette", 1)) {175          return false;176        }177        var numColors = sock.rQpeek8() + 1;178        var paletteSize = numColors * 3;179        if (sock.rQwait("TIGHT palette", 1 + paletteSize)) {180          return false;181        }182        this._numColors = numColors;183        sock.rQskipBytes(1);184        sock.rQshiftTo(this._palette, paletteSize);185      }186      var bpp = this._numColors <= 2 ? 1 : 8;187      var rowSize = Math.floor((width * bpp + 7) / 8);188      var uncompressedSize = rowSize * height;189      var data;190      if (uncompressedSize === 0) {191        return true;192      }193      if (uncompressedSize < 12) {194        if (sock.rQwait("TIGHT", uncompressedSize)) {195          return false;196        }197        data = sock.rQshiftBytes(uncompressedSize);198      } else {199        data = this._readData(sock);200        if (data === null) {201          return false;202        }203        this._zlibs[streamId].setInput(data);204        data = this._zlibs[streamId].inflate(uncompressedSize);205        this._zlibs[streamId].setInput(null);206      }207208      // Convert indexed (palette based) image data to RGB209      if (this._numColors == 2) {210        this._monoRect(x, y, width, height, data, this._palette, display);211      } else {212        this._paletteRect(x, y, width, height, data, this._palette, display);213      }214      this._numColors = 0;215      return true;216    }217  }, {218    key: "_monoRect",219    value: function _monoRect(x, y, width, height, data, palette, display) {220      // Convert indexed (palette based) image data to RGB221      // TODO: reduce number of calculations inside loop222      var dest = this._getScratchBuffer(width * height * 4);223      var w = Math.floor((width + 7) / 8);224      var w1 = Math.floor(width / 8);225      for (var _y = 0; _y < height; _y++) {226        var dp = void 0,227          sp = void 0,228          _x = void 0;229        for (_x = 0; _x < w1; _x++) {230          for (var b = 7; b >= 0; b--) {231            dp = (_y * width + _x * 8 + 7 - b) * 4;232            sp = (data[_y * w + _x] >> b & 1) * 3;233            dest[dp] = palette[sp];234            dest[dp + 1] = palette[sp + 1];235            dest[dp + 2] = palette[sp + 2];236            dest[dp + 3] = 255;237          }238        }239        for (var _b = 7; _b >= 8 - width % 8; _b--) {240          dp = (_y * width + _x * 8 + 7 - _b) * 4;241          sp = (data[_y * w + _x] >> _b & 1) * 3;242          dest[dp] = palette[sp];243          dest[dp + 1] = palette[sp + 1];244          dest[dp + 2] = palette[sp + 2];245          dest[dp + 3] = 255;246        }247      }248      display.blitImage(x, y, width, height, dest, 0, false);249    }250  }, {251    key: "_paletteRect",252    value: function _paletteRect(x, y, width, height, data, palette, display) {253      // Convert indexed (palette based) image data to RGB254      var dest = this._getScratchBuffer(width * height * 4);255      var total = width * height * 4;256      for (var i = 0, j = 0; i < total; i += 4, j++) {257        var sp = data[j] * 3;258        dest[i] = palette[sp];259        dest[i + 1] = palette[sp + 1];260        dest[i + 2] = palette[sp + 2];261        dest[i + 3] = 255;262      }263      display.blitImage(x, y, width, height, dest, 0, false);264    }265  }, {266    key: "_gradientFilter",267    value: function _gradientFilter(streamId, x, y, width, height, sock, display, depth) {268      // assume the TPIXEL is 3 bytes long269      var uncompressedSize = width * height * 3;270      var data;271      if (uncompressedSize === 0) {272        return true;273      }274      if (uncompressedSize < 12) {275        if (sock.rQwait("TIGHT", uncompressedSize)) {276          return false;277        }278        data = sock.rQshiftBytes(uncompressedSize);279      } else {280        data = this._readData(sock);281        if (data === null) {282          return false;283        }284        this._zlibs[streamId].setInput(data);285        data = this._zlibs[streamId].inflate(uncompressedSize);286        this._zlibs[streamId].setInput(null);287      }288      var rgbx = new Uint8Array(4 * width * height);289      var rgbxIndex = 0,290        dataIndex = 0;291      var left = new Uint8Array(3);292      for (var _x2 = 0; _x2 < width; _x2++) {293        for (var c = 0; c < 3; c++) {294          var prediction = left[c];295          var value = data[dataIndex++] + prediction;296          rgbx[rgbxIndex++] = value;297          left[c] = value;298        }299        rgbx[rgbxIndex++] = 255;300      }301      var upperIndex = 0;302      var upper = new Uint8Array(3),303        upperleft = new Uint8Array(3);304      for (var _y2 = 1; _y2 < height; _y2++) {305        left.fill(0);306        upperleft.fill(0);307        for (var _x3 = 0; _x3 < width; _x3++) {308          for (var _c = 0; _c < 3; _c++) {309            upper[_c] = rgbx[upperIndex++];310            var _prediction = left[_c] + upper[_c] - upperleft[_c];311            if (_prediction < 0) {312              _prediction = 0;313            } else if (_prediction > 255) {314              _prediction = 255;315            }316            var _value = data[dataIndex++] + _prediction;317            rgbx[rgbxIndex++] = _value;318            upperleft[_c] = upper[_c];319            left[_c] = _value;320          }321          rgbx[rgbxIndex++] = 255;322          upperIndex++;323        }324      }325      display.blitImage(x, y, width, height, rgbx, 0, false);326      return true;327    }328  }, {329    key: "_readData",330    value: function _readData(sock) {331      if (this._len === 0) {332        if (sock.rQwait("TIGHT", 3)) {333          return null;334        }335        var _byte;336        _byte = sock.rQshift8();337        this._len = _byte & 0x7f;338        if (_byte & 0x80) {339          _byte = sock.rQshift8();340          this._len |= (_byte & 0x7f) << 7;341          if (_byte & 0x80) {342            _byte = sock.rQshift8();343            this._len |= _byte << 14;344          }345        }346      }347      if (sock.rQwait("TIGHT", this._len)) {348        return null;349      }350      var data = sock.rQshiftBytes(this._len, false);351      this._len = 0;352      return data;353    }354  }, {355    key: "_getScratchBuffer",356    value: function _getScratchBuffer(size) {357      if (!this._scratchBuffer || this._scratchBuffer.length < size) {358        this._scratchBuffer = new Uint8Array(size);359      }360      return this._scratchBuffer;361    }362  }]);363}();