/** * Create instance from the two bytes starting at offset. * @param bytes the bytes to store as a ZipShort * @param offset the offset to start */ public ZipShort (final byte[] bytes, final int offset) { value = ZipShort.getValue(bytes, offset); }
/** * Override to make two instances with same value equal. * @param o an object to compare * @return true if the objects are equal */ @Override public boolean equals(final Object o) { if (o == null || !(o instanceof ZipShort)) { return false; } return value == ((ZipShort) o).getValue(); }
/** * Helper method to get the value as a java int from a two-byte array * @param bytes the array of bytes * @return the corresponding java int value */ public static int getValue(final byte[] bytes) { return getValue(bytes, 0); }
@Override public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) { super.parseFromCentralDirectoryData(data, offset, length); this.rcount = ZipShort.getValue(data, offset); this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2)); } }
@Override public void parseFromCentralDirectoryData(byte[] buffer, int offset, int length) throws ZipException { if (length < BASE_SIZE) { throw new ZipException("Too short content for ResourceAlignmentExtraField (0xa11e): " + length); } int alignmentValue = ZipShort.getValue(buffer, offset); this.alignment = (short) (alignmentValue & (ALLOW_METHOD_MESSAGE_CHANGE_FLAG - 1)); this.allowMethodChange = (alignmentValue & ALLOW_METHOD_MESSAGE_CHANGE_FLAG) != 0; } }
/** * Parse central directory format. * * @param data the buffer to read data from * @param offset offset into buffer to read data * @param length the length of data */ public void parseCentralDirectoryFormat(final byte[] data, final int offset, final int length) { this.format = ZipShort.getValue(data, offset); this.algId = EncryptionAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2)); this.bitlen = ZipShort.getValue(data, offset + 4); this.flags = ZipShort.getValue(data, offset + 6); this.rcount = ZipLong.getValue(data, offset + 8); if (rcount > 0) { this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 12)); this.hashSize = ZipShort.getValue(data, offset + 14); // srlist... hashed public keys for (long i = 0; i < this.rcount; i++) { for (int j = 0; j < this.hashSize; j++) { // ZipUtil.signedByteToUnsignedInt(data[offset + 16 + (i * this.hashSize) + j])); } } } }
final int ivSize = ZipShort.getValue(data, offset); this.ivData = new byte[ivSize]; System.arraycopy(data, offset + 4, this.ivData, 0, ivSize); this.format = ZipShort.getValue(data, offset + ivSize + 6); this.algId = EncryptionAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + ivSize + 8)); this.bitlen = ZipShort.getValue(data, offset + ivSize + 10); this.flags = ZipShort.getValue(data, offset + ivSize + 12); final int erdSize = ZipShort.getValue(data, offset + ivSize + 14); this.erdData = new byte[erdSize]; System.arraycopy(data, offset + ivSize + 16, this.erdData, 0, erdSize); System.out.println("rcount: " + rcount); if (rcount == 0) { final int vSize = ZipShort.getValue(data, offset + ivSize + 20 + erdSize); this.vData = new byte[vSize - 4]; this.vCRC32 = new byte[4]; System.arraycopy(data, offset + ivSize + 22 + erdSize + vSize - 4, vCRC32, 0, 4); } else { this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + ivSize + 20 + erdSize)); this.hashSize = ZipShort.getValue(data, offset + ivSize + 22 + erdSize); final int resize = ZipShort.getValue(data, offset + ivSize + 24 + erdSize); this.recipientKeyHash = new byte[this.hashSize]; this.keyBlob = new byte[resize - this.hashSize]; final int vSize = ZipShort.getValue(data, offset + ivSize + 26 + erdSize + resize); this.vData = new byte[vSize - 4];
/** * The actual data to put into central directory data - without Header-ID * or length specifier. * * @return the central directory data */ @Override public byte[] getCentralDirectoryData() { final byte[] centralData = new byte[getCentralDirectoryLength().getValue()]; final byte[] localData = getLocalFileDataData(); // Truncate out create & access time (last 8 bytes) from // the copy of the local data we obtained: System.arraycopy(localData, 0, centralData, 0, centralData.length); return centralData; }
@Override public byte[] getCentralDirectoryData() { final byte[] data = new byte[getCentralDirectoryLength().getValue()]; int off = addSizes(data); if (relativeHeaderOffset != null) { System.arraycopy(relativeHeaderOffset.getBytes(), 0, data, off, DWORD); off += DWORD; } if (diskStart != null) { System.arraycopy(diskStart.getBytes(), 0, data, off, WORD); off += WORD; // NOSONAR - assignment as documentation } return data; }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * * @return get the data */ @Override public byte[] getLocalFileDataData() { final byte[] data = new byte[getLocalFileDataLength().getValue()]; int pos = 4; System.arraycopy(TIME_ATTR_TAG.getBytes(), 0, data, pos, 2); pos += 2; System.arraycopy(TIME_ATTR_SIZE.getBytes(), 0, data, pos, 2); pos += 2; System.arraycopy(modifyTime.getBytes(), 0, data, pos, 8); pos += 8; System.arraycopy(accessTime.getBytes(), 0, data, pos, 8); pos += 8; System.arraycopy(createTime.getBytes(), 0, data, pos, 8); return data; }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * * @return get the data */ @Override public byte[] getLocalFileDataData() { final byte[] data = new byte[getLocalFileDataLength().getValue()]; int pos = 0; data[pos++] = 0; if (bit0_modifyTimePresent) { data[0] |= MODIFY_TIME_BIT; System.arraycopy(modifyTime.getBytes(), 0, data, pos, 4); pos += 4; } if (bit1_accessTimePresent && accessTime != null) { data[0] |= ACCESS_TIME_BIT; System.arraycopy(accessTime.getBytes(), 0, data, pos, 4); pos += 4; } if (bit2_createTimePresent && createTime != null) { data[0] |= CREATE_TIME_BIT; System.arraycopy(createTime.getBytes(), 0, data, pos, 4); pos += 4; // NOSONAR - assignment as documentation } return data; }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * @return get the data */ @Override public byte[] getLocalFileDataData() { // CRC will be added later final byte[] data = new byte[getLocalFileDataLength().getValue() - WORD]; System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2); final byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc // CheckStyle:MagicNumber OFF System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data, 2, WORD); System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2); System.arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8, 2); System.arraycopy(linkArray, 0, data, 10, linkArray.length); // CheckStyle:MagicNumber ON crc.reset(); crc.update(data); final long checksum = crc.getValue(); final byte[] result = new byte[data.length + WORD]; System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD); System.arraycopy(data, 0, result, WORD, data.length); return result; }
sum += element.getLocalFileDataLength().getValue();
sum += element.getCentralDirectoryLength().getValue();
/** * Populate data from this array as if it was in local file data. * * @param data an array of bytes * @param offset the start offset * @param length the number of bytes in the array from offset * @throws java.util.zip.ZipException on error */ @Override public void parseFromLocalFileData( final byte[] data, int offset, final int length ) throws ZipException { final int len = offset + length; // skip reserved offset += 4; while (offset + 4 <= len) { final ZipShort tag = new ZipShort(data, offset); offset += 2; if (tag.equals(TIME_ATTR_TAG)) { readTimeAttr(data, offset, len - offset); break; } final ZipShort size = new ZipShort(data, offset); offset += 2 + size.getValue(); } }
/** * Reads the stream until it find the "End of central directory * record" and consumes it as well. */ private void skipRemainderOfArchive() throws IOException { // skip over central directory. One LFH has been read too much // already. The calculation discounts file names and extra // data so it will be too short. realSkip((long) entriesRead * CFH_LEN - LFH_LEN); findEocdRecord(); realSkip((long) ZipFile.MIN_EOCD_SIZE - WORD /* signature */ - SHORT /* comment len */); readFully(shortBuf); // file comment realSkip(ZipShort.getValue(shortBuf)); }
/** * Parses the supported flags from the given archive data. * * @param data local file header or a central directory entry. * @param offset offset at which the general purpose bit starts * @return parsed flags */ public static GeneralPurposeBit parse(final byte[] data, final int offset) { final int generalPurposeFlag = ZipShort.getValue(data, offset); final GeneralPurposeBit b = new GeneralPurposeBit(); b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0); b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0); b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG) != 0); b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0); b.slidingDictionarySize = (generalPurposeFlag & SLIDING_DICTIONARY_SIZE_FLAG) != 0 ? 8192 : 4096; b.numberOfShannonFanoTrees = (generalPurposeFlag & NUMBER_OF_SHANNON_FANO_TREES_FLAG) != 0 ? 3 : 2; return b; }