Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update COSParser #599

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public int getBufferBegin() {
}

/**
* @return index of the end of of valid unread data in buffer.
* @return index of the end of valid unread data in buffer.
*/
public int getBufferEnd() {
return bufferEnd;
Expand Down Expand Up @@ -369,7 +369,7 @@ private void readFromStreamToBuffer(int offset, int len) throws IOException {
int read = getInputStream().read(buffer, offset, len);
this.bufferEnd = read;
if (read < len) {
// TODO: in fact, read may be less then len even if eof is not reached.
// TODO: in fact, read may be less than len even if eof is not reached.
// Fix problem for this case.
eod = offset + (read == -1 ? 0 : read);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/verapdf/io/InternalInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void seek(long offset) throws IOException {
if (offset > this.getStreamLength()) {
throw new IllegalArgumentException("Destination offset is greater than stream length");
}
this.offset = offset < 0 ? 0 : offset;
this.offset = offset;
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/verapdf/parser/BaseParserInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
* @author Maxim Plushchov
*/
public interface BaseParserInputStream {

int read() throws IOException;

int read(byte[] buffer) throws IOException;

byte readByte() throws IOException;

void unread() throws IOException;

void unread(int i) throws IOException;

int peek() throws IOException;

int skip(int size) throws IOException;

void close() throws IOException;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/verapdf/parser/COSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,20 @@ protected COSObject getDictionary() throws IOException {
}

private COSObject decryptCOSString(COSObject string) {
StandardSecurityHandler ssh =
this.document.getStandardSecurityHandler();
StandardSecurityHandler ssh = this.document.getStandardSecurityHandler();
try {
ssh.decryptString((COSString) string.getDirectBase(), this.keyOfCurrentObject);
return string;
} catch (IOException | GeneralSecurityException e) {
LOGGER.log(Level.WARNING, getErrorMessage("Can't decrypt string in object " + this.keyOfCurrentObject));
LOGGER.log(Level.WARNING, getErrorMessage("Can't decrypt string"));
return string;
}
}

protected String getErrorMessage(String message) {
if (keyOfCurrentObject != null) {
return message + "(object key = " + keyOfCurrentObject + ")";
}
return getBaseParser().getErrorMessage(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
*/
public class DecodedObjectStreamParser extends SeekableCOSParser {

private COSStream objectStream;
private Map<Integer, Long> internalOffsets;
private final COSStream objectStream;
private final Map<Integer, Long> internalOffsets;

/**
* Constructor from decoded object stream data and COSStream.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/verapdf/parser/PDFParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class PDFParser extends SeekableCOSParser {

private static final Logger LOGGER = Logger.getLogger(PDFParser.class.getCanonicalName());

/**
* Linearization dictionary must be in first 1024 bytes of document
*/
protected final int LINEARIZATION_DICTIONARY_LOOKUP_SIZE = 1024;
private static final String HEADER_PATTERN = "%PDF-";
private static final String PDF_DEFAULT_VERSION = "1.4";
private static final byte[] STARTXREF = "startxref".getBytes(StandardCharsets.ISO_8859_1);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/verapdf/parser/PDFStreamParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public Object parseNextToken() throws IOException {
case '7':
case '8':
case '9':
case '+':
case '-': {
Token token = getBaseParser().getToken();
getBaseParser().nextToken();
Expand Down
22 changes: 2 additions & 20 deletions src/main/java/org/verapdf/parser/SeekableCOSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
import org.verapdf.exceptions.VeraPDFParserException;
import org.verapdf.io.InternalInputStream;
import org.verapdf.io.SeekableInputStream;
import org.verapdf.pd.encryption.StandardSecurityHandler;
import org.verapdf.tools.resource.ASFileStreamCloser;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -43,11 +41,6 @@ public class SeekableCOSParser extends COSParser {

private static final Logger LOGGER = Logger.getLogger(SeekableCOSParser.class.getCanonicalName());

/**
* Linearization dictionary must be in first 1024 bytes of document
*/
protected final int LINEARIZATION_DICTIONARY_LOOKUP_SIZE = 1024;

public SeekableCOSParser(final SeekableInputStream seekableInputStream) throws IOException {
super(new SeekableBaseParser(seekableInputStream));
}
Expand Down Expand Up @@ -280,20 +273,9 @@ public COSDocument getDocument() {
return document;
}

private COSObject decryptCOSString(COSObject string) {
StandardSecurityHandler ssh =
this.document.getStandardSecurityHandler();
try {
ssh.decryptString((COSString) string.getDirectBase(), this.keyOfCurrentObject);
return string;
} catch (IOException | GeneralSecurityException e) {
LOGGER.log(Level.WARNING, getErrorMessage("Can't decrypt string in object"));
return string;
}
}

@Override
protected String getErrorMessage(String message) {
return getBaseParser().getErrorMessage(message);
return getErrorMessage(message, getSource().getCurrentOffset());
}

protected String getErrorMessage(String message, long offset) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/verapdf/pd/font/cmap/CMapParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CMapParser extends PSParser {
private static final Logger LOGGER = Logger.getLogger(CMapParser.class.getCanonicalName());
private COSObject lastCOSName;

private CMap cMap;
private final CMap cMap;

private static final String WMODE_STRING = "WMode";
private static final String REGISTRY_SRTRING = "Registry";
Expand Down
Loading