2015-06-01 23:08:21 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
|
|
|
*/
|
2015-06-22 23:45:42 +02:00
|
|
|
|
|
|
|
function ProtoParser(arrayBuffer, protobuf) {
|
|
|
|
this.protobuf = protobuf;
|
2015-06-03 21:51:37 +02:00
|
|
|
this.buffer = new dcodeIO.ByteBuffer();
|
|
|
|
this.buffer.append(arrayBuffer);
|
|
|
|
this.buffer.offset = 0;
|
|
|
|
this.buffer.limit = arrayBuffer.byteLength;
|
2015-06-01 23:08:21 +02:00
|
|
|
}
|
2015-06-22 23:45:42 +02:00
|
|
|
ProtoParser.prototype = {
|
|
|
|
constructor: ProtoParser,
|
|
|
|
next: function() {
|
2015-06-01 23:08:21 +02:00
|
|
|
try {
|
2015-06-18 00:59:14 +02:00
|
|
|
if (this.buffer.limit === this.buffer.offset) {
|
|
|
|
return undefined; // eof
|
|
|
|
}
|
2015-06-22 23:45:42 +02:00
|
|
|
var len = this.buffer.readVarint32();
|
|
|
|
var nextBuffer = this.buffer.slice(
|
|
|
|
this.buffer.offset, this.buffer.offset+len
|
|
|
|
).toArrayBuffer();
|
|
|
|
// TODO: de-dupe ByteBuffer.js includes in libaxo/libts
|
|
|
|
// then remove this toArrayBuffer call.
|
|
|
|
|
|
|
|
var proto = this.protobuf.decode(nextBuffer);
|
2015-06-01 23:08:21 +02:00
|
|
|
this.buffer.skip(len);
|
2015-06-22 23:45:42 +02:00
|
|
|
|
|
|
|
if (proto.avatar) {
|
|
|
|
var attachmentLen = proto.avatar.length;
|
|
|
|
proto.avatar.data = this.buffer.slice(
|
|
|
|
this.buffer.offset, this.buffer.offset + attachmentLen
|
|
|
|
).toArrayBuffer();
|
2015-06-18 00:59:14 +02:00
|
|
|
this.buffer.skip(attachmentLen);
|
|
|
|
}
|
2015-06-01 23:08:21 +02:00
|
|
|
|
2015-06-22 23:45:42 +02:00
|
|
|
return proto;
|
2015-06-01 23:08:21 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-06-22 23:45:42 +02:00
|
|
|
var GroupBuffer = function(arrayBuffer) {
|
|
|
|
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.GroupDetails);
|
|
|
|
};
|
|
|
|
GroupBuffer.prototype = Object.create(ProtoParser.prototype);
|
|
|
|
GroupBuffer.prototype.constructor = GroupBuffer;
|
|
|
|
var ContactBuffer = function(arrayBuffer) {
|
|
|
|
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.ContactDetails);
|
|
|
|
};
|
|
|
|
ContactBuffer.prototype = Object.create(ProtoParser.prototype);
|
|
|
|
ContactBuffer.prototype.constructor = ContactBuffer;
|