Sleuth Kit Java Bindings (JNI)  4.2
Java bindings for using The Sleuth Kit
LayoutFile.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.List;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
30 
45 public class LayoutFile extends AbstractFile {
46 
47  private long imageHandle = -1;
48 
49  protected LayoutFile(SleuthkitCase db, long objId, String name,
53  long size, String md5Hash, FileKnown knownState, String parentPath) {
54  super(db, objId, TSK_FS_ATTR_TYPE_ENUM.TSK_FS_ATTR_TYPE_DEFAULT, (short) 0, name, fileType, 0L, 0, dirType, metaType, dirFlag, metaFlags, size, 0L, 0L, 0L, 0L, (short) 0, 0, 0, md5Hash, knownState, parentPath);
55  //this.size = calcSize(); //update calculated size
56  }
57 
63  public int getNumParts() {
64  int numParts = 0;
65  try {
66  numParts = getRanges().size();
67  } catch (TskCoreException ex) {
68  Logger.getLogger(LayoutFile.class.getName()).log(Level.INFO, "Error getting layout content ranges for size", ex); //NON-NLS
69  }
70  return numParts;
71  }
72 
73  @Override
74  public List<Content> getChildren() throws TskCoreException {
75  return getSleuthkitCase().getAbstractFileChildren(this, TskData.TSK_DB_FILES_TYPE_ENUM.DERIVED);
76  }
77 
78  @Override
79  public List<Long> getChildrenIds() throws TskCoreException {
80  return getSleuthkitCase().getAbstractFileChildrenIds(this, TskData.TSK_DB_FILES_TYPE_ENUM.DERIVED);
81  }
82 
88  private long calcSize() {
89  long calcSize = 0;
90  try {
91  for (TskFileRange range : getRanges()) {
92  calcSize += range.getByteLen();
93  }
94  } catch (TskCoreException ex) {
95  Logger.getLogger(LayoutFile.class.getName()).log(Level.SEVERE, "Error calculating layout file size from ranges", ex); //NON-NLS
96  }
97  return calcSize;
98  }
99 
100  @Override
101  public void close() {
102  //nothing to be closed
103  }
104 
105  @Override
106  protected int readInt(byte[] buf, long offset, long len) throws TskCoreException {
107  long offsetInThisLayoutContent = 0; // current offset in this LayoutContent
108  int bytesRead = 0; // Bytes read so far
109 
110  if (imageHandle == -1) {
111  Content dataSource = getDataSource();
112  if ((dataSource != null) && (dataSource instanceof Image)) {
113  Image image = (Image) dataSource;
114  imageHandle = image.getImageHandle();
115  } else {
116  throw new TskCoreException("Data Source of LayoutFile is not Image");
117  }
118  }
119 
120  for (TskFileRange range : getRanges()) {
121  if (bytesRead < len) { // we haven't read enough yet
122  if (offset < offsetInThisLayoutContent + range.getByteLen()) { // if we are in a range object we want to read from
123  long offsetInRange = 0; // how far into the current range object to start reading
124  if (bytesRead == 0) { // we haven't read anything yet so we want to read from the correct offset in this range object
125  offsetInRange = offset - offsetInThisLayoutContent; // start reading from the correct offset
126  }
127  long offsetInImage = range.getByteStart() + offsetInRange; // how far into the image to start reading
128  long lenToRead = Math.min(range.getByteLen() - offsetInRange, len - bytesRead); // how much we can read this time
129  int lenRead = readImgToOffset(imageHandle, buf, bytesRead, offsetInImage, (int) lenToRead);
130  bytesRead += lenRead;
131  if (lenToRead != lenRead) { // If image read failed or was cut short
132  break;
133  }
134  }
135  offsetInThisLayoutContent += range.getByteLen();
136  } else { // we're done reading
137  break;
138  }
139  }
140  return bytesRead;
141  }
142 
143  /*
144  * Read bytes from an image into a buffer, starting at given position in buffer
145  *
146  * @param imgHandle the image to read from
147  * @param buf the array to read into
148  * @param offsetInBuf where to start in the array
149  * @param offsetInImage where to start in the image
150  * @param lenToRead how far to read in the image
151  */
152  private int readImgToOffset(long imgHandle, byte[] buf, int offsetInBuf, long offsetInImage, int lenToRead) throws TskCoreException {
153  byte[] currentBuffer = new byte[lenToRead]; // the buffer for the current range object
154  int lenRead = SleuthkitJNI.readImg(imgHandle, currentBuffer, offsetInImage, lenToRead);
155  System.arraycopy(currentBuffer, 0, buf, offsetInBuf, lenToRead); // copy what we just read into the main buffer
156  return lenRead;
157  }
158 
159  @Override
160  public <T> T accept(ContentVisitor<T> v) {
161  return v.visit(this);
162  }
163 
164  @Override
165  public <T> T accept(SleuthkitItemVisitor<T> v) {
166  return v.visit(this);
167  }
168 
169  @Override
170  public boolean isRoot() {
171  return false;
172  }
173 
174  @Override
175  public String toString(boolean preserveState) {
176  return super.toString(preserveState) + "LayoutFile [\t" + "]\t"; //NON-NLS
177  }
178 }
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)
LayoutFile(SleuthkitCase db, long objId, String name, TSK_DB_FILES_TYPE_ENUM fileType, TSK_FS_NAME_TYPE_ENUM dirType, TSK_FS_META_TYPE_ENUM metaType, TSK_FS_NAME_FLAG_ENUM dirFlag, short metaFlags, long size, String md5Hash, FileKnown knownState, String parentPath)
Definition: LayoutFile.java:49
final Set< TSK_FS_META_FLAG_ENUM > metaFlags
int readInt(byte[] buf, long offset, long len)
DERIVED
File derived from a parent file (i.e. from ZIP)
Definition: TskData.java:646
int readImgToOffset(long imgHandle, byte[] buf, int offsetInBuf, long offsetInImage, int lenToRead)
final TSK_FS_META_TYPE_ENUM metaType
synchronized long getImageHandle()
Definition: Image.java:72

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