Sleuth Kit Java Bindings (JNI)  4.10.1
Java bindings for using The Sleuth Kit
LayoutFile.java
Go to the documentation of this file.
1 /*
2  * SleuthKit Java Bindings
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
29 
43 public class LayoutFile extends AbstractFile {
44 
45  private long imageHandle = -1;
46 
86  long objId,
87  long dataSourceObjectId,
88  String name,
92  long size,
93  long ctime, long crtime, long atime, long mtime,
94  String md5Hash, String sha256Hash, FileKnown knownState,
95  String parentPath, String mimeType) {
96  super(db, objId, dataSourceObjectId, TSK_FS_ATTR_TYPE_ENUM.TSK_FS_ATTR_TYPE_DEFAULT, 0, name, fileType, 0L, 0, dirType, metaType, dirFlag, metaFlags, size, ctime, crtime, atime, mtime, (short) 0, 0, 0, md5Hash, sha256Hash, knownState, parentPath, mimeType, SleuthkitCase.extractExtension(name));
97  }
98 
104  public int getNumParts() {
105  int numParts = 0;
106  try {
107  numParts = getRanges().size();
108  } catch (TskCoreException ex) {
109  Logger.getLogger(LayoutFile.class.getName()).log(Level.SEVERE, String.format("Error getting layout ranges for layout file (objId = %d)", getId()), ex); //NON-NLS
110  }
111  return numParts;
112  }
113 
120  @Override
121  public boolean isRoot() {
122  return false;
123  }
124 
129  @Override
130  public void close() {
131  }
132 
144  @Override
145  protected int readInt(byte[] buf, long offset, long len) throws TskCoreException {
146  long offsetInThisLayoutContent = 0; // current offset in this LayoutContent
147  int bytesRead = 0; // Bytes read so far
148 
149  // if the caller has requested more data than we have in the file
150  // then make sure we don't go beyond the end of the file
151  long readLen = len;
152  if (offset + readLen > size)
153  readLen = size - offset;
154 
155  if (imageHandle == -1) {
156  Content dataSource = getDataSource();
157  if ((dataSource != null) && (dataSource instanceof Image)) {
158  Image image = (Image) dataSource;
159  imageHandle = image.getImageHandle();
160  } else {
161  throw new TskCoreException("Data Source of LayoutFile is not Image");
162  }
163  }
164 
165  for (TskFileRange range : getRanges()) {
166  if (bytesRead < readLen) { // we haven't read enough yet
167  if (offset < offsetInThisLayoutContent + range.getByteLen()) { // if we are in a range object we want to read from
168  long offsetInRange = 0; // how far into the current range object to start reading
169  if (bytesRead == 0) { // we haven't read anything yet so we want to read from the correct offset in this range object
170  offsetInRange = offset - offsetInThisLayoutContent; // start reading from the correct offset
171  }
172  long offsetInImage = range.getByteStart() + offsetInRange; // how far into the image to start reading
173  long lenToReadInRange = Math.min(range.getByteLen() - offsetInRange, readLen - bytesRead); // how much we can read this time
174  int lenRead = readImgToOffset(imageHandle, buf, bytesRead, offsetInImage, (int) lenToReadInRange);
175  bytesRead += lenRead;
176  if (lenToReadInRange != lenRead) { // If image read failed or was cut short
177  break;
178  }
179  }
180  offsetInThisLayoutContent += range.getByteLen();
181  } else { // we're done reading
182  break;
183  }
184  }
185  return bytesRead;
186  }
187 
204  private int readImgToOffset(long imgHandle, byte[] buf, int offsetInBuf, long offsetInImage, int lenToRead) throws TskCoreException {
205  byte[] currentBuffer = new byte[lenToRead]; // the buffer for the current range object
206  int lenRead = SleuthkitJNI.readImg(imgHandle, currentBuffer, offsetInImage, lenToRead);
207  System.arraycopy(currentBuffer, 0, buf, offsetInBuf, lenToRead); // copy what we just read into the main buffer
208  return lenRead;
209  }
210 
219  @Override
220  public <T> T accept(ContentVisitor<T> visitor) {
221  return visitor.visit(this);
222  }
223 
232  @Override
233  public <T> T accept(SleuthkitItemVisitor<T> visitor) {
234  return visitor.visit(this);
235  }
236 
243  @Override
244  public String toString(boolean preserveState) {
245  return super.toString(preserveState) + "LayoutFile [\t" + "]\t"; //NON-NLS
246  }
247 
276  @Deprecated
277  @SuppressWarnings("deprecation")
278  protected LayoutFile(SleuthkitCase db, long objId, String name,
279  TSK_DB_FILES_TYPE_ENUM fileType,
281  TSK_FS_NAME_FLAG_ENUM dirFlag, short metaFlags,
282  long size, String md5Hash, FileKnown knownState, String parentPath) {
283  this(db, objId, db.getDataSourceObjectId(objId), name, fileType, dirType, metaType, dirFlag, metaFlags, size, 0L, 0L, 0L, 0L, md5Hash, null, knownState, parentPath, null);
284  }
285 }
final TSK_FS_NAME_TYPE_ENUM dirType
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
final TSK_FS_NAME_FLAG_ENUM dirFlag
final TskData.TSK_DB_FILES_TYPE_ENUM fileType
String toString(boolean preserveState)
final Set< TSK_FS_META_FLAG_ENUM > metaFlags
int readInt(byte[] buf, long offset, long len)
final TSK_FS_META_TYPE_ENUM metaType
synchronized long getImageHandle()
Definition: Image.java:121

Copyright © 2011-2020 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.