Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Jan 27, 2025
1 parent f88aa03 commit 7f42992
Show file tree
Hide file tree
Showing 48 changed files with 159 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DecryptionHeader {
// size:2 - bit length of encryption key
private int bitLength;
// size:2 - Processing flags
private Flags flags;
private Flag flags;
// size:2 - size of Encrypted Random Data (m)
// size:m - encrypted random data
private byte[] encryptedRandomData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum Flags {
public enum Flag {

PASSWORD_KEY(0x1, "password"),
CERTIFICATE_KEY(0x2, "certificate"),
Expand All @@ -41,11 +41,11 @@ public enum Flags {
private final int code;
private final String title;

public static Flags parseCode(int code) {
for (Flags flags : values())
public static Flag parseCode(int code) {
for (Flag flags : values())
if (flags.code == code)
return flags;

throw new EnumConstantNotPresentException(Flags.class, "code: " + code);
throw new EnumConstantNotPresentException(Flag.class, "code: " + code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void add(ZipEntry entry) {
if (fileNameWriter.containsKey(entry.getFileName()))
throw new EntryDuplicationException(entry.getFileName());

tempZipModel.addEntry(entry);
tempZipModel.addZipEntry(entry);
fileNameWriter.put(entry.getFileName(), ZipEntryWriter.create(entry, tempZipModel.getTempDir()));
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/ru/olegcherednik/zip4jvm/io/BaseMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@
public class BaseMarker implements Marker {

private final Map<String, Long> map = new HashMap<>();
@Setter
private long offs;
//@Setter
private long absOffs;

public void setAbsOffs(long absOffs) {
this.absOffs = absOffs;
}

// ---------- Marker ----------

@Override
public final void mark(String id) {
map.put(id, offs);
map.put(id, absOffs);
}

@Override
Expand All @@ -51,7 +55,7 @@ public final long getMark(String id) {

@Override
public final long getMarkSize(String id) {
return offs - map.getOrDefault(id, 0L);
return absOffs - map.getOrDefault(id, 0L);
}

}
8 changes: 4 additions & 4 deletions src/main/java/ru/olegcherednik/zip4jvm/io/in/DataInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ default byte[] readBytes(int total) throws IOException {
return ArrayUtils.EMPTY_BYTE_ARRAY;

byte[] buf = new byte[total];
int n = read(buf, 0, buf.length);
int nowRead = read(buf, 0, buf.length);

if (n == IOUtils.EOF)
if (nowRead == IOUtils.EOF)
return ArrayUtils.EMPTY_BYTE_ARRAY;
if (n < total)
return Arrays.copyOfRange(buf, 0, n);
if (nowRead < total)
return Arrays.copyOfRange(buf, 0, nowRead);
return buf;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class MarkerDataInput extends BaseDataInput {

@Override
public void mark(String id) {
marker.setOffs(getAbsOffs());
marker.setAbsOffs(getAbsOffs());
marker.mark(id);
}

Expand All @@ -43,7 +43,7 @@ public final long getMark(String id) {

@Override
public final long getMarkSize(String id) {
marker.setOffs(getAbsOffs());
marker.setAbsOffs(getAbsOffs());
return marker.getMarkSize(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface RandomAccessDataInput extends DataInput {

void seek(long absOffs) throws IOException;

// TODO this should not be here -> this is from Marker
void seek(String id) throws IOException;

long available() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void write(int b) throws IOException {

@Override
public final void mark(String id) {
marker.setOffs(absOffs);
marker.setAbsOffs(absOffs);
marker.mark(id);
}

Expand All @@ -53,7 +53,7 @@ public final long getMark(String id) {

@Override
public final long getMarkSize(String id) {
marker.setOffs(absOffs);
marker.setAbsOffs(absOffs);
return marker.getMarkSize(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private void readCentralDirectory(RandomAccessDataInput in) throws IOException {
int mainDiskNo = ZipModelBuilder.getMainDiskNo(endCentralDirectory, zip64);
long relativeOffs = ZipModelBuilder.getCentralDirectoryRelativeOffs(endCentralDirectory, zip64);
long totalEntries = ZipModelBuilder.getTotalEntries(endCentralDirectory, zip64);

in.seek(srcZip.getAbsOffs(mainDiskNo, relativeOffs));
centralDirectory = getCentralDirectoryReader(totalEntries).read(in);
}
Expand All @@ -115,17 +116,17 @@ private void readCentralDirectory(RandomAccessDataInput in) throws IOException {

public static void findEndCentralDirectorySignature(RandomAccessDataInput in) throws IOException {
int commentLength = ZipModel.MAX_COMMENT_SIZE;
long available = in.available() - EndCentralDirectory.MIN_SIZE;
long absOffs = in.available() - EndCentralDirectory.MIN_SIZE;

do {
in.seek(available--);
in.seek(absOffs--);
commentLength--;

if (in.isDwordSignature(EndCentralDirectory.SIGNATURE)) {
in.mark(MARKER_END_CENTRAL_DIRECTORY);
return;
}
} while (commentLength >= 0 && available >= 0);
} while (commentLength >= 0 && absOffs >= 0);

throw new SignatureNotFoundException(EndCentralDirectory.SIGNATURE, "EndCentralDirectory");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ private String readComment(DataInput in) throws IOException {
}

private static void checkSignature(DataInput in) throws IOException {
long offs = in.getAbsOffs();
long absOffs = in.getAbsOffs();

if (in.readDwordSignature() != EndCentralDirectory.SIGNATURE)
throw new SignatureNotFoundException(EndCentralDirectory.SIGNATURE, "EndCentralDirectory", offs);
throw new SignatureNotFoundException(EndCentralDirectory.SIGNATURE, "EndCentralDirectory", absOffs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ru.olegcherednik.zip4jvm.model.CentralDirectory;
import ru.olegcherednik.zip4jvm.model.CompressionMethod;
import ru.olegcherednik.zip4jvm.model.ExternalFileAttributes;
import ru.olegcherednik.zip4jvm.model.GeneralPurposeFlag;
import ru.olegcherednik.zip4jvm.model.InternalFileAttributes;
import ru.olegcherednik.zip4jvm.model.Version;
import ru.olegcherednik.zip4jvm.model.extrafield.PkwareExtraField;
Expand Down Expand Up @@ -64,7 +65,7 @@ protected CentralDirectory.FileHeader readFileHeader(DataInput in) throws IOExce

fileHeader.setVersionMadeBy(Version.of(in.readWord()));
fileHeader.setVersionToExtract(Version.of(in.readWord()));
fileHeader.setGeneralPurposeFlagData(in.readWord());
fileHeader.setGeneralPurposeFlag(new GeneralPurposeFlag(in.readWord()));
fileHeader.setCompressionMethod(CompressionMethod.parseCode(in.readWord()));
fileHeader.setLastModifiedTime((int) in.readDword());
fileHeader.setCrc32(in.readDword());
Expand All @@ -73,25 +74,25 @@ protected CentralDirectory.FileHeader readFileHeader(DataInput in) throws IOExce

int fileNameLength = in.readWord();
int extraFieldLength = in.readWord();
int fileCommentLength = in.readWord();
Charset charset = customizeCharset.apply(fileHeader.getGeneralPurposeFlag().getCharset());

fileHeader.setCommentLength(in.readWord());
fileHeader.setDiskNo(in.readWord());
fileHeader.setInternalFileAttributes(getInternalFileAttribute(in.readBytes(InternalFileAttributes.SIZE)));
fileHeader.setExternalFileAttributes(getExternalFileAttribute(in.readBytes(ExternalFileAttributes.SIZE)));
fileHeader.setLocalFileHeaderRelativeOffs(in.readDword());
fileHeader.setFileName(in.readString(fileNameLength, charset));
fileHeader.setExtraField((PkwareExtraField) getExtraFiledReader(extraFieldLength, fileHeader).read(in));
fileHeader.setComment(in.readString(fileCommentLength, charset));
fileHeader.setComment(in.readString(fileHeader.getCommentLength(), charset));

return fileHeader;
}

private static void checkSignature(DataInput in) throws IOException {
long offs = in.getAbsOffs();
long absOffs = in.getAbsOffs();

if (in.readDwordSignature() != CentralDirectory.FileHeader.SIGNATURE)
throw new SignatureNotFoundException(CentralDirectory.FileHeader.SIGNATURE, "CentralDirectory", offs);
throw new SignatureNotFoundException(CentralDirectory.FileHeader.SIGNATURE, "CentralDirectory", absOffs);
}

private static InternalFileAttributes getInternalFileAttribute(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.io.readers.crypto.strong;

import ru.olegcherednik.zip4jvm.crypto.strong.DecryptionHeader;
import ru.olegcherednik.zip4jvm.crypto.strong.Flags;
import ru.olegcherednik.zip4jvm.crypto.strong.Flag;
import ru.olegcherednik.zip4jvm.crypto.strong.Recipient;
import ru.olegcherednik.zip4jvm.exception.Zip4jvmException;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
Expand All @@ -31,7 +31,7 @@
import java.util.LinkedList;
import java.util.List;

import static ru.olegcherednik.zip4jvm.utils.ValidationUtils.realBigZip64;
import static ru.olegcherednik.zip4jvm.utils.ValidationUtils.realBigZip64Check;

/**
* @author Oleg Cherednik
Expand All @@ -55,13 +55,13 @@ public DecryptionHeader read(DataInput in) throws IOException {
decryptionHeader.setVersion(in.readWord());
decryptionHeader.setEncryptionAlgorithm(in.readWord());
decryptionHeader.setBitLength(in.readWord());
decryptionHeader.setFlags(Flags.parseCode(in.readWord()));
boolean passwordKey = decryptionHeader.getFlags() == Flags.PASSWORD_KEY;
decryptionHeader.setFlags(Flag.parseCode(in.readWord()));
boolean passwordKey = decryptionHeader.getFlags() == Flag.PASSWORD_KEY;
int encryptedRandomDataSize = in.readWord();
decryptionHeader.setEncryptedRandomData(in.readBytes(encryptedRandomDataSize));
int recipientCount = (int) in.readDword();

realBigZip64(recipientCount, "zip64.decryptionHeader.recipientCount");
realBigZip64Check(recipientCount, "zip64.decryptionHeader.recipientCount");

decryptionHeader.setHashAlgorithm(passwordKey ? 0 : in.readWord());
int hashSize = passwordKey ? 0x0 : in.readWord();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class AesExtraFieldRecordReader implements Reader<AesExtraFieldReco

@Override
public AesExtraFieldRecord read(DataInput in) throws IOException {
AesVersion version = AesVersion.parseNumber(in.readWord());
AesVersion version = AesVersion.parseCode(in.readWord());
String vendor = in.readString(2, Charsets.UTF_8);
AesStrength strength = AesStrength.of(in.readByte());
CompressionMethod compressionMethod = CompressionMethod.parseCode(in.readWord());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package ru.olegcherednik.zip4jvm.io.readers.extrafiled;

import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.model.extrafield.PkwareExtraField;
import ru.olegcherednik.zip4jvm.model.extrafield.records.ExtendedTimestampExtraFieldRecord;
import ru.olegcherednik.zip4jvm.utils.function.Reader;
import ru.olegcherednik.zip4jvm.utils.time.UnixTimestampConverterUtils;
Expand All @@ -39,9 +40,9 @@ public final class ExtendedTimestampExtraFieldRecordReader implements Reader<Ext
@Override
public ExtendedTimestampExtraFieldRecord read(DataInput in) throws IOException {
ExtendedTimestampExtraFieldRecord.Flag flag = new ExtendedTimestampExtraFieldRecord.Flag(in.readByte());
long lastModificationTime = -1;
long lastAccessTime = -1;
long creationTime = -1;
long lastModificationTime = PkwareExtraField.NO_DATA;
long lastAccessTime = PkwareExtraField.NO_DATA;
long creationTime = PkwareExtraField.NO_DATA;

if (flag.isLastModificationTime())
lastModificationTime = UnixTimestampConverterUtils.unixToJavaTime(in.readDword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private static Map<Integer, Function<Integer, Reader<? extends PkwareExtraField.
map.put(Zip64.ExtendedInfo.SIGNATURE, size -> new ExtendedInfoReader(size,
uncompressedSize,
compressedSize,
offs, disk));
offs,
disk));
map.put(AesExtraFieldRecord.SIGNATURE, AesExtraFieldRecordReader::new);
map.put(NtfsTimestampExtraFieldRecord.SIGNATURE, NtfsTimestampExtraFieldRecordReader::new);
map.put(InfoZipOldUnixExtraFieldRecord.SIGNATURE, InfoZipOldUnixExtraFieldRecordReader::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public InfoZipNewUnixExtraFieldRecord read(DataInput in) throws IOException {
int version = in.readByte();

InfoZipNewUnixExtraFieldRecord.Payload payload = version == 1 ? readVersionOnePayload(in)
: readVersionUnknown(version, in);
: readUnknownPayload(version, in);

return InfoZipNewUnixExtraFieldRecord.builder()
.dataSize(size)
Expand All @@ -59,11 +59,11 @@ private static InfoZipNewUnixExtraFieldRecord.VersionOnePayload readVersionOnePa
.gid(String.valueOf(gid)).build();
}

private InfoZipNewUnixExtraFieldRecord.VersionUnknownPayload readVersionUnknown(int version, DataInput in)
private InfoZipNewUnixExtraFieldRecord.UnknownPayload readUnknownPayload(int version, DataInput in)
throws IOException {
byte[] data = in.readBytes(size - ByteUtils.BYTE_SIZE);
return InfoZipNewUnixExtraFieldRecord.VersionUnknownPayload.builder()
.version(version)
.data(data).build();
return InfoZipNewUnixExtraFieldRecord.UnknownPayload.builder()
.version(version)
.data(data).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public final class NtfsTimestampExtraFieldRecordReader implements Reader<NtfsTim

@Override
public NtfsTimestampExtraFieldRecord read(DataInput in) throws IOException {
long offs = in.getAbsOffs();
long absOffs = in.getAbsOffs();
in.skip(4);

List<NtfsTimestampExtraFieldRecord.Tag> tags = readTags(offs, in);
List<NtfsTimestampExtraFieldRecord.Tag> tags = readTags(absOffs, in);

return NtfsTimestampExtraFieldRecord.builder()
.dataSize(size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package ru.olegcherednik.zip4jvm.io.readers.extrafiled;

import ru.olegcherednik.zip4jvm.crypto.strong.EncryptionAlgorithm;
import ru.olegcherednik.zip4jvm.crypto.strong.Flags;
import ru.olegcherednik.zip4jvm.crypto.strong.Flag;
import ru.olegcherednik.zip4jvm.io.in.DataInput;
import ru.olegcherednik.zip4jvm.model.extrafield.records.StrongEncryptionHeaderExtraFieldRecord;
import ru.olegcherednik.zip4jvm.utils.function.Reader;
Expand All @@ -43,16 +43,17 @@ public StrongEncryptionHeaderExtraFieldRecord read(DataInput in) throws IOExcept
int format = in.readWord();
EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithm.parseCode(in.readWord());
int bitLength = in.readWord();
Flags flags = Flags.parseCode(in.readWord());
Flag flag = Flag.parseCode(in.readWord());
byte[] unknown = in.readBytes(4);

return StrongEncryptionHeaderExtraFieldRecord.builder()
.dataSize(size)
.format(format)
.encryptionAlgorithm(encryptionAlgorithm)
.bitLength(bitLength)
.flag(flag)
.unknown(unknown)
.flags(flags).build();
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.io.IOException;

import static ru.olegcherednik.zip4jvm.utils.ValidationUtils.realBigZip64;
import static ru.olegcherednik.zip4jvm.utils.ValidationUtils.realBigZip64Check;

/**
* @author Oleg Cherednik
Expand All @@ -42,20 +42,20 @@ public Zip64.EndCentralDirectoryLocator read(DataInput in) throws IOException {
locator.setEndCentralDirectoryRelativeOffs(in.readQword());
locator.setTotalDisks(in.readDword());

realBigZip64(locator.getMainDiskNo(), "zip64.locator.mainDisk");
realBigZip64(locator.getMainDiskNo(), "zip64.locator.totalDisks");
realBigZip64(locator.getEndCentralDirectoryRelativeOffs(), "zip64.locator.centralDirectoryOffs");
realBigZip64Check(locator.getMainDiskNo(), "zip64.locator.mainDisk");
realBigZip64Check(locator.getMainDiskNo(), "zip64.locator.totalDisks");
realBigZip64Check(locator.getEndCentralDirectoryRelativeOffs(), "zip64.locator.centralDirectoryOffs");

return locator;
}

private static void checkSignature(DataInput in) throws IOException {
long offs = in.getAbsOffs();
long absOffs = in.getAbsOffs();

if (in.readDwordSignature() != Zip64.EndCentralDirectoryLocator.SIGNATURE)
throw new SignatureNotFoundException(Zip64.EndCentralDirectoryLocator.SIGNATURE,
"Zip64.EndCentralDirectoryLocator",
offs);
absOffs);
}

}
Loading

0 comments on commit 7f42992

Please sign in to comment.