mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-01 12:34:28 +00:00
32789bb6e2
* Utils: move package utils/heart to internal/heart * Utils: move package utils/moreatomic to internal/moreatomic * Utils: move package utils/zlib to internal/zlib
40 lines
669 B
Go
40 lines
669 B
Go
package zlib
|
|
|
|
import (
|
|
"compress/flate"
|
|
"compress/zlib"
|
|
"io"
|
|
)
|
|
|
|
type Reader interface {
|
|
io.ReadCloser
|
|
zlib.Resetter
|
|
}
|
|
|
|
func zlibStreamer(r flate.Reader) (Reader, error) {
|
|
// verify header
|
|
h := make([]byte, 2)
|
|
|
|
if _, err := io.ReadFull(r, h); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// verify header
|
|
if err := verifyHeader(h); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return flate.NewReader(r).(Reader), nil
|
|
}
|
|
|
|
// https://golang.org/src/compress/zlib/reader.go#L35
|
|
const zlibDeflate = 8
|
|
|
|
func verifyHeader(scratch []byte) error {
|
|
h := uint(scratch[0])<<8 | uint(scratch[1])
|
|
if (scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) {
|
|
return zlib.ErrHeader
|
|
}
|
|
return nil
|
|
}
|