Check-in [bf36d53942]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add end of file magic number and check that on read.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:bf36d539426255f66acfa196764293fd21534c8d
User & Date: stephanie.gawroriski 2019-04-17 16:29:06
Context
2019-04-17
16:36
Add the read padding byte to the error message. check-in: 274fc0cd3e user: stephanie.gawroriski tags: trunk
16:29
Add end of file magic number and check that on read. check-in: bf36d53942 user: stephanie.gawroriski tags: trunk
16:22
Make sure the input byte array length matches the length in the class header. check-in: 0e7a9af42e user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to runt/libs/tool-classfile/dev/shadowtail/classfile/mini/MinimizedClassFile.java.

141
142
143
144
145
146
147

148
149
150











151
152
153
154
155
156
157
		
		// Read class header
		MinimizedClassHeader header = MinimizedClassHeader.decode(
			new ByteArrayInputStream(__is));
		
		// {@squirreljme.error JC45 Length of class file does not match
		// length of array. (The file length; The array length)}

		if (header.filesize != __is.length)
			throw new InvalidClassFormatException("JC45 " + header.filesize +
				" " + __is.length);











		
		// Read constant pool
		MinimizedPool pool = MinimizedPool.decode(header.poolcount,
			__is, header.pooloff, header.poolsize);
		
		// Read static and instance fields
		MinimizedField[] sfields = MinimizedField.decodeFields(







>
|
|

>
>
>
>
>
>
>
>
>
>
>







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
		
		// Read class header
		MinimizedClassHeader header = MinimizedClassHeader.decode(
			new ByteArrayInputStream(__is));
		
		// {@squirreljme.error JC45 Length of class file does not match
		// length of array. (The file length; The array length)}
		int fsz = header.filesize;
		if (fsz != __is.length)
			throw new InvalidClassFormatException("JC45 " + fsz +
				" " + __is.length);
		
		// {@squirreljme.error JC46 End of file magic number is invalid.
		// (The read magic number)}
		int endmagic;
		if (MinimizedClassHeader.END_MAGIC_NUMBER !=
			(endmagic = (((__is[fsz - 4] & 0xFF) << 24) |
			((__is[fsz - 3] & 0xFF) << 16) |
			((__is[fsz - 2] & 0xFF) << 8) |
			(__is[fsz - 1] & 0xFF))))
			throw new InvalidClassFormatException(
				String.format("JC46 %08x", endmagic));
		
		// Read constant pool
		MinimizedPool pool = MinimizedPool.decode(header.poolcount,
			__is, header.pooloff, header.poolsize);
		
		// Read static and instance fields
		MinimizedField[] sfields = MinimizedField.decodeFields(

Changes to runt/libs/tool-classfile/dev/shadowtail/classfile/mini/MinimizedClassHeader.java.

21
22
23
24
25
26
27




28
29
30
31
32
33
34
 * @since 2019/04/16
 */
public final class MinimizedClassHeader
{
	/** The magic number for the header. */
	public static final int MAGIC_NUMBER =
		0x00586572;




	
	/** Unused A. */
	public final int unuseda;
	
	/** Unused B. */
	public final int unusedb;
	







>
>
>
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * @since 2019/04/16
 */
public final class MinimizedClassHeader
{
	/** The magic number for the header. */
	public static final int MAGIC_NUMBER =
		0x00586572;
	
	/** Magic number for the end of file. */
	public static final int END_MAGIC_NUMBER =
		0x42796521;
	
	/** Unused A. */
	public final int unuseda;
	
	/** Unused B. */
	public final int unusedb;
	

Changes to runt/libs/tool-classfile/dev/shadowtail/classfile/mini/Minimizer.java.

195
196
197
198
199
200
201
202
203



204
205
206
207
208
209
210
...
215
216
217
218
219
220
221



222
223
224
225
226
227
228
...
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
			// Round
			reloff = Minimizer.__relAdd(reloff, data.length);
		}
		
		// Write absolute file size! This saves time in calculating how big
		// a file we have and we can just read that many bytes for all the
		// data areas or similar if needed
		__dos.writeInt(reloff);
		__dos.writeInt(reloff - baserel);



		
		// Write constant pool
		__dos.write(pooldata);
		Minimizer.__dosRound(__dos);
		
		// Write field data
		for (int i = 0; i < 2; i++)
................................................................................
		
		// Write method data
		for (int i = 0; i < 2; i++)
		{
			__dos.write(methoddata[i]);
			Minimizer.__dosRound(__dos);
		}



	}
	
	/**
	 * Process fields.
	 *
	 * @return The resulting fields, static and instance split into each.
	 * @since 2019/03/11
................................................................................
	static final void __dosRound(DataOutputStream __dos)
		throws IOException, NullPointerException
	{
		if (__dos == null)
			throw new NullPointerException("NARG");
		
		// Add padding
		int at = __dos.size();
		while ((at & 3) != 0)
		{
			__dos.write(0);
			at++;
		}
	}
	
	/**
	 * Adds length data to the relative offset.
	 *
	 * @param __rel Current relative offset.
	 * @param __v The offset to add.







|
|
>
>
>







 







>
>
>







 







|
<
<

<
<







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
...
728
729
730
731
732
733
734
735


736


737
738
739
740
741
742
743
			// Round
			reloff = Minimizer.__relAdd(reloff, data.length);
		}
		
		// Write absolute file size! This saves time in calculating how big
		// a file we have and we can just read that many bytes for all the
		// data areas or similar if needed
		__dos.writeInt(reloff + 4);
		__dos.writeInt((reloff - baserel) + 4);
		
		// Constant pool is rounded
		Minimizer.__dosRound(__dos);
		
		// Write constant pool
		__dos.write(pooldata);
		Minimizer.__dosRound(__dos);
		
		// Write field data
		for (int i = 0; i < 2; i++)
................................................................................
		
		// Write method data
		for (int i = 0; i < 2; i++)
		{
			__dos.write(methoddata[i]);
			Minimizer.__dosRound(__dos);
		}
		
		// Write end magic number
		__dos.writeInt(MinimizedClassHeader.END_MAGIC_NUMBER);
	}
	
	/**
	 * Process fields.
	 *
	 * @return The resulting fields, static and instance split into each.
	 * @since 2019/03/11
................................................................................
	static final void __dosRound(DataOutputStream __dos)
		throws IOException, NullPointerException
	{
		if (__dos == null)
			throw new NullPointerException("NARG");
		
		// Add padding
		while ((__dos.size() & 3) != 0)


			__dos.write(0);


	}
	
	/**
	 * Adds length data to the relative offset.
	 *
	 * @param __rel Current relative offset.
	 * @param __v The offset to add.