Commit 4c60569e authored by Nigel Tao's avatar Nigel Tao

image: allow "?" wildcards when registering image formats.

R=r, nigeltao_gnome
CC=golang-dev
https://golang.org/cl/4404041
parent a2e28682
...@@ -25,7 +25,8 @@ var formats []format ...@@ -25,7 +25,8 @@ var formats []format
// RegisterFormat registers an image format for use by Decode. // RegisterFormat registers an image format for use by Decode.
// Name is the name of the format, like "jpeg" or "png". // Name is the name of the format, like "jpeg" or "png".
// Magic is the magic prefix that identifies the format's encoding. // Magic is the magic prefix that identifies the format's encoding. The magic
// string can contain "?" wildcards that each match any one byte.
// Decode is the function that decodes the encoded image. // Decode is the function that decodes the encoded image.
// DecodeConfig is the function that decodes just its configuration. // DecodeConfig is the function that decodes just its configuration.
func RegisterFormat(name, magic string, decode func(io.Reader) (Image, os.Error), decodeConfig func(io.Reader) (Config, os.Error)) { func RegisterFormat(name, magic string, decode func(io.Reader) (Image, os.Error), decodeConfig func(io.Reader) (Config, os.Error)) {
...@@ -46,11 +47,24 @@ func asReader(r io.Reader) reader { ...@@ -46,11 +47,24 @@ func asReader(r io.Reader) reader {
return bufio.NewReader(r) return bufio.NewReader(r)
} }
// sniff determines the format of r's data. // Match returns whether magic matches b. Magic may contain "?" wildcards.
func match(magic string, b []byte) bool {
if len(magic) != len(b) {
return false
}
for i, c := range b {
if magic[i] != c && magic[i] != '?' {
return false
}
}
return true
}
// Sniff determines the format of r's data.
func sniff(r reader) format { func sniff(r reader) format {
for _, f := range formats { for _, f := range formats {
s, err := r.Peek(len(f.magic)) b, err := r.Peek(len(f.magic))
if err == nil && string(s) == f.magic { if err == nil && match(f.magic, b) {
return f return f
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment