Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcalabro/leb128
Go implementation of signed/unsigned LEB128
https://github.com/jcalabro/leb128
dwarf go golang leb128 wasm
Last synced: 20 days ago
JSON representation
Go implementation of signed/unsigned LEB128
- Host: GitHub
- URL: https://github.com/jcalabro/leb128
- Owner: jcalabro
- License: mit
- Created: 2022-10-01T13:17:26.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-17T01:32:28.000Z (over 1 year ago)
- Last Synced: 2024-06-21T03:17:20.951Z (6 months ago)
- Topics: dwarf, go, golang, leb128, wasm
- Language: Go
- Homepage: https://pkg.go.dev/github.com/jcalabro/leb128
- Size: 18.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# leb128
[![GoDoc](https://pkg.go.dev/badge/github.com/jcalabro/leb128?status.svg)](https://pkg.go.dev/github.com/jcalabro/leb128) [![Tests](https://github.com/jcalabro/leb128/actions/workflows/ci.yaml/badge.svg)](https://github.com/jcalabro/leb128/actions/workflows/ci.yaml) [![codecov](https://codecov.io/github/jcalabro/leb128/branch/main/graph/badge.svg?token=ILKTKORT5D)](https://codecov.io/github/jcalabro/leb128)
Go implementation of signed/unsigned LEB128. Encodes/decodes 8 byte integers.
## Usage
Full documentation is available at [gopkg.dev](https://pkg.go.dev/github.com/jcalabro/leb128).
#### Decoding
Read from an `io.Reader` in to an `int64` or `uint64`:
```go
package mainimport (
"bytes"
"fmt""github.com/jcalabro/leb128"
)func unsigned() {
// using a buffer of length >10 in either the signed/unsigned case
// will return the error leb128.ErrOverflowbuf := bytes.NewBuffer([]byte{128, 2})
num, err := leb128.DecodeU64(buf)
if err != nil {
panic(err)
}
fmt.Println(num) // 256
}func signed() {
buf := bytes.NewBuffer([]byte{128, 126})
num, err := leb128.DecodeS64(buf)
if err != nil {
panic(err)
}
fmt.Println(num) // -256
}func main() {
unsigned()
signed()
}
```#### Encoding
Convert an `int64` or `uint64` to a `[]byte`:
```go
package mainimport (
"bytes"
"fmt""github.com/jcalabro/leb128"
)func unsigned() {
buf := leb128.EncodeU64(256)
fmt.Println(buf) // [128 2]
}func signed() {
buf := leb128.EncodeS64(-256)
fmt.Println(buf) // [128 126]
}func main() {
unsigned()
signed()
}
```