github.com/Rookout/GoSDK@v0.1.48/pkg/services/instrumentation/dwarf/util/buf.go (about) 1 // The MIT License (MIT) 2 3 // Copyright (c) 2014 Derek Parker 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy of 6 // this software and associated documentation files (the "Software"), to deal in 7 // the Software without restriction, including without limitation the rights to 8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 // the Software, and to permit persons to whom the Software is furnished to do so, 10 // subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22 // Copyright 2009 The Go Authors. All rights reserved. 23 // Use of this source code is governed by a BSD-style 24 // license that can be found in the LICENSE file. 25 26 // Buffered reading and decoding of DWARF data streams. 27 28 //lint:file-ignore ST1021 imported file 29 30 package util 31 32 import ( 33 "debug/dwarf" 34 "fmt" 35 ) 36 37 38 type buf struct { 39 dwarf *dwarf.Data 40 format dataFormat 41 name string 42 off dwarf.Offset 43 data []byte 44 Err error 45 } 46 47 48 49 type dataFormat interface { 50 51 version() int 52 53 54 dwarf64() (dwarf64 bool, isKnown bool) 55 56 57 addrsize() int 58 } 59 60 61 type UnknownFormat struct{} 62 63 func (u UnknownFormat) version() int { 64 return 0 65 } 66 67 func (u UnknownFormat) dwarf64() (bool, bool) { 68 return false, false 69 } 70 71 func (u UnknownFormat) addrsize() int { 72 return 0 73 } 74 75 func MakeBuf(d *dwarf.Data, format dataFormat, name string, off dwarf.Offset, data []byte) buf { 76 return buf{d, format, name, off, data, nil} 77 } 78 79 func (b *buf) Uint8() uint8 { 80 if len(b.data) < 1 { 81 b.error("underflow") 82 return 0 83 } 84 val := b.data[0] 85 b.data = b.data[1:] 86 b.off++ 87 return val 88 } 89 90 91 92 func (b *buf) Varint() (c uint64, bits uint) { 93 for i := 0; i < len(b.data); i++ { 94 byte := b.data[i] 95 c |= uint64(byte&0x7F) << bits 96 bits += 7 97 if byte&0x80 == 0 { 98 b.off += dwarf.Offset(i + 1) 99 b.data = b.data[i+1:] 100 return c, bits 101 } 102 } 103 return 0, 0 104 } 105 106 107 func (b *buf) Uint() uint64 { 108 x, _ := b.Varint() 109 return x 110 } 111 112 113 func (b *buf) Int() int64 { 114 ux, bits := b.Varint() 115 x := int64(ux) 116 if x&(1<<(bits-1)) != 0 { 117 x |= -1 << bits 118 } 119 return x 120 } 121 122 123 func (b *buf) AssertEmpty() { 124 if len(b.data) == 0 { 125 return 126 } 127 if len(b.data) > 5 { 128 b.error(fmt.Sprintf("unexpected extra data: %x...", b.data[0:5])) 129 } 130 b.error(fmt.Sprintf("unexpected extra data: %x", b.data)) 131 } 132 133 func (b *buf) error(s string) { 134 if b.Err == nil { 135 b.data = nil 136 b.Err = dwarf.DecodeError{Name: b.name, Offset: b.off, Err: s} 137 } 138 }