Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

image.gno

4.52 Kb · 141 lines
  1package media
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
  7)
  8
  9// CheckImage checks a complete file against its declared type: the magic
 10// bytes and the header's pixel dimensions (each 1..MaxDim, the
 11// decompression-bomb guard). SVG has no header dimensions (w, h = 0) and
 12// must pass safe.AssertSVG with the on-chain cap MaxSVGBytes (§0a), the same
 13// allowlist core applies to every render. Nothing else of a raster file is
 14// parsed: raster bytes are only ever shown through <img> (SA-M3).
 15func CheckImage(mime int, data string) (w, h int, err error) {
 16	switch mime {
 17	case MimeSVG:
 18		if err := safe.AssertSVG(data, MaxSVGBytes); err != nil {
 19			return 0, 0, err
 20		}
 21		return 0, 0, nil
 22	case MimePNG:
 23		w, h, err = pngDims(data)
 24	case MimeWebP:
 25		w, h, err = webpDims(data)
 26	case MimeJPEG:
 27		w, h, err = jpegDims(data)
 28	case MimeGIF:
 29		w, h, err = gifDims(data)
 30	default:
 31		return 0, 0, mediaErr("unknown media type " + strconv.Itoa(mime))
 32	}
 33	if err != nil {
 34		return 0, 0, err
 35	}
 36	if w < 1 || h < 1 || w > MaxDim || h > MaxDim {
 37		return 0, 0, mediaErr(MimeName(mime) + ": dimensions " + strconv.Itoa(w) + "x" + strconv.Itoa(h) +
 38			" outside 1.." + strconv.Itoa(MaxDim))
 39	}
 40	return w, h, nil
 41}
 42
 43func be16(s string, i int) int { return int(s[i])<<8 | int(s[i+1]) }
 44func le16(s string, i int) int { return int(s[i]) | int(s[i+1])<<8 }
 45func le24(s string, i int) int { return int(s[i]) | int(s[i+1])<<8 | int(s[i+2])<<16 }
 46func be32(s string, i int) int64 {
 47	return int64(s[i])<<24 | int64(s[i+1])<<16 | int64(s[i+2])<<8 | int64(s[i+3])
 48}
 49func le32(s string, i int) int64 {
 50	return int64(s[i]) | int64(s[i+1])<<8 | int64(s[i+2])<<16 | int64(s[i+3])<<24
 51}
 52
 53// pngDims: signature, then the IHDR chunk (length 13) with big-endian
 54// width and height. The 4-byte CRC after IHDR must be present.
 55func pngDims(s string) (int, int, error) {
 56	if len(s) < 33 || s[:8] != "\x89PNG\r\n\x1a\n" {
 57		return 0, 0, mediaErr("png: bad signature or truncated header")
 58	}
 59	if be32(s, 8) != 13 || s[12:16] != "IHDR" {
 60		return 0, 0, mediaErr("png: first chunk must be IHDR")
 61	}
 62	return int(be32(s, 16)), int(be32(s, 20)), nil
 63}
 64
 65// webpDims: RIFF container whose size field covers the whole file, form
 66// type WEBP, then one of the three bitstream headers.
 67func webpDims(s string) (int, int, error) {
 68	if len(s) < 30 || s[:4] != "RIFF" || s[8:12] != "WEBP" {
 69		return 0, 0, mediaErr("webp: bad RIFF/WEBP header or truncated")
 70	}
 71	if le32(s, 4)+8 != int64(len(s)) {
 72		return 0, 0, mediaErr("webp: RIFF size does not match the file size")
 73	}
 74	switch s[12:16] {
 75	case "VP8 ": // lossy: 3-byte frame tag, start code 9d 01 2a, 14-bit dims
 76		if s[23:26] != "\x9d\x01\x2a" {
 77			return 0, 0, mediaErr("webp: bad VP8 start code")
 78		}
 79		return le16(s, 26) & 0x3fff, le16(s, 28) & 0x3fff, nil
 80	case "VP8L": // lossless: signature 0x2f, 14-bit width-1 and height-1
 81		if s[20] != 0x2f {
 82			return 0, 0, mediaErr("webp: bad VP8L signature")
 83		}
 84		bits := le32(s, 21)
 85		return int(bits&0x3fff) + 1, int((bits>>14)&0x3fff) + 1, nil
 86	case "VP8X": // extended: 24-bit canvas width-1 and height-1
 87		return le24(s, 24) + 1, le24(s, 27) + 1, nil
 88	}
 89	return 0, 0, mediaErr("webp: unknown bitstream chunk")
 90}
 91
 92// jpegDims walks the marker segments after SOI up to the first SOFn
 93// (C0..CF except C4 DHT, C8 JPG, CC DAC). Reaching SOS or EOI first, or
 94// running out of bytes, is an error.
 95func jpegDims(s string) (int, int, error) {
 96	if len(s) < 4 || s[0] != 0xff || s[1] != 0xd8 || s[2] != 0xff {
 97		return 0, 0, mediaErr("jpeg: bad SOI")
 98	}
 99	i := 2
100	for i < len(s) {
101		if s[i] != 0xff {
102			return 0, 0, mediaErr("jpeg: expected a marker at byte " + strconv.Itoa(i))
103		}
104		for i < len(s) && s[i] == 0xff { // fill bytes
105			i++
106		}
107		if i >= len(s) {
108			break
109		}
110		m := s[i]
111		i++
112		switch {
113		case m == 0xd8 || m == 0x01 || (m >= 0xd0 && m <= 0xd7):
114			continue // standalone markers
115		case m == 0xd9 || m == 0xda:
116			return 0, 0, mediaErr("jpeg: no frame header before scan data")
117		}
118		if i+2 > len(s) {
119			break
120		}
121		n := be16(s, i)
122		if n < 2 || i+n > len(s) {
123			return 0, 0, mediaErr("jpeg: bad segment length")
124		}
125		if m >= 0xc0 && m <= 0xcf && m != 0xc4 && m != 0xc8 && m != 0xcc {
126			if n < 8 {
127				return 0, 0, mediaErr("jpeg: short frame header")
128			}
129			return be16(s, i+5), be16(s, i+3), nil // width after height
130		}
131		i += n
132	}
133	return 0, 0, mediaErr("jpeg: truncated before the frame header")
134}
135
136func gifDims(s string) (int, int, error) {
137	if len(s) < 13 || (s[:6] != "GIF87a" && s[:6] != "GIF89a") {
138		return 0, 0, mediaErr("gif: bad signature or truncated")
139	}
140	return le16(s, 6), le16(s, 8), nil
141}