2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 08:34:13 +02:00
|
|
|
*/
|
|
|
|
|
2014-05-17 07:53:58 +02:00
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
|
2014-01-12 08:32:13 +01:00
|
|
|
/*********************************
|
|
|
|
*** Type conversion utilities ***
|
|
|
|
*********************************/
|
2014-01-15 08:46:05 +01:00
|
|
|
// Strings/arrays
|
2014-03-06 22:44:59 +01:00
|
|
|
//TODO: Throw all this shit in favor of consistent types
|
2014-07-21 04:06:04 +02:00
|
|
|
//TODO: Namespace
|
2014-01-12 15:07:13 +01:00
|
|
|
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
2014-01-22 07:23:41 +01:00
|
|
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
2014-03-06 19:18:11 +01:00
|
|
|
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
2014-01-12 08:32:13 +01:00
|
|
|
function getString(thing) {
|
2014-10-20 02:53:17 +02:00
|
|
|
if (thing === Object(thing)) {
|
|
|
|
if (thing.__proto__ == StaticUint8ArrayProto)
|
|
|
|
return String.fromCharCode.apply(null, thing);
|
|
|
|
if (thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return getString(new Uint8Array(thing));
|
|
|
|
if (thing.__proto__ == StaticByteBufferProto)
|
|
|
|
return thing.toString("binary");
|
|
|
|
}
|
|
|
|
return thing;
|
2014-01-12 08:32:13 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:31:15 +01:00
|
|
|
function getStringable(thing) {
|
2014-10-20 02:53:17 +02:00
|
|
|
return (typeof thing == "string" || typeof thing == "number" || typeof thing == "boolean" ||
|
|
|
|
(thing === Object(thing) &&
|
|
|
|
(thing.__proto__ == StaticArrayBufferProto ||
|
|
|
|
thing.__proto__ == StaticUint8ArrayProto ||
|
2014-11-10 05:03:08 +01:00
|
|
|
thing.__proto__ == StaticByteBufferProto)));
|
2014-03-05 02:31:15 +01:00
|
|
|
}
|
|
|
|
|
2014-07-23 07:49:13 +02:00
|
|
|
function isEqual(a, b, mayBeShort) {
|
2014-10-20 02:53:17 +02:00
|
|
|
// TODO: Special-case arraybuffers, etc
|
|
|
|
if (a === undefined || b === undefined)
|
|
|
|
return false;
|
|
|
|
a = getString(a);
|
|
|
|
b = getString(b);
|
|
|
|
var maxLength = mayBeShort ? Math.min(a.length, b.length) : Math.max(a.length, b.length);
|
|
|
|
if (maxLength < 5)
|
|
|
|
throw new Error("a/b compare too short");
|
|
|
|
return a.substring(0, Math.min(maxLength, a.length)) == b.substring(0, Math.min(maxLength, b.length));
|
2014-07-21 04:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:31:15 +01:00
|
|
|
function toArrayBuffer(thing) {
|
2014-10-20 02:53:17 +02:00
|
|
|
//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;
|
2014-03-05 02:31:15 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 00:09:07 +02:00
|
|
|
// Number formatting utils
|
|
|
|
window.textsecure.utils = function() {
|
2014-10-20 02:53:17 +02:00
|
|
|
var self = {};
|
|
|
|
self.unencodeNumber = function(number) {
|
|
|
|
return number.split(".");
|
|
|
|
};
|
|
|
|
|
2015-01-13 22:33:11 +01:00
|
|
|
self.isNumberSane = function(number) {
|
|
|
|
return number[0] == "+" &&
|
|
|
|
/^[0-9]+$/.test(number.substring(1));
|
|
|
|
}
|
|
|
|
|
2014-10-20 02:53:17 +02:00
|
|
|
/**************************
|
|
|
|
*** JSON'ing Utilities ***
|
|
|
|
**************************/
|
|
|
|
function ensureStringed(thing) {
|
|
|
|
if (getStringable(thing))
|
|
|
|
return getString(thing);
|
|
|
|
else if (thing instanceof Array) {
|
|
|
|
var res = [];
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
|
|
|
res[i] = ensureStringed(thing[i]);
|
|
|
|
return res;
|
|
|
|
} else if (thing === Object(thing)) {
|
|
|
|
var res = {};
|
|
|
|
for (var key in thing)
|
|
|
|
res[key] = ensureStringed(thing[key]);
|
|
|
|
return res;
|
2015-04-01 22:08:09 +02:00
|
|
|
} else if (thing === null) {
|
|
|
|
return null;
|
2014-10-20 02:53:17 +02:00
|
|
|
}
|
|
|
|
throw new Error("unsure of how to jsonify object of type " + typeof thing);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jsonThing = function(thing) {
|
|
|
|
return JSON.stringify(ensureStringed(thing));
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2014-05-26 00:45:22 +02:00
|
|
|
}();
|
|
|
|
|