Skip to content

Commit

Permalink
pcap.core: Add getRemaining
Browse files Browse the repository at this point in the history
Puts similar file access in one place.

Change-Id: Id985d2734235a10ad2f8400fa2e312cd77bd0058
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/201867
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
  • Loading branch information
MatthewKhouzam committed May 19, 2023
1 parent bca0acc commit f0f09cb
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class PcapOldFile extends PcapFile {
PcapTimestampScale fTimestampPrecision;

public static boolean preValidate(Path filePath) throws IOException {
if(!PcapFile.preValidate(filePath)) {
if (!PcapFile.preValidate(filePath)) {
return false;
}
// Check file validity
Expand All @@ -57,23 +57,25 @@ public static boolean preValidate(Path filePath) throws IOException {
return false;
}
File reader = filePath.toFile();
try (FileInputStream fis = new FileInputStream(reader)){
try (FileInputStream fis = new FileInputStream(reader)) {
byte[] data = new byte[PcapFileValues.GLOBAL_HEADER_SIZE];

int size = fis.read(data);
if(size != PcapFileValues.GLOBAL_HEADER_SIZE) {
if (size != PcapFileValues.GLOBAL_HEADER_SIZE) {
return false;
}
ByteBuffer globalHeader = ByteBuffer.wrap(data);
int magicNumber = globalHeader.getInt();
switch (magicNumber) {
case PcapFileValues.MAGIC_BIG_ENDIAN_MICRO: // file is big endian
break;
case PcapFileValues.MAGIC_LITTLE_ENDIAN_MICRO: // file is little endian
case PcapFileValues.MAGIC_LITTLE_ENDIAN_MICRO: // file is little
// endian
break;
case PcapFileValues.MAGIC_BIG_ENDIAN_NANO: // file is big endian
break;
case PcapFileValues.MAGIC_LITTLE_ENDIAN_NANO: // file is little endian
case PcapFileValues.MAGIC_LITTLE_ENDIAN_NANO: // file is little
// endian
break;
default:
return false;
Expand Down Expand Up @@ -160,10 +162,10 @@ public PcapOldFile(Path filePath) throws BadPcapFileException, IOException {
public synchronized @Nullable PcapOldPacket parseNextPacket() throws IOException, BadPcapFileException, BadPacketException {

// Parse the packet header
if (getFileChannel().size() - getFileChannel().position() == 0) {
if (getRemaining() == 0) {
return null;
}
if (getFileChannel().size() - getFileChannel().position() < PcapFileValues.PACKET_HEADER_SIZE) {
if (getRemaining() < PcapFileValues.PACKET_HEADER_SIZE) {
throw new BadPcapFileException("A pcap header is invalid."); //$NON-NLS-1$
}

Expand All @@ -176,7 +178,7 @@ public PcapOldFile(Path filePath) throws BadPcapFileException, IOException {
pcapPacketHeader.position(PcapFileValues.INCLUDED_LENGTH_POSITION);
long includedPacketLength = ConversionHelper.unsignedIntToLong(pcapPacketHeader.getInt());

if (getFileChannel().size() - getFileChannel().position() < includedPacketLength) {
if (getRemaining() < includedPacketLength) {
throw new BadPcapFileException("A packet header is invalid."); //$NON-NLS-1$
}

Expand All @@ -199,14 +201,25 @@ public PcapOldFile(Path filePath) throws BadPcapFileException, IOException {

}

/**
* Get the remaining size in bytes
*
* @return the remaining size, in bytes
* @throws IOException
* if the file is inaccessible
*/
private long getRemaining() throws IOException {
return getFileChannel().size() - getFileChannel().position();
}

@Override
public synchronized boolean skipNextPacket() throws IOException, BadPcapFileException {

// Parse the packet header
if (getFileChannel().size() - getFileChannel().position() == 0) {
if (getRemaining() == 0) {
return false;
}
if (getFileChannel().size() - getFileChannel().position() < PcapFileValues.GLOBAL_HEADER_SIZE) {
if (getRemaining() < PcapFileValues.GLOBAL_HEADER_SIZE) {
throw new BadPcapFileException("A pcap header is invalid."); //$NON-NLS-1$
}

Expand All @@ -218,7 +231,7 @@ public synchronized boolean skipNextPacket() throws IOException, BadPcapFileExce
pcapPacketHeader.position(PcapFileValues.INCLUDED_LENGTH_POSITION);
long includedPacketLength = ConversionHelper.unsignedIntToLong(pcapPacketHeader.getInt());

if (getFileChannel().size() - getFileChannel().position() < includedPacketLength) {
if (getRemaining() < includedPacketLength) {
throw new BadPcapFileException("A packet header is invalid."); //$NON-NLS-1$
}

Expand Down

0 comments on commit f0f09cb

Please sign in to comment.