mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
script: add print-jpeg-markers.go
Also make truncate-progressive-jpeg.go work with reset markers.
This commit is contained in:
1 parent
d89ec2d40b
commit
268b959462
2 files changed
+165
-8
No files matched your search
@@ -52,13 +52,14 @@ func main1() error {
|
||||
prefix = prefix[:len(prefix)-5]
|
||||
}
|
||||
|
||||
pos := 0
|
||||
if len(src) < 2 {
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
return posError(pos)
|
||||
}
|
||||
scanNumber := 0
|
||||
for pos := 0; pos <= (len(src) - 2); {
|
||||
for pos <= (len(src) - 2) {
|
||||
if src[pos] != 0xFF {
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
return posError(pos)
|
||||
}
|
||||
marker := src[pos+1]
|
||||
pos += 2
|
||||
@@ -72,12 +73,12 @@ func main1() error {
|
||||
}
|
||||
|
||||
if pos >= (len(src) - 2) {
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
return posError(pos)
|
||||
}
|
||||
payloadLength := (int(src[pos]) << 8) | int(src[pos+1])
|
||||
pos += payloadLength
|
||||
if (payloadLength < 2) || (pos < 0) || (pos > len(src)) {
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
return posError(pos)
|
||||
}
|
||||
|
||||
if marker != 0xDA { // SOS (Start Of Scan) marker.
|
||||
@@ -87,8 +88,8 @@ func main1() error {
|
||||
if src[pos] != 0xFF {
|
||||
pos += 1
|
||||
} else if pos >= (len(src) - 1) {
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
} else if src[pos+1] != 0x00 {
|
||||
return posError(pos)
|
||||
} else if m := src[pos+1]; (m != 0x00) && ((m < 0xD0) || (0xD7 < m)) {
|
||||
break
|
||||
} else {
|
||||
pos += 2
|
||||
@@ -104,5 +105,9 @@ func main1() error {
|
||||
}
|
||||
fmt.Printf("Wrote %s (cut at 0x%08X = %6d bytes).\n", outName, pos, pos)
|
||||
}
|
||||
return fmt.Errorf("invalid JPEG")
|
||||
return posError(pos)
|
||||
}
|
||||
|
||||
func posError(pos int) error {
|
||||
return fmt.Errorf("invalid JPEG at pos = 0x%08X = %10d", pos, pos)
|
||||
}
|
||||
Reference in new issue
Block a user