macho/macho-go/pkg/leb128/leb128.go

41 lines
732 B
Go
Raw Permalink Normal View History

2023-05-31 16:17:03 +07:00
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
}
}
}