mirror of
https://github.com/go-gitea/gitea
synced 2024-11-08 19:24:25 +00:00
c5d5d63c9c
* update macaron to v1.5 of fork * update macaron to v1.5 of fork * test gzip PR * add push method impl to context_tests * use proper gzip commit Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
105 lines
2.4 KiB
Go
Vendored
105 lines
2.4 KiB
Go
Vendored
package zstd
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/klauspost/compress/huff0"
|
|
)
|
|
|
|
type dict struct {
|
|
id uint32
|
|
|
|
litDec *huff0.Scratch
|
|
llDec, ofDec, mlDec sequenceDec
|
|
offsets [3]int
|
|
content []byte
|
|
}
|
|
|
|
var dictMagic = [4]byte{0x37, 0xa4, 0x30, 0xec}
|
|
|
|
// Load a dictionary as described in
|
|
// https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format
|
|
func loadDict(b []byte) (*dict, error) {
|
|
// Check static field size.
|
|
if len(b) <= 8+(3*4) {
|
|
return nil, io.ErrUnexpectedEOF
|
|
}
|
|
d := dict{
|
|
llDec: sequenceDec{fse: &fseDecoder{}},
|
|
ofDec: sequenceDec{fse: &fseDecoder{}},
|
|
mlDec: sequenceDec{fse: &fseDecoder{}},
|
|
}
|
|
if !bytes.Equal(b[:4], dictMagic[:]) {
|
|
return nil, ErrMagicMismatch
|
|
}
|
|
d.id = binary.LittleEndian.Uint32(b[4:8])
|
|
if d.id == 0 {
|
|
return nil, errors.New("dictionaries cannot have ID 0")
|
|
}
|
|
|
|
// Read literal table
|
|
var err error
|
|
d.litDec, b, err = huff0.ReadTable(b[8:], nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
br := byteReader{
|
|
b: b,
|
|
off: 0,
|
|
}
|
|
readDec := func(i tableIndex, dec *fseDecoder) error {
|
|
if err := dec.readNCount(&br, uint16(maxTableSymbol[i])); err != nil {
|
|
return err
|
|
}
|
|
if br.overread() {
|
|
return io.ErrUnexpectedEOF
|
|
}
|
|
err = dec.transform(symbolTableX[i])
|
|
if err != nil {
|
|
println("Transform table error:", err)
|
|
return err
|
|
}
|
|
if debug {
|
|
println("Read table ok", "symbolLen:", dec.symbolLen)
|
|
}
|
|
// Set decoders as predefined so they aren't reused.
|
|
dec.preDefined = true
|
|
return nil
|
|
}
|
|
|
|
if err := readDec(tableOffsets, d.ofDec.fse); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := readDec(tableMatchLengths, d.mlDec.fse); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := readDec(tableLiteralLengths, d.llDec.fse); err != nil {
|
|
return nil, err
|
|
}
|
|
if br.remain() < 12 {
|
|
return nil, io.ErrUnexpectedEOF
|
|
}
|
|
|
|
d.offsets[0] = int(br.Uint32())
|
|
br.advance(4)
|
|
d.offsets[1] = int(br.Uint32())
|
|
br.advance(4)
|
|
d.offsets[2] = int(br.Uint32())
|
|
br.advance(4)
|
|
if d.offsets[0] <= 0 || d.offsets[1] <= 0 || d.offsets[2] <= 0 {
|
|
return nil, errors.New("invalid offset in dictionary")
|
|
}
|
|
d.content = make([]byte, br.remain())
|
|
copy(d.content, br.unread())
|
|
if d.offsets[0] > len(d.content) || d.offsets[1] > len(d.content) || d.offsets[2] > len(d.content) {
|
|
return nil, fmt.Errorf("initial offset bigger than dictionary content size %d, offsets: %v", len(d.content), d.offsets)
|
|
}
|
|
|
|
return &d, nil
|
|
}
|