Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 74b438ea46e28b9ac77aabc93f71d46722877e96 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.internal.image;


import org.eclipse.swt.*;
import java.io.*;

class PngChunk extends Object {
	byte[] reference;

	static final int LENGTH_OFFSET = 0;
	static final int TYPE_OFFSET = 4;
	static final int DATA_OFFSET = 8;

	static final int TYPE_FIELD_LENGTH = 4;
	static final int LENGTH_FIELD_LENGTH = 4;
	static final int MIN_LENGTH = 12;

	static final int CHUNK_UNKNOWN = -1;
	// Critical chunks.
	static final int CHUNK_IHDR = 0;
	static final int CHUNK_PLTE = 1;
	static final int CHUNK_IDAT = 2;
	static final int CHUNK_IEND = 3;
	// Non-critical chunks.
	static final int CHUNK_tRNS = 5;
	
	static final byte[] TYPE_IHDR = {(byte) 'I', (byte) 'H', (byte) 'D', (byte) 'R'};
	static final byte[] TYPE_PLTE = {(byte) 'P', (byte) 'L', (byte) 'T', (byte) 'E'};
	static final byte[] TYPE_IDAT = {(byte) 'I', (byte) 'D', (byte) 'A', (byte) 'T'};
	static final byte[] TYPE_IEND = {(byte) 'I', (byte) 'E', (byte) 'N', (byte) 'D'};
	static final byte[] TYPE_tRNS = {(byte) 't', (byte) 'R', (byte) 'N', (byte) 'S'};
	
	static final int[] CRC_TABLE;
	static {
		CRC_TABLE = new int[256];
		for (int i = 0; i < 256; i++) {
			CRC_TABLE[i] = i;
			for (int j = 0; j < 8; j++) {
				if ((CRC_TABLE[i] & 0x1) == 0) {
					CRC_TABLE[i] = (CRC_TABLE[i] >> 1) & 0x7FFFFFFF;
				} else {
					CRC_TABLE[i] = 0xEDB88320 ^ ((CRC_TABLE[i] >> 1) & 0x7FFFFFFF);
				}
			}
		}	
	}
	
