| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) { | 561 jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) { |
| 562 var result = new Array(hashes.length); | 562 var result = new Array(hashes.length); |
| 563 for (var i = 0; i < hashes.length; i++) { | 563 for (var i = 0; i < hashes.length; i++) { |
| 564 result[i] = jspb.utils.hash64ToDecimalString(hashes[i], signed); | 564 result[i] = jspb.utils.hash64ToDecimalString(hashes[i], signed); |
| 565 } | 565 } |
| 566 return result; | 566 return result; |
| 567 }; | 567 }; |
| 568 | 568 |
| 569 | 569 |
| 570 /** | 570 /** |
| 571 * Converts a signed or unsigned decimal string into its hash string |
| 572 * representation. |
| 573 * @param {string} dec |
| 574 * @return {string} |
| 575 */ |
| 576 jspb.utils.decimalStringToHash64 = function(dec) { |
| 577 goog.asserts.assert(dec.length > 0); |
| 578 |
| 579 // Check for minus sign. |
| 580 var minus = false; |
| 581 if (dec[0] === '-') { |
| 582 minus = true; |
| 583 dec = dec.slice(1); |
| 584 } |
| 585 |
| 586 // Store result as a byte array. |
| 587 var resultBytes = [0, 0, 0, 0, 0, 0, 0, 0]; |
| 588 |
| 589 // Set result to m*result + c. |
| 590 function muladd(m, c) { |
| 591 for (var i = 0; i < 8 && (m !== 1 || c > 0); i++) { |
| 592 var r = m * resultBytes[i] + c; |
| 593 resultBytes[i] = r & 0xFF; |
| 594 c = r >>> 8; |
| 595 } |
| 596 } |
| 597 |
| 598 // Negate the result bits. |
| 599 function neg() { |
| 600 for (var i = 0; i < 8; i++) { |
| 601 resultBytes[i] = (~resultBytes[i]) & 0xFF; |
| 602 } |
| 603 } |
| 604 |
| 605 // For each decimal digit, set result to 10*result + digit. |
| 606 for (var i = 0; i < dec.length; i++) { |
| 607 muladd(10, jspb.utils.DIGITS.indexOf(dec[i])); |
| 608 } |
| 609 |
| 610 // If there's a minus sign, convert into two's complement. |
| 611 if (minus) { |
| 612 neg(); |
| 613 muladd(1, 1); |
| 614 } |
| 615 |
| 616 return String.fromCharCode.apply(null, resultBytes); |
| 617 }; |
| 618 |
| 619 |
| 620 /** |
| 571 * Converts an 8-character hash string into its hexadecimal representation. | 621 * Converts an 8-character hash string into its hexadecimal representation. |
| 572 * @param {string} hash | 622 * @param {string} hash |
| 573 * @return {string} | 623 * @return {string} |
| 574 */ | 624 */ |
| 575 jspb.utils.hash64ToHexString = function(hash) { | 625 jspb.utils.hash64ToHexString = function(hash) { |
| 576 var temp = new Array(18); | 626 var temp = new Array(18); |
| 577 temp[0] = '0'; | 627 temp[0] = '0'; |
| 578 temp[1] = 'x'; | 628 temp[1] = 'x'; |
| 579 | 629 |
| 580 for (var i = 0; i < 8; i++) { | 630 for (var i = 0; i < 8; i++) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 } | 882 } |
| 833 | 883 |
| 834 // Advance the cursor past the blob. | 884 // Advance the cursor past the blob. |
| 835 cursor += length; | 885 cursor += length; |
| 836 } | 886 } |
| 837 return count; | 887 return count; |
| 838 }; | 888 }; |
| 839 | 889 |
| 840 | 890 |
| 841 /** | 891 /** |
| 842 * Clones a scalar field. Pulling this out to a helper method saves us a few | |
| 843 * bytes of generated code. | |
| 844 * @param {Array} array | |
| 845 * @return {Array} | |
| 846 */ | |
| 847 jspb.utils.cloneRepeatedScalarField = function(array) { | |
| 848 return array ? array.slice() : null; | |
| 849 }; | |
| 850 | |
| 851 | |
| 852 /** | |
| 853 * Clones an array of messages using the provided cloner function. | |
| 854 * @param {Array.<jspb.BinaryMessage>} messages | |
| 855 * @param {jspb.ClonerFunction} cloner | |
| 856 * @return {Array.<jspb.BinaryMessage>} | |
| 857 */ | |
| 858 jspb.utils.cloneRepeatedMessageField = function(messages, cloner) { | |
| 859 if (messages === null) return null; | |
| 860 var result = []; | |
| 861 for (var i = 0; i < messages.length; i++) { | |
| 862 result.push(cloner(messages[i])); | |
| 863 } | |
| 864 return result; | |
| 865 }; | |
| 866 | |
| 867 | |
| 868 /** | |
| 869 * Clones an array of byte blobs. | |
| 870 * @param {Array.<Uint8Array>} blobs | |
| 871 * @return {Array.<Uint8Array>} | |
| 872 */ | |
| 873 jspb.utils.cloneRepeatedBlobField = function(blobs) { | |
| 874 if (blobs === null) return null; | |
| 875 var result = []; | |
| 876 for (var i = 0; i < blobs.length; i++) { | |
| 877 result.push(new Uint8Array(blobs[i])); | |
| 878 } | |
| 879 return result; | |
| 880 }; | |
| 881 | |
| 882 | |
| 883 /** | |
| 884 * String-ify bytes for text format. Should be optimized away in non-debug. | 892 * String-ify bytes for text format. Should be optimized away in non-debug. |
| 885 * The returned string uses \xXX escapes for all values and is itself quoted. | 893 * The returned string uses \xXX escapes for all values and is itself quoted. |
| 886 * [1, 31] serializes to '"\x01\x1f"'. | 894 * [1, 31] serializes to '"\x01\x1f"'. |
| 887 * @param {jspb.ByteSource} byteSource The bytes to serialize. | 895 * @param {jspb.ByteSource} byteSource The bytes to serialize. |
| 888 * @param {boolean=} opt_stringIsRawBytes The string is interpreted as a series | |
| 889 * of raw bytes rather than base64 data. | |
| 890 * @return {string} Stringified bytes for text format. | 896 * @return {string} Stringified bytes for text format. |
| 891 */ | 897 */ |
| 892 jspb.utils.debugBytesToTextFormat = function(byteSource, | 898 jspb.utils.debugBytesToTextFormat = function(byteSource) { |
| 893 opt_stringIsRawBytes) { | |
| 894 var s = '"'; | 899 var s = '"'; |
| 895 if (byteSource) { | 900 if (byteSource) { |
| 896 var bytes = | 901 var bytes = jspb.utils.byteSourceToUint8Array(byteSource); |
| 897 jspb.utils.byteSourceToUint8Array(byteSource, opt_stringIsRawBytes); | |
| 898 for (var i = 0; i < bytes.length; i++) { | 902 for (var i = 0; i < bytes.length; i++) { |
| 899 s += '\\x'; | 903 s += '\\x'; |
| 900 if (bytes[i] < 16) s += '0'; | 904 if (bytes[i] < 16) s += '0'; |
| 901 s += bytes[i].toString(16); | 905 s += bytes[i].toString(16); |
| 902 } | 906 } |
| 903 } | 907 } |
| 904 return s + '"'; | 908 return s + '"'; |
| 905 }; | 909 }; |
| 906 | 910 |
| 907 | 911 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 918 } | 922 } |
| 919 }; | 923 }; |
| 920 | 924 |
| 921 | 925 |
| 922 /** | 926 /** |
| 923 * Utility function: convert a string with codepoints 0--255 inclusive to a | 927 * Utility function: convert a string with codepoints 0--255 inclusive to a |
| 924 * Uint8Array. If any codepoints greater than 255 exist in the string, throws an | 928 * Uint8Array. If any codepoints greater than 255 exist in the string, throws an |
| 925 * exception. | 929 * exception. |
| 926 * @param {string} str | 930 * @param {string} str |
| 927 * @return {!Uint8Array} | 931 * @return {!Uint8Array} |
| 928 * @private | |
| 929 */ | 932 */ |
| 930 jspb.utils.stringToByteArray_ = function(str) { | 933 jspb.utils.stringToByteArray = function(str) { |
| 931 var arr = new Uint8Array(str.length); | 934 var arr = new Uint8Array(str.length); |
| 932 for (var i = 0; i < str.length; i++) { | 935 for (var i = 0; i < str.length; i++) { |
| 933 var codepoint = str.charCodeAt(i); | 936 var codepoint = str.charCodeAt(i); |
| 934 if (codepoint > 255) { | 937 if (codepoint > 255) { |
| 935 throw new Error('Conversion error: string contains codepoint ' + | 938 throw new Error('Conversion error: string contains codepoint ' + |
| 936 'outside of byte range'); | 939 'outside of byte range'); |
| 937 } | 940 } |
| 938 arr[i] = codepoint; | 941 arr[i] = codepoint; |
| 939 } | 942 } |
| 940 return arr; | 943 return arr; |
| 941 }; | 944 }; |
| 942 | 945 |
| 943 | 946 |
| 944 /** | 947 /** |
| 945 * Converts any type defined in jspb.ByteSource into a Uint8Array. | 948 * Converts any type defined in jspb.ByteSource into a Uint8Array. |
| 946 * @param {!jspb.ByteSource} data | 949 * @param {!jspb.ByteSource} data |
| 947 * @param {boolean=} opt_stringIsRawBytes Interpret a string as a series of raw | |
| 948 * bytes (encoded as codepoints 0--255 inclusive) rather than base64 data | |
| 949 * (default behavior). | |
| 950 * @return {!Uint8Array} | 950 * @return {!Uint8Array} |
| 951 * @suppress {invalidCasts} | 951 * @suppress {invalidCasts} |
| 952 */ | 952 */ |
| 953 jspb.utils.byteSourceToUint8Array = function(data, opt_stringIsRawBytes) { | 953 jspb.utils.byteSourceToUint8Array = function(data) { |
| 954 if (data.constructor === Uint8Array) { | 954 if (data.constructor === Uint8Array) { |
| 955 return /** @type {!Uint8Array} */(data); | 955 return /** @type {!Uint8Array} */(data); |
| 956 } | 956 } |
| 957 | 957 |
| 958 if (data.constructor === ArrayBuffer) { | 958 if (data.constructor === ArrayBuffer) { |
| 959 data = /** @type {!ArrayBuffer} */(data); | 959 data = /** @type {!ArrayBuffer} */(data); |
| 960 return /** @type {!Uint8Array} */(new Uint8Array(data)); | 960 return /** @type {!Uint8Array} */(new Uint8Array(data)); |
| 961 } | 961 } |
| 962 | 962 |
| 963 if (data.constructor === Array) { | 963 if (data.constructor === Array) { |
| 964 data = /** @type {!Array.<number>} */(data); | 964 data = /** @type {!Array.<number>} */(data); |
| 965 return /** @type {!Uint8Array} */(new Uint8Array(data)); | 965 return /** @type {!Uint8Array} */(new Uint8Array(data)); |
| 966 } | 966 } |
| 967 | 967 |
| 968 if (data.constructor === String) { | 968 if (data.constructor === String) { |
| 969 data = /** @type {string} */(data); | 969 data = /** @type {string} */(data); |
| 970 if (opt_stringIsRawBytes) { | 970 return goog.crypt.base64.decodeStringToUint8Array(data); |
| 971 return jspb.utils.stringToByteArray_(data); | |
| 972 } else { | |
| 973 return goog.crypt.base64.decodeStringToUint8Array(data); | |
| 974 } | |
| 975 } | 971 } |
| 976 | 972 |
| 977 goog.asserts.fail('Type not convertible to Uint8Array.'); | 973 goog.asserts.fail('Type not convertible to Uint8Array.'); |
| 978 return /** @type {!Uint8Array} */(new Uint8Array(0)); | 974 return /** @type {!Uint8Array} */(new Uint8Array(0)); |
| 979 }; | 975 }; |
| OLD | NEW |