github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/convert/pprof/streaming/vt_profile.go (about)

     1  package streaming
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  )
     8  
     9  const (
    10  	opFlagCountStructs           = 1 << 0
    11  	opFlagParseStructs           = 1 << 1
    12  	opFlagParseSamples           = 1 << 2
    13  	opFlagParseSamplesWriteBatch = 1 << 3
    14  )
    15  
    16  // revive:disable-next-line:cognitive-complexity,cyclomatic necessary complexity
    17  func (p *VTStreamingParser) UnmarshalVTProfile(dAtA []byte, opFlag uint64) error {
    18  	l := len(dAtA)
    19  	iNdEx := 0
    20  	countStructs := opFlag&opFlagCountStructs == opFlagCountStructs
    21  	parseStructs := opFlag&opFlagParseStructs == opFlagParseStructs
    22  	parseSamples := opFlag&opFlagParseSamples == opFlagParseSamples
    23  	parseSamplesWriteBatch := opFlag&opFlagParseSamplesWriteBatch == opFlagParseSamplesWriteBatch
    24  	strWriteIndex := 0
    25  	locationWriteIndex := 0
    26  	functionWriteIndex := 0
    27  	sampleTypeWriteIndex := 0
    28  	p.strings = p.strings[:p.nStrings]
    29  	p.locations = p.locations[:p.nLocations]
    30  	p.functions = p.functions[:p.nFunctions]
    31  	p.sampleTypes = p.sampleTypes[:p.nSampleTypes]
    32  	if countStructs {
    33  		p.period = 0
    34  		p.nStrings = 0
    35  		p.nFunctions = 0
    36  		p.nLocations = 0
    37  		p.nSampleTypes = 0
    38  	}
    39  	for iNdEx < l {
    40  		//preIndex := iNdEx
    41  		var wire uint64
    42  		for shift := uint(0); ; shift += 7 {
    43  			if shift >= 64 {
    44  				return ErrIntOverflow
    45  			}
    46  			if iNdEx >= l {
    47  				return io.ErrUnexpectedEOF
    48  			}
    49  			b := dAtA[iNdEx]
    50  			iNdEx++
    51  			wire |= uint64(b&0x7F) << shift
    52  			if b < 0x80 {
    53  				break
    54  			}
    55  		}
    56  		fieldNum := int32(wire >> 3)
    57  		wireType := int(wire & 0x7)
    58  		if wireType == 4 {
    59  			return fmt.Errorf("proto: Profile: wiretype end group for non-group")
    60  		}
    61  		if fieldNum <= 0 {
    62  			return fmt.Errorf("proto: Profile: illegal tag %d (wire type %d)", fieldNum, wire)
    63  		}
    64  		switch fieldNum {
    65  		case 1:
    66  			if wireType != 2 {
    67  				return fmt.Errorf("proto: wrong wireType = %d for field SampleType", wireType)
    68  			}
    69  			var msglen int
    70  			for shift := uint(0); ; shift += 7 {
    71  				if shift >= 64 {
    72  					return ErrIntOverflow
    73  				}
    74  				if iNdEx >= l {
    75  					return io.ErrUnexpectedEOF
    76  				}
    77  				b := dAtA[iNdEx]
    78  				iNdEx++
    79  				msglen |= int(b&0x7F) << shift
    80  				if b < 0x80 {
    81  					break
    82  				}
    83  			}
    84  			if msglen < 0 {
    85  				return ErrInvalidLength
    86  			}
    87  			postIndex := iNdEx + msglen
    88  			if postIndex < 0 {
    89  				return ErrInvalidLength
    90  			}
    91  			if postIndex > l {
    92  				return io.ErrUnexpectedEOF
    93  			}
    94  			if countStructs {
    95  				p.nSampleTypes++
    96  			}
    97  			if parseStructs {
    98  				//p.sampleTypes = append(p.sampleTypes, valueType{})
    99  				if err := p.sampleTypes[sampleTypeWriteIndex].UnmarshalVT(dAtA[iNdEx:postIndex]); err != nil {
   100  					return err
   101  				}
   102  				sampleTypeWriteIndex++
   103  			}
   104  			iNdEx = postIndex
   105  		case 2:
   106  			if wireType != 2 {
   107  				return fmt.Errorf("proto: wrong wireType = %d for field Sample", wireType)
   108  			}
   109  			var msglen int
   110  			for shift := uint(0); ; shift += 7 {
   111  				if shift >= 64 {
   112  					return ErrIntOverflow
   113  				}
   114  				if iNdEx >= l {
   115  					return io.ErrUnexpectedEOF
   116  				}
   117  				b := dAtA[iNdEx]
   118  				iNdEx++
   119  				msglen |= int(b&0x7F) << shift
   120  				if b < 0x80 {
   121  					break
   122  				}
   123  			}
   124  			if msglen < 0 {
   125  				return ErrInvalidLength
   126  			}
   127  			postIndex := iNdEx + msglen
   128  			if postIndex < 0 {
   129  				return ErrInvalidLength
   130  			}
   131  			if postIndex > l {
   132  				return io.ErrUnexpectedEOF
   133  			}
   134  			if parseSamples {
   135  				if err := p.parseSampleVT(dAtA[iNdEx:postIndex]); err != nil {
   136  					return err
   137  				}
   138  			}
   139  			if parseSamplesWriteBatch {
   140  				if err := p.parseSampleWB(dAtA[iNdEx:postIndex]); err != nil {
   141  					return err
   142  				}
   143  			}
   144  			iNdEx = postIndex
   145  		case 3:
   146  			if wireType != 2 {
   147  				return fmt.Errorf("proto: wrong wireType = %d for field Mapping", wireType)
   148  			}
   149  			var msglen int
   150  			for shift := uint(0); ; shift += 7 {
   151  				if shift >= 64 {
   152  					return ErrIntOverflow
   153  				}
   154  				if iNdEx >= l {
   155  					return io.ErrUnexpectedEOF
   156  				}
   157  				b := dAtA[iNdEx]
   158  				iNdEx++
   159  				msglen |= int(b&0x7F) << shift
   160  				if b < 0x80 {
   161  					break
   162  				}
   163  			}
   164  			if msglen < 0 {
   165  				return ErrInvalidLength
   166  			}
   167  			postIndex := iNdEx + msglen
   168  			if postIndex < 0 {
   169  				return ErrInvalidLength
   170  			}
   171  			if postIndex > l {
   172  				return io.ErrUnexpectedEOF
   173  			}
   174  			iNdEx = postIndex
   175  		case 4:
   176  			if wireType != 2 {
   177  				return fmt.Errorf("proto: wrong wireType = %d for field Location", wireType)
   178  			}
   179  			var msglen int
   180  			for shift := uint(0); ; shift += 7 {
   181  				if shift >= 64 {
   182  					return ErrIntOverflow
   183  				}
   184  				if iNdEx >= l {
   185  					return io.ErrUnexpectedEOF
   186  				}
   187  				b := dAtA[iNdEx]
   188  				iNdEx++
   189  				msglen |= int(b&0x7F) << shift
   190  				if b < 0x80 {
   191  					break
   192  				}
   193  			}
   194  			if msglen < 0 {
   195  				return ErrInvalidLength
   196  			}
   197  			postIndex := iNdEx + msglen
   198  			if postIndex < 0 {
   199  				return ErrInvalidLength
   200  			}
   201  			if postIndex > l {
   202  				return io.ErrUnexpectedEOF
   203  			}
   204  			if countStructs {
   205  				p.nLocations++
   206  			}
   207  			if parseStructs {
   208  				//p.locations = append(p.locations, location{})
   209  				if err := p.locations[locationWriteIndex].UnmarshalVT(dAtA[iNdEx:postIndex], &p.lineRefs, p.arena); err != nil {
   210  					return err
   211  				}
   212  				locationWriteIndex++
   213  			}
   214  			iNdEx = postIndex
   215  		case 5:
   216  			if wireType != 2 {
   217  				return fmt.Errorf("proto: wrong wireType = %d for field Function", wireType)
   218  			}
   219  			var msglen int
   220  			for shift := uint(0); ; shift += 7 {
   221  				if shift >= 64 {
   222  					return ErrIntOverflow
   223  				}
   224  				if iNdEx >= l {
   225  					return io.ErrUnexpectedEOF
   226  				}
   227  				b := dAtA[iNdEx]
   228  				iNdEx++
   229  				msglen |= int(b&0x7F) << shift
   230  				if b < 0x80 {
   231  					break
   232  				}
   233  			}
   234  			if msglen < 0 {
   235  				return ErrInvalidLength
   236  			}
   237  			postIndex := iNdEx + msglen
   238  			if postIndex < 0 {
   239  				return ErrInvalidLength
   240  			}
   241  			if postIndex > l {
   242  				return io.ErrUnexpectedEOF
   243  			}
   244  			if countStructs {
   245  				p.nFunctions++
   246  			}
   247  			if parseStructs {
   248  				//p.functions = append(p.functions, function{})
   249  				if err := p.functions[functionWriteIndex].UnmarshalVT(dAtA[iNdEx:postIndex]); err != nil {
   250  					return err
   251  				}
   252  				functionWriteIndex++
   253  			}
   254  			iNdEx = postIndex
   255  		case 6:
   256  			if wireType != 2 {
   257  				return fmt.Errorf("proto: wrong wireType = %d for field StringTable", wireType)
   258  			}
   259  			var stringLen uint64
   260  			for shift := uint(0); ; shift += 7 {
   261  				if shift >= 64 {
   262  					return ErrIntOverflow
   263  				}
   264  				if iNdEx >= l {
   265  					return io.ErrUnexpectedEOF
   266  				}
   267  				b := dAtA[iNdEx]
   268  				iNdEx++
   269  				stringLen |= uint64(b&0x7F) << shift
   270  				if b < 0x80 {
   271  					break
   272  				}
   273  			}
   274  			intStringLen := int(stringLen)
   275  			if intStringLen < 0 {
   276  				return ErrInvalidLength
   277  			}
   278  			postIndex := iNdEx + intStringLen
   279  			if postIndex < 0 {
   280  				return ErrInvalidLength
   281  			}
   282  			if postIndex > l {
   283  				return io.ErrUnexpectedEOF
   284  			}
   285  			if countStructs {
   286  				p.nStrings++
   287  			}
   288  			if parseStructs {
   289  				s := dAtA[iNdEx:postIndex]
   290  				if bytes.Equal(s, profileIDLabel) {
   291  					p.profileIDLabelIndex = int64(strWriteIndex)
   292  				}
   293  				ps := istr(iNdEx<<32 | postIndex)
   294  				p.strings[strWriteIndex] = ps
   295  				strWriteIndex++
   296  			}
   297  			iNdEx = postIndex
   298  		case 7:
   299  			if wireType != 0 {
   300  				return fmt.Errorf("proto: wrong wireType = %d for field DropFrames", wireType)
   301  			}
   302  			//p.DropFrames = 0
   303  			for shift := uint(0); ; shift += 7 {
   304  				if shift >= 64 {
   305  					return ErrIntOverflow
   306  				}
   307  				if iNdEx >= l {
   308  					return io.ErrUnexpectedEOF
   309  				}
   310  				b := dAtA[iNdEx]
   311  				iNdEx++
   312  				//p.DropFrames |= int64(b&0x7F) << shift
   313  				if b < 0x80 {
   314  					break
   315  				}
   316  			}
   317  		case 8:
   318  			if wireType != 0 {
   319  				return fmt.Errorf("proto: wrong wireType = %d for field KeepFrames", wireType)
   320  			}
   321  			//p.KeepFrames = 0
   322  			for shift := uint(0); ; shift += 7 {
   323  				if shift >= 64 {
   324  					return ErrIntOverflow
   325  				}
   326  				if iNdEx >= l {
   327  					return io.ErrUnexpectedEOF
   328  				}
   329  				b := dAtA[iNdEx]
   330  				iNdEx++
   331  				//p.KeepFrames |= int64(b&0x7F) << shift
   332  				if b < 0x80 {
   333  					break
   334  				}
   335  			}
   336  		case 9:
   337  			if wireType != 0 {
   338  				return fmt.Errorf("proto: wrong wireType = %d for field TimeNanos", wireType)
   339  			}
   340  			//p.TimeNanos = 0
   341  			for shift := uint(0); ; shift += 7 {
   342  				if shift >= 64 {
   343  					return ErrIntOverflow
   344  				}
   345  				if iNdEx >= l {
   346  					return io.ErrUnexpectedEOF
   347  				}
   348  				b := dAtA[iNdEx]
   349  				iNdEx++
   350  				//p.TimeNanos |= int64(b&0x7F) << shift
   351  				if b < 0x80 {
   352  					break
   353  				}
   354  			}
   355  		case 10:
   356  			if wireType != 0 {
   357  				return fmt.Errorf("proto: wrong wireType = %d for field DurationNanos", wireType)
   358  			}
   359  			//p.DurationNanos = 0
   360  			for shift := uint(0); ; shift += 7 {
   361  				if shift >= 64 {
   362  					return ErrIntOverflow
   363  				}
   364  				if iNdEx >= l {
   365  					return io.ErrUnexpectedEOF
   366  				}
   367  				b := dAtA[iNdEx]
   368  				iNdEx++
   369  				//p.DurationNanos |= int64(b&0x7F) << shift
   370  				if b < 0x80 {
   371  					break
   372  				}
   373  			}
   374  		case 11:
   375  			if wireType != 2 {
   376  				return fmt.Errorf("proto: wrong wireType = %d for field periodType", wireType)
   377  			}
   378  			var msglen int
   379  			for shift := uint(0); ; shift += 7 {
   380  				if shift >= 64 {
   381  					return ErrIntOverflow
   382  				}
   383  				if iNdEx >= l {
   384  					return io.ErrUnexpectedEOF
   385  				}
   386  				b := dAtA[iNdEx]
   387  				iNdEx++
   388  				msglen |= int(b&0x7F) << shift
   389  				if b < 0x80 {
   390  					break
   391  				}
   392  			}
   393  			if msglen < 0 {
   394  				return ErrInvalidLength
   395  			}
   396  			postIndex := iNdEx + msglen
   397  			if postIndex < 0 {
   398  				return ErrInvalidLength
   399  			}
   400  			if postIndex > l {
   401  				return io.ErrUnexpectedEOF
   402  			}
   403  			if parseStructs {
   404  				if err := p.periodType.UnmarshalVT(dAtA[iNdEx:postIndex]); err != nil {
   405  					return err
   406  				}
   407  			}
   408  			iNdEx = postIndex
   409  		case 12:
   410  			if wireType != 0 {
   411  				return fmt.Errorf("proto: wrong wireType = %d for field period", wireType)
   412  			}
   413  			p.period = 0
   414  			for shift := uint(0); ; shift += 7 {
   415  				if shift >= 64 {
   416  					return ErrIntOverflow
   417  				}
   418  				if iNdEx >= l {
   419  					return io.ErrUnexpectedEOF
   420  				}
   421  				b := dAtA[iNdEx]
   422  				iNdEx++
   423  				p.period |= int64(b&0x7F) << shift
   424  				if b < 0x80 {
   425  					break
   426  				}
   427  			}
   428  		case 13:
   429  			if wireType == 0 {
   430  				var v int64
   431  				for shift := uint(0); ; shift += 7 {
   432  					if shift >= 64 {
   433  						return ErrIntOverflow
   434  					}
   435  					if iNdEx >= l {
   436  						return io.ErrUnexpectedEOF
   437  					}
   438  					b := dAtA[iNdEx]
   439  					iNdEx++
   440  					v |= int64(b&0x7F) << shift
   441  					if b < 0x80 {
   442  						break
   443  					}
   444  				}
   445  			} else if wireType == 2 {
   446  				var packedLen int
   447  				for shift := uint(0); ; shift += 7 {
   448  					if shift >= 64 {
   449  						return ErrIntOverflow
   450  					}
   451  					if iNdEx >= l {
   452  						return io.ErrUnexpectedEOF
   453  					}
   454  					b := dAtA[iNdEx]
   455  					iNdEx++
   456  					packedLen |= int(b&0x7F) << shift
   457  					if b < 0x80 {
   458  						break
   459  					}
   460  				}
   461  				if packedLen < 0 {
   462  					return ErrInvalidLength
   463  				}
   464  				postIndex := iNdEx + packedLen
   465  				if postIndex < 0 {
   466  					return ErrInvalidLength
   467  				}
   468  				if postIndex > l {
   469  					return io.ErrUnexpectedEOF
   470  				}
   471  				for iNdEx < postIndex {
   472  					var v int64
   473  					for shift := uint(0); ; shift += 7 {
   474  						if shift >= 64 {
   475  							return ErrIntOverflow
   476  						}
   477  						if iNdEx >= l {
   478  							return io.ErrUnexpectedEOF
   479  						}
   480  						b := dAtA[iNdEx]
   481  						iNdEx++
   482  						v |= int64(b&0x7F) << shift
   483  						if b < 0x80 {
   484  							break
   485  						}
   486  					}
   487  				}
   488  			} else {
   489  				return fmt.Errorf("proto: wrong wireType = %d for field Comment", wireType)
   490  			}
   491  		case 14:
   492  			if wireType != 0 {
   493  				return fmt.Errorf("proto: wrong wireType = %d for field DefaultSampleType", wireType)
   494  			}
   495  			//p.DefaultSampleType = 0
   496  			for shift := uint(0); ; shift += 7 {
   497  				if shift >= 64 {
   498  					return ErrIntOverflow
   499  				}
   500  				if iNdEx >= l {
   501  					return io.ErrUnexpectedEOF
   502  				}
   503  				b := dAtA[iNdEx]
   504  				iNdEx++
   505  				//p.DefaultSampleType |= int64(b&0x7F) << shift
   506  				if b < 0x80 {
   507  					break
   508  				}
   509  			}
   510  		default:
   511  			return ErrUnknownField
   512  		}
   513  	}
   514  
   515  	if iNdEx > l {
   516  		return io.ErrUnexpectedEOF
   517  	}
   518  	return nil
   519  }