mirror of
https://github.com/muun/recovery.git
synced 2025-11-10 14:09:50 -05:00
Release v0.1.0
This commit is contained in:
2
vendor/github.com/kkdai/bstream/.gitignore
generated
vendored
Normal file
2
vendor/github.com/kkdai/bstream/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
debug.test
|
||||
.vscode
|
||||
15
vendor/github.com/kkdai/bstream/.travis.yml
generated
vendored
Normal file
15
vendor/github.com/kkdai/bstream/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.6
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
|
||||
script:
|
||||
- go vet ./...
|
||||
# - $HOME/gopath/bin/goveralls -coverprofile=coverage.cov -service=travis-ci
|
||||
# - bash <(curl -s https://codecov.io/bash)
|
||||
- go test -bench=. -benchmem ./...
|
||||
#- sh ./install_all_cmd.sh
|
||||
22
vendor/github.com/kkdai/bstream/LICENSE
generated
vendored
Normal file
22
vendor/github.com/kkdai/bstream/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Evan Lin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
61
vendor/github.com/kkdai/bstream/README.md
generated
vendored
Normal file
61
vendor/github.com/kkdai/bstream/README.md
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
BStream: A Bit Stream helper in Golang
|
||||
==================
|
||||
|
||||
[](https://raw.githubusercontent.com/kkdai/bloomfilter/master/LICENSE) [](https://godoc.org/github.com/kkdai/bstream) [](https://travis-ci.org/kkdai/bstream)[
|
||||
](https://goreportcard.com/report/github.com/kkdai/bstream)
|
||||
|
||||
|
||||
|
||||
Install
|
||||
---------------
|
||||
`go get github.com/kkdai/bstream`
|
||||
|
||||
|
||||
Usage
|
||||
---------------
|
||||
|
||||
```go
|
||||
//New a bit stream writer with default 5 byte
|
||||
b := NewBStreamWriter(5)
|
||||
|
||||
//Write 0xa0a0 into bstream
|
||||
b.WriteBits(0xa0a0, 16)
|
||||
|
||||
//Read 4 bit out
|
||||
result, err := b.ReadBits(4)
|
||||
if err != nil {
|
||||
log.Printf("result:%x", result)
|
||||
//result:a
|
||||
}
|
||||
```
|
||||
|
||||
Notes
|
||||
---------------
|
||||
|
||||
- BStream is *not* thread safe. When you need to use it from different goroutines, you need to manage locking yourself.
|
||||
- You can use the same `*BStream` object for reading & writing. However note that currently it is possible to read zero-value data if you read more bits than what has been written.
|
||||
- BStream will *not* modify the data provided to `NewBStreamReader`. You can reuse the same `*BStream` object for writing and it will allocate a new underlying buffer.
|
||||
|
||||
Inspired
|
||||
---------------
|
||||
|
||||
- [https://github.com/dgryski/go-tsz](https://github.com/dgryski/go-tsz)
|
||||
|
||||
Benchmark
|
||||
---------------
|
||||
```
|
||||
BenchmarkWriteBits-4 100000000 15.3 ns/op
|
||||
BenchmarkReadBits-4 50000000 26.5 ns/op
|
||||
```
|
||||
|
||||
Project52
|
||||
---------------
|
||||
|
||||
It is one of my [project 52](https://github.com/kkdai/project52).
|
||||
|
||||
|
||||
License
|
||||
---------------
|
||||
|
||||
This package is licensed under MIT license. See LICENSE for details.
|
||||
|
||||
179
vendor/github.com/kkdai/bstream/bstream.go
generated
vendored
Normal file
179
vendor/github.com/kkdai/bstream/bstream.go
generated
vendored
Normal file
@@ -0,0 +1,179 @@
|
||||
package bstream
|
||||
|
||||
import "io"
|
||||
|
||||
type bit bool
|
||||
|
||||
const (
|
||||
zero bit = false
|
||||
one bit = true
|
||||
)
|
||||
|
||||
//BStream :
|
||||
type BStream struct {
|
||||
stream []byte
|
||||
rCount uint8 // Number of bits still unread from the first byte of the stream
|
||||
wCount uint8 // Number of bits still empty in the last byte of the stream
|
||||
}
|
||||
|
||||
//NewBStreamReader :
|
||||
func NewBStreamReader(data []byte) *BStream {
|
||||
return &BStream{stream: data, rCount: 8}
|
||||
}
|
||||
|
||||
//NewBStreamWriter :
|
||||
func NewBStreamWriter(nByte uint8) *BStream {
|
||||
return &BStream{stream: make([]byte, 0, nByte), rCount: 8}
|
||||
}
|
||||
|
||||
//WriteBit :
|
||||
func (b *BStream) WriteBit(input bit) {
|
||||
if b.wCount == 0 {
|
||||
b.stream = append(b.stream, 0)
|
||||
b.wCount = 8
|
||||
}
|
||||
|
||||
latestIndex := len(b.stream) - 1
|
||||
if input {
|
||||
b.stream[latestIndex] |= 1 << (b.wCount - 1)
|
||||
}
|
||||
b.wCount--
|
||||
}
|
||||
|
||||
//WriteOneByte :
|
||||
func (b *BStream) WriteOneByte(data byte) {
|
||||
if b.wCount == 0 {
|
||||
b.stream = append(b.stream, data)
|
||||
return
|
||||
}
|
||||
|
||||
latestIndex := len(b.stream) - 1
|
||||
|
||||
b.stream[latestIndex] |= data >> (8 - b.wCount)
|
||||
b.stream = append(b.stream, 0)
|
||||
latestIndex++
|
||||
b.stream[latestIndex] = data << b.wCount
|
||||
}
|
||||
|
||||
//WriteBits :
|
||||
func (b *BStream) WriteBits(data uint64, count int) {
|
||||
data <<= uint(64 - count)
|
||||
|
||||
//handle write byte if count over 8
|
||||
for count >= 8 {
|
||||
byt := byte(data >> (64 - 8))
|
||||
b.WriteOneByte(byt)
|
||||
|
||||
data <<= 8
|
||||
count -= 8
|
||||
}
|
||||
|
||||
//handle write bit
|
||||
for count > 0 {
|
||||
bi := data >> (64 - 1)
|
||||
b.WriteBit(bi == 1)
|
||||
|
||||
data <<= 1
|
||||
count--
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BStream) ReadBit() (bit, error) {
|
||||
//empty return io.EOF
|
||||
if len(b.stream) == 0 {
|
||||
return zero, io.EOF
|
||||
}
|
||||
|
||||
//if first byte already empty, move to next byte to retrieval
|
||||
if b.rCount == 0 {
|
||||
b.stream = b.stream[1:]
|
||||
|
||||
if len(b.stream) == 0 {
|
||||
return zero, io.EOF
|
||||
}
|
||||
|
||||
b.rCount = 8
|
||||
}
|
||||
|
||||
// handle bit retrieval
|
||||
retBit := b.stream[0] & (1 << (b.rCount - 1))
|
||||
b.rCount--
|
||||
|
||||
return retBit != 0, nil
|
||||
}
|
||||
|
||||
func (b *BStream) ReadByte() (byte, error) {
|
||||
//empty return io.EOF
|
||||
if len(b.stream) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
//if first byte already empty, move to next byte to retrieval
|
||||
if b.rCount == 0 {
|
||||
b.stream = b.stream[1:]
|
||||
|
||||
if len(b.stream) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
b.rCount = 8
|
||||
}
|
||||
|
||||
//just remain 8 bit, just return this byte directly
|
||||
if b.rCount == 8 {
|
||||
byt := b.stream[0]
|
||||
b.stream = b.stream[1:]
|
||||
return byt, nil
|
||||
}
|
||||
|
||||
//handle byte retrieval
|
||||
retByte := b.stream[0] << (8 - b.rCount)
|
||||
b.stream = b.stream[1:]
|
||||
|
||||
//check if we could finish retrieval on next byte
|
||||
if len(b.stream) == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
//handle remain bit on next stream
|
||||
retByte |= b.stream[0] >> b.rCount
|
||||
return retByte, nil
|
||||
}
|
||||
|
||||
//ReadBits :
|
||||
func (b *BStream) ReadBits(count int) (uint64, error) {
|
||||
|
||||
var retValue uint64
|
||||
|
||||
//handle byte reading
|
||||
for count >= 8 {
|
||||
retValue <<= 8
|
||||
byt, err := b.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
retValue |= uint64(byt)
|
||||
count = count - 8
|
||||
}
|
||||
|
||||
for count > 0 {
|
||||
retValue <<= 1
|
||||
bi, err := b.ReadBit()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if bi {
|
||||
retValue |= 1
|
||||
}
|
||||
|
||||
count--
|
||||
}
|
||||
|
||||
return retValue, nil
|
||||
}
|
||||
|
||||
// Bytes returns the bytes in the stream - taken from
|
||||
// https://github.com/dgryski/go-tsz/bstream.go
|
||||
func (b *BStream) Bytes() []byte {
|
||||
return b.stream
|
||||
}
|
||||
Reference in New Issue
Block a user