	int length;
	
/**
 * Construct a PngChunk using the reference bytes
 * given.
 */	
PngChunk(byte[] reference) {
	super();
	setReference(reference);
	if (reference.length < LENGTH_OFFSET + LENGTH_FIELD_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE);
	length = getInt32(LENGTH_OFFSET);
}

/**
 * Construct a PngChunk with the specified number of
 * data bytes.
 */	
PngChunk(int dataLength) {
	this(new byte[MIN_LENGTH + dataLength]);
	setLength(dataLength);
}

/**
 * Get the PngChunk's reference byteArray;
 */	
byte[] getReference() {
	return reference;
}

/**
 * Set the PngChunk's reference byteArray;
 */	
void setReference(byte[] reference) {
	this.reference = reference;
}

/**
 * Get the 16-bit integer from the reference byte
 * array at the given offset.
 */	
int getInt16(int offset) {
	int answer = 0;
	answer |= (reference[offset] & 0xFF) << 8;
	answer |= (reference[offset + 1] & 0xFF);
	return answer;	
}

/**
 * Set the 16-bit integer in the reference byte
 * array at the given offset.
 */	
void setInt16(int offset, int value) {
	reference[offset] = (byte) ((value >> 8) & 0xFF);
	reference[offset + 1] = (byte) (value & 0xFF);
}

/**
 * Get the 32-bit integer from the reference byte
 * array at the given offset.
 */	
int getInt32(int offset) {
	int answer = 0;
	answer |= (reference[offset] & 0xFF) << 24;
	answer |= (reference[offset + 1] & 0xFF) << 16;
	answer |= (reference[offset + 2] & 0xFF) << 8;
	answer |= (reference[offset + 3] & 0xFF);
	return answer;	
}

/**
 * Set the 32-bit integer in the reference byte
 * array at the given offset.
 */	
void setInt32(int offset, int value) {
	reference[offset] = (byte) ((value >> 24) & 0xFF);
	reference[offset + 1] = (byte) ((value >> 16) & 0xFF);
	reference[offset + 2] = (byte) ((value >> 8) & 0xFF);
	reference[offset + 3] = (byte) (value & 0xFF);
}

/**
 * Get the length of the data component of this chunk.
 * This is not the length of the entire chunk.
 */	
int getLength() {
	return length;
}

/**
 * Set the length of the data component of this chunk.
 * This is not the length of the entire chunk.
 */	
void setLength(int value) {
	setInt32(LENGTH_OFFSET, value);
	length = value;
}

/**
 * Get the chunk type. This is a four byte value.
 * Each byte should be an ASCII character.
 * The first byte is upper case if the chunk is critical.
 * The second byte is upper case if the chunk is publicly defined.
 * The third byte must be upper case.
 * The fourth byte is upper case if the chunk is unsafe to copy. 
 * Public chunk types are defined by the PNG Development Group.
 */	
byte[] getTypeBytes() {
	byte[] type = new byte[4];
	System.arraycopy(reference, TYPE_OFFSET, type, 0, TYPE_FIELD_LENGTH);
	return type;
}	

/**
 * Set the chunk type. This is a four byte value.
 * Each byte should be an ASCII character.
 * The first byte is upper case if the chunk is critical.
 * The second byte is upper case if the chunk is publicly defined.
 * The third byte must be upper case.
 * The fourth byte is upper case if the chunk is unsafe to copy. 
 * Public chunk types are defined by the PNG Development Group.
 */	
void setType(byte[] value) {
	if (value.length != TYPE_FIELD_LENGTH) {
		SWT.error (SWT.ERROR_INVALID_ARGUMENT);
	}
	System.arraycopy(value, 0, reference, TYPE_OFFSET, TYPE_FIELD_LENGTH);
}

/**
 * Get the chunk's data.
 */
byte[] getData() {
	int dataLength = getLength();
	if (reference.length < MIN_LENGTH + dataLength) {
		SWT.error (SWT.ERROR_INVALID_RANGE);
	}
	byte[] data = new byte[dataLength];
	System.arraycopy(reference, DATA_OFFSET, data, 0, dataLength);
	return data;
}

/**
 * Set the chunk's data.
 * This method has two side-effects.
 * 1. It will set the length field to be the length
 *    of the data array given.
 * 2. It will set the CRC field to the computed CRC
 *    value of the data array given.
 */
void setData(byte[] data) {
	setLength(data.length);
	System.arraycopy(data, 0, reference, DATA_OFFSET, data.length);
	setCRC(computeCRC());
}

/**
 * Get the CRC value for the chunk's data.
 * Ensure that the length field has a good
 * value before making this call.
 */
int getCRC() {
	int crcOffset = DATA_OFFSET + getLength();
	return getInt32(crcOffset);
}

/**
 * Set the CRC value for the chunk's data.
 * Ensure that the length field has a good
 * value before making this call.
 */
void setCRC(int value) {
	int crcOffset = DATA_OFFSET + getLength();
	setInt32(crcOffset, value);
}

/**
 * Get the chunk's total size including the length, type, and crc fields.
 */
int getSize() {
	return MIN_LENGTH + getLength();
}

/**
 * Compute the CRC value for the chunk's data. Answer
 * whether this value matches the value stored in the
 * chunk.
 */
boolean checkCRC() {
	int crc = computeCRC();
	int storedCRC = getCRC();
	return crc == storedCRC;
}

/**
 * Answer the CRC value of chunk's data.
 */
int computeCRC() {
	int crc = 0xFFFFFFFF;
	int start = TYPE_OFFSET;
	int stop = DATA_OFFSET + getLength();
	for (int i = start; i < stop; i++) {
		int index = (crc ^ reference[i]) & 0xFF;
		crc =  CRC_TABLE[index] ^ ((crc >> 8) & 0x00FFFFFF);
	}
	return ~crc;
}

boolean typeMatchesArray(byte[] array) {
	for (int i = 0; i < TYPE_FIELD_LENGTH; i++) {
		if (reference[TYPE_OFFSET + i] != array[i]){
			return false;
		}
	}	
	return true;
}

boolean isCritical() {
	char c = (char) getTypeBytes()[0]; 
	return 'A' <= c && c <= 'Z';
}

int getChunkType() {
	if (typeMatchesArray(TYPE_IHDR)) return CHUNK_IHDR;
	if (typeMatchesArray(TYPE_PLTE)) return CHUNK_PLTE;
	if (typeMatchesArray(TYPE_IDAT)) return CHUNK_IDAT;
	if (typeMatchesArray(TYPE_IEND)) return CHUNK_IEND;
	if (typeMatchesArray(TYPE_tRNS)) return CHUNK_tRNS;	
	return CHUNK_UNKNOWN;
}

/**
 * Read the next PNG chunk from the input stream given.
 * If unable to read a chunk, return null.
 */
static PngChunk readNextFromStream(LEDataInputStream stream) {
	try {
		int headerLength = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH;
		byte[] headerBytes = new byte[headerLength];
		int result = stream.read(headerBytes, 0, headerLength);
		stream.unread(headerBytes);
		if (result != headerLength) return null;
		
		PngChunk tempChunk = new PngChunk(headerBytes);
		
		int chunkLength = tempChunk.getSize();
		byte[] chunk = new byte[chunkLength];
		result = stream.read(chunk, 0, chunkLength);
		if (result != chunkLength) return null;
	
		switch (tempChunk.getChunkType()) {
			case CHUNK_IHDR:
				return new PngIhdrChunk(chunk);
			case CHUNK_PLTE:
				return new PngPlteChunk(chunk);
			case CHUNK_IDAT:
				return new PngIdatChunk(chunk);
			case CHUNK_IEND:
				return new PngIendChunk(chunk);
			case CHUNK_tRNS:
				return new PngTrnsChunk(chunk);
			default:
				return new PngChunk(chunk);
		}		
	} catch (IOException e) {
		return null;
	}
}

/**
 * Answer whether the chunk is a valid PNG chunk.
 */
void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
	if (reference.length < MIN_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE);
	
	byte[] type = getTypeBytes();
	
	// The third character MUST be upper case.
	char c = (char) type[2];
	if (!('A' <= c && c <= 'Z')) SWT.error(SWT.ERROR_INVALID_IMAGE);
	
	// All characters must be letters.
	for (int i = 0; i < TYPE_FIELD_LENGTH; i++) {
		c = (char) type[i];
		if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))) {
			SWT.error(SWT.ERROR_INVALID_IMAGE);
		}
	}
	
	// The stored CRC must match the data's computed CRC.
	if (!checkCRC()) SWT.error(SWT.ERROR_INVALID_IMAGE);
}

/**
 * Provided so that subclasses can override and add
 * data to the toString() call.
 */
void contributeToString(StringBuffer buffer) {}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public String toString() {
	StringBuffer buffer = new StringBuffer();
	buffer.append("{");
	buffer.append("\n\tLength: ");
	buffer.append(getLength());
	buffer.append("\n\tType: ");
	byte[] type = getTypeBytes();
	for(int i = 0; i < type.length; i++) {
		buffer.append((char) type[i]);
	}
	
	contributeToString(buffer);
	
	buffer.append("\n\tCRC: ");
	buffer.append(Integer.toHexString(getCRC()));
	buffer.append("\n}");
	return buffer.toString();
}

}

Back to the top