Commit f48bfd7c301e9d07c8ade896bb9a452f0f17771d

Authored by OpenWapp Developer
1 parent d011342874

Receiving images is now more efficient.

Showing 4 changed files with 21 additions and 55 deletions

src/common/utils.js View file @ f48bfd7
... ... @@ -312,15 +312,11 @@
312 312 }
313 313  
314 314 function latin1FromBytes(buffer) {
315   - var result = '';
316   - var l = buffer.length;
317   - for (var i = 0; i < l; i++) {
318   - result = result + String.fromCharCode(buffer[i]);
  315 + var charArray = [];
  316 + for (var i = 0, l = buffer.length; i < l; i++) {
  317 + charArray[i] = String.fromCharCode(buffer[i]);
319 318 }
320   - return result;
321   - // was
322   - // return String.fromCharCode.apply(null, buffer);
323   - // but for some unknown reason, this is way slower...
  319 + return charArray.join('');
324 320 }
325 321  
326 322 var utils = {
src/protocol/binary_reader.js View file @ f48bfd7
... ... @@ -390,7 +390,7 @@
390 390 }
391 391  
392 392 // Read tag.
393   - var tag = this.readString(stringMark);
  393 + var tag = this.readString(stringMark).toString();
394 394 if (listSize === 0 || tag === null)
395 395 throw new SyntaxError('0 list or null tag!');
396 396  
... ... @@ -508,8 +508,8 @@
508 508 BinaryReader.prototype.readAttributes = function(attributeCount) {
509 509 var key, value, attributes = {};
510 510 while (attributeCount > 0) {
511   - key = this.readString(this.message.read());
512   - value = this.readString(this.message.read());
  511 + key = this.readString(this.message.read()).toString();
  512 + value = this.readString(this.message.read()).toString();
513 513 attributes[key] = value;
514 514 attributeCount--;
515 515 }
516 516  
... ... @@ -526,8 +526,10 @@
526 526 /**
527 527 * Parses a string from the message buffer.
528 528 */
529   - BinaryReader.prototype.readString = function(stringMark, returnRaw) {
  529 + BinaryReader.prototype.readString = function(stringMark) {
530 530 var string = null;
  531 + var buffer = null;
  532 + var bufferToString = CoSeMe.utils.latin1FromBytes;
531 533  
532 534 if (stringMark === 0) {
533 535 string = '';
534 536  
535 537  
536 538  
537 539  
... ... @@ -545,21 +547,19 @@
545 547 // Short 8-bit length string.
546 548 } else if (stringMark === SHORT_STRING_MARK) {
547 549 var size = this.readInt8();
548   - var buffer = new Uint8Array(size)
  550 + buffer = new Uint8Array(size)
549 551 this.fillArray(buffer, size);
550   - string = { hexdata: this.bufferToString(buffer) };
551 552  
552 553 // Long 24-bit length string.
553 554 } else if (stringMark === LONG_STRING_MARK) {
554 555 var size = this.readInt24();
555   - var buffer = new Uint8Array(size)
  556 + buffer = new Uint8Array(size)
556 557 this.fillArray(buffer, size);
557   - string = { hexdata: this.bufferToString(buffer) };
558 558  
559 559 // Jabber ID.
560 560 } else if (stringMark === JID_MARK) {
561   - var user = this.readString(this.message.read(), true);
562   - var server = this.readString(this.message.read(), true);
  561 + var user = this.readString(this.message.read(), true).toString();
  562 + var server = this.readString(this.message.read(), true).toString();
563 563 if (user && server) {
564 564 string = user + '@' + server;
565 565 }
... ... @@ -573,11 +573,9 @@
573 573 throw new SyntaxError('could not find a string.');
574 574 }
575 575  
576   - if (returnRaw && string && string.hexdata) {
577   - string = CryptoJS.enc.Latin1.stringify(CryptoJS.enc.Hex.parse(string.hexdata));
578   - }
579   -
580   - return string;
  576 + return buffer ? { buffer: buffer, toString: function () {
  577 + return bufferToString(this.buffer);
  578 + }} : string;
581 579 };
582 580  
583 581 /**
... ... @@ -599,26 +597,6 @@
599 597 for (var i = 0; i < length; i++) {
600 598 buffer[i] = this.message.read();
601 599 }
602   - };
603   -
604   - /**
605   - * Converts a byte buffer to a string. Returns it in Hex form
606   - */
607   - BinaryReader.prototype.bufferToString = function(buffer) {
608   - var charArray = [];
609   - var c;
610   -/* for (var i = 0, l = buffer.length; i < l; i++) {
611   - charArray.push(String.fromCharCode(buffer[i]));
612   - }
613   -*/
614   - for (var i = 0, l = buffer.length; i < l; i++) {
615   - c = Number(buffer[i]).toString(16);
616   - if (buffer[i] < 16)
617   - c = '0' + c;
618   - charArray.push(c);
619   - }
620   -
621   - return charArray.join('');
622 600 };
623 601  
624 602 return BinaryReader;
src/protocol/tree.js View file @ f48bfd7
... ... @@ -60,10 +60,7 @@
60 60  
61 61 Object.defineProperty(Tree.prototype, "data", {
62 62 get: function() {
63   - var value = this._data;
64   - if (value && value.hexdata) {
65   - value = CryptoJS.enc.Latin1.stringify(CryptoJS.enc.Hex.parse(value.hexdata));
66   - };
  63 + var value = this._data && this._data.toString();
67 64 return value;
68 65 },
69 66 set: function(data) {
70 67  
... ... @@ -71,9 +68,9 @@
71 68 }
72 69 });
73 70  
74   - Object.defineProperty(Tree.prototype, "hexData", {
  71 + Object.defineProperty(Tree.prototype, "dataBytes", {
75 72 get: function() {
76   - return this._data && this._data.hexdata;
  73 + return this._data && this._data.buffer || null;
77 74 }
78 75 });
79 76  
... ... @@ -82,11 +79,6 @@
82 79 return null;
83 80  
84 81 var value = this.attributes[attributeName];
85   - if (value && value.hexdata) {
86   - value = getHex ? value.hexdata :
87   - CryptoJS.enc.Latin1.stringify(CryptoJS.enc.Hex.parse(value.hexdata));
88   - };
89   -
90 82 return typeof value !== 'undefined' ? value : null;
91 83 };
92 84  
src/yowsup_if.js View file @ f48bfd7
... ... @@ -590,7 +590,7 @@
590 590 }
591 591 var pictureNode = node.getChild("picture");
592 592 if (pictureNode) {
593   - var picture = CoSeMe.utils.latin1ToBlob(pictureNode.data, 'image/jpeg');
  593 + var picture = new Blob([pictureNode.dataBytes], {type: 'image/jpeg'});
594 594 var pictureId = Number(pictureNode.getAttributeValue('id')).valueOf();
595 595 if (isGroup) {
596 596 _signalInterface.send("group_gotPicture", [jid, pictureId, picture]);