Skip to content

Commit

Permalink
gif: Support GIF87a
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Oct 18, 2021
1 parent b643e22 commit 6ff5aca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion format/gif/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 14 additions & 8 deletions pkg/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6ff5aca

Please sign in to comment.