Skip to content

Instantly share code, notes, and snippets.

@mpfund
Created March 6, 2015 12:40
Show Gist options
  • Save mpfund/ab6194853cec733b9d3d to your computer and use it in GitHub Desktop.
Save mpfund/ab6194853cec733b9d3d to your computer and use it in GitHub Desktop.
find gzip in streams
package main;
import (
"fmt"
"flag"
"bufio"
"os"
"compress/gzip"
"strconv"
"io"
)
var fileName = flag.String("f", "", "filename")
var detectBytes = []byte{0x1f,0x8b}
func main(){
flag.Parse();
file, err := os.Open(*fileName)
if err!=nil {
panic(err)
}
defer file.Close()
r := bufio.NewReader(file)
var bcount int64 = 0
for true {
bval, err := r.ReadByte();
if err != nil {
fmt.Println(err.Error())
break;
}
if bval == detectBytes[0] {
r.UnreadByte()
nextBytes,_ := r.Peek(4)
var arr = [4]byte{}
copy(arr[:],nextBytes[0:3])
if isGzipHeader(arr){
fmt.Println("Found gzip header, start at:", bcount, arr)
gzipReader, err := gzip.NewReader(r)
if err == nil {
fw, err := os.Create("test" + strconv.FormatInt(bcount,10) + ".raw" )
if err == nil {
defer fw.Close()
bytescopied,err:=io.Copy(fw,gzipReader)
if err != nil{
fmt.Println(err.Error())
} else{
fmt.Println("bytes copied ",bytescopied)
}
} else {
fmt.Println(err.Error())
}
} else {
fmt.Println(err.Error())
}
} else {
// read byte again to proceed
r.ReadByte();
}
}
bcount+=1;
}
fmt.Printf("done, read %d bytes",bcount)
}
func isGzipHeader(data [4]byte) bool{
var flags = []byte{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}
if !testEq(data[0:2], detectBytes){
return false
}
var compressionMethod = data[2]
if compressionMethod >= 9 {
return false
}
hasValidFlag:=false;
for x:= range(flags){
if flags[x]==data[3]{
hasValidFlag = true
}
}
return hasValidFlag
}
func testEq(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment