add old go tooling
This commit is contained in:
40
macho-go/pkg/leb128/leb128.go
Normal file
40
macho-go/pkg/leb128/leb128.go
Normal file
@ -0,0 +1,40 @@
|
||||
package leb128
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// Read unsigned leb128, no error
|
||||
func ReadULEB(buf *bytes.Buffer) (uint, uint) {
|
||||
result := uint(0)
|
||||
shift := uint(0)
|
||||
bytes_read := uint(0)
|
||||
for {
|
||||
b, _ := buf.ReadByte()
|
||||
bytes_read += 1
|
||||
result |= uint(b&0b0111_1111) << shift
|
||||
shift += 7
|
||||
if (b & 0b1000_0000) == 0 {
|
||||
return result, bytes_read
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read signed leb128, no error
|
||||
func ReadSLEB(buf *bytes.Buffer) (int, uint) {
|
||||
result := int(0)
|
||||
shift := uint(0)
|
||||
bytes_read := uint(0)
|
||||
for {
|
||||
b, _ := buf.ReadByte()
|
||||
bytes_read += 1
|
||||
result |= int(b&0b0111_1111) << shift
|
||||
shift += 7
|
||||
if (b & 0b1000_0000) == 0 {
|
||||
if b&0x40 != 0 {
|
||||
return result | (-1 << shift), bytes_read
|
||||
}
|
||||
return result, bytes_read
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user