Sleuth Kit Java Bindings (JNI)  4.4.1
Java bindings for using The Sleuth Kit
Image.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2013 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.text.MessageFormat;
22 import java.util.ResourceBundle;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import java.io.File;
29 
36 public class Image extends AbstractContent {
37  //data about image
38 
39  private long type, ssize, size;
40  private String[] paths;
41  private volatile long imageHandle = 0;
42  private String timezone, md5;
43  private static ResourceBundle bundle = ResourceBundle.getBundle("org.sleuthkit.datamodel.Bundle");
44 
59  @Deprecated
60  protected Image(SleuthkitCase db, long obj_id, long type, long ssize, String name, String[] paths, String timezone, String md5) throws TskCoreException {
61  super(db, obj_id, name);
62  this.type = type;
63  this.ssize = ssize;
64  this.paths = paths;
65  this.timezone = timezone;
66  this.size = 0;
67  this.md5 = md5;
68  }
69 
83  Image(SleuthkitCase db, long obj_id, long type, long ssize, String name, String[] paths, String timezone, String md5, long size) throws TskCoreException {
84  super(db, obj_id, name);
85  this.type = type;
86  this.ssize = ssize;
87  this.paths = paths;
88  this.timezone = timezone;
89  this.size = size;
90  this.md5 = md5;
91  }
92 
98  public synchronized long getImageHandle() throws TskCoreException {
99  if (imageHandle == 0) {
100  imageHandle = SleuthkitJNI.openImage(paths);
101  }
102 
103  return imageHandle;
104  }
105 
106  @Override
108  return this;
109  }
110 
111  @Override
112  public void close() {
113  //frees nothing, as we are caching image handles
114  }
115 
116  @Override
117  public void finalize() throws Throwable {
118  try {
119  if (imageHandle != 0) {
120  SleuthkitJNI.closeImg(imageHandle);
121  imageHandle = 0;
122  }
123  } finally {
124  super.finalize();
125  }
126  }
127 
128  @Override
129  public int read(byte[] buf, long offset, long len) throws TskCoreException {
130  // read from the image
131  return SleuthkitJNI.readImg(getImageHandle(), buf, offset, len);
132  }
133 
134  @Override
135  public long getSize() {
136  if (size == 0) {
137  try {
138  if (paths.length > 0) {
139  //should always had at least one path
140  size = SleuthkitJNI.findDeviceSize(paths[0]);
141  }
142  } catch (TskCoreException ex) {
143  Logger.getLogger(Image.class.getName()).log(Level.SEVERE, "Could not find image size, image: " + this.getId(), ex); //NON-NLS
144  }
145  }
146  return size;
147  }
148 
149  //Methods for retrieval of meta-data attributes
156  return TskData.TSK_IMG_TYPE_ENUM.valueOf(type);
157  }
158 
164  public long getSsize() {
165  return ssize;
166  }
167 
168  @Override
169  public String getUniquePath() throws TskCoreException {
170  return "/img_" + getName(); //NON-NLS
171  }
172 
178  public String[] getPaths() {
179  return paths;
180  }
181 
187  public List<VolumeSystem> getVolumeSystems() throws TskCoreException {
188 
189  List<Content> children = getChildren();
190  List<VolumeSystem> vs = new ArrayList<VolumeSystem>();
191  for (Content child : children) {
192  if (child instanceof VolumeSystem) {
193  vs.add((VolumeSystem) child);
194  }
195  }
196 
197  return vs;
198  }
199 
205  public List<Volume> getVolumes() throws TskCoreException {
206 
207  List<Content> children = getChildren();
208  List<Volume> volumes = new ArrayList<Volume>();
209  for (Content child : children) {
210  if (child instanceof Volume) {
211  volumes.add((Volume) child);
212  }
213  }
214 
215  return volumes;
216  }
217 
225  public List<FileSystem> getFileSystems() throws TskCoreException {
226  List<FileSystem> fs = new ArrayList<FileSystem>();
227  fs.addAll(getSleuthkitCase().getFileSystems(this));
228  return fs;
229  }
230 
236  public String getTimeZone() {
237  return timezone;
238  }
239 
240  @Override
241  public <T> T accept(SleuthkitItemVisitor<T> v) {
242  return v.visit(this);
243  }
244 
245  @Override
246  public <T> T accept(ContentVisitor<T> v) {
247  return v.visit(this);
248  }
249 
250  @Override
251  public List<Content> getChildren() throws TskCoreException {
252  return getSleuthkitCase().getImageChildren(this);
253  }
254 
255  @Override
256  public List<Long> getChildrenIds() throws TskCoreException {
257  return getSleuthkitCase().getImageChildrenIds(this);
258  }
259 
260  @Override
261  public String toString(boolean preserveState) {
262  return super.toString(preserveState) + "Image [\t" + "\t" + "paths " + Arrays.toString(paths) + "\t" + "size " + size + "\t" + "ssize " + ssize + "\t" + "timezone " + timezone + "\t" + "type " + type + "]\t"; //NON-NLS
263  }
264 
271  public Boolean imageFileExists() {
272  if (paths.length > 0) {
273  File imageFile = new File(paths[0]);
274  return imageFile.exists();
275  }
276 
277  return false;
278  }
279 
287  public String verifyImageSize() {
288  Logger logger1 = Logger.getLogger("verifyImageSizes"); //NON-NLS
289  String errorString = "";
290  try {
291  List<VolumeSystem> volumeSystems = getVolumeSystems();
292  for (VolumeSystem vs : volumeSystems) {
293  List<Volume> volumes = vs.getVolumes();
294  for (Volume v : volumes) {
295  byte[] buf = new byte[512];
296  long endOffset = (v.getStart() + v.getLength()) * 512 - 512;
297  try {
298  int readBytes = read(buf, endOffset, 512);
299  if (readBytes < 0) {
300  logger1.warning("Possible Incomplete Image: Error reading volume at offset " + endOffset); //NON-NLS
301  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr1.text"), endOffset);
302  }
303  } catch (TskCoreException ex) {
304  logger1.warning("Possible Incomplete Image: Error reading volume at offset " + endOffset + ": " + ex.getLocalizedMessage()); //NON-NLS
305  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr2.text"), endOffset);
306  }
307  }
308  }
309 
310  List<FileSystem> fileSystems = getFileSystems();
311  for (FileSystem fs : fileSystems) {
312  long block_size = fs.getBlock_size();
313  long endOffset = fs.getImageOffset() + fs.getSize() - block_size;
314  try {
315  byte[] buf = new byte[(int) block_size];
316  int readBytes = read(buf, endOffset, block_size);
317  if (readBytes < 0) {
318  logger1.warning("Possible Incomplete Image: Error reading file system at offset " + endOffset); //NON-NLS
319  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr3.text"), endOffset);
320  }
321  } catch (TskCoreException ex) {
322  logger1.warning("Possible Incomplete Image: Error reading file system at offset " + endOffset + ": " + ex.getLocalizedMessage()); //NON-NLS
323  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr4.text"), endOffset);
324  }
325  }
326  } catch (TskException ex) {
327  // do nothing if we got an exception from trying to get file systems and volume systems
328  }
329  return errorString;
330  }
331 
337  public String getMd5() {
338  return md5;
339  }
340 }
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
List< Volume > getVolumes()
Definition: Image.java:205
List< FileSystem > getFileSystems()
Definition: Image.java:225
Image(SleuthkitCase db, long obj_id, long type, long ssize, String name, String[] paths, String timezone, String md5)
Definition: Image.java:60
TskData.TSK_IMG_TYPE_ENUM getType()
Definition: Image.java:155
List< Long > getChildrenIds()
Definition: Image.java:256
int read(byte[] buf, long offset, long len)
Definition: Image.java:129
volatile long imageHandle
Definition: Image.java:41
static long openImage(String[] imageFiles)
static long findDeviceSize(String devPath)
static ResourceBundle bundle
Definition: Image.java:43
static void closeImg(long imgHandle)
List< Content > getChildren()
Definition: Image.java:251
List< VolumeSystem > getVolumeSystems()
Definition: Image.java:187
static TSK_IMG_TYPE_ENUM valueOf(long imgType)
Definition: TskData.java:537
String toString(boolean preserveState)
Definition: Image.java:261
synchronized long getImageHandle()
Definition: Image.java:98

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.