2018-07-21 21:51:20 +00:00
|
|
|
/* global dcodeIO, window, textsecure */
|
|
|
|
|
2015-06-22 21:45:42 +00:00
|
|
|
function ProtoParser(arrayBuffer, protobuf) {
|
2018-05-02 01:54:43 +00:00
|
|
|
this.protobuf = protobuf;
|
|
|
|
this.buffer = new dcodeIO.ByteBuffer();
|
|
|
|
this.buffer.append(arrayBuffer);
|
|
|
|
this.buffer.offset = 0;
|
|
|
|
this.buffer.limit = arrayBuffer.byteLength;
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-06-22 21:45:42 +00:00
|
|
|
ProtoParser.prototype = {
|
2018-05-02 01:54:43 +00:00
|
|
|
constructor: ProtoParser,
|
2018-07-21 21:51:20 +00:00
|
|
|
next() {
|
2018-05-02 01:54:43 +00:00
|
|
|
try {
|
|
|
|
if (this.buffer.limit === this.buffer.offset) {
|
|
|
|
return undefined; // eof
|
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
const len = this.buffer.readVarint32();
|
|
|
|
const nextBuffer = this.buffer
|
2018-05-02 01:54:43 +00:00
|
|
|
.slice(this.buffer.offset, this.buffer.offset + len)
|
|
|
|
.toArrayBuffer();
|
|
|
|
// TODO: de-dupe ByteBuffer.js includes in libaxo/libts
|
|
|
|
// then remove this toArrayBuffer call.
|
2015-06-22 21:45:42 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
const proto = this.protobuf.decode(nextBuffer);
|
2018-05-02 01:54:43 +00:00
|
|
|
this.buffer.skip(len);
|
2015-06-22 21:45:42 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
if (proto.avatar) {
|
2018-07-21 21:51:20 +00:00
|
|
|
const attachmentLen = proto.avatar.length;
|
2018-05-02 01:54:43 +00:00
|
|
|
proto.avatar.data = this.buffer
|
|
|
|
.slice(this.buffer.offset, this.buffer.offset + attachmentLen)
|
|
|
|
.toArrayBuffer();
|
|
|
|
this.buffer.skip(attachmentLen);
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
if (proto.profileKey) {
|
|
|
|
proto.profileKey = proto.profileKey.toArrayBuffer();
|
|
|
|
}
|
2017-09-11 16:50:35 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return proto;
|
2018-07-21 19:00:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'ProtoParser.next error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
|
|
|
|
return null;
|
2018-05-02 01:54:43 +00:00
|
|
|
},
|
2015-06-01 21:08:21 +00:00
|
|
|
};
|
2018-07-21 21:51:20 +00:00
|
|
|
const GroupBuffer = function Constructor(arrayBuffer) {
|
2018-05-02 01:54:43 +00:00
|
|
|
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.GroupDetails);
|
2015-06-22 21:45:42 +00:00
|
|
|
};
|
|
|
|
GroupBuffer.prototype = Object.create(ProtoParser.prototype);
|
|
|
|
GroupBuffer.prototype.constructor = GroupBuffer;
|
2018-07-21 21:51:20 +00:00
|
|
|
const ContactBuffer = function Constructor(arrayBuffer) {
|
2018-05-02 01:54:43 +00:00
|
|
|
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.ContactDetails);
|
2015-06-22 21:45:42 +00:00
|
|
|
};
|
|
|
|
ContactBuffer.prototype = Object.create(ProtoParser.prototype);
|
|
|
|
ContactBuffer.prototype.constructor = ContactBuffer;
|