1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-15 01:46:03 +00:00
arikawa/internal/zlib/flate.go
Maximilian von Lindern 32789bb6e2 *: Separate utils and internal (#129)
* Utils: move package utils/heart to internal/heart

* Utils: move package utils/moreatomic to internal/moreatomic

* Utils: move package utils/zlib to internal/zlib
2020-07-29 16:58:33 -07:00

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
}