aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 6cdb38c35306382d30c49cfc394eecb45d06c3af (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
57
58
59
60
61
62
63
# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader)
A simple bit reader with big/little-endian support for golang.\
Reads stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).\
Uses bitwise operations.\
Support reading up to 64 bits at one time.\
Includes wrapper functions for most used data types.\
Error checking on all but wrapper functions.

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

## Usage

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

// data:  io.Reader  Data to read from an 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

// Read String
text, err := reader.ReadString() // null-terminated
text, err := reader.ReadStringLen(256) // length-specified

// Wrapper functions
text := reader.TryReadString()      // string
text := reader.TryReadStringLen(64) // string
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(), ReadString(), ReadStringLen(x) 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/pektezol/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/pektezol/bitreader

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