Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,2 @@
node_modules/
npm-debug.log

View File

@@ -0,0 +1,396 @@
var Buffer = require('buffer').Buffer;
function OffsetBuffer() {
this.offset = 0;
this.size = 0;
this.buffers = [];
}
module.exports = OffsetBuffer;
OffsetBuffer.prototype.isEmpty = function isEmpty() {
return this.size === 0;
};
OffsetBuffer.prototype.clone = function clone(size) {
var r = new OffsetBuffer();
r.offset = this.offset;
r.size = size;
r.buffers = this.buffers.slice();
return r;
};
OffsetBuffer.prototype.toChunks = function toChunks() {
if (this.size === 0)
return [];
// We are going to slice it anyway
if (this.offset !== 0) {
this.buffers[0] = this.buffers[0].slice(this.offset);
this.offset = 0;
}
var chunks = [ ];
var off = 0;
for (var i = 0; off <= this.size && i < this.buffers.length; i++) {
var buf = this.buffers[i];
off += buf.length;
// Slice off last buffer
if (off > this.size) {
buf = buf.slice(0, buf.length - (off - this.size));
this.buffers[i] = buf;
}
chunks.push(buf);
}
// If some buffers were skipped - trim length
if (i < this.buffers.length)
this.buffers.length = i;
return chunks;
};
OffsetBuffer.prototype.toString = function toString(enc) {
return this.toChunks().map(function(c) {
return c.toString(enc);
}).join('');
};
OffsetBuffer.prototype.use = function use(buf, off, n) {
this.buffers = [ buf ];
this.offset = off;
this.size = n;
};
OffsetBuffer.prototype.push = function push(data) {
// Ignore empty writes
if (data.length === 0)
return;
this.size += data.length;
this.buffers.push(data);
};
OffsetBuffer.prototype.has = function has(n) {
return this.size >= n;
};
OffsetBuffer.prototype.skip = function skip(n) {
if (this.size === 0)
return;
this.size -= n;
// Fast case, skip bytes in a first buffer
if (this.offset + n < this.buffers[0].length) {
this.offset += n;
return;
}
var left = n - (this.buffers[0].length - this.offset);
this.offset = 0;
for (var shift = 1; left > 0 && shift < this.buffers.length; shift++) {
var buf = this.buffers[shift];
if (buf.length > left) {
this.offset = left;
break;
}
left -= buf.length;
}
this.buffers = this.buffers.slice(shift);
};
OffsetBuffer.prototype.copy = function copy(target, targetOff, off, n) {
if (this.size === 0)
return;
if (off !== 0)
throw new Error('Unsupported offset in .copy()');
var toff = targetOff;
var first = this.buffers[0];
var toCopy = Math.min(n, first.length - this.offset);
first.copy(target, toff, this.offset, this.offset + toCopy);
toff += toCopy;
var left = n - toCopy;
for (var i = 1; left > 0 && i < this.buffers.length; i++) {
var buf = this.buffers[i];
var toCopy = Math.min(left, buf.length);
buf.copy(target, toff, 0, toCopy);
toff += toCopy;
left -= toCopy;
}
};
OffsetBuffer.prototype.take = function take(n) {
if (n === 0)
return new Buffer(0);
this.size -= n;
// Fast cases
var first = this.buffers[0].length - this.offset;
if (first === n) {
var r = this.buffers.shift();
if (this.offset !== 0) {
r = r.slice(this.offset);
this.offset = 0;
}
return r;
} else if (first > n) {
var r = this.buffers[0].slice(this.offset, this.offset + n);
this.offset += n;
return r;
}
// Allocate and fill buffer
var out = new Buffer(n);
var toOff = 0;
var startOff = this.offset;
for (var i = 0; toOff !== n && i < this.buffers.length; i++) {
var buf = this.buffers[i];
var toCopy = Math.min(buf.length - startOff, n - toOff);
buf.copy(out, toOff, startOff, startOff + toCopy);
if (startOff + toCopy < buf.length) {
this.offset = startOff + toCopy;
break;
} else {
toOff += toCopy;
startOff = 0;
}
}
this.buffers = this.buffers.slice(i);
if (this.buffers.length === 0)
this.offset = 0;
return out;
};
OffsetBuffer.prototype.peekUInt8 = function peekUInt8() {
return this.buffers[0][this.offset];
};
OffsetBuffer.prototype.readUInt8 = function readUInt8() {
this.size -= 1;
var first = this.buffers[0];
var r = first[this.offset];
if (++this.offset === first.length) {
this.offset = 0;
this.buffers.shift();
}
return r;
};
OffsetBuffer.prototype.readUInt16LE = function readUInt16LE() {
var first = this.buffers[0];
this.size -= 2;
var r;
var shift;
// Fast case - first buffer has all bytes
if (first.length - this.offset >= 2) {
r = first.readUInt16LE(this.offset, true);
shift = 0;
this.offset += 2;
// One byte here - one byte there
} else {
r = first[this.offset] | (this.buffers[1][0] << 8);
shift = 1;
this.offset = 1;
}
if (this.offset === this.buffers[shift].length) {
this.offset = 0;
shift++;
}
if (shift !== 0)
this.buffers = this.buffers.slice(shift);
return r;
};
OffsetBuffer.prototype.readUInt24LE = function readUInt24LE() {
var first = this.buffers[0];
var r;
var shift;
var firstHas = first.length - this.offset;
// Fast case - first buffer has all bytes
if (firstHas >= 3) {
r = first.readUInt16LE(this.offset, true) | (first[this.offset + 2] << 16);
shift = 0;
this.offset += 3;
// First buffer has 2 of 3 bytes
} else if (firstHas >= 2) {
r = first.readUInt16LE(this.offset, true) | (this.buffers[1][0] << 16);
shift = 1;
this.offset = 1;
// Slow case: First buffer has 1 of 3 bytes
} else {
r = first[this.offset];
this.offset = 0;
this.buffers.shift();
this.size -= 1;
r |= this.readUInt16LE() << 8;
return r;
}
this.size -= 3;
if (this.offset === this.buffers[shift].length) {
this.offset = 0;
shift++;
}
if (shift !== 0)
this.buffers = this.buffers.slice(shift);
return r;
};
OffsetBuffer.prototype.readUInt32LE = function readUInt32LE() {
var first = this.buffers[0];
var r;
var shift;
var firstHas = first.length - this.offset;
// Fast case - first buffer has all bytes
if (firstHas >= 4) {
r = first.readUInt32LE(this.offset, true);
shift = 0;
this.offset += 4;
// First buffer has 3 of 4 bytes
} else if (firstHas >= 3) {
r = (first.readUInt16LE(this.offset, true) |
(first[this.offset + 2] << 16)) +
(this.buffers[1][0] * 0x1000000);
shift = 1;
this.offset = 1;
// Slow case: First buffer has 2 of 4 bytes
} else if (firstHas >= 2) {
r = first.readUInt16LE(this.offset, true);
this.offset = 0;
this.buffers.shift();
this.size -= 2;
r += this.readUInt16LE() * 0x10000;
return r;
// Slow case: First buffer has 1 of 4 bytes
} else {
r = first[this.offset];
this.offset = 0;
this.buffers.shift();
this.size -= 1;
r += this.readUInt24LE() * 0x100;
return r;
}
this.size -= 4;
if (this.offset === this.buffers[shift].length) {
this.offset = 0;
shift++;
}
if (shift !== 0)
this.buffers = this.buffers.slice(shift);
return r;
};
OffsetBuffer.prototype.readUInt16BE = function readUInt16BE() {
var r = this.readUInt16LE();
return ((r & 0xff) << 8) | (r >> 8);
};
OffsetBuffer.prototype.readUInt24BE = function readUInt24BE() {
var r = this.readUInt24LE();
return ((r & 0xff) << 16) | (((r >> 8) & 0xff) << 8) | (r >> 16);
};
OffsetBuffer.prototype.readUInt32BE = function readUInt32BE() {
var r = this.readUInt32LE();
return (((r & 0xff) << 24) |
(((r >>> 8) & 0xff) << 16) |
(((r >>> 16) & 0xff) << 8) |
(r >>> 24)) >>> 0;
};
// Signed number APIs
function signedInt8(num) {
if (num >= 0x80)
return -(0xff ^ num) - 1;
else
return num;
}
OffsetBuffer.prototype.peekInt8 = function peekInt8() {
return signedInt8(this.peekUInt8());
};
OffsetBuffer.prototype.readInt8 = function readInt8() {
return signedInt8(this.readUInt8());
};
function signedInt16(num) {
if (num >= 0x8000)
return -(0xffff ^ num) - 1;
else
return num;
}
OffsetBuffer.prototype.readInt16BE = function readInt16BE() {
return signedInt16(this.readUInt16BE());
};
OffsetBuffer.prototype.readInt16LE = function readInt16LE() {
return signedInt16(this.readUInt16LE());
};
function signedInt24(num) {
if (num >= 0x800000)
return -(0xffffff ^ num) - 1;
else
return num;
}
OffsetBuffer.prototype.readInt24BE = function readInt24BE() {
return signedInt24(this.readUInt24BE());
};
OffsetBuffer.prototype.readInt24LE = function readInt24LE() {
return signedInt24(this.readUInt24LE());
};
function signedInt32(num) {
if (num >= 0x80000000)
return -(0xffffffff ^ num) - 1;
else
return num;
}
OffsetBuffer.prototype.readInt32BE = function readInt32BE() {
return signedInt32(this.readUInt32BE());
};
OffsetBuffer.prototype.readInt32LE = function readInt32LE() {
return signedInt32(this.readUInt32LE());
};

