Go implementation of Little Endian Base 128 codec.
$ go get -u ekyu.moe/leb128
# or better
$ dep ensure -add ekyu.moe/leb128
package main
import (
"fmt"
"ekyu.moe/leb128"
)
func main() {
// Notes: These specs are taken from https://en.wikipedia.org/wiki/LEB128
// Encode unsigned LEB128
fmt.Printf("%x\n", leb128.AppendUleb128(nil, 624485)) //=> e58e26
// Encode signed LEB128
fmt.Printf("%x\n", leb128.AppendSleb128(nil, -624485)) //=> 9bf159
// Decode unsigned LEB128, n is the number of bytes read
u, n := leb128.DecodeUleb128([]byte{0xe5, 0x8e, 0x26, 'a', 'b', 'c'})
fmt.Printf("%d %d\n", u, n) //=> 624485 3
// Decode signed LEB128, n is the number of bytes read
s, n := leb128.DecodeSleb128([]byte{0x9b, 0xf1, 0x59, 'd', 'e', 'f'})
fmt.Printf("%d %d\n", s, n) //=> -624485 3
}
Notes: The encode part is an edited fork of /src/cmd/internal/dwarf/dwarf.go licensed under a BSD-style license.