| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import java.io.ByteArrayInputStream;
|
| 4 | import java.io.DataInputStream;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStream;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import java.nio.ByteOrder;
|
| 9 | import java.util.HexFormat;
|
| 10 |
|
| 11 | public record PngInfo(int width, int height, byte bitDepth, byte colorType) {
|
| 12 | private static final HexFormat FORMAT = HexFormat.of().withUpperCase().withPrefix("0x");
|
| 13 | private static final int PNG_HEADER_SIZE = 8;
|
| 14 | private static final long PNG_HEADER = -8552249625308161526L;
|
| 15 | private static final int PNG_CHUNK_HEADER_SIZE = 8;
|
| 16 | private static final int PNG_CHUNK_TRAILER_SIZE = 4;
|
| 17 | private static final int IHDR_TYPE = 1229472850;
|
| 18 | private static final int IHDR_SIZE = 13;
|
| 19 | private static final byte COLOR_TYPE_GRAYSCALE = 0;
|
| 20 | private static final byte COLOR_TYPE_TRUECOLOR = 2;
|
| 21 | private static final byte COLOR_TYPE_INDEXED = 3;
|
| 22 | private static final byte COLOR_TYPE_GRAYSCALE_ALPHA = 4;
|
| 23 | private static final byte COLOR_TYPE_TRUECOLOR_ALPHA = 6;
|
| 24 |
|
| 25 | public static PngInfo fromStream(final InputStream inputStream) throws IOException {
|
| 26 | DataInputStream stream = new DataInputStream(inputStream);
|
| 27 | long magic = stream.readLong();
|
| 28 | if (magic != -8552249625308161526L) {
|
| 29 | throw new IOException("Bad PNG Signature: " + FORMAT.toHexDigits(magic));
|
| 30 | } else {
|
| 31 | int headerSize = stream.readInt();
|
| 32 | if (headerSize != 13) {
|
| 33 | throw new IOException("Bad length for IHDR chunk: " + headerSize);
|
| 34 | } else {
|
| 35 | int headerType = stream.readInt();
|
| 36 | if (headerType != 1229472850) {
|
| 37 | throw new IOException("Bad type for IHDR chunk: " + FORMAT.toHexDigits(headerType));
|
| 38 | } else {
|
| 39 | int width = stream.readInt();
|
| 40 | int height = stream.readInt();
|
| 41 | byte bitDepth = stream.readByte();
|
| 42 | if (!validateBitDepth(bitDepth)) {
|
| 43 | throw new IOException("Invalid bit depth: " + bitDepth);
|
| 44 | } else {
|
| 45 | byte colorType = stream.readByte();
|
| 46 | if (!validateColorType(colorType)) {
|
| 47 | throw new IOException("Invalid color type: " + colorType);
|
| 48 | } else if (!validateBounds(width, height, bitDepth, colorType)) {
|
| 49 | throw new IOException("Bounds check failed, " + width + "x" + height + "@" + bitDepth + ", type: " + colorType);
|
| 50 | } else {
|
| 51 | return new PngInfo(width, height, bitDepth, colorType);
|
| 52 | }
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public static PngInfo fromBytes(final byte[] bytes) throws IOException {
|
| 60 | return fromStream(new ByteArrayInputStream(bytes));
|
| 61 | }
|
| 62 |
|
| 63 | public static void validateHeader(final ByteBuffer buffer) throws IOException {
|
| 64 | ByteOrder order = buffer.order();
|
| 65 | buffer.order(ByteOrder.BIG_ENDIAN);
|
| 66 | if (buffer.limit() < 33) {
|
| 67 | throw new IOException("PNG header missing");
|
| 68 | }
|
| 69 |
|
| 70 | if (buffer.getLong(0) != -8552249625308161526L) {
|
| 71 | throw new IOException("Bad PNG Signature");
|
| 72 | }
|
| 73 |
|
| 74 | if (buffer.getInt(8) != 13) {
|
| 75 | throw new IOException("Bad length for IHDR chunk!");
|
| 76 | }
|
| 77 |
|
| 78 | if (buffer.getInt(12) != 1229472850) {
|
| 79 | throw new IOException("Bad type for IHDR chunk!");
|
| 80 | }
|
| 81 |
|
| 82 | int width = buffer.getInt(16);
|
| 83 | int height = buffer.getInt(20);
|
| 84 | byte bitDepth = buffer.get(24);
|
| 85 | if (!validateBitDepth(bitDepth)) {
|
| 86 | throw new IOException("Invalid bit depth: " + bitDepth);
|
| 87 | }
|
| 88 |
|
| 89 | byte colorType = buffer.get(25);
|
| 90 | if (!validateColorType(colorType)) {
|
| 91 | throw new IOException("Invalid color type: " + colorType);
|
| 92 | }
|
| 93 |
|
| 94 | if (!validateBounds(width, height, bitDepth, colorType)) {
|
| 95 | throw new IOException("Bounds check failed, " + width + "x" + height + "@" + bitDepth + ", type: " + colorType);
|
| 96 | }
|
| 97 |
|
| 98 | buffer.order(order);
|
| 99 | }
|
| 100 |
|
| 101 | private static boolean validateBitDepth(final int bitDepth) {
|
| 102 | return switch (bitDepth) {
|
| 103 | case 1, 2, 4, 8, 16 -> true;
|
| 104 | default -> false;
|
| 105 | };
|
| 106 | }
|
| 107 |
|
| 108 | private static boolean validateColorType(final int colorType) {
|
| 109 | return switch (colorType) {
|
| 110 | case 0, 2, 3, 4, 6 -> true;
|
| 111 | default -> false;
|
| 112 | };
|
| 113 | }
|
| 114 |
|
| 115 | private static boolean validateBounds(final int width, final int height, final byte bitDepth, final byte colorType) {
|
| 116 | if (bitDepth == 16) {
|
| 117 | long componentCount = 4L;
|
| 118 | long bufferEstimate = 4L * width * height * 2L;
|
| 119 | if (bufferEstimate != (int)bufferEstimate) {
|
| 120 | return false;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | return true;
|
| 125 | }
|
| 126 | }
|
| 127 |
|