forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts_parser.js
52 lines (48 loc) · 1.72 KB
/
contacts_parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* vim: ts=4:sw=4:expandtab
*/
function ProtoParser(arrayBuffer, protobuf) {
this.protobuf = protobuf;
this.buffer = new dcodeIO.ByteBuffer();
this.buffer.append(arrayBuffer);
this.buffer.offset = 0;
this.buffer.limit = arrayBuffer.byteLength;
}
ProtoParser.prototype = {
constructor: ProtoParser,
next: function() {
try {
if (this.buffer.limit === this.buffer.offset) {
return undefined; // eof
}
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);
this.buffer.skip(len);
if (proto.avatar) {
var attachmentLen = proto.avatar.length;
proto.avatar.data = this.buffer.slice(
this.buffer.offset, this.buffer.offset + attachmentLen
).toArrayBuffer();
this.buffer.skip(attachmentLen);
}
return proto;
} catch(e) {
console.log(e);
}
}
};
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;