1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00
imgproxy/imagemeta/svg.go

55 lines
797 B
Go
Raw Normal View History

2019-12-25 11:06:15 +02:00
package imagemeta
import (
2020-09-22 14:03:05 +02:00
"bytes"
2020-02-11 15:28:13 +02:00
"encoding/xml"
"io"
2020-02-11 15:28:13 +02:00
"sync/atomic"
)
2020-02-11 15:28:13 +02:00
var maxSvgBytes int64 = 32 * 1024
type svgHeader struct {
XMLName xml.Name
}
func SetMaxSvgCheckRead(n int) {
atomic.StoreInt64(&maxSvgBytes, int64(n))
}
func IsSVG(r io.Reader) (bool, error) {
maxBytes := int(atomic.LoadInt64(&maxSvgBytes))
var h svgHeader
buf := make([]byte, 0, maxBytes)
b := make([]byte, 1024)
2020-09-22 14:03:05 +02:00
rr := bytes.NewReader(buf)
2020-02-11 15:28:13 +02:00
for {
n, err := r.Read(b)
if err != nil && err != io.EOF {
return false, err
}
if n <= 0 {
return false, nil
}
buf = append(buf, b[:n]...)
2020-09-22 14:03:05 +02:00
rr.Reset(buf)
2020-02-11 15:28:13 +02:00
2020-09-22 14:03:05 +02:00
dec := xml.NewDecoder(rr)
dec.Strict = false
if dec.Decode(&h); h.XMLName.Local == "svg" {
2020-02-11 15:28:13 +02:00
return true, nil
}
if len(buf) >= maxBytes {
break
}
}
2020-02-11 15:28:13 +02:00
return false, nil
}