Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reader.go « png « gitlab-resize-image « cmd « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8229454ee3b02bf1bd49a04bfd71cc75929a8d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package png

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io"
	"io/ioutil"
	"os"
)

const (
	pngMagicLen = 8
	pngMagic    = "\x89PNG\r\n\x1a\n"
)

// Reader is an io.Reader decorator that skips certain PNG chunks known to cause problems.
// If the image stream is not a PNG, it will yield all bytes unchanged to the underlying
// reader.
// See also https://gitlab.com/gitlab-org/gitlab/-/issues/287614
type Reader struct {
	underlying     io.Reader
	chunk          io.Reader
	bytesRemaining int64
}

func NewReader(r io.Reader) (io.Reader, error) {
	magicBytes, err := readMagic(r)
	if err != nil {
		return nil, err
	}

	if string(magicBytes) != pngMagic {
		debug("Not a PNG - read file unchanged")
		return io.MultiReader(bytes.NewReader(magicBytes), r), nil
	}

	return io.MultiReader(bytes.NewReader(magicBytes), &Reader{underlying: r}), nil
}

func (r *Reader) Read(p []byte) (int, error) {
	for r.bytesRemaining == 0 {
		const (
			headerLen = 8
			crcLen    = 4
		)
		var header [headerLen]byte
		_, err := io.ReadFull(r.underlying, header[:])
		if err != nil {
			return 0, err
		}

		chunkLen := int64(binary.BigEndian.Uint32(header[:4]))
		if chunkType := string(header[4:]); chunkType == "iCCP" {
			debug("!! iCCP chunk found; skipping")
			if _, err := io.CopyN(ioutil.Discard, r.underlying, chunkLen+crcLen); err != nil {
				return 0, err
			}
			continue
		}

		r.bytesRemaining = headerLen + chunkLen + crcLen
		r.chunk = io.MultiReader(bytes.NewReader(header[:]), io.LimitReader(r.underlying, r.bytesRemaining-headerLen))
	}

	n, err := r.chunk.Read(p)
	r.bytesRemaining -= int64(n)
	return n, err
}

func debug(args ...interface{}) {
	if os.Getenv("DEBUG") == "1" {
		fmt.Fprintln(os.Stderr, args...)
	}
}

// Consume PNG magic and proceed to reading the IHDR chunk.
func readMagic(r io.Reader) ([]byte, error) {
	var magicBytes []byte = make([]byte, pngMagicLen)
	_, err := io.ReadFull(r, magicBytes)
	if err != nil {
		return nil, err
	}

	return magicBytes, nil
}