1
0
mirror of https://github.com/golang/go synced 2024-11-18 21:05:02 -07:00

image/jpeg: check for component uniqueness and total sampling factors.

Change-Id: I83de9d83708edc8d196bbcfdc7d2ba7ffaff50d2
Reviewed-on: https://go-review.googlesource.com/6586
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Nigel Tao 2015-03-04 17:42:39 +11:00
parent d2918cbcaa
commit 9b73ecc327
2 changed files with 27 additions and 0 deletions

View File

@ -318,7 +318,16 @@ func (d *decoder) processSOF(n int) error {
} }
for i := 0; i < d.nComp; i++ { for i := 0; i < d.nComp; i++ {
d.comp[i].c = d.tmp[6+3*i] d.comp[i].c = d.tmp[6+3*i]
// Section B.2.2 states that "the value of C_i shall be different from
// the values of C_1 through C_(i-1)".
for j := 0; j < i; j++ {
if d.comp[i].c == d.comp[j].c {
return FormatError("repeated component identifier")
}
}
d.comp[i].tq = d.tmp[8+3*i] d.comp[i].tq = d.tmp[8+3*i]
if d.nComp == 1 { if d.nComp == 1 {
// If a JPEG image has only one component, section A.2 says "this data // If a JPEG image has only one component, section A.2 says "this data
// is non-interleaved by definition" and section A.2.2 says "[in this // is non-interleaved by definition" and section A.2.2 says "[in this

View File

@ -63,6 +63,7 @@ func (d *decoder) processSOS(n int) error {
td uint8 // DC table selector. td uint8 // DC table selector.
ta uint8 // AC table selector. ta uint8 // AC table selector.
} }
totalHV := 0
for i := 0; i < nComp; i++ { for i := 0; i < nComp; i++ {
cs := d.tmp[1+2*i] // Component selector. cs := d.tmp[1+2*i] // Component selector.
compIndex := -1 compIndex := -1
@ -75,6 +76,18 @@ func (d *decoder) processSOS(n int) error {
return FormatError("unknown component selector") return FormatError("unknown component selector")
} }
scan[i].compIndex = uint8(compIndex) scan[i].compIndex = uint8(compIndex)
// Section B.2.3 states that "the value of Cs_j shall be different from
// the values of Cs_1 through Cs_(j-1)". Since we have previously
// verified that a frame's component identifiers (C_i values in section
// B.2.2) are unique, it suffices to check that the implicit indexes
// into d.comp are unique.
for j := 0; j < i; j++ {
if scan[i].compIndex == scan[j].compIndex {
return FormatError("repeated component selector")
}
}
totalHV += d.comp[compIndex].h * d.comp[compIndex].v
scan[i].td = d.tmp[2+2*i] >> 4 scan[i].td = d.tmp[2+2*i] >> 4
if scan[i].td > maxTh { if scan[i].td > maxTh {
return FormatError("bad Td value") return FormatError("bad Td value")
@ -84,6 +97,11 @@ func (d *decoder) processSOS(n int) error {
return FormatError("bad Ta value") return FormatError("bad Ta value")
} }
} }
// Section B.2.3 states that if there is more than one component then the
// total H*V values in a scan must be <= 10.
if d.nComp > 1 && totalHV > 10 {
return FormatError("total sampling factors too large")
}
// zigStart and zigEnd are the spectral selection bounds. // zigStart and zigEnd are the spectral selection bounds.
// ah and al are the successive approximation high and low values. // ah and al are the successive approximation high and low values.