SshDecoder.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SshDecoder = void 0;
  4. const Reader_1 = require("@jsonjoy.com/buffers/lib/Reader");
  5. const JsonPackMpint_1 = require("../JsonPackMpint");
  6. class SshDecoder {
  7. constructor(reader = new Reader_1.Reader()) {
  8. this.reader = reader;
  9. }
  10. read(uint8) {
  11. this.reader.reset(uint8);
  12. return this.readAny();
  13. }
  14. decode(uint8) {
  15. this.reader.reset(uint8);
  16. return this.readAny();
  17. }
  18. readAny() {
  19. throw new Error('SshDecoder.readAny() requires explicit type methods');
  20. }
  21. readBoolean() {
  22. return this.reader.u8() !== 0;
  23. }
  24. readByte() {
  25. return this.reader.u8();
  26. }
  27. readUint32() {
  28. const reader = this.reader;
  29. const value = reader.view.getUint32(reader.x, false);
  30. reader.x += 4;
  31. return value;
  32. }
  33. readUint64() {
  34. const reader = this.reader;
  35. const value = reader.view.getBigUint64(reader.x, false);
  36. reader.x += 8;
  37. return value;
  38. }
  39. readBinStr() {
  40. const length = this.readUint32();
  41. const reader = this.reader;
  42. const data = new Uint8Array(length);
  43. for (let i = 0; i < length; i++) {
  44. data[i] = reader.u8();
  45. }
  46. return data;
  47. }
  48. readStr() {
  49. const length = this.readUint32();
  50. const reader = this.reader;
  51. const utf8Bytes = new Uint8Array(length);
  52. for (let i = 0; i < length; i++) {
  53. utf8Bytes[i] = reader.u8();
  54. }
  55. return new TextDecoder('utf-8').decode(utf8Bytes);
  56. }
  57. readAsciiStr() {
  58. const length = this.readUint32();
  59. const reader = this.reader;
  60. let str = '';
  61. for (let i = 0; i < length; i++) {
  62. str += String.fromCharCode(reader.u8());
  63. }
  64. return str;
  65. }
  66. readMpint() {
  67. const length = this.readUint32();
  68. const reader = this.reader;
  69. const data = new Uint8Array(length);
  70. for (let i = 0; i < length; i++) {
  71. data[i] = reader.u8();
  72. }
  73. return new JsonPackMpint_1.JsonPackMpint(data);
  74. }
  75. readNameList() {
  76. const nameListStr = this.readAsciiStr();
  77. if (nameListStr === '') {
  78. return [];
  79. }
  80. return nameListStr.split(',');
  81. }
  82. readBin() {
  83. return this.readBinStr();
  84. }
  85. }
  86. exports.SshDecoder = SshDecoder;
  87. //# sourceMappingURL=SshDecoder.js.map