How to use the binary.Buffer function in binary

To help you get started, we’ve selected a few binary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ondras / TeaJS / lib / websocket.js View on Github external
Server.prototype._receive = function(socket) {
	var received = new Buffer(0);
	do {
		var part = socket.receive(1024);
		if (!part) { break; } /* no more available (would block) */
		if (!part.length) { throw new Error("Connection closed by remote side"); }
		var tmp = new Buffer(received.length + part.length);
		tmp.copyFrom(received);
		tmp.copyFrom(part, 0, received.length);
		received = tmp;
	} while (part.length > 0);
	
	return received;
}
github ondras / TeaJS / lib / http.js View on Github external
/* fetch hex number at the beginning of each chunk. this is terminated by \r\n */
		while (index < body.length) {
			num = body[index];
			if (num == 13) { break; }
			hex += String.fromCharCode(num);
			index++; 
		}
		
		/* skip CRLF */
		index += 2; 
		
		var chunkLength = parseInt(hex, 16);
		if (!chunkLength) { break; }

		/* read the chunk */
		var tmp = new Buffer(result.length + chunkLength);
		tmp.copyFrom(result);
		tmp.copyFrom(body, index, result.length);
		result = tmp;
		index += chunkLength;
		
		/* skip CRLF after chunk */
		index += 2;
	}

	return result;
}
github ondras / TeaJS / lib / websocket.js View on Github external
frame.opcode = 0x2;
		frame.payload = data;
	} else {
		frame.opcode = 0x1;
		frame.payload = new Buffer(data, "utf-8");
	}
	this._debug("["+id+"] sending " + frame.payload.length + " bytes");

	var buffer = frame.toBuffer();
	var remain = buffer.length;
	while (1) {
		var sent = this._clients[id].socket.send(buffer); 
		if (sent === false) { continue; } /* blocked */
		remain -= sent;
		if (!remain) { break; }
		var newBuffer = new Buffer(remain);
		newBuffer.copyFrom(buffer, sent);
		buffer = newBuffer;
	}
}
github ondras / TeaJS / lib / websocket.js View on Github external
Server.prototype.send = function(id, data) {
	if (!(id in this._clients)) { return; }
	
	var frame = new Frame();
	if (data instanceof Buffer) {
		frame.opcode = 0x2;
		frame.payload = data;
	} else {
		frame.opcode = 0x1;
		frame.payload = new Buffer(data, "utf-8");
	}
	this._debug("["+id+"] sending " + frame.payload.length + " bytes");

	var buffer = frame.toBuffer();
	var remain = buffer.length;
	while (1) {
		var sent = this._clients[id].socket.send(buffer); 
		if (sent === false) { continue; } /* blocked */
		remain -= sent;
		if (!remain) { break; }
		var newBuffer = new Buffer(remain);
		newBuffer.copyFrom(buffer, sent);
		buffer = newBuffer;
	}
}
github ondras / TeaJS / lib / http.js View on Github external
ClientResponse.prototype._parseChunked = function(body) {
	var Buffer = require("binary").Buffer;
	
	var index = 0
	var num, hex;
	var result = new Buffer(0);
	
	while (index < body.length) {
		hex = "";
		
		/* fetch hex number at the beginning of each chunk. this is terminated by \r\n */
		while (index < body.length) {
			num = body[index];
			if (num == 13) { break; }
			hex += String.fromCharCode(num);
			index++; 
		}
		
		/* skip CRLF */
		index += 2; 
		
		var chunkLength = parseInt(hex, 16);
github ondras / TeaJS / lib / websocket.js View on Github external
if (lengthBytes == 8 && value && (i < 4)) { throw new ProtocolError(1009, "Payload size larger than 4 bytes not supported"); }
			this.payloadLength += value * Math.pow(2, 8*(lengthBytes-i-1));
		}

		this.state = Frame.STATE_WAIT_PAYLOAD;
	}
	
	/* need mask + payload */
	if (this.state == Frame.STATE_WAIT_PAYLOAD) {
		var bytes = 4 + this.payloadLength; /* mask + payload */
		if (buffer.length-offset < bytes) { return offset-bufferOffset; }
		
		var maskOffset = offset;
		offset += 4;
		
		this.payload = new Buffer(this.payloadLength);
		for (var i=0;i
github ondras / TeaJS / lib / hash.js View on Github external
exports.md5 = function(input) {
	if (!(input instanceof Buffer)) { input = new Buffer(input.toString(), "utf-8"); }

	var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
	var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */

	/*
	* Calculate the MD5 of an array of little-endian words, and a bit length
	*/
	function core_md5(x, len) {
		/* append padding */
		x[len >> 5] |= 0x80 << ((len) % 32);
		x[(((len + 64) >>> 9) << 4) + 14] = len;

		var a =  1732584193;
		var b = -271733879;
		var c = -1732584194;
		var d =  271733878;
github ondras / TeaJS / lib / base64.js View on Github external
exports.decode = function(input) {
	if (!(input instanceof Buffer)) { input = new Buffer(input, "utf-8"); }

	var output = new Buffer(Math.ceil(3 * input.length / 4));
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i=0, j=0;
	
	try {

		do {
			enc1 = alphabetBuffer.indexOf(input[i++]);
			enc2 = alphabetBuffer.indexOf(input[i++]);
			enc3 = alphabetBuffer.indexOf(input[i++]);
			enc4 = alphabetBuffer.indexOf(input[i++]);

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output[j++] = chr1;
github ondras / TeaJS / lib / base64.js View on Github external
exports.encode = function(input) {
	if (!(input instanceof Buffer)) { input = new Buffer(input, "utf-8"); }

	var output = new Buffer(4 * Math.ceil(input.length / 3));
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i=0, j=0;
	do {
		chr1 = (i < input.length ? input[i++] : NaN);
		chr2 = (i < input.length ? input[i++] : NaN);
		chr3 = (i < input.length ? input[i++] : NaN);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) { 
			enc3 = enc4 = 64;
github ondras / TeaJS / lib / base64.js View on Github external
exports.encode = function(input) {
	if (!(input instanceof Buffer)) { input = new Buffer(input, "utf-8"); }

	var output = new Buffer(4 * Math.ceil(input.length / 3));
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i=0, j=0;
	do {
		chr1 = (i < input.length ? input[i++] : NaN);
		chr2 = (i < input.length ? input[i++] : NaN);
		chr3 = (i < input.length ? input[i++] : NaN);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) { 
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;