OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Buffered reading and decoding of DWARF data streams. | 5 // Buffered reading and decoding of DWARF data streams. |
6 | 6 |
7 package dwarf | 7 package dwarf |
8 | 8 |
9 import ( | 9 import ( |
10 "encoding/binary" | 10 "encoding/binary" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 } | 47 } |
48 | 48 |
49 func (u unknownFormat) addrsize() int { | 49 func (u unknownFormat) addrsize() int { |
50 return 0 | 50 return 0 |
51 } | 51 } |
52 | 52 |
53 func makeBuf(d *Data, format dataFormat, name string, off Offset, data []byte) b
uf { | 53 func makeBuf(d *Data, format dataFormat, name string, off Offset, data []byte) b
uf { |
54 return buf{d, d.order, format, name, off, data, nil} | 54 return buf{d, d.order, format, name, off, data, nil} |
55 } | 55 } |
56 | 56 |
| 57 func (b *buf) slice(length int) buf { |
| 58 n := *b |
| 59 data := b.data |
| 60 b.skip(length) // Will validate length. |
| 61 n.data = data[:length] |
| 62 return n |
| 63 } |
| 64 |
57 func (b *buf) uint8() uint8 { | 65 func (b *buf) uint8() uint8 { |
58 if len(b.data) < 1 { | 66 if len(b.data) < 1 { |
59 b.error("underflow") | 67 b.error("underflow") |
60 return 0 | 68 return 0 |
61 } | 69 } |
62 val := b.data[0] | 70 val := b.data[0] |
63 b.data = b.data[1:] | 71 b.data = b.data[1:] |
64 b.off++ | 72 b.off++ |
65 return val | 73 return val |
66 } | 74 } |
67 | 75 |
68 func (b *buf) bytes(n int) []byte { | 76 func (b *buf) bytes(n int) []byte { |
69 if len(b.data) < n { | 77 if len(b.data) < n { |
70 b.error("underflow") | 78 b.error("underflow") |
71 return nil | 79 return nil |
72 } | 80 } |
73 data := b.data[0:n] | 81 data := b.data[0:n] |
74 b.data = b.data[n:] | 82 b.data = b.data[n:] |
75 b.off += Offset(n) | 83 b.off += Offset(n) |
76 return data | 84 return data |
77 } | 85 } |
78 | 86 |
79 func (b *buf) skip(n int) { b.bytes(n) } | 87 func (b *buf) skip(n int) { b.bytes(n) } |
80 | 88 |
| 89 // string returns the NUL-terminated (C-like) string at the start of the buffer. |
| 90 // The terminal NUL is discarded. |
81 func (b *buf) string() string { | 91 func (b *buf) string() string { |
82 for i := 0; i < len(b.data); i++ { | 92 for i := 0; i < len(b.data); i++ { |
83 if b.data[i] == 0 { | 93 if b.data[i] == 0 { |
84 s := string(b.data[0:i]) | 94 s := string(b.data[0:i]) |
85 b.data = b.data[i+1:] | 95 b.data = b.data[i+1:] |
86 b.off += Offset(i + 1) | 96 b.off += Offset(i + 1) |
87 return s | 97 return s |
88 } | 98 } |
89 } | 99 } |
90 b.error("underflow") | 100 b.error("underflow") |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 182 |
173 type DecodeError struct { | 183 type DecodeError struct { |
174 Name string | 184 Name string |
175 Offset Offset | 185 Offset Offset |
176 Err string | 186 Err string |
177 } | 187 } |
178 | 188 |
179 func (e DecodeError) Error() string { | 189 func (e DecodeError) Error() string { |
180 return "decoding dwarf section " + e.Name + " at offset 0x" + strconv.Fo
rmatInt(int64(e.Offset), 16) + ": " + e.Err | 190 return "decoding dwarf section " + e.Name + " at offset 0x" + strconv.Fo
rmatInt(int64(e.Offset), 16) + ": " + e.Err |
181 } | 191 } |
OLD | NEW |