Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
'use strict';
var Iconv = require('../lib/iconv').Iconv;
var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;
var iconv = new Iconv('UTF-8', 'UTF-16LE');
var utf8 = Buffer.alloc(20000000);
for (var i = 0; i < utf8.length; i++) {
utf8[i] = 97 + i % 26; // cycle from 'a' to 'z'.
}
var utf16 = iconv.convert(utf8);
assert.equal(utf16.length, utf8.length * 2);
for (i = 0; i < utf8.length; ++i) {
assert.equal(utf16[i * 2], utf8[i]);
assert.equal(utf16[i * 2 + 1], 0);
}
function writeBitField(setBits, bitIndex) {
var bitLen = bitIndex.length;
var blen = Math.ceil(bitLen / 8);
var unused = blen * 8 - bitLen;
var bits = Buffer.alloc(1 + blen); // zero-filled
bits[0] = unused;
for (var i = 0; i < bitLen; ++i) {
var byteN = 1 + Math.floor(i / 8);
var bit = 7 - (i % 8);
var mask = 1 << bit;
var name = bitIndex[i];
if (name === undefined)
continue;
var bitVal = (setBits.indexOf(name) !== -1);
if (bitVal) {
bits[byteN] |= mask;
}
}
return (bits);
}
describe('cstring', function() {
var b = Buffer.alloc(9), expected = 'abcd1234';
b[8] = 0;
b.write(expected, 0);
describe('#read()', function() {
it('should read a C-Octet String (null-terminated string) from the buffer', function() {
var result = types.cstring.read(b, 0);
assert.equal(result, expected);
});
});
describe('#size()', function() {
it('should return the length of a C-Octet String (null-terminated string)', function() {
assert.equal(9, types.cstring.size(expected));
});
});
function zeroPadToLength(buf, len) {
assert.buffer(buf);
assert.number(len);
while (buf.length > len) {
assert.equal(buf[0], 0x00);
buf = buf.slice(1);
}
while (buf.length < len) {
var b = Buffer.alloc(buf.length + 1);
b[0] = 0x00;
buf.copy(b, 1);
buf = b;
}
return (buf);
}
function mpNormalize(buf) {
assert.buffer(buf);
while (buf.length > 1 && buf[0] === 0x00 && (buf[1] & 0x80) === 0x00)
buf = buf.slice(1);
if ((buf[0] & 0x80) === 0x80) {
var b = Buffer.alloc(buf.length + 1);
b[0] = 0x00;
buf.copy(b, 1);
buf = b;
}
return (buf);
}
input_size,
output,
output_start,
output_size,
out,
input === FLUSH);
var input_consumed = out[0];
var output_consumed = out[1];
input_start += input_consumed;
input_size -= input_consumed;
output_start += output_consumed;
output_size -= output_consumed;
if (errno) {
if (errno === E2BIG) {
output_size += output.length;
var newbuf = Buffer.alloc(output.length * 2);
output.copy(newbuf, 0, 0, output_start);
output = newbuf;
continue;
}
else if (errno === EILSEQ) {
throw errnoException('EILSEQ', 'Illegal character sequence.');
}
else if (errno === EINVAL) {
if (context === null || input === FLUSH) {
throw errnoException('EINVAL', 'Incomplete character sequence.');
}
else {
context.trailer = input.slice(input_start);
return output.slice(0, output_start);
}
}
Utf16BEDecoder.prototype.write = function(buf) {
if (buf.length == 0)
return '';
var buf2 = Buffer.alloc(buf.length + 1),
i = 0, j = 0;
if (this.overflowByte !== -1) {
buf2[0] = buf[0];
buf2[1] = this.overflowByte;
i = 1; j = 2;
}
for (; i < buf.length-1; i += 2, j+= 2) {
buf2[j] = buf[i+1];
buf2[j+1] = buf[i];
}
this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
return buf2.slice(0, j).toString('ucs2');
function mpNormalize(buf) {
assert.buffer(buf);
while (buf.length > 1 && buf[0] === 0x00 && (buf[1] & 0x80) === 0x00)
buf = buf.slice(1);
if ((buf[0] & 0x80) === 0x80) {
var b = Buffer.alloc(buf.length + 1);
b[0] = 0x00;
buf.copy(b, 1);
buf = b;
}
return (buf);
}
SSHBuffer.prototype.expand = function () {
this._size *= 2;
var buf = Buffer.alloc(this._size);
this._buffer.copy(buf, 0);
this._buffer = buf;
};