Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert WordArrays to ArrayBuffers when msgpack-encoding #143

Merged
merged 2 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions browser/lib/util/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ var BufferUtils = (function() {

function isWordArray(ob) { return ob !== null && ob !== undefined && ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob !== null && ob !== undefined && ob.constructor === ArrayBuffer; }
// msgpack.decode for raw binary data gives a wordArray-like object that isn't a proper wordArray,
// so doesn't have a wordArray.clamp() method
function ensureProperWordArray(wordArray) {
return (wordArray.clamp !== undefined ? wordArray : WordArray.create(wordArray.words));
}

// https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js
function arrayBufferToBase64(ArrayBuffer) {
Expand Down Expand Up @@ -81,6 +76,29 @@ var BufferUtils = (function() {

BufferUtils.isBuffer = function(buf) { return isArrayBuffer(buf) || isWordArray(buf); };

BufferUtils.toArrayBuffer = function(buf) {
if(!ArrayBuffer)
throw new Error("Can't convert to ArrayBuffer: ArrayBuffer not supported");

if(isArrayBuffer(buf))
return buf;

if(isWordArray(buf)) {
/* Backported from unreleased CryptoJS
* https://code.google.com/p/crypto-js/source/browse/branches/3.x/src/lib-typedarrays.js?r=661 */
var arrayBuffer = new ArrayBuffer(buf.sigBytes);
var uint8View = new Uint8Array(arrayBuffer);

for (var i = 0; i < buf.sigBytes; i++) {
uint8View[i] = (buf.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still isn't great efficiency-wise, because we should be writing words into a unit32View, but we can leave this and file a lower-priority issue to improve it unless you want to do it now anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is definitely not worth optimising now, let's please land this as it works and performance of this obscure requirement for now is not a high priority

}

return arrayBuffer;
};

throw new Error("BufferUtils.toArrayBuffer expected a buffer");
};

BufferUtils.toWordArray = function(buf) {
return isWordArray(buf) ? buf : WordArray.create(buf);
};
Expand All @@ -89,7 +107,7 @@ var BufferUtils = (function() {
if(isArrayBuffer(buf))
return arrayBufferToBase64(buf);
if(isWordArray(buf))
return CryptoJS.enc.Base64.stringify(ensureProperWordArray(buf));
return CryptoJS.enc.Base64.stringify(buf);
};

BufferUtils.base64Decode = function(str) {
Expand Down
15 changes: 11 additions & 4 deletions common/lib/types/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ var Message = (function() {
* although msgpack calls toJSON(), we know it is a stringify()
* call if it has a non-empty arguments list */
var data = this.data;
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
if(data && BufferUtils.isBuffer(data)) {
if(arguments.length > 0) {
/* stringify call */
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
} else {
/* Called by msgpack. Need to feed it an ArrayBuffer, msgpack doesn't
* understand WordArrays */
data = BufferUtils.toArrayBuffer(data);
}
}
result.data = data;
return result;
Expand Down
19 changes: 13 additions & 6 deletions common/lib/types/presencemessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ var PresenceMessage = (function() {
encoding: this.encoding
};

/* encode to base64 if we're returning real JSON;
/* encode data to base64 if present and we're returning real JSON;
* although msgpack calls toJSON(), we know it is a stringify()
* call if it passes on the stringify arguments */
* call if it has a non-empty arguments list */
var data = this.data;
if(data && arguments.length > 0 && BufferUtils.isBuffer(data)) {
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = data.toString('base64');
if(data && BufferUtils.isBuffer(data)) {
if(arguments.length > 0) {
/* stringify call */
var encoding = this.encoding;
result.encoding = encoding ? (encoding + '/base64') : 'base64';
data = BufferUtils.base64Encode(data);
} else {
/* Called by msgpack. Need to feed it an ArrayBuffer, msgpack doesn't
* understand WordArrays */
data = BufferUtils.toArrayBuffer(data);
}
}
result.data = data;
return result;
Expand Down
2 changes: 2 additions & 0 deletions nodejs/lib/util/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ this.BufferUtils = (function() {

BufferUtils.isBuffer = Buffer.isBuffer;

BufferUtils.toArrayBuffer = function(buf) { return buf; };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I like the name of this, but it's not better than mightBeWordArrayOrBufferToBuffer.


BufferUtils.base64Encode = function(buf) { return buf.toString('base64'); };

BufferUtils.base64Decode = function(string) { return new Buffer(string, 'base64'); };
Expand Down