Merge pull request #5851 from proppy/archive-simple-detect

archive: simplify DetectCompression
This commit is contained in:
Guillaume J. Charmes 2014-05-16 16:32:55 -07:00
commit 6f651ec62b

View file

@ -2,6 +2,7 @@ package archive
import (
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"errors"
@ -42,26 +43,16 @@ const (
)
func DetectCompression(source []byte) Compression {
sourceLen := len(source)
for compression, m := range map[Compression][]byte{
Bzip2: {0x42, 0x5A, 0x68},
Gzip: {0x1F, 0x8B, 0x08},
Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
} {
fail := false
if len(m) > sourceLen {
if len(source) < len(m) {
utils.Debugf("Len too short")
continue
}
i := 0
for _, b := range m {
if b != source[i] {
fail = true
break
}
i++
}
if !fail {
if bytes.Compare(m, source[:len(m)]) == 0 {
return compression
}
}