From 6ff5acae03650fd2dcf693ff953e74731cc3ab82 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Tue, 19 Oct 2021 01:53:37 +0200 Subject: [PATCH] gif: Support GIF87a --- format/gif/gif.go | 3 ++- pkg/decode/decode.go | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/format/gif/gif.go b/format/gif/gif.go index 689c0129..451652ea 100644 --- a/format/gif/gif.go +++ b/format/gif/gif.go @@ -53,7 +53,8 @@ func fieldColorMap(d *decode.D, name string, bitDepth int) { func gifDecode(d *decode.D, in interface{}) interface{} { d.Endian = decode.LittleEndian - d.FieldValidateUTF8("header", "GIF89a") + d.FieldValidateUTF8Any("header", 6, []string{"GIF87a", "GIF89a"}) + d.FieldU16("width") d.FieldU16("height") gcpFollows := d.FieldBool("gcp_follows") diff --git a/pkg/decode/decode.go b/pkg/decode/decode.go index 79e2f699..da836f04 100644 --- a/pkg/decode/decode.go +++ b/pkg/decode/decode.go @@ -727,25 +727,31 @@ func (d *D) FieldValidateUTF8Fn(name string, v string, fn func() string) { } } -func (d *D) FieldValidateUTF8(name string, v string) { +func (d *D) FieldValidateUTF8Any(name string, nBytes int, vs []string) { pos := d.Pos() + found := false s := d.FieldStrFn(name, func() (string, string) { - nBytes := len(v) str, err := d.TryUTF8(nBytes) if err != nil { panic(IOError{Err: err, Name: name, Op: "FieldValidateUTF8", Size: int64(nBytes) * 8, Pos: d.Pos()}) } - s := "Correct" - if str != v { - s = "Incorrect" + for _, v := range vs { + if v == str { + found = true + return str, "Correct" + } } - return str, s + return str, "Incorrect" }) - if s != v { - panic(ValidateError{Reason: fmt.Sprintf("expected %s found %s", v, s), Pos: pos}) + if !found { + panic(ValidateError{Reason: fmt.Sprintf("expected any of %v found %s", vs, s), Pos: pos}) } } +func (d *D) FieldValidateUTF8(name string, v string) { + d.FieldValidateUTF8Any(name, len(v), []string{v}) +} + func (d *D) ValidateAtLeastBitsLeft(nBits int64) { bl := d.BitsLeft() if bl < nBits {