Sleuth Kit Java Bindings (JNI)  4.11.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-2022 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.Collections;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
30 
44 public class LayoutFile extends AbstractFile {
45 
46  private long imageHandle = -1;
47 
93  long objId,
94  long dataSourceObjectId,
95  Long fileSystemObjectId,
96  String name,
100  long size,
101  long ctime, long crtime, long atime, long mtime,
102  String md5Hash, String sha256Hash, String sha1Hash,
104  String parentPath, String mimeType,
105  String ownerUid,
106  Long osAccountObjId) {
107 
108  super(db, objId, dataSourceObjectId, fileSystemObjectId, 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, sha1Hash, knownState, parentPath, mimeType, SleuthkitCase.extractExtension(name), ownerUid, osAccountObjId, Collections.emptyList());
109  }
110 
116  public int getNumParts() {
117  int numParts = 0;
118  try {
119  numParts = getRanges().size();
120  } catch (TskCoreException ex) {
121  Logger.getLogger(LayoutFile.class.getName()).log(Level.SEVERE, String.format("Error getting layout ranges for layout file (objId = %d)", getId()), ex); //NON-NLS
122  }
123  return numParts;
124  }
125 
132  @Override
133  public boolean isRoot() {
134  return false;
135  }
136 
141  @Override
142  public void close() {
143  }
144 
156  @Override
157  protected int readInt(byte[] buf, long offset, long len) throws TskCoreException {
158  long offsetInThisLayoutContent = 0; // current offset in this LayoutContent
159  int bytesRead = 0; // Bytes read so far
160 
161  // if the caller has requested more data than we have in the file
162  // then make sure we don't go beyond the end of the file
163  long readLen = len;
164  if (offset + readLen > size)
165  readLen = size - offset;
166 
167  if (imageHandle == -1) {
168  Content dataSource = getDataSource();
169  if ((dataSource != null) && (dataSource instanceof Image)) {
170  Image image = (Image) dataSource;
171  imageHandle = image.getImageHandle();
172  } else {
173  throw new TskCoreException("Data Source of LayoutFile is not Image");
174  }
175  }
176 
177  for (TskFileRange range : getRanges()) {
178  if (bytesRead < readLen) { // we haven't read enough yet
179  if (offset < offsetInThisLayoutContent + range.getByteLen()) { // if we are in a range object we want to read from
180  long offsetInRange = 0; // how far into the current range object to start reading
181  if (bytesRead == 0) { // we haven't read anything yet so we want to read from the correct offset in this range object
182  offsetInRange = offset - offsetInThisLayoutContent; // start reading from the correct offset
183  }
184  long offsetInImage = range.getByteStart() + offsetInRange; // how far into the image to start reading
185  long lenToReadInRange = Math.min(range.getByteLen() - offsetInRange, readLen - bytesRead); // how much we can read this time
186  int lenRead = readImgToOffset(imageHandle, buf, bytesRead, offsetInImage, (int) lenToReadInRange);
187  bytesRead += lenRead;
188  if (lenToReadInRange != lenRead) { // If image read failed or was cut short
189  break;
190  }
191  }
192  offsetInThisLayoutContent += range.getByteLen();
193  } else { // we're done reading
194  break;
195  }
196  }
197  return bytesRead;
198  }
199 
216  private int readImgToOffset(long imgHandle, byte[] buf, int offsetInBuf, long offsetInImage, int lenToRead) throws TskCoreException {
217  byte[] currentBuffer = new byte[lenToRead]; // the buffer for the current range object
218  int lenRead = SleuthkitJNI.readImg(imgHandle, currentBuffer, offsetInImage, lenToRead);
219  System.arraycopy(currentBuffer, 0, buf, offsetInBuf, lenToRead); // copy what we just read into the main buffer
220  return lenRead;
221  }
222 
231  @Override
232  public <T> T accept(ContentVisitor<T> visitor) {
233  return visitor.visit(this);
234  }
235 
244  @Override
245  public <T> T accept(SleuthkitItemVisitor<T> visitor) {
246  return visitor.visit(this);
247  }
248 
255  @Override
256  public String toString(boolean preserveState) {
257  return super.toString(preserveState) + "LayoutFile [\t" + "]\t"; //NON-NLS
258  }
259 }
final TSK_FS_NAME_TYPE_ENUM dirType
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
final TskData.TSK_DB_FILES_TYPE_ENUM fileType
String toString(boolean preserveState)
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:122

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