Skip to content

Commit

Permalink
Read JPEG Tables field correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Jul 8, 2021
1 parent 8be2bd6 commit fdb89ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tiff/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
dtShort = 3
dtLong = 4
dtRational = 5

dtUndefined = 7 // JPEGTables field is required to have type code UNDEFINED
)

// The length of one instance of each data type in bytes.
Expand Down
20 changes: 15 additions & 5 deletions tiff/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,25 @@ func (d *decoder) ifdUint(p []byte) (u []uint, err error) {
return nil, FormatError("bad IFD entry")
}

tag := d.byteOrder.Uint16(p[0:2])
datatype := d.byteOrder.Uint16(p[2:4])
if dt := int(datatype); dt <= 0 || dt >= len(lengths) {
if dt := int(datatype); dt <= 0 || dt >= len(lengths) && tag != tJPEG {
return nil, UnsupportedError("IFD entry datatype")
}

// tJPEG's type is dtUndefined which size is same as dtByte.
var length uint32
if tag != tJPEG {
length = lengths[datatype]
} else {
length = 1
}

count := d.byteOrder.Uint32(p[4:8])
if count > math.MaxInt32/lengths[datatype] {
if count > math.MaxInt32/length {
return nil, FormatError("IFD data too large")
}
if datalen := lengths[datatype] * count; datalen > 4 {
if datalen := length * count; datalen > 4 {
// The IFD contains a pointer to the real value.
raw = make([]byte, datalen)
_, err = d.r.ReadAt(raw, int64(d.byteOrder.Uint32(p[8:12])))
Expand All @@ -98,7 +107,7 @@ func (d *decoder) ifdUint(p []byte) (u []uint, err error) {

u = make([]uint, count)
switch datatype {
case dtByte:
case dtByte, dtUndefined:
for i := uint32(0); i < count; i++ {
u[i] = uint(raw[i])
}
Expand Down Expand Up @@ -138,7 +147,8 @@ func (d *decoder) parseIFD(p []byte) (int, error) {
tImageWidth,
tFillOrder,
tT4Options,
tT6Options:
tT6Options,
tJPEG:
val, err := d.ifdUint(p)
if err != nil {
return 0, err
Expand Down

0 comments on commit fdb89ce

Please sign in to comment.