Source file src/cmd/vendor/github.com/google/pprof/profile/proto.go

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // This file is a simple protocol buffer encoder and decoder.
    16  // The format is described at
    17  // https://developers.google.com/protocol-buffers/docs/encoding
    18  //
    19  // A protocol message must implement the message interface:
    20  //   decoder() []decoder
    21  //   encode(*buffer)
    22  //
    23  // The decode method returns a slice indexed by field number that gives the
    24  // function to decode that field.
    25  // The encode method encodes its receiver into the given buffer.
    26  //
    27  // The two methods are simple enough to be implemented by hand rather than
    28  // by using a protocol compiler.
    29  //
    30  // See profile.go for examples of messages implementing this interface.
    31  //
    32  // There is no support for groups, message sets, or "has" bits.
    33  
    34  package profile
    35  
    36  import (
    37  	"errors"
    38  	"fmt"
    39  )
    40  
    41  type buffer struct {
    42  	field    int // field tag
    43  	typ      int // proto wire type code for field
    44  	u64      uint64
    45  	data     []byte
    46  	tmp      [16]byte
    47  	tmpLines []Line // temporary storage used while decoding "repeated Line".
    48  }
    49  
    50  type decoder func(*buffer, message) error
    51  
    52  type message interface {
    53  	decoder() []decoder
    54  	encode(*buffer)
    55  }
    56  
    57  func marshal(m message) []byte {
    58  	var b buffer
    59  	m.encode(&b)
    60  	return b.data
    61  }
    62  
    63  func encodeVarint(b *buffer, x uint64) {
    64  	for x >= 128 {
    65  		b.data = append(b.data, byte(x)|0x80)
    66  		x >>= 7
    67  	}
    68  	b.data = append(b.data, byte(x))
    69  }
    70  
    71  func encodeLength(b *buffer, tag int, len int) {
    72  	encodeVarint(b, uint64(tag)<<3|2)
    73  	encodeVarint(b, uint64(len))
    74  }
    75  
    76  func encodeUint64(b *buffer, tag int, x uint64) {
    77  	// append varint to b.data
    78  	encodeVarint(b, uint64(tag)<<3)
    79  	encodeVarint(b, x)
    80  }
    81  
    82  func encodeUint64s(b *buffer, tag int, x []uint64) {
    83  	if len(x) > 2 {
    84  		// Use packed encoding
    85  		n1 := len(b.data)
    86  		for _, u := range x {
    87  			encodeVarint(b, u)
    88  		}
    89  		n2 := len(b.data)
    90  		encodeLength(b, tag, n2-n1)
    91  		n3 := len(b.data)
    92  		copy(b.tmp[:], b.data[n2:n3])
    93  		copy(b.data[n1+(n3-n2):], b.data[n1:n2])
    94  		copy(b.data[n1:], b.tmp[:n3-n2])
    95  		return
    96  	}
    97  	for _, u := range x {
    98  		encodeUint64(b, tag, u)
    99  	}
   100  }
   101  
   102  func encodeUint64Opt(b *buffer, tag int, x uint64) {
   103  	if x == 0 {
   104  		return
   105  	}
   106  	encodeUint64(b, tag, x)
   107  }
   108  
   109  func encodeInt64(b *buffer, tag int, x int64) {
   110  	u := uint64(x)
   111  	encodeUint64(b, tag, u)
   112  }
   113  
   114  func encodeInt64s(b *buffer, tag int, x []int64) {
   115  	if len(x) > 2 {
   116  		// Use packed encoding
   117  		n1 := len(b.data)
   118  		for _, u := range x {
   119  			encodeVarint(b, uint64(u))
   120  		}
   121  		n2 := len(b.data)
   122  		encodeLength(b, tag, n2-n1)
   123  		n3 := len(b.data)
   124  		copy(b.tmp[:], b.data[n2:n3])
   125  		copy(b.data[n1+(n3-n2):], b.data[n1:n2])
   126  		copy(b.data[n1:], b.tmp[:n3-n2])
   127  		return
   128  	}
   129  	for _, u := range x {
   130  		encodeInt64(b, tag, u)
   131  	}
   132  }
   133  
   134  func encodeInt64Opt(b *buffer, tag int, x int64) {
   135  	if x == 0 {
   136  		return
   137  	}
   138  	encodeInt64(b, tag, x)
   139  }
   140  
   141  func encodeString(b *buffer, tag int, x string) {
   142  	encodeLength(b, tag, len(x))
   143  	b.data = append(b.data, x...)
   144  }
   145  
   146  func encodeStrings(b *buffer, tag int, x []string) {
   147  	for _, s := range x {
   148  		encodeString(b, tag, s)
   149  	}
   150  }
   151  
   152  func encodeBool(b *buffer, tag int, x bool) {
   153  	if x {
   154  		encodeUint64(b, tag, 1)
   155  	} else {
   156  		encodeUint64(b, tag, 0)
   157  	}
   158  }
   159  
   160  func encodeBoolOpt(b *buffer, tag int, x bool) {
   161  	if x {
   162  		encodeBool(b, tag, x)
   163  	}
   164  }
   165  
   166  func encodeMessage(b *buffer, tag int, m message) {
   167  	n1 := len(b.data)
   168  	m.encode(b)
   169  	n2 := len(b.data)
   170  	encodeLength(b, tag, n2-n1)
   171  	n3 := len(b.data)
   172  	copy(b.tmp[:], b.data[n2:n3])
   173  	copy(b.data[n1+(n3-n2):], b.data[n1:n2])
   174  	copy(b.data[n1:], b.tmp[:n3-n2])
   175  }
   176  
   177  func unmarshal(data []byte, m message) (err error) {
   178  	b := buffer{data: data, typ: 2}
   179  	return decodeMessage(&b, m)
   180  }
   181  
   182  func le64(p []byte) uint64 {
   183  	return uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 | uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56
   184  }
   185  
   186  func le32(p []byte) uint32 {
   187  	return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
   188  }
   189  
   190  func decodeVarint(data []byte) (uint64, []byte, error) {
   191  	var u uint64
   192  	for i := 0; ; i++ {
   193  		if i >= 10 || i >= len(data) {
   194  			return 0, nil, errors.New("bad varint")
   195  		}
   196  		u |= uint64(data[i]&0x7F) << uint(7*i)
   197  		if data[i]&0x80 == 0 {
   198  			return u, data[i+1:], nil
   199  		}
   200  	}
   201  }
   202  
   203  func decodeField(b *buffer, data []byte) ([]byte, error) {
   204  	x, data, err := decodeVarint(data)
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	b.field = int(x >> 3)
   209  	b.typ = int(x & 7)
   210  	b.data = nil
   211  	b.u64 = 0
   212  	switch b.typ {
   213  	case 0:
   214  		b.u64, data, err = decodeVarint(data)
   215  		if err != nil {
   216  			return nil, err
   217  		}
   218  	case 1:
   219  		if len(data) < 8 {
   220  			return nil, errors.New("not enough data")
   221  		}
   222  		b.u64 = le64(data[:8])
   223  		data = data[8:]
   224  	case 2:
   225  		var n uint64
   226  		n, data, err = decodeVarint(data)
   227  		if err != nil {
   228  			return nil, err
   229  		}
   230  		if n > uint64(len(data)) {
   231  			return nil, errors.New("too much data")
   232  		}
   233  		b.data = data[:n]
   234  		data = data[n:]
   235  	case 5:
   236  		if len(data) < 4 {
   237  			return nil, errors.New("not enough data")
   238  		}
   239  		b.u64 = uint64(le32(data[:4]))
   240  		data = data[4:]
   241  	default:
   242  		return nil, fmt.Errorf("unknown wire type: %d", b.typ)
   243  	}
   244  
   245  	return data, nil
   246  }
   247  
   248  func checkType(b *buffer, typ int) error {
   249  	if b.typ != typ {
   250  		return errors.New("type mismatch")
   251  	}
   252  	return nil
   253  }
   254  
   255  func decodeMessage(b *buffer, m message) error {
   256  	if err := checkType(b, 2); err != nil {
   257  		return err
   258  	}
   259  	dec := m.decoder()
   260  	data := b.data
   261  	for len(data) > 0 {
   262  		// pull varint field# + type
   263  		var err error
   264  		data, err = decodeField(b, data)
   265  		if err != nil {
   266  			return err
   267  		}
   268  		if b.field >= len(dec) || dec[b.field] == nil {
   269  			continue
   270  		}
   271  		if err := dec[b.field](b, m); err != nil {
   272  			return err
   273  		}
   274  	}
   275  	return nil
   276  }
   277  
   278  func decodeInt64(b *buffer, x *int64) error {
   279  	if err := checkType(b, 0); err != nil {
   280  		return err
   281  	}
   282  	*x = int64(b.u64)
   283  	return nil
   284  }
   285  
   286  func decodeInt64s(b *buffer, x *[]int64) error {
   287  	if b.typ == 2 {
   288  		// Packed encoding
   289  		data := b.data
   290  		for len(data) > 0 {
   291  			var u uint64
   292  			var err error
   293  
   294  			if u, data, err = decodeVarint(data); err != nil {
   295  				return err
   296  			}
   297  			*x = append(*x, int64(u))
   298  		}
   299  		return nil
   300  	}
   301  	var i int64
   302  	if err := decodeInt64(b, &i); err != nil {
   303  		return err
   304  	}
   305  	*x = append(*x, i)
   306  	return nil
   307  }
   308  
   309  func decodeUint64(b *buffer, x *uint64) error {
   310  	if err := checkType(b, 0); err != nil {
   311  		return err
   312  	}
   313  	*x = b.u64
   314  	return nil
   315  }
   316  
   317  func decodeUint64s(b *buffer, x *[]uint64) error {
   318  	if b.typ == 2 {
   319  		data := b.data
   320  		// Packed encoding
   321  		for len(data) > 0 {
   322  			var u uint64
   323  			var err error
   324  
   325  			if u, data, err = decodeVarint(data); err != nil {
   326  				return err
   327  			}
   328  			*x = append(*x, u)
   329  		}
   330  		return nil
   331  	}
   332  	var u uint64
   333  	if err := decodeUint64(b, &u); err != nil {
   334  		return err
   335  	}
   336  	*x = append(*x, u)
   337  	return nil
   338  }
   339  
   340  func decodeString(b *buffer, x *string) error {
   341  	if err := checkType(b, 2); err != nil {
   342  		return err
   343  	}
   344  	*x = string(b.data)
   345  	return nil
   346  }
   347  
   348  func decodeStrings(b *buffer, x *[]string) error {
   349  	var s string
   350  	if err := decodeString(b, &s); err != nil {
   351  		return err
   352  	}
   353  	*x = append(*x, s)
   354  	return nil
   355  }
   356  
   357  func decodeBool(b *buffer, x *bool) error {
   358  	if err := checkType(b, 0); err != nil {
   359  		return err
   360  	}
   361  	if int64(b.u64) == 0 {
   362  		*x = false
   363  	} else {
   364  		*x = true
   365  	}
   366  	return nil
   367  }
   368  

View as plain text