-
Notifications
You must be signed in to change notification settings - Fork 6
/
sections.go
52 lines (45 loc) · 1.37 KB
/
sections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package elf
import (
"compress/zlib"
"io"
)
// Data reads and returns the contents of the ELF section.
// Even if the section is stored compressed in the ELF file,
// Data returns uncompressed data.
func (s *ELF32Section) Data() ([]byte, error) {
var rs io.ReadSeeker
data := make([]byte, s.Size)
if s.Flags&uint32(SHF_COMPRESSED) == 0 {
rs = io.NewSectionReader(s.sr, 0, 1<<63-1)
} else if s.compressionType == COMPRESS_ZLIB {
rs = &readSeekerFromReader{
reset: func() (io.Reader, error) {
fr := io.NewSectionReader(s.sr, s.compressionOffset, int64(s.Size)-s.compressionOffset)
return zlib.NewReader(fr)
},
size: int64(s.Size),
}
}
n, err := io.ReadFull(rs, data)
return data[0:n], err
}
// Data reads and returns the contents of the ELF section.
// Even if the section is stored compressed in the ELF file,
// Data returns uncompressed data.
func (s *ELF64Section) Data() ([]byte, error) {
var rs io.ReadSeeker
data := make([]byte, s.Size)
if s.Flags&uint64(SHF_COMPRESSED) == 0 {
rs = io.NewSectionReader(s.sr, 0, 1<<63-1)
} else if s.compressionType == COMPRESS_ZLIB {
rs = &readSeekerFromReader{
reset: func() (io.Reader, error) {
fr := io.NewSectionReader(s.sr, s.compressionOffset, int64(s.Size)-s.compressionOffset)
return zlib.NewReader(fr)
},
size: int64(s.Size),
}
}
n, err := io.ReadFull(rs, data)
return data[0:n], err
}