diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 11785e00..488c84e4 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -36107,32 +36107,6 @@ function getStringable(thing) { thing.__proto__ == StaticByteBufferProto))); } -function toArrayBuffer(thing) { - //TODO: Optimize this for specific cases - if (thing === undefined) - return undefined; - if (thing === Object(thing) && thing.__proto__ == StaticArrayBufferProto) - return thing; - - if (thing instanceof Array) { - // Assuming Uint16Array from curve25519 - var res = new ArrayBuffer(thing.length * 2); - var uint = new Uint16Array(res); - for (var i = 0; i < thing.length; i++) - uint[i] = thing[i]; - return res; - } - - if (!getStringable(thing)) - throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer"); - var str = getString(thing); - var res = new ArrayBuffer(str.length); - var uint = new Uint8Array(res); - for (var i = 0; i < str.length; i++) - uint[i] = str.charCodeAt(i); - return res; -} - // Number formatting utils window.textsecure.utils = function() { var self = {}; @@ -37525,6 +37499,18 @@ OutgoingMessage.prototype = { * vim: ts=4:sw=4:expandtab */ +function stringToArrayBuffer(str) { + if (typeof str !== 'string') { + throw new Error('Passed non-string to stringToArrayBuffer'); + } + var res = new ArrayBuffer(str.length); + var uint = new Uint8Array(res); + for (var i = 0; i < str.length; i++) { + uint[i] = str.charCodeAt(i); + } + return res; +} + function Message(options) { this.body = options.body; this.attachments = options.attachments || []; @@ -37591,7 +37577,7 @@ Message.prototype = { } if (this.group) { proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(this.group.id); + proto.group.id = stringToArrayBuffer(this.group.id); proto.group.type = this.group.type } @@ -37847,7 +37833,7 @@ MessageSender.prototype = { proto.group = new textsecure.protobuf.GroupContext(); return textsecure.storage.groups.createNewGroup(numbers).then(function(group) { - proto.group.id = toArrayBuffer(group.id); + proto.group.id = stringToArrayBuffer(group.id); var numbers = group.numbers; proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; @@ -37867,7 +37853,7 @@ MessageSender.prototype = { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; proto.group.name = name; @@ -37889,7 +37875,7 @@ MessageSender.prototype = { addNumberToGroup: function(groupId, number) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; return textsecure.storage.groups.addNumbers(groupId, [number]).then(function(numbers) { @@ -37904,7 +37890,7 @@ MessageSender.prototype = { setGroupName: function(groupId, name) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; proto.group.name = name; @@ -37920,7 +37906,7 @@ MessageSender.prototype = { setGroupAvatar: function(groupId, avatar) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) { @@ -37938,7 +37924,7 @@ MessageSender.prototype = { leaveGroup: function(groupId) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.QUIT; return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) { diff --git a/libtextsecure/helpers.js b/libtextsecure/helpers.js index f08db68e..c1d5e5bd 100644 --- a/libtextsecure/helpers.js +++ b/libtextsecure/helpers.js @@ -33,32 +33,6 @@ function getStringable(thing) { thing.__proto__ == StaticByteBufferProto))); } -function toArrayBuffer(thing) { - //TODO: Optimize this for specific cases - if (thing === undefined) - return undefined; - if (thing === Object(thing) && thing.__proto__ == StaticArrayBufferProto) - return thing; - - if (thing instanceof Array) { - // Assuming Uint16Array from curve25519 - var res = new ArrayBuffer(thing.length * 2); - var uint = new Uint16Array(res); - for (var i = 0; i < thing.length; i++) - uint[i] = thing[i]; - return res; - } - - if (!getStringable(thing)) - throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer"); - var str = getString(thing); - var res = new ArrayBuffer(str.length); - var uint = new Uint8Array(res); - for (var i = 0; i < str.length; i++) - uint[i] = str.charCodeAt(i); - return res; -} - // Number formatting utils window.textsecure.utils = function() { var self = {}; diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index f05eb258..d062665c 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -2,6 +2,18 @@ * vim: ts=4:sw=4:expandtab */ +function stringToArrayBuffer(str) { + if (typeof str !== 'string') { + throw new Error('Passed non-string to stringToArrayBuffer'); + } + var res = new ArrayBuffer(str.length); + var uint = new Uint8Array(res); + for (var i = 0; i < str.length; i++) { + uint[i] = str.charCodeAt(i); + } + return res; +} + function Message(options) { this.body = options.body; this.attachments = options.attachments || []; @@ -68,7 +80,7 @@ Message.prototype = { } if (this.group) { proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(this.group.id); + proto.group.id = stringToArrayBuffer(this.group.id); proto.group.type = this.group.type } @@ -324,7 +336,7 @@ MessageSender.prototype = { proto.group = new textsecure.protobuf.GroupContext(); return textsecure.storage.groups.createNewGroup(numbers).then(function(group) { - proto.group.id = toArrayBuffer(group.id); + proto.group.id = stringToArrayBuffer(group.id); var numbers = group.numbers; proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; @@ -344,7 +356,7 @@ MessageSender.prototype = { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; proto.group.name = name; @@ -366,7 +378,7 @@ MessageSender.prototype = { addNumberToGroup: function(groupId, number) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; return textsecure.storage.groups.addNumbers(groupId, [number]).then(function(numbers) { @@ -381,7 +393,7 @@ MessageSender.prototype = { setGroupName: function(groupId, name) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; proto.group.name = name; @@ -397,7 +409,7 @@ MessageSender.prototype = { setGroupAvatar: function(groupId, avatar) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE; return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) { @@ -415,7 +427,7 @@ MessageSender.prototype = { leaveGroup: function(groupId) { var proto = new textsecure.protobuf.DataMessage(); proto.group = new textsecure.protobuf.GroupContext(); - proto.group.id = toArrayBuffer(groupId); + proto.group.id = stringToArrayBuffer(groupId); proto.group.type = textsecure.protobuf.GroupContext.Type.QUIT; return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) { diff --git a/libtextsecure/test/helpers_test.js b/libtextsecure/test/helpers_test.js index 5deea254..7c883b27 100644 --- a/libtextsecure/test/helpers_test.js +++ b/libtextsecure/test/helpers_test.js @@ -16,20 +16,19 @@ describe("Helpers", function() { }); }); - describe("toArrayBuffer", function() { - it('returns undefined when passed undefined', function() { - assert.strictEqual(toArrayBuffer(undefined), undefined); - }); - it('returns ArrayBuffer when passed ArrayBuffer', function() { + describe("stringToArrayBuffer", function() { + it('returns ArrayBuffer when passed string', function() { var StaticArrayBufferProto = new ArrayBuffer().__proto__; - var anArrayBuffer = new ArrayBuffer(); - assert.strictEqual(toArrayBuffer(anArrayBuffer), anArrayBuffer); + var anArrayBuffer = new ArrayBuffer(1); + var typedArray = new Uint8Array(anArrayBuffer); + typedArray[0] = 'a'.charCodeAt(0); + assertEqualArrayBuffers(stringToArrayBuffer('a'), anArrayBuffer); }); - it('throws an error when passed a non Stringable thing', function() { - var madeUpObject = function() {}; - var notStringable = new madeUpObject(); - assert.throw(function() { toArrayBuffer(notStringable) }, - Error, /Tried to convert a non-stringable thing/); + it('throws an error when passed a non string', function() { + var notStringable = [{}, undefined, null, new ArrayBuffer()]; + notStringable.forEach(function(notString) { + assert.throw(function() { stringToArrayBuffer(notString) }, Error); + }); }); }); });