HumanMedia.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.lang.*;
16: import java.util.*;
17:
18: public class HumanMedia {
19:
20:
21: protected static final int HUM_EXECUTABLE = 0b10000000;
22: protected static final int HUM_LINK = 0b01000000;
23: protected static final int HUM_ARCHIVE = 0b00100000;
24: protected static final int HUM_DIRECTORY = 0b00010000;
25: protected static final int HUM_VOLUME = 0b00001000;
26: protected static final int HUM_SYSTEM = 0b00000100;
27: protected static final int HUM_HIDDEN = 0b00000010;
28: protected static final int HUM_READONLY = 0b00000001;
29:
30: public int humBytesShiftSector;
31: public int humBytesPerSector;
32: public int humSectorsShiftCluster;
33: public int humSectorsPerCluster;
34: public int humBytesShiftCluster;
35: public int humBytesPerCluster;
36: public int humFatCount;
37: public int humReservedSectors;
38: public int humRootEntries;
39: public int humEntriesPerSector;
40: public int humRootSectors;
41: public int humMediaByte;
42: public int humFatID;
43: public boolean humFat12;
44: public int humFatMarker;
45: public int humFatTailCode;
46: public int humFatSectors;
47: public int humPartitionSectors;
48: public int humFatAndDataSectors;
49: public int humDataClustersPlus2;
50: public long humPartitionStartByte;
51: public long humFatStartByte;
52: public int humRootStartSector;
53: public long humRootStartByte;
54: public int humDataStartSector;
55: public long humDataStartByte;
56: public long humDataEndByte;
57: public long humPartitionEndByte;
58: public long humDiskEndByte;
59:
60: public HumanMedia (int bytesPerSector, int sectorsPerCluster, int fatCount,
61: int reservedSectors, int rootEntries, int mediaByte, int fatID, int fatSectors,
62: long diskEndByte) {
63: this (bytesPerSector, sectorsPerCluster, fatCount,
64: reservedSectors, rootEntries, mediaByte, fatID, fatSectors,
65: diskEndByte, 0L, diskEndByte);
66: }
67: public HumanMedia (int bytesPerSector, int sectorsPerCluster, int fatCount,
68: int reservedSectors, int rootEntries, int mediaByte, int fatID, int fatSectors,
69: long diskEndByte, long partitionStartByte) {
70: this (bytesPerSector, sectorsPerCluster, fatCount,
71: reservedSectors, rootEntries, mediaByte, fatID, fatSectors,
72: diskEndByte, partitionStartByte, diskEndByte);
73: }
74: public HumanMedia (int bytesPerSector, int sectorsPerCluster, int fatCount,
75: int reservedSectors, int rootEntries, int mediaByte, int fatID, int fatSectors,
76: long diskEndByte, long partitionStartByte, long partitionEndByte) {
77:
78: humBytesPerSector = bytesPerSector;
79: humFatCount = fatCount;
80: humReservedSectors = reservedSectors;
81: humRootEntries = rootEntries;
82: humMediaByte = mediaByte;
83: humFatID = fatID;
84: humPartitionStartByte = partitionStartByte;
85: humPartitionEndByte = partitionEndByte;
86: humDiskEndByte = diskEndByte;
87:
88: humBytesShiftSector = Integer.numberOfTrailingZeros (humBytesPerSector);
89: humPartitionSectors = (int) (humPartitionEndByte - humPartitionStartByte >> humBytesShiftSector);
90: humFatStartByte = humPartitionStartByte + (long) (humReservedSectors << humBytesShiftSector);
91: humEntriesPerSector = humBytesPerSector >> 5;
92: humRootSectors = humBytesToSectors (humRootEntries << 5);
93:
94:
95:
96:
97:
98:
99:
100:
101:
102: humFatAndDataSectors = humPartitionSectors - humReservedSectors - humRootSectors;
103: if (sectorsPerCluster >= 0) {
104: humSectorsShiftCluster = Integer.numberOfTrailingZeros (sectorsPerCluster);
105: humSectorsPerCluster = sectorsPerCluster;
106: } else {
107: humSectorsShiftCluster = 0;
108: humSectorsPerCluster = 1;
109: while (humBytesToSectors (2 * (65520 + 2)) * humFatCount + (65520 << humSectorsShiftCluster) < humFatAndDataSectors) {
110: humSectorsShiftCluster++;
111: }
112: humSectorsPerCluster = 1 << humSectorsShiftCluster;
113: }
114: humBytesShiftCluster = humBytesShiftSector + humSectorsShiftCluster;
115: humBytesPerCluster = 1 << humBytesShiftCluster;
116:
117:
118:
119:
120:
121:
122: humFat12 = humFatAndDataSectors < humBytesToSectors (2 * (4085 + 2)) * humFatCount + (4085 << humSectorsShiftCluster);
123: humFatMarker = humFatID << 24 | (humFat12 ? 0x00ffff00 : 0x00ffffff);
124: humFatTailCode = humFat12 ? 0xfff : 0xffff;
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140: if (fatSectors >= 0) {
141: humFatSectors = fatSectors;
142: humDataClustersPlus2 = humFatAndDataSectors - humFatSectors * humFatCount >> humSectorsShiftCluster;
143: } else {
144: humDataClustersPlus2 = (humFatAndDataSectors >> humSectorsShiftCluster) + 2;
145: humDataClustersPlus2 = (
146: humFat12 ?
147: Math.min (4084,
148: (humFatAndDataSectors - humBytesToSectors (3 * humDataClustersPlus2 + 1 >>> 1) * humFatCount) >> humSectorsShiftCluster) :
149: (humFatAndDataSectors - humBytesToSectors (2 * humDataClustersPlus2) * humFatCount) >> humSectorsShiftCluster
150: ) + 2;
151: humFatSectors = (humFatAndDataSectors - (humDataClustersPlus2 - 2 << humSectorsShiftCluster)) / humFatCount;
152: }
153:
154: humRootStartSector = humReservedSectors + humFatSectors * humFatCount;
155:
156: humRootStartByte = humPartitionStartByte + (long) (humRootStartSector << humBytesShiftSector);
157:
158: humDataStartSector = humRootStartSector + humRootSectors;
159:
160: humDataStartByte = humPartitionStartByte + (long) (humDataStartSector << humBytesShiftSector);
161: humDataEndByte = humDataStartByte + ((long) (humDataClustersPlus2 - 2) << humBytesShiftCluster);
162: }
163:
164:
165: public final void humPrintInfo () {
166: System.out.printf (" humBytesShiftSector= %12d\n", humBytesShiftSector);
167: System.out.printf (" humBytesPerSector= %12d\n", humBytesPerSector);
168: System.out.printf ("humSectorsShiftCluster= %12d\n", humSectorsShiftCluster);
169: System.out.printf (" humSectorsPerCluster= %12d\n", humSectorsPerCluster);
170: System.out.printf (" humBytesShiftCluster= %12d\n", humBytesShiftCluster);
171: System.out.printf (" humBytesPerCluster= %12d\n", humBytesPerCluster);
172: System.out.printf (" humFatCount= %12d\n", humFatCount);
173: System.out.printf (" humReservedSectors= %12d\n", humReservedSectors);
174: System.out.printf (" humRootEntries= %12d\n", humRootEntries);
175: System.out.printf (" humEntriesPerSector= %12d\n", humEntriesPerSector);
176: System.out.printf (" humRootSectors= %12d\n", humRootSectors);
177: System.out.printf (" humMediaByte= 0x%08x\n", humMediaByte);
178: System.out.printf (" humFatID= 0x%08x\n", humFatID);
179: System.out.printf (" humFat12= %12b\n", humFat12);
180: System.out.printf (" humFatMarker= 0x%08x\n", humFatMarker);
181: System.out.printf (" humFatTailCode= 0x%08x\n", humFatTailCode);
182: System.out.printf (" humFatSectors= %12d\n", humFatSectors);
183: System.out.printf (" humPartitionSectors= %12d\n", humPartitionSectors);
184: System.out.printf (" humFatAndDataSectors= %12d\n", humFatAndDataSectors);
185: System.out.printf (" humDataClustersPlus2= %12d\n", humDataClustersPlus2);
186: System.out.printf (" humPartitionStartByte=0x%012x\n", humPartitionStartByte);
187: System.out.printf (" humFatStartByte=0x%012x\n", humFatStartByte);
188: System.out.printf (" humRootStartSector= %12d\n", humRootStartSector);
189: System.out.printf (" humRootStartByte=0x%012x\n", humRootStartByte);
190: System.out.printf (" humDataStartSector= %12d\n", humDataStartSector);
191: System.out.printf (" humDataStartByte=0x%012x\n", humDataStartByte);
192: System.out.printf (" humDataEndByte=0x%012x\n", humDataEndByte);
193: System.out.printf (" humPartitionEndByte=0x%012x\n", humPartitionEndByte);
194: System.out.printf (" humDiskEndByte=0x%012x\n", humDiskEndByte);
195: }
196:
197:
198:
199: public final int humBytesToSectors (int bytes) {
200: return (bytes + (humBytesPerSector - 1) >> humBytesShiftSector);
201: }
202: public final int humBytesToSectors (long bytes) {
203: return (int) (bytes + (humBytesPerSector - 1) >> humBytesShiftSector);
204: }
205:
206:
207:
208:
209: public void humWriteFatMarker (byte[] bb) {
210:
211:
212: for (int i = 0; i < humFatCount; i++) {
213: ByteArray.byaWl (bb, (int) humFatStartByte + (humFatSectors * i << humBytesShiftSector), humFatMarker);
214: }
215: }
216:
217:
218:
219:
220: public boolean humCopyHumanSys (byte[] bb) {
221:
222:
223: byte[] rr = XEiJ.rscGetResource ("HUMAN.SYS");
224: if (rr == null || rr.length != 58496) {
225: return false;
226: }
227: humCopySystemFile (bb, "HUMAN SYS", HUM_ARCHIVE | HUM_SYSTEM, 12 << 11 | 0 << 5 | 0 >> 1, 1993 - 1980 << 9 | 9 << 5 | 15, rr);
228: return true;
229: }
230:
231:
232:
233:
234: public boolean humCopyCommandX (byte[] bb) {
235:
236:
237: byte[] rr = XEiJ.rscGetResource ("COMMAND.X");
238: if (rr == null || rr.length != 28382) {
239: return false;
240: }
241: humCopySystemFile (bb, "COMMAND X ", HUM_ARCHIVE, 12 << 11 | 0 << 5 | 0 >> 1, 1993 - 1980 << 9 | 2 << 5 | 25, rr);
242: return true;
243: }
244:
245:
246:
247:
248: public void humCopySystemFile (byte[] bb, String name1ext, int attr, int time, int date, byte[] rr) {
249: int entry;
250: for (entry = (int) humRootStartByte; bb[entry] != 0x00; entry += 32) {
251: }
252: ByteArray.byaWstr (bb, entry, name1ext);
253: ByteArray.byaWb (bb, entry + 11, attr);
254: ByteArray.byaWstr (bb, entry + 12, "\0\0\0\0\0\0\0\0\0\0");
255: ByteArray.byaWiw (bb, entry + 22, time);
256: ByteArray.byaWiw (bb, entry + 24, date);
257: int start = 0;
258: int size = rr == null ? 0 : rr.length;
259: if (size > 0) {
260: for (start = 2; humGetFat (bb, start) != 0; start++) {
261: }
262: int end = start + (size - 1 >> humBytesShiftCluster);
263: for (int cluster = start; cluster < end; cluster++) {
264: humSetFat (bb, cluster, cluster + 1);
265: }
266: humSetFat (bb, end, humFatTailCode);
267: System.arraycopy (rr, 0, bb, (int) humDataStartByte + (start - 2 << humBytesShiftCluster), size);
268: }
269: ByteArray.byaWiw (bb, entry + 26, start);
270: ByteArray.byaWil (bb, entry + 28, size);
271: }
272:
273:
274:
275: public boolean humUndel (byte[] bb, int entry, int letter) {
276: if ((bb[entry] & 255) != 0xe5) {
277: return false;
278: }
279: int size = ByteArray.byaRils (bb, entry + 28);
280: if (size > 0) {
281: int start = ByteArray.byaRiwz (bb, entry + 26);
282: int end = start + (size - 1 >> humBytesShiftCluster);
283: if (humDataClustersPlus2 <= end) {
284: return false;
285: }
286:
287: for (int cluster = start; cluster <= end; cluster++) {
288: if (humGetFat (bb, cluster) != 0) {
289: return false;
290: }
291: }
292:
293: for (int cluster = start; cluster < end; cluster++) {
294: humSetFat (bb, cluster, cluster + 1);
295: }
296: humSetFat (bb, end, humFatTailCode);
297: }
298:
299: bb[entry] = (byte) letter;
300: return true;
301: }
302:
303:
304:
305:
306:
307: public void humDumpFat (byte[] bb) {
308: final int cols = 20;
309: StringBuilder sb = new StringBuilder ();
310: sb.append (" ");
311: for (int i = 0; i < cols; i++) {
312: sb.append (String.format (" %+5d", i));
313: }
314: sb.append ('\n');
315: for (int cluster0 = 0, cluster1 = cols; cluster0 < humDataClustersPlus2; cluster0 = cluster1, cluster1 += cols) {
316: sb.append (String.format ("%5d|", cluster0));
317: for (int cluster = cluster0; cluster < cluster1; cluster++) {
318: if (cluster < 2 || humDataClustersPlus2 <= cluster) {
319: sb.append (" ");
320: } else {
321: int next = humGetFat (bb, cluster);
322: sb.append (next == 0 ? " ---" :
323: next != humFatTailCode ? String.format (" %5d", next) :
324: " END");
325: }
326: }
327: sb.append ('\n');
328: }
329: System.out.println (sb.toString ());
330: }
331:
332:
333:
334: public int humGetFat (byte[] bb, int cluster) {
335: if (humFat12) {
336: int i = (int) humFatStartByte + 3 * (cluster >> 1);
337: return ((cluster & 1) == 0 ?
338: bb[i + 1] << 8 & 0xf00 | bb[i ] & 0x0ff :
339: bb[i + 2] << 4 & 0xff0 | bb[i + 1] >> 4 & 0x00f);
340: } else {
341: int i = (int) humFatStartByte + (cluster << 1);
342: return (char) (bb[i ] << 8 | bb[i + 1] & 255);
343: }
344: }
345:
346:
347:
348: public void humSetFat (byte[] bb, int cluster, int next) {
349: if (humFat12) {
350: int i = (int) humFatStartByte + 3 * (cluster >> 1);
351: if ((cluster & 1) == 0) {
352: bb[i ] = (byte) next;
353: bb[i + 1] = (byte) (bb[i + 1] & 0xf0 | next >> 8 & 15);
354: } else {
355: bb[i + 1] = (byte) (next << 4 | bb[i + 1] & 15);
356: bb[i + 2] = (byte) (next >> 4);
357: }
358: } else {
359: int i = (int) humFatStartByte + (cluster << 1);
360: bb[i ] = (byte) (next >> 8);
361: bb[i + 1] = (byte) next;
362: }
363: }
364:
365: }
366:
367:
368: