leb128

package module
v0.0.0-...-d3722dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2019 License: BSD-3-Clause Imports: 0 Imported by: 7

README

leb128

Travis Coverage Status Release Go Report Card License GoDoc

Go implementation of Little Endian Base 128 codec.

Install

$ go get -u ekyu.moe/leb128
# or better
$ dep ensure -add ekyu.moe/leb128

Example

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
}

LICENSE

BSD-3-clause

Notes: The encode part is an edited fork of /src/cmd/internal/dwarf/dwarf.go licensed under a BSD-style license.

Documentation

Overview

Package leb128 implements LEB128 codec.

The encode part is a fork of https://golang.org/src/cmd/internal/dwarf/dwarf.go

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendSleb128

func AppendSleb128(b []byte, v int64) []byte

AppendSleb128 appends v to b using signed LEB128 encoding.

Example
package main

import (
	"fmt"

	"ekyu.moe/leb128"
)

func main() {
	fmt.Printf("Encoded: %x\n", leb128.AppendSleb128(nil, -624485))
}
Output:

Encoded: 9bf159

func AppendUleb128

func AppendUleb128(b []byte, v uint64) []byte

AppendUleb128 appends v to b using unsigned LEB128 encoding.

Example
package main

import (
	"fmt"

	"ekyu.moe/leb128"
)

func main() {
	fmt.Printf("Encoded: %x\n", leb128.AppendUleb128(nil, 624485))
}
Output:

Encoded: e58e26

func DecodeSleb128

func DecodeSleb128(b []byte) (s int64, n uint8)

DecodeSleb128 decodes b to s with signed LEB128 encoding and returns the number of bytes read. On error (bad encoded b), n will be 0 and therefore s must not be trusted.

Example
package main

import (
	"fmt"

	"ekyu.moe/leb128"
)

func main() {
	s, n := leb128.DecodeSleb128([]byte{0x9b, 0xf1, 0x59, 'd', 'e', 'f'})
	fmt.Printf("Decoded: %d\nRead: %d bytes", s, n)
}
Output:

Decoded: -624485
Read: 3 bytes

func DecodeUleb128

func DecodeUleb128(b []byte) (u uint64, n uint8)

DecodeUleb128 decodes b to u with unsigned LEB128 encoding and returns the number of bytes read. On error (bad encoded b), n will be 0 and therefore u must not be trusted.

Example
package main

import (
	"fmt"

	"ekyu.moe/leb128"
)

func main() {
	u, n := leb128.DecodeUleb128([]byte{0xe5, 0x8e, 0x26, 'a', 'b', 'c'})
	fmt.Printf("Decoded: %d\nRead: %d bytes", u, n)
}
Output:

Decoded: 624485
Read: 3 bytes

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL