Last active
August 29, 2015 14:16
-
-
Save mpfund/aacf06fe03338fc828c2 to your computer and use it in GitHub Desktop.
find png in dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"image/png" | |
"io" | |
"os" | |
"strconv" | |
) | |
var fileName = flag.String("f", "", "filename") | |
var pngHeader = []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a} | |
func main() { | |
flag.Parse() | |
file, err := os.Open(*fileName) | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
r := bufio.NewReader(file) | |
var fileCount int64 = 0 | |
for true { | |
bval, err := r.ReadByte() | |
if err == io.EOF { | |
break | |
} | |
check(err) | |
if bval == pngHeader[0] { | |
// unread last byte and read full header bytes | |
r.UnreadByte() | |
nextBytes, _ := r.Peek(len(pngHeader)) | |
if !isPngHeader(nextBytes) { | |
// not a png header ? | |
// read byte again to continue | |
r.ReadByte() | |
continue | |
} | |
// ok we found a header, try to decode it. | |
fmt.Println("Found png header, try to decode.") | |
img, err := png.Decode(r) | |
if err != nil { | |
// can't decode, skip it. | |
// the png.Decode reads some bytes | |
// so we already p | |
continue | |
} | |
fw, err := os.Create("test" + strconv.FormatInt(fileCount, 10) + ".png") | |
// can't create new file ? theres something wrong. panic! | |
check(err) | |
defer fw.Close() | |
png.Encode(fw, img) | |
fileCount += 1 | |
} | |
} | |
fmt.Printf("done") | |
} | |
func isPngHeader(header []byte) bool { | |
// easy header check for png | |
return testEq(header, pngHeader) | |
} | |
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 | |
} | |
func check(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment