"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/ssh-buffer.js" (7 Feb 2017, 3829 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2015 Joyent, Inc.
    2 
    3 module.exports = SSHBuffer;
    4 
    5 var assert = require('assert-plus');
    6 
    7 function SSHBuffer(opts) {
    8     assert.object(opts, 'options');
    9     if (opts.buffer !== undefined)
   10         assert.buffer(opts.buffer, 'options.buffer');
   11 
   12     this._size = opts.buffer ? opts.buffer.length : 1024;
   13     this._buffer = opts.buffer || (new Buffer(this._size));
   14     this._offset = 0;
   15 }
   16 
   17 SSHBuffer.prototype.toBuffer = function () {
   18     return (this._buffer.slice(0, this._offset));
   19 };
   20 
   21 SSHBuffer.prototype.atEnd = function () {
   22     return (this._offset >= this._buffer.length);
   23 };
   24 
   25 SSHBuffer.prototype.remainder = function () {
   26     return (this._buffer.slice(this._offset));
   27 };
   28 
   29 SSHBuffer.prototype.skip = function (n) {
   30     this._offset += n;
   31 };
   32 
   33 SSHBuffer.prototype.expand = function () {
   34     this._size *= 2;
   35     var buf = new Buffer(this._size);
   36     this._buffer.copy(buf, 0);
   37     this._buffer = buf;
   38 };
   39 
   40 SSHBuffer.prototype.readPart = function () {
   41     return ({data: this.readBuffer()});
   42 };
   43 
   44 SSHBuffer.prototype.readBuffer = function () {
   45     var len = this._buffer.readUInt32BE(this._offset);
   46     this._offset += 4;
   47     assert.ok(this._offset + len <= this._buffer.length,
   48         'length out of bounds at +0x' + this._offset.toString(16) +
   49         ' (data truncated?)');
   50     var buf = this._buffer.slice(this._offset, this._offset + len);
   51     this._offset += len;
   52     return (buf);
   53 };
   54 
   55 SSHBuffer.prototype.readString = function () {
   56     return (this.readBuffer().toString());
   57 };
   58 
   59 SSHBuffer.prototype.readCString = function () {
   60     var offset = this._offset;
   61     while (offset < this._buffer.length &&
   62         this._buffer[offset] !== 0x00)
   63         offset++;
   64     assert.ok(offset < this._buffer.length, 'c string does not terminate');
   65     var str = this._buffer.slice(this._offset, offset).toString();
   66     this._offset = offset + 1;
   67     return (str);
   68 };
   69 
   70 SSHBuffer.prototype.readInt = function () {
   71     var v = this._buffer.readUInt32BE(this._offset);
   72     this._offset += 4;
   73     return (v);
   74 };
   75 
   76 SSHBuffer.prototype.readInt64 = function () {
   77     assert.ok(this._offset + 8 < this._buffer.length,
   78         'buffer not long enough to read Int64');
   79     var v = this._buffer.slice(this._offset, this._offset + 8);
   80     this._offset += 8;
   81     return (v);
   82 };
   83 
   84 SSHBuffer.prototype.readChar = function () {
   85     var v = this._buffer[this._offset++];
   86     return (v);
   87 };
   88 
   89 SSHBuffer.prototype.writeBuffer = function (buf) {
   90     while (this._offset + 4 + buf.length > this._size)
   91         this.expand();
   92     this._buffer.writeUInt32BE(buf.length, this._offset);
   93     this._offset += 4;
   94     buf.copy(this._buffer, this._offset);
   95     this._offset += buf.length;
   96 };
   97 
   98 SSHBuffer.prototype.writeString = function (str) {
   99     this.writeBuffer(new Buffer(str, 'utf8'));
  100 };
  101 
  102 SSHBuffer.prototype.writeCString = function (str) {
  103     while (this._offset + 1 + str.length > this._size)
  104         this.expand();
  105     this._buffer.write(str, this._offset);
  106     this._offset += str.length;
  107     this._buffer[this._offset++] = 0;
  108 };
  109 
  110 SSHBuffer.prototype.writeInt = function (v) {
  111     while (this._offset + 4 > this._size)
  112         this.expand();
  113     this._buffer.writeUInt32BE(v, this._offset);
  114     this._offset += 4;
  115 };
  116 
  117 SSHBuffer.prototype.writeInt64 = function (v) {
  118     assert.buffer(v, 'value');
  119     if (v.length > 8) {
  120         var lead = v.slice(0, v.length - 8);
  121         for (var i = 0; i < lead.length; ++i) {
  122             assert.strictEqual(lead[i], 0,
  123                 'must fit in 64 bits of precision');
  124         }
  125         v = v.slice(v.length - 8, v.length);
  126     }
  127     while (this._offset + 8 > this._size)
  128         this.expand();
  129     v.copy(this._buffer, this._offset);
  130     this._offset += 8;
  131 };
  132 
  133 SSHBuffer.prototype.writeChar = function (v) {
  134     while (this._offset + 1 > this._size)
  135         this.expand();
  136     this._buffer[this._offset++] = v;
  137 };
  138 
  139 SSHBuffer.prototype.writePart = function (p) {
  140     this.writeBuffer(p.data);
  141 };
  142 
  143 SSHBuffer.prototype.write = function (buf) {
  144     while (this._offset + buf.length > this._size)
  145         this.expand();
  146     buf.copy(this._buffer, this._offset);
  147     this._offset += buf.length;
  148 };