aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: b204345aac62e5353092c37ed4f993d7048791f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/bisaxa/bitreader)](https://goreportcard.com/report/github.com/bisaxa/bitreader) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/bisaxa/bitreader/blob/main/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/bisaxa/bitreader.svg)](https://pkg.go.dev/github.com/bisaxa/bitreader)
A simple bit reader with big/little-endian support for golang.\
Reads data from an existing byte array.\
Uses string manipulation (for now).\
Support reading up to 64 bits at one time.\
Checking for overflowing the data.

## Installation
```bash
$ go get github.com/bisaxa/bitreader
```

## Usage

```go
import "github.com/bisaxa/bitreader"

// data:  io.Reader  Data to read from a io stream
// le:    bool       Little-endian(true) or big-endian(false) state
reader := bitreader.Reader(data, le)

// Read First Bit
state, err := reader.ReadBool()

// Skip Bits/Bytes
err := reader.SkipBits(8)
err := reader.SkipBytes(4)

// Read Bits/Bytes
value, err := reader.ReadBytes(4)
value, err := reader.ReadBits(64) // up to 64 bits

// Wrapper functions
state := reader.TryReadBool()       // bool
value := reader.TryReadInt1()       // uint8
value := reader.TryReadInt8()       // uint8
value := reader.TryReadInt16()      // uint16
value := reader.TryReadInt32()      // uint32
value := reader.TryReadInt64()      // uint64
value := reader.TryReadFloat32()    // float32
value := reader.TryReadFloat64()    // float64
```

## Error Handling
ReadBits(x), ReadBytes(x), ReadBool(), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \
Wrapper functions, however, only returns the value and panics if an error is encountered.

## Bug Report / Feature Request
Using [Github Issues](https://github.com/BiSaXa/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added.

## Documentation

Full documentation can be found in https://pkg.go.dev/github.com/bisaxa/bitreader

## License
This project is licensed under [MIT License](LICENSE).