Browse Source

magic numbers

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@569089 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 18 years ago
parent
commit
4c69b551ec
6 changed files with 66 additions and 32 deletions
  1. +12
    -10
      src/main/org/apache/tools/zip/ExtraFieldUtils.java
  2. +5
    -1
      src/main/org/apache/tools/zip/ZipEntry.java
  3. +13
    -5
      src/main/org/apache/tools/zip/ZipFile.java
  4. +25
    -9
      src/main/org/apache/tools/zip/ZipLong.java
  5. +2
    -1
      src/main/org/apache/tools/zip/ZipOutputStream.java
  6. +9
    -6
      src/main/org/apache/tools/zip/ZipShort.java

+ 12
- 10
src/main/org/apache/tools/zip/ExtraFieldUtils.java View File

@@ -29,6 +29,8 @@ import java.util.zip.ZipException;
// CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
public class ExtraFieldUtils {

private static final int WORD = 4;

/**
* Static registry of known extra fields.
*
@@ -95,23 +97,23 @@ public class ExtraFieldUtils {
public static ZipExtraField[] parse(byte[] data) throws ZipException {
Vector v = new Vector();
int start = 0;
while (start <= data.length - 4) {
while (start <= data.length - WORD) {
ZipShort headerId = new ZipShort(data, start);
int length = (new ZipShort(data, start + 2)).getValue();
if (start + 4 + length > data.length) {
if (start + WORD + length > data.length) {
throw new ZipException("data starting at " + start
+ " is in unknown format");
}
try {
ZipExtraField ze = createExtraField(headerId);
ze.parseFromLocalFileData(data, start + 4, length);
ze.parseFromLocalFileData(data, start + WORD, length);
v.addElement(ze);
} catch (InstantiationException ie) {
throw new ZipException(ie.getMessage());
} catch (IllegalAccessException iae) {
throw new ZipException(iae.getMessage());
}
start += (length + 4);
start += (length + WORD);
}
if (start != data.length) { // array not exhausted
throw new ZipException("data starting at " + start
@@ -130,7 +132,7 @@ public class ExtraFieldUtils {
* @since 1.1
*/
public static byte[] mergeLocalFileDataData(ZipExtraField[] data) {
int sum = 4 * data.length;
int sum = WORD * data.length;
for (int i = 0; i < data.length; i++) {
sum += data[i].getLocalFileDataLength().getValue();
}
@@ -142,8 +144,8 @@ public class ExtraFieldUtils {
System.arraycopy(data[i].getLocalFileDataLength().getBytes(),
0, result, start + 2, 2);
byte[] local = data[i].getLocalFileDataData();
System.arraycopy(local, 0, result, start + 4, local.length);
start += (local.length + 4);
System.arraycopy(local, 0, result, start + WORD, local.length);
start += (local.length + WORD);
}
return result;
}
@@ -155,7 +157,7 @@ public class ExtraFieldUtils {
* @since 1.1
*/
public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) {
int sum = 4 * data.length;
int sum = WORD * data.length;
for (int i = 0; i < data.length; i++) {
sum += data[i].getCentralDirectoryLength().getValue();
}
@@ -167,8 +169,8 @@ public class ExtraFieldUtils {
System.arraycopy(data[i].getCentralDirectoryLength().getBytes(),
0, result, start + 2, 2);
byte[] local = data[i].getCentralDirectoryData();
System.arraycopy(local, 0, result, start + 4, local.length);
start += (local.length + 4);
System.arraycopy(local, 0, result, start + WORD, local.length);
start += (local.length + WORD);
}
return result;
}


+ 5
- 1
src/main/org/apache/tools/zip/ZipEntry.java View File

@@ -30,6 +30,8 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {

private static final int PLATFORM_UNIX = 3;
private static final int PLATFORM_FAT = 0;
private static final int SHORT_MASK = 0xFFFF;
private static final int SHORT_SHIFT = 16;

private int internalAttributes = 0;
private int platform = PLATFORM_FAT;
@@ -142,11 +144,13 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
* @since Ant 1.5.2
*/
public void setUnixMode(int mode) {
// CheckStyle:MagicNumberCheck OFF - no point
setExternalAttributes((mode << 16)
// MS-DOS read-only attribute
| ((mode & 0200) == 0 ? 1 : 0)
// MS-DOS directory flag
| (isDirectory() ? 0x10 : 0));
// CheckStyle:MagicNumberCheck ON
platform = PLATFORM_UNIX;
}

@@ -156,7 +160,7 @@ public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
* @since Ant 1.6
*/
public int getUnixMode() {
return (int) ((getExternalAttributes() >> 16) & 0xFFFF);
return (int) ((getExternalAttributes() >> SHORT_SHIFT) & SHORT_MASK);
}

/**


+ 13
- 5
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -62,6 +62,12 @@ public class ZipFile {
private static final int HASH_SIZE = 509;
private static final int SHORT = 2;
private static final int WORD = 4;
private static final int NIBLET_MASK = 0x0f;
private static final int BYTE_SHIFT = 8;
private static final int POS_0 = 0;
private static final int POS_1 = 1;
private static final int POS_2 = 2;
private static final int POS_3 = 3;

/**
* Maps ZipEntrys to Longs, recording the offsets of the local
@@ -277,7 +283,7 @@ public class ZipFile {

int versionMadeBy = ZipShort.getValue(cfh, off);
off += SHORT;
ze.setPlatform((versionMadeBy >> 8) & 0x0F);
ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK);

off += WORD; // skip version info and general purpose byte

@@ -381,13 +387,13 @@ public class ZipFile {
byte[] sig = ZipOutputStream.EOCD_SIG;
int curr = archive.read();
while (curr != -1) {
if (curr == sig[0]) {
if (curr == sig[POS_0]) {
curr = archive.read();
if (curr == sig[1]) {
if (curr == sig[POS_1]) {
curr = archive.read();
if (curr == sig[SHORT]) {
if (curr == sig[POS_2]) {
curr = archive.read();
if (curr == sig[3]) {
if (curr == sig[POS_3]) {
found = true;
break;
}
@@ -471,12 +477,14 @@ public class ZipFile {
*/
private static long dosToJavaTime(long dosTime) {
Calendar cal = Calendar.getInstance();
// CheckStyle:MagicNumberCheck OFF - no point
cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0x7f) + 1980);
cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1);
cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f);
cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f);
cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f);
cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e);
// CheckStyle:MagicNumberCheck ON
return cal.getTime().getTime();
}



+ 25
- 9
src/main/org/apache/tools/zip/ZipLong.java View File

@@ -25,6 +25,22 @@ package org.apache.tools.zip;
*/
public final class ZipLong implements Cloneable {

private static final int WORD = 4;
private static final int BYTE_BIT_SIZE = 8;
private static final int BYTE_MASK = 0xFF;

private static final int BYTE_1 = 1;
private static final int BYTE_1_MASK = 0xFF00;
private static final int BYTE_1_SHIFT = 8;

private static final int BYTE_2 = 2;
private static final int BYTE_2_MASK = 0xFF0000;
private static final int BYTE_2_SHIFT = 16;

private static final int BYTE_3 = 3;
private static final long BYTE_3_MASK = 0xFF000000L;
private static final int BYTE_3_SHIFT = 24;

private long value;

/**
@@ -79,11 +95,11 @@ public final class ZipLong implements Cloneable {
* @return value as four bytes in big endian byte order
*/
public static byte[] getBytes(long value) {
byte[] result = new byte[4];
result[0] = (byte) ((value & 0xFF));
result[1] = (byte) ((value & 0xFF00) >> 8);
result[2] = (byte) ((value & 0xFF0000) >> 16);
result[3] = (byte) ((value & 0xFF000000L) >> 24);
byte[] result = new byte[WORD];
result[0] = (byte) ((value & BYTE_MASK));
result[BYTE_1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
result[BYTE_2] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
result[BYTE_3] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
return result;
}

@@ -94,10 +110,10 @@ public final class ZipLong implements Cloneable {
* @return the correspondanding Java long value
*/
public static long getValue(byte[] bytes, int offset) {
long value = (bytes[offset + 3] << 24) & 0xFF000000L;
value += (bytes[offset + 2] << 16) & 0xFF0000;
value += (bytes[offset + 1] << 8) & 0xFF00;
value += (bytes[offset] & 0xFF);
long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
value += (bytes[offset] & BYTE_MASK);
return value;
}



+ 2
- 1
src/main/org/apache/tools/zip/ZipOutputStream.java View File

@@ -57,6 +57,7 @@ public class ZipOutputStream extends FilterOutputStream {
private static final int BYTE_MASK = 0xFF;
private static final int SHORT = 2;
private static final int WORD = 4;
private static final int BUFFER_SIZE = 512;

/**
* Compression method for deflated entries.
@@ -221,7 +222,7 @@ public class ZipOutputStream extends FilterOutputStream {
*
* @since 1.14
*/
protected byte[] buf = new byte[512];
protected byte[] buf = new byte[BUFFER_SIZE];

// CheckStyle:VisibilityModifier ON



+ 9
- 6
src/main/org/apache/tools/zip/ZipShort.java View File

@@ -24,6 +24,9 @@ package org.apache.tools.zip;
*
*/
public final class ZipShort implements Cloneable {
private static final int BYTE_MASK = 0xFF;
private static final int BYTE_1_MASK = 0xFF00;
private static final int BYTE_1_SHIFT = 8;

private int value;

@@ -62,8 +65,8 @@ public final class ZipShort implements Cloneable {
*/
public byte[] getBytes() {
byte[] result = new byte[2];
result[0] = (byte) (value & 0xFF);
result[1] = (byte) ((value & 0xFF00) >> 8);
result[0] = (byte) (value & BYTE_MASK);
result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
return result;
}

@@ -83,8 +86,8 @@ public final class ZipShort implements Cloneable {
*/
public static byte[] getBytes(int value) {
byte[] result = new byte[2];
result[0] = (byte) (value & 0xFF);
result[1] = (byte) ((value & 0xFF00) >> 8);
result[0] = (byte) (value & BYTE_MASK);
result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
return result;
}

@@ -95,8 +98,8 @@ public final class ZipShort implements Cloneable {
* @return the correspondanding java int value
*/
public static int getValue(byte[] bytes, int offset) {
int value = (bytes[offset + 1] << 8) & 0xFF00;
value += (bytes[offset] & 0xFF);
int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK;
value += (bytes[offset] & BYTE_MASK);
return value;
}



Loading…
Cancel
Save