| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package org.apache.commons.compress.archivers.dump; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.util.Arrays; |
| 23 |
|
| 24 |
import org.apache.commons.compress.archivers.zip.ZipEncoding; |
| 25 |
import org.apache.commons.compress.utils.ByteUtils; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
final class DumpArchiveUtil { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static int calculateChecksum(final byte[] buffer) { |
| 39 |
int calc = 0; |
| 40 |
for (int i = 0; i < 256; i++) { |
| 41 |
calc += convert32(buffer, 4 * i); |
| 42 |
} |
| 43 |
return DumpArchiveConstants.CHECKSUM - (calc - convert32(buffer, 28)); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static int convert16(final byte[] buffer, final int offset) { |
| 54 |
return (int) ByteUtils.fromLittleEndian(buffer, offset, 2); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static int convert32(final byte[] buffer, final int offset) { |
| 65 |
return (int) ByteUtils.fromLittleEndian(buffer, offset, 4); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static long convert64(final byte[] buffer, final int offset) { |
| 76 |
return ByteUtils.fromLittleEndian(buffer, offset, 8); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static String decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len) throws IOException { |
| 83 |
if (offset > offset + len) { |
| 84 |
throw new IOException("Invalid offset/length combination"); |
| 85 |
} |
| 86 |
return encoding.decode(Arrays.copyOfRange(b, offset, offset + len)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static int getIno(final byte[] buffer) { |
| 96 |
return convert32(buffer, 20); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static boolean verify(final byte[] buffer) { |
| 106 |
if (buffer == null) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
final int magic = convert32(buffer, 24); |
| 111 |
if (magic != DumpArchiveConstants.NFS_MAGIC) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
final int checksum = convert32(buffer, 28); |
| 116 |
return checksum == calculateChecksum(buffer); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private DumpArchiveUtil() { |
| 123 |
} |
| 124 |
} |