@@ -8,7 +8,8 @@ const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
88const BufferDecoder = goog . require ( 'protobuf.binary.BufferDecoder' ) ;
99const WireType = goog . require ( 'protobuf.binary.WireType' ) ;
1010const { Field} = goog . require ( 'protobuf.binary.field' ) ;
11- const { checkCriticalElementIndex, checkCriticalState} = goog . require ( 'protobuf.internal.checks' ) ;
11+ const { checkCriticalState} = goog . require ( 'protobuf.internal.checks' ) ;
12+ const { skipField, tagToFieldNumber, tagToWireType} = goog . require ( 'protobuf.binary.tag' ) ;
1213
1314/**
1415 * Appends a new entry in the index array for the given field number.
@@ -26,26 +27,6 @@ function addIndexEntry(storage, fieldNumber, wireType, startIndex) {
2627 }
2728}
2829
29- /**
30- * Returns wire type stored in a tag.
31- * Protos store the wire type as the first 3 bit of a tag.
32- * @param {number } tag
33- * @return {!WireType }
34- */
35- function tagToWireType ( tag ) {
36- return /** @type {!WireType } */ ( tag & 0x07 ) ;
37- }
38-
39- /**
40- * Returns the field number stored in a tag.
41- * Protos store the field number in the upper 29 bits of a 32 bit number.
42- * @param {number } tag
43- * @return {number }
44- */
45- function tagToFieldNumber ( tag ) {
46- return tag >>> 3 ;
47- }
48-
4930/**
5031 * Creates an index of field locations in a given binary protobuf.
5132 * @param {!BufferDecoder } bufferDecoder
@@ -62,83 +43,13 @@ function buildIndex(bufferDecoder, pivot) {
6243 const wireType = tagToWireType ( tag ) ;
6344 const fieldNumber = tagToFieldNumber ( tag ) ;
6445 checkCriticalState ( fieldNumber > 0 , `Invalid field number ${ fieldNumber } ` ) ;
65-
6646 addIndexEntry ( storage , fieldNumber , wireType , bufferDecoder . cursor ( ) ) ;
67-
68- checkCriticalState (
69- ! skipField_ ( bufferDecoder , wireType , fieldNumber ) ,
70- 'Found unmatched stop group.' ) ;
47+ skipField ( bufferDecoder , wireType , fieldNumber ) ;
7148 }
7249 return storage ;
7350}
7451
75- /**
76- * Skips over fields until the next field of the message.
77- * @param {!BufferDecoder } bufferDecoder
78- * @param {!WireType } wireType
79- * @param {number } fieldNumber
80- * @return {boolean } Whether the field we skipped over was a stop group.
81- * @private
82- */
83- function skipField_ ( bufferDecoder , wireType , fieldNumber ) {
84- switch ( wireType ) {
85- case WireType . VARINT :
86- checkCriticalElementIndex (
87- bufferDecoder . cursor ( ) , bufferDecoder . endIndex ( ) ) ;
88- bufferDecoder . skipVarint ( ) ;
89- return false ;
90- case WireType . FIXED64 :
91- bufferDecoder . skip ( 8 ) ;
92- return false ;
93- case WireType . DELIMITED :
94- checkCriticalElementIndex (
95- bufferDecoder . cursor ( ) , bufferDecoder . endIndex ( ) ) ;
96- const length = bufferDecoder . getUnsignedVarint32 ( ) ;
97- bufferDecoder . skip ( length ) ;
98- return false ;
99- case WireType . START_GROUP :
100- checkCriticalState (
101- skipGroup_ ( bufferDecoder , fieldNumber ) , 'No end group found.' ) ;
102- return false ;
103- case WireType . END_GROUP :
104- // Signal that we found a stop group to the caller
105- return true ;
106- case WireType . FIXED32 :
107- bufferDecoder . skip ( 4 ) ;
108- return false ;
109- default :
110- throw new Error ( `Invalid wire type: ${ wireType } ` ) ;
111- }
112- }
113-
114- /**
115- * Skips over fields until it finds the end of a given group.
116- * @param {!BufferDecoder } bufferDecoder
117- * @param {number } groupFieldNumber
118- * @return {boolean } Returns true if an end was found.
119- * @private
120- */
121- function skipGroup_ ( bufferDecoder , groupFieldNumber ) {
122- // On a start group we need to keep skipping fields until we find a
123- // corresponding stop group
124- // Note: Since we are calling skipField from here nested groups will be
125- // handled by recursion of this method and thus we will not see a nested
126- // STOP GROUP here unless there is something wrong with the input data.
127- while ( bufferDecoder . hasNext ( ) ) {
128- const tag = bufferDecoder . getUnsignedVarint32 ( ) ;
129- const wireType = tagToWireType ( tag ) ;
130- const fieldNumber = tagToFieldNumber ( tag ) ;
131-
132- if ( skipField_ ( bufferDecoder , wireType , fieldNumber ) ) {
133- checkCriticalState (
134- groupFieldNumber === fieldNumber ,
135- `Expected stop group for fieldnumber ${ groupFieldNumber } not found.` ) ;
136- return true ;
137- }
138- }
139- return false ;
140- }
141-
14252exports = {
14353 buildIndex,
54+ tagToWireType,
14455} ;
0 commit comments