contacts_parser.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. function ProtoParser(arrayBuffer, protobuf) {
  5. this.protobuf = protobuf;
  6. this.buffer = new dcodeIO.ByteBuffer();
  7. this.buffer.append(arrayBuffer);
  8. this.buffer.offset = 0;
  9. this.buffer.limit = arrayBuffer.byteLength;
  10. }
  11. ProtoParser.prototype = {
  12. constructor: ProtoParser,
  13. next: function() {
  14. try {
  15. if (this.buffer.limit === this.buffer.offset) {
  16. return undefined; // eof
  17. }
  18. var len = this.buffer.readVarint32();
  19. var nextBuffer = this.buffer.slice(
  20. this.buffer.offset, this.buffer.offset+len
  21. ).toArrayBuffer();
  22. // TODO: de-dupe ByteBuffer.js includes in libaxo/libts
  23. // then remove this toArrayBuffer call.
  24. var proto = this.protobuf.decode(nextBuffer);
  25. this.buffer.skip(len);
  26. if (proto.avatar) {
  27. var attachmentLen = proto.avatar.length;
  28. proto.avatar.data = this.buffer.slice(
  29. this.buffer.offset, this.buffer.offset + attachmentLen
  30. ).toArrayBuffer();
  31. this.buffer.skip(attachmentLen);
  32. }
  33. return proto;
  34. } catch(e) {
  35. console.log(e);
  36. }
  37. }
  38. };
  39. var GroupBuffer = function(arrayBuffer) {
  40. ProtoParser.call(this, arrayBuffer, textsecure.protobuf.GroupDetails);
  41. };
  42. GroupBuffer.prototype = Object.create(ProtoParser.prototype);
  43. GroupBuffer.prototype.constructor = GroupBuffer;
  44. var ContactBuffer = function(arrayBuffer) {
  45. ProtoParser.call(this, arrayBuffer, textsecure.protobuf.ContactDetails);
  46. };
  47. ContactBuffer.prototype = Object.create(ProtoParser.prototype);
  48. ContactBuffer.prototype.constructor = ContactBuffer;