diff options
author | 2023-02-19 00:52:15 +0900 | |
---|---|---|
committer | 2023-02-18 16:52:15 +0100 | |
commit | fe66a2aed2c6c2f3237f2515bbd54fa4fb24b18f (patch) | |
tree | 8d7bb4c635643fb6c8456c0deee247c55f737835 | |
parent | [bugfix] fix oob token route, update templates+css for oob and errors (#1519) (diff) | |
download | gotosocial-fe66a2aed2c6c2f3237f2515bbd54fa4fb24b18f.tar.xz |
[bugfix] Keep png transparency (#1522)
* keep png transparency
* rewrite to switch case
-rw-r--r-- | internal/media/png-stripper.go | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/internal/media/png-stripper.go b/internal/media/png-stripper.go index e23ac2cd0..485f4c930 100644 --- a/internal/media/png-stripper.go +++ b/internal/media/png-stripper.go @@ -78,9 +78,29 @@ import ( "io" ) -// chunkTypeAncillaryBit is whether the first byte of a big-endian uint32 chunk -// type (the first of four ASCII letters) is lower-case. -const chunkTypeAncillaryBit = 0x20000000 +const ( + chunkTypeIHDR = 0x49484452 + chunkTypePLTE = 0x504C5445 + chunkTypeIDAT = 0x49444154 + chunkTypeIEND = 0x49454E44 + chunkTypeTRNS = 0x74524e53 +) + +func isNecessaryChunkType(chunkType uint32) bool { + switch chunkType { + case chunkTypeIHDR: + return true + case chunkTypePLTE: + return true + case chunkTypeIDAT: + return true + case chunkTypeIEND: + return true + case chunkTypeTRNS: + return true + } + return false +} // pngAncillaryChunkStripper wraps another io.Reader to strip ancillary chunks, // if the data is in the PNG file format. If the data isn't PNG, it is passed @@ -179,7 +199,7 @@ func (r *pngAncillaryChunkStripper) Read(p []byte) (int, error) { // byte trailer, a checksum. r.pending = int64(binary.BigEndian.Uint32(r.buffer[:4])) + 4 chunkType := binary.BigEndian.Uint32(r.buffer[4:]) - r.discard = (chunkType & chunkTypeAncillaryBit) != 0 + r.discard = !isNecessaryChunkType(chunkType) if r.discard { r.rIndex = r.wIndex } |