View File

@@ -0,0 +1,59 @@
{
"_args": [
[
"obuf@1.1.1",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "obuf@1.1.1",
"_id": "obuf@1.1.1",
"_inBundle": false,
"_integrity": "sha1-EEEktsYCxnlogaBCVB0220OlJk4=",
"_location": "/react-scripts/obuf",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "obuf@1.1.1",
"name": "obuf",
"escapedName": "obuf",
"rawSpec": "1.1.1",
"saveSpec": null,
"fetchSpec": "1.1.1"
},
"_requiredBy": [
"/react-scripts/hpack.js",
"/react-scripts/spdy-transport"
],
"_resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.1.tgz",
"_spec": "1.1.1",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"author": {
"name": "Fedor Indutny",
"email": "fedor@indutny.com"
},
"bugs": {
"url": "https://github.com/indutny/offset-buffer/issues"
},
"description": "",
"devDependencies": {
"mocha": "^1.21.4"
},
"homepage": "https://github.com/indutny/offset-buffer",
"keywords": [
"Offset",
"Buffer",
"reader"
],
"license": "MIT",
"main": "index.js",
"name": "obuf",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/indutny/offset-buffer.git"
},
"scripts": {
"test": "mocha test/**/*-test.js"
},
"version": "1.1.1"
}

View File

@@ -0,0 +1,268 @@
var assert = require('assert');
var OffsetBuffer = require('../');
describe('OffsetBuffer', function() {
var o;
beforeEach(function() {
o = new OffsetBuffer();
});
describe('.take()', function() {
it('should return empty buffer', function() {
var b = new Buffer('hello world');
o.push(b);
var r = o.take(0);
assert.equal(r.length, 0);
assert.equal(o.size, b.length);
});
it('should return the first buffer itself', function() {
var b = new Buffer('hello world');
o.push(b);
var r = o.take(b.length);
assert(r === b);
assert(o.isEmpty());
});
it('should return the slice of the buffer ', function() {
var b = new Buffer('hello world');
o.push(b);
assert.equal(o.take(5).toString(), 'hello');
assert.equal(o.take(1).toString(), ' ');
assert.equal(o.take(5).toString(), 'world');
assert(o.isEmpty());
});
it('should concat buffers', function() {
o.push(new Buffer('hello'));
o.push(new Buffer(' '));
o.push(new Buffer('world!'));
assert.equal(o.take(11).toString(), 'hello world');
assert.equal(o.take(1).toString(), '!');
assert(o.isEmpty());
});
});
describe('.skip', function() {
it('should skip bytes', function() {
o.push(new Buffer('hello '));
o.push(new Buffer('world'));
o.push(new Buffer(' oh gosh'));
assert.equal(o.take(2).toString(), 'he');
o.skip(1);
assert.equal(o.take(2).toString(), 'lo');
o.skip(1);
assert.equal(o.take(2).toString(), 'wo');
o.skip(4);
assert.equal(o.take(7).toString(), 'oh gosh');
assert(o.isEmpty());
});
});
describe('.peekUInt8', function() {
it('should return and not move by one byte', function() {
o.push(new Buffer([ 0x1, 0x2 ]));
assert.equal(o.peekUInt8(), 1);
assert.equal(o.readUInt8(), 1);
assert.equal(o.peekUInt8(), 2);
assert.equal(o.readUInt8(), 2);
assert(o.isEmpty());
});
});
describe('.peekInt8', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x80 ]));
assert.equal(o.peekInt8(), -128);
assert.equal(o.readInt8(), -128);
assert(o.isEmpty());
});
});
describe('.readUInt8', function() {
it('should return and move by one byte', function() {
o.push(new Buffer([ 0x1, 0x2 ]));
o.push(new Buffer([ 0x3, 0x4 ]));
assert.equal(o.readUInt8(), 1);
assert.equal(o.readUInt8(), 2);
assert.equal(o.readUInt8(), 3);
assert.equal(o.readUInt8(), 4);
assert(o.isEmpty());
});
});
describe('.readInt8', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x8f, 0x7f ]));
assert.equal(o.readInt8(), -113);
assert.equal(o.readInt8(), 127);
assert(o.isEmpty());
});
});
describe('.readUInt16LE', function() {
it('should return and move by two bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
assert.equal(o.readUInt16LE(), 0x0201);
assert.equal(o.readUInt16LE(), 0x0403);
assert.equal(o.readUInt16LE(), 0x0605);
assert(o.isEmpty());
});
it('should return and move by two bytes (regression #1)', function() {
o.push(new Buffer([ 0x1 ]));
o.push(new Buffer([ 0x2, 0x3, 0x4 ]));
assert.equal(o.readUInt16LE(), 0x0201);
assert.equal(o.readUInt16LE(), 0x0403);
assert(o.isEmpty());
});
});
describe('.readInt16LE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x23, 0x81 ]));
assert.equal(o.readInt16LE(), -32477);
assert(o.isEmpty());
});
});
describe('.readUInt24LE', function() {
it('should return and move by three bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
o.push(new Buffer([ 0x6, 0x7 ]));
o.push(new Buffer([ 0x8, 0x9 ]));
assert.equal(o.readUInt24LE(), 0x030201);
assert.equal(o.readUInt24LE(), 0x060504);
assert.equal(o.readUInt24LE(), 0x090807);
assert(o.isEmpty());
});
it('should return and move by three bytes (regression #1)', function() {
o.push(new Buffer([ 0x1, 0x2 ]));
o.push(new Buffer([ 0x3 ]));
assert.equal(o.readUInt24LE(), 0x030201);
assert.equal(o.buffers.length, 0);
assert(o.isEmpty());
});
});
describe('.readInt24LE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x23, 0x45, 0x81 ]));
assert.equal(o.readInt24LE(), -8305373);
assert(o.isEmpty());
});
});
describe('.readUInt32LE', function() {
it('should return and move by four bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
o.push(new Buffer([ 0x8, 0x9, 0xa ]));
o.push(new Buffer([ 0xb, 0xc, 0xd ]));
o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
assert.equal(o.readUInt32LE(), 0x04030201);
assert.equal(o.readUInt32LE(), 0x08070605);
assert.equal(o.readUInt32LE(), 0x0c0b0a09);
assert.equal(o.readUInt32LE(), 0x100f0e0d);
assert(o.isEmpty());
});
it('should return and move by four bytes (regression #1)', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
o.push(new Buffer([ 0x4 ]));
assert.equal(o.readUInt32LE(), 0x04030201);
assert.equal(o.buffers.length, 0);
assert(o.isEmpty());
});
});
describe('.readInt32LE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
assert.equal(o.readInt32LE(), -1);
assert(o.isEmpty());
});
});
describe('.readUInt16BE', function() {
it('should return and move by two bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3 ]));
o.push(new Buffer([ 0x4, 0x5, 0x6 ]));
assert.equal(o.readUInt16BE(), 0x0102);
assert.equal(o.readUInt16BE(), 0x0304);
assert.equal(o.readUInt16BE(), 0x0506);
assert(o.isEmpty());
});
});
describe('.readInt16BE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x81, 0x23 ]));
assert.equal(o.readInt16BE(), -32477);
assert(o.isEmpty());
});
});
describe('.readUInt24BE', function() {
it('should return and move by three bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5 ]));
o.push(new Buffer([ 0x6, 0x7 ]));
o.push(new Buffer([ 0x8, 0x9 ]));
assert.equal(o.readUInt24BE(), 0x010203);
assert.equal(o.readUInt24BE(), 0x040506);
assert.equal(o.readUInt24BE(), 0x070809);
assert(o.isEmpty());
});
});
describe('.readInt24BE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0x81, 0x45, 0x23 ]));
assert.equal(o.readInt24BE(), -8305373);
assert(o.isEmpty());
});
});
describe('.readUInt32BE', function() {
it('should return and move by four bytes', function() {
o.push(new Buffer([ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 ]));
o.push(new Buffer([ 0x8, 0x9, 0xa ]));
o.push(new Buffer([ 0xb, 0xc, 0xd ]));
o.push(new Buffer([ 0xe, 0xf, 0x10 ]));
assert.equal(o.readUInt32BE(), 0x01020304);
assert.equal(o.readUInt32BE(), 0x05060708);
assert.equal(o.readUInt32BE(), 0x090a0b0c);
assert.equal(o.readUInt32BE(), 0x0d0e0f10);
assert(o.isEmpty());
});
it('should return positive values', function() {
o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
assert.equal(o.readUInt32BE(), 0xffffffff);
assert(o.isEmpty());
});
});
describe('.readInt32BE', function() {
it('should return signed number', function() {
o.push(new Buffer([ 0xff, 0xff, 0xff, 0xff ]));
assert.equal(o.readInt32BE(), -1);
assert(o.isEmpty());
});
});
describe('.has', function() {
it('should properly check the amount of the remaining bytes', function() {
o.push(new Buffer([ 1, 2, 3 ]));
assert(o.has(3));
assert.equal(o.readUInt8(), 0x01);
assert(!o.has(3));
assert(o.has(2));
assert.equal(o.readUInt16BE(), 0x0203);
assert(!o.has(1));
});
});
});