Skip to content

Instantly share code, notes, and snippets.

@wathiede
Created December 16, 2013 04:36
Show Gist options
  • Save wathiede/7982372 to your computer and use it in GitHub Desktop.
Save wathiede/7982372 to your computer and use it in GitHub Desktop.
Minimum size required for EXIF parsing.
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"camlistore.org/third_party/github.com/camlistore/goexif/exif"
)
// keepFirstN keeps the first N bytes written to it in Bytes.
type keepFirstN struct {
N int
Bytes []byte
}
func (w *keepFirstN) Write(p []byte) (n int, err error) {
if n := w.N - len(w.Bytes); n > 0 {
if n > len(p) {
n = len(p)
}
w.Bytes = append(w.Bytes, p[:n]...)
}
return len(p), nil
}
func tryParse(n int, fn string) error {
f, err := os.Open(fn)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var imageBuf *keepFirstN
copyDest := ioutil.Discard
imageBuf = &keepFirstN{N: n}
copyDest = io.MultiWriter(copyDest, imageBuf)
_, err = io.Copy(copyDest, f)
if err != nil {
log.Fatal(err)
}
_, err = exif.Decode(bytes.NewReader(imageBuf.Bytes))
return err
}
func main() {
for _, fn := range os.Args[1:] {
n := 1 << 30
for {
err := tryParse(n, fn)
if err != nil {
log.Println(fn, n, "too small")
break
}
n >>= 1
}
}
}
@wathiede
Copy link
Author

Example run:

$ go run ~/test/exif.go $(find www.rawsamples.ch/raws/canon/ -iname '*cr2')                                                                                            
2013/12/15 20:35:05 www.rawsamples.ch/raws/canon/g10/RAW_CANON_G10.CR2 8192 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/400d/RAW_CANON_400D_ARGB.CR2 65536 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/1dm3/RAW_CANON_1DMARK3.CR2 32768 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/20d/RAW_CANON_20D.CR2 524288 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/1dm2n/RAW_CANON_1DM2N.CR2 65536 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/30d/RAW_CANON_30D.CR2 65536 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/1dsm2/RAW_CANON_1DSM2.CR2 524288 too small
2013/12/15 20:35:06 www.rawsamples.ch/raws/canon/5d/RAW_CANON_5D_ARGB.CR2 65536 too small
2013/12/15 20:35:07 www.rawsamples.ch/raws/canon/1dm2/RAW_CANON_1DM2.CR2 524288 too small
2013/12/15 20:35:07 www.rawsamples.ch/raws/canon/350d/RAW_CANON_350D.CR2 262144 too small
2013/12/15 20:35:07 www.rawsamples.ch/raws/canon/g9/RAW_CANON_G9.CR2 16384 too small
2013/12/15 20:35:07 www.rawsamples.ch/raws/canon/1dsm3/RAW_CANON_1DSM3.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/450d/RAW_CANON_450D.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/40d/RAW_CANON_40D_RAW_V105.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/40d/RAW_CANON_40D_SRAW_V103.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/40d/RAW_CANON_40D_RAW_V103.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/40d/RAW_CANON_40D_RAW_V104.CR2 32768 too small
2013/12/15 20:35:08 www.rawsamples.ch/raws/canon/40d/RAW_CANON_40D_RAW_V336643C.CR2 32768 too small
2013/12/15 20:35:09 www.rawsamples.ch/raws/canon/50d/RAW_CANON_50D.CR2 32768 too small
2013/12/15 20:35:09 www.rawsamples.ch/raws/canon/5dm2/RAW_CANON_5DMARK2_PREPROD.CR2 32768 too small

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment