mirror of
https://github.com/go-gitea/gitea.git
synced 2024-08-26 21:54:19 -04:00
* update github.com/alecthomas/chroma v0.8.0 -> v0.8.1 * github.com/blevesearch/bleve v1.0.10 -> v1.0.12 * editorconfig-core-go v2.1.1 -> v2.3.7 * github.com/gliderlabs/ssh v0.2.2 -> v0.3.1 * migrate editorconfig.ParseBytes to Parse * github.com/shurcooL/vfsgen to 0d455de96546 * github.com/go-git/go-git/v5 v5.1.0 -> v5.2.0 * github.com/google/uuid v1.1.1 -> v1.1.2 * github.com/huandu/xstrings v1.3.0 -> v1.3.2 * github.com/klauspost/compress v1.10.11 -> v1.11.1 * github.com/markbates/goth v1.61.2 -> v1.65.0 * github.com/mattn/go-sqlite3 v1.14.0 -> v1.14.4 * github.com/mholt/archiver v3.3.0 -> v3.3.2 * github.com/microcosm-cc/bluemonday 4f7140c49acb -> v1.0.4 * github.com/minio/minio-go v7.0.4 -> v7.0.5 * github.com/olivere/elastic v7.0.9 -> v7.0.20 * github.com/urfave/cli v1.20.0 -> v1.22.4 * github.com/prometheus/client_golang v1.1.0 -> v1.8.0 * github.com/xanzy/go-gitlab v0.37.0 -> v0.38.1 * mvdan.cc/xurls v2.1.0 -> v2.2.0 Co-authored-by: Lauris BH <lauris@nix.lv>
249 lines
5.5 KiB
Go
Vendored
249 lines
5.5 KiB
Go
Vendored
// Copyright 2014-2019 Ulrich Kunitz. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package lzma
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
// rangeEncoder implements range encoding of single bits. The low value can
|
|
// overflow therefore we need uint64. The cache value is used to handle
|
|
// overflows.
|
|
type rangeEncoder struct {
|
|
lbw *LimitedByteWriter
|
|
nrange uint32
|
|
low uint64
|
|
cacheLen int64
|
|
cache byte
|
|
}
|
|
|
|
// maxInt64 provides the maximal value of the int64 type
|
|
const maxInt64 = 1<<63 - 1
|
|
|
|
// newRangeEncoder creates a new range encoder.
|
|
func newRangeEncoder(bw io.ByteWriter) (re *rangeEncoder, err error) {
|
|
lbw, ok := bw.(*LimitedByteWriter)
|
|
if !ok {
|
|
lbw = &LimitedByteWriter{BW: bw, N: maxInt64}
|
|
}
|
|
return &rangeEncoder{
|
|
lbw: lbw,
|
|
nrange: 0xffffffff,
|
|
cacheLen: 1}, nil
|
|
}
|
|
|
|
// Available returns the number of bytes that still can be written. The
|
|
// method takes the bytes that will be currently written by Close into
|
|
// account.
|
|
func (e *rangeEncoder) Available() int64 {
|
|
return e.lbw.N - (e.cacheLen + 4)
|
|
}
|
|
|
|
// writeByte writes a single byte to the underlying writer. An error is
|
|
// returned if the limit is reached. The written byte will be counted if
|
|
// the underlying writer doesn't return an error.
|
|
func (e *rangeEncoder) writeByte(c byte) error {
|
|
if e.Available() < 1 {
|
|
return ErrLimit
|
|
}
|
|
return e.lbw.WriteByte(c)
|
|
}
|
|
|
|
// DirectEncodeBit encodes the least-significant bit of b with probability 1/2.
|
|
func (e *rangeEncoder) DirectEncodeBit(b uint32) error {
|
|
e.nrange >>= 1
|
|
e.low += uint64(e.nrange) & (0 - (uint64(b) & 1))
|
|
|
|
// normalize
|
|
const top = 1 << 24
|
|
if e.nrange >= top {
|
|
return nil
|
|
}
|
|
e.nrange <<= 8
|
|
return e.shiftLow()
|
|
}
|
|
|
|
// EncodeBit encodes the least significant bit of b. The p value will be
|
|
// updated by the function depending on the bit encoded.
|
|
func (e *rangeEncoder) EncodeBit(b uint32, p *prob) error {
|
|
bound := p.bound(e.nrange)
|
|
if b&1 == 0 {
|
|
e.nrange = bound
|
|
p.inc()
|
|
} else {
|
|
e.low += uint64(bound)
|
|
e.nrange -= bound
|
|
p.dec()
|
|
}
|
|
|
|
// normalize
|
|
const top = 1 << 24
|
|
if e.nrange >= top {
|
|
return nil
|
|
}
|
|
e.nrange <<= 8
|
|
return e.shiftLow()
|
|
}
|
|
|
|
// Close writes a complete copy of the low value.
|
|
func (e *rangeEncoder) Close() error {
|
|
for i := 0; i < 5; i++ {
|
|
if err := e.shiftLow(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// shiftLow shifts the low value for 8 bit. The shifted byte is written into
|
|
// the byte writer. The cache value is used to handle overflows.
|
|
func (e *rangeEncoder) shiftLow() error {
|
|
if uint32(e.low) < 0xff000000 || (e.low>>32) != 0 {
|
|
tmp := e.cache
|
|
for {
|
|
err := e.writeByte(tmp + byte(e.low>>32))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmp = 0xff
|
|
e.cacheLen--
|
|
if e.cacheLen <= 0 {
|
|
if e.cacheLen < 0 {
|
|
panic("negative cacheLen")
|
|
}
|
|
break
|
|
}
|
|
}
|
|
e.cache = byte(uint32(e.low) >> 24)
|
|
}
|
|
e.cacheLen++
|
|
e.low = uint64(uint32(e.low) << 8)
|
|
return nil
|
|
}
|
|
|
|
// rangeDecoder decodes single bits of the range encoding stream.
|
|
type rangeDecoder struct {
|
|
br io.ByteReader
|
|
nrange uint32
|
|
code uint32
|
|
}
|
|
|
|
// init initializes the range decoder, by reading from the byte reader.
|
|
func (d *rangeDecoder) init() error {
|
|
d.nrange = 0xffffffff
|
|
d.code = 0
|
|
|
|
b, err := d.br.ReadByte()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if b != 0 {
|
|
return errors.New("newRangeDecoder: first byte not zero")
|
|
}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
if err = d.updateCode(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if d.code >= d.nrange {
|
|
return errors.New("newRangeDecoder: d.code >= d.nrange")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// newRangeDecoder initializes a range decoder. It reads five bytes from the
|
|
// reader and therefore may return an error.
|
|
func newRangeDecoder(br io.ByteReader) (d *rangeDecoder, err error) {
|
|
d = &rangeDecoder{br: br, nrange: 0xffffffff}
|
|
|
|
b, err := d.br.ReadByte()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if b != 0 {
|
|
return nil, errors.New("newRangeDecoder: first byte not zero")
|
|
}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
if err = d.updateCode(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if d.code >= d.nrange {
|
|
return nil, errors.New("newRangeDecoder: d.code >= d.nrange")
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
// possiblyAtEnd checks whether the decoder may be at the end of the stream.
|
|
func (d *rangeDecoder) possiblyAtEnd() bool {
|
|
return d.code == 0
|
|
}
|
|
|
|
// DirectDecodeBit decodes a bit with probability 1/2. The return value b will
|
|
// contain the bit at the least-significant position. All other bits will be
|
|
// zero.
|
|
func (d *rangeDecoder) DirectDecodeBit() (b uint32, err error) {
|
|
d.nrange >>= 1
|
|
d.code -= d.nrange
|
|
t := 0 - (d.code >> 31)
|
|
d.code += d.nrange & t
|
|
b = (t + 1) & 1
|
|
|
|
// d.code will stay less then d.nrange
|
|
|
|
// normalize
|
|
// assume d.code < d.nrange
|
|
const top = 1 << 24
|
|
if d.nrange >= top {
|
|
return b, nil
|
|
}
|
|
d.nrange <<= 8
|
|
// d.code < d.nrange will be maintained
|
|
return b, d.updateCode()
|
|
}
|
|
|
|
// decodeBit decodes a single bit. The bit will be returned at the
|
|
// least-significant position. All other bits will be zero. The probability
|
|
// value will be updated.
|
|
func (d *rangeDecoder) DecodeBit(p *prob) (b uint32, err error) {
|
|
bound := p.bound(d.nrange)
|
|
if d.code < bound {
|
|
d.nrange = bound
|
|
p.inc()
|
|
b = 0
|
|
} else {
|
|
d.code -= bound
|
|
d.nrange -= bound
|
|
p.dec()
|
|
b = 1
|
|
}
|
|
// normalize
|
|
// assume d.code < d.nrange
|
|
const top = 1 << 24
|
|
if d.nrange >= top {
|
|
return b, nil
|
|
}
|
|
d.nrange <<= 8
|
|
// d.code < d.nrange will be maintained
|
|
return b, d.updateCode()
|
|
}
|
|
|
|
// updateCode reads a new byte into the code.
|
|
func (d *rangeDecoder) updateCode() error {
|
|
b, err := d.br.ReadByte()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.code = (d.code << 8) | uint32(b)
|
|
return nil
|
|
}
|