Sleuth Kit Java Bindings (JNI)  4.2
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 
57  protected Image(SleuthkitCase db, long obj_id, long type, long ssize, String name, String[] paths, String timezone, String md5) throws TskCoreException {
58  super(db, obj_id, name);
59  this.type = type;
60  this.ssize = ssize;
61  this.paths = paths;
62  this.timezone = timezone;
63  this.size = 0;
64  this.md5 = md5;
65  }
66 
72  public synchronized long getImageHandle() throws TskCoreException {
73  if (imageHandle == 0) {
74  imageHandle = SleuthkitJNI.openImage(paths);
75  }
76 
77  return imageHandle;
78  }
79 
80  @Override
82  return this;
83  }
84 
85  @Override
86  public void close() {
87  //frees nothing, as we are caching image handles
88  }
89 
90  @Override
91  public void finalize() throws Throwable {
92  try {
93  if (imageHandle != 0) {
94  SleuthkitJNI.closeImg(imageHandle);
95  imageHandle = 0;
96  }
97  } finally {
98  super.finalize();
99  }
100  }
101 
102  @Override
103  public int read(byte[] buf, long offset, long len) throws TskCoreException {
104  // read from the image
105  return SleuthkitJNI.readImg(getImageHandle(), buf, offset, len);
106  }
107 
108  @Override
109  public long getSize() {
110  if (size == 0) {
111  try {
112  if (paths.length > 0) {
113  //should always had at least one path
114  size = SleuthkitJNI.findDeviceSize(paths[0]);
115  }
116  } catch (TskCoreException ex) {
117  Logger.getLogger(Image.class.getName()).log(Level.SEVERE, "Could not find image size, image: " + this.getId(), ex); //NON-NLS
118  }
119  }
120  return size;
121  }
122 
123  //Methods for retrieval of meta-data attributes
130  return TskData.TSK_IMG_TYPE_ENUM.valueOf(type);
131  }
132 
138  public long getSsize() {
139  return ssize;
140  }
141 
142  @Override
143  public String getUniquePath() throws TskCoreException {
144  return "/img_" + getName(); //NON-NLS
145  }
146 
152  public String[] getPaths() {
153  return paths;
154  }
155 
160  public List<VolumeSystem> getVolumeSystems() throws TskCoreException {
161 
162  List<Content> children = getChildren();
163  List<VolumeSystem> vs = new ArrayList<VolumeSystem>();
164  for (Content child : children) {
165  if (child instanceof VolumeSystem) {
166  vs.add((VolumeSystem) child);
167  }
168  }
169 
170  return vs;
171  }
172 
177  public List<Volume> getVolumes() throws TskCoreException {
178 
179  List<Content> children = getChildren();
180  List<Volume> volumes = new ArrayList<Volume>();
181  for (Content child : children) {
182  if (child instanceof Volume) {
183  volumes.add((Volume) child);
184  }
185  }
186 
187  return volumes;
188  }
189 
196  public List<FileSystem> getFileSystems() throws TskCoreException {
197  List<FileSystem> fs = new ArrayList<FileSystem>();
198  fs.addAll(getSleuthkitCase().getFileSystems(this));
199  return fs;
200  }
201 
207  public String getTimeZone() {
208  return timezone;
209  }
210 
211  @Override
212  public <T> T accept(SleuthkitItemVisitor<T> v) {
213  return v.visit(this);
214  }
215 
216  @Override
217  public <T> T accept(ContentVisitor<T> v) {
218  return v.visit(this);
219  }
220 
221  @Override
222  public List<Content> getChildren() throws TskCoreException {
223  return getSleuthkitCase().getImageChildren(this);
224  }
225 
226  @Override
227  public List<Long> getChildrenIds() throws TskCoreException {
228  return getSleuthkitCase().getImageChildrenIds(this);
229  }
230 
231  @Override
232  public String toString(boolean preserveState) {
233  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
234  }
235 
241  public Boolean imageFileExists() {
242  if (paths.length > 0) {
243  File imageFile = new File(paths[0]);
244  return imageFile.exists();
245  }
246 
247  return false;
248  }
249 
257  public String verifyImageSize() {
258  Logger logger1 = Logger.getLogger("verifyImageSizes"); //NON-NLS
259  String errorString = "";
260  try {
261  List<VolumeSystem> volumeSystems = getVolumeSystems();
262  for (VolumeSystem vs : volumeSystems) {
263  List<Volume> volumes = vs.getVolumes();
264  for (Volume v : volumes) {
265  byte[] buf = new byte[512];
266  long endOffset = (v.getStart() + v.getLength()) * 512 - 512;
267  try {
268  int readBytes = read(buf, endOffset, 512);
269  if (readBytes < 0) {
270  logger1.warning("Possible Incomplete Image: Error reading volume at offset " + endOffset); //NON-NLS
271  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr1.text"), endOffset);
272  }
273  } catch (TskCoreException ex) {
274  logger1.warning("Possible Incomplete Image: Error reading volume at offset " + endOffset + ": " + ex.getLocalizedMessage()); //NON-NLS
275  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr2.text"), endOffset);
276  }
277  }
278  }
279 
280  List<FileSystem> fileSystems = getFileSystems();
281  for (FileSystem fs : fileSystems) {
282  long block_size = fs.getBlock_size();
283  long endOffset = fs.getImageOffset() + fs.getSize() - block_size;
284  try {
285  byte[] buf = new byte[(int) block_size];
286  int readBytes = read(buf, endOffset, block_size);
287  if (readBytes < 0) {
288  logger1.warning("Possible Incomplete Image: Error reading file system at offset " + endOffset); //NON-NLS
289  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr3.text"), endOffset);
290  }
291  } catch (TskCoreException ex) {
292  logger1.warning("Possible Incomplete Image: Error reading file system at offset " + endOffset + ": " + ex.getLocalizedMessage()); //NON-NLS
293  errorString = MessageFormat.format(bundle.getString("Image.verifyImageSize.errStr4.text"), endOffset);
294  }
295  }
296  } catch (TskException ex) {
297  // do nothing if we got an exception from trying to get file systems and volume systems
298  }
299  return errorString;
300  }
301 
307  public String getMd5() {
308  return md5;
309  }
310 }
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
List< Volume > getVolumes()
Definition: Image.java:177
List< FileSystem > getFileSystems()
Definition: Image.java:196
Image(SleuthkitCase db, long obj_id, long type, long ssize, String name, String[] paths, String timezone, String md5)
Definition: Image.java:57
TskData.TSK_IMG_TYPE_ENUM getType()
Definition: Image.java:129
List< Long > getChildrenIds()
Definition: Image.java:227
int read(byte[] buf, long offset, long len)
Definition: Image.java:103
volatile long imageHandle
Definition: Image.java:41
static long findDeviceSize(String devPath)
static ResourceBundle bundle
Definition: Image.java:43
static void closeImg(long imgHandle)
List< Content > getChildren()
Definition: Image.java:222
List< VolumeSystem > getVolumeSystems()
Definition: Image.java:160
static TSK_IMG_TYPE_ENUM valueOf(long imgType)
Definition: TskData.java:513
String toString(boolean preserveState)
Definition: Image.java:232
static synchronized long openImage(String[] imageFiles)
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.