1 // Copyright 2015 Joyent, Inc.
3 module.exports = SSHBuffer;
5 var assert = require('assert-plus');
7 function SSHBuffer(opts) {
8 assert.object(opts, 'options');
9 if (opts.buffer !== undefined)
10 assert.buffer(opts.buffer, 'options.buffer');
12 this._size = opts.buffer ? opts.buffer.length : 1024;
13 this._buffer = opts.buffer || (new Buffer(this._size));
17 SSHBuffer.prototype.toBuffer = function () {
18 return (this._buffer.slice(0, this._offset));
21 SSHBuffer.prototype.atEnd = function () {
22 return (this._offset >= this._buffer.length);
25 SSHBuffer.prototype.remainder = function () {
26 return (this._buffer.slice(this._offset));
29 SSHBuffer.prototype.skip = function (n) {
33 SSHBuffer.prototype.expand = function () {
35 var buf = new Buffer(this._size);
36 this._buffer.copy(buf, 0);
40 SSHBuffer.prototype.readPart = function () {
41 return ({data: this.readBuffer()});
44 SSHBuffer.prototype.readBuffer = function () {
45 var len = this._buffer.readUInt32BE(this._offset);
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);
55 SSHBuffer.prototype.readString = function () {
56 return (this.readBuffer().toString());
59 SSHBuffer.prototype.readCString = function () {
60 var offset = this._offset;
61 while (offset < this._buffer.length &&
62 this._buffer[offset] !== 0x00)
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;
70 SSHBuffer.prototype.readInt = function () {
71 var v = this._buffer.readUInt32BE(this._offset);
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);
84 SSHBuffer.prototype.readChar = function () {
85 var v = this._buffer[this._offset++];
89 SSHBuffer.prototype.writeBuffer = function (buf) {
90 while (this._offset + 4 + buf.length > this._size)
92 this._buffer.writeUInt32BE(buf.length, this._offset);
94 buf.copy(this._buffer, this._offset);
95 this._offset += buf.length;
98 SSHBuffer.prototype.writeString = function (str) {
99 this.writeBuffer(new Buffer(str, 'utf8'));
102 SSHBuffer.prototype.writeCString = function (str) {
103 while (this._offset + 1 + str.length > this._size)
105 this._buffer.write(str, this._offset);
106 this._offset += str.length;
107 this._buffer[this._offset++] = 0;
110 SSHBuffer.prototype.writeInt = function (v) {
111 while (this._offset + 4 > this._size)
113 this._buffer.writeUInt32BE(v, this._offset);
117 SSHBuffer.prototype.writeInt64 = function (v) {
118 assert.buffer(v, 'value');
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');
125 v = v.slice(v.length - 8, v.length);
127 while (this._offset + 8 > this._size)
129 v.copy(this._buffer, this._offset);
133 SSHBuffer.prototype.writeChar = function (v) {
134 while (this._offset + 1 > this._size)
136 this._buffer[this._offset++] = v;
139 SSHBuffer.prototype.writePart = function (p) {
140 this.writeBuffer(p.data);
143 SSHBuffer.prototype.write = function (buf) {
144 while (this._offset + buf.length > this._size)
146 buf.copy(this._buffer, this._offset);
147 this._offset += buf.length;