Sleuth Kit Java Bindings (JNI) 4.14.0
Java bindings for using The Sleuth Kit
Loading...
Searching...
No Matches
EncodedFileUtil.java
Go to the documentation of this file.
1/*
2 * SleuthKit Java Bindings
3 *
4 * Copyright 2011-2016 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 */
19package org.sleuthkit.datamodel;
20
21import java.io.IOException;
22import java.io.RandomAccessFile;
23import java.util.Arrays;
24
28class EncodedFileUtil {
29
30 final static private int HEADER_LENGTH = 32; // All headers must be this long
31 final static private String XOR1_HEADER = "TSK_CONTAINER_XOR1_xxxxxxxxxxxxx";
32
39 static String getHeader(TskData.EncodingType type) throws IOException{
40 switch (type){
41 case XOR1:
42 return XOR1_HEADER;
43 default:
44 throw new IOException("Can not get header for " + type.toString());
45 }
46 }
47
57 static byte [] getEncodedHeader(TskData.EncodingType type) throws IOException{
58 if(type.equals(TskData.EncodingType.NONE)){
59 throw new IOException("Can not get encoded header for " + type.toString());
60 }
61 byte [] encHeader = new byte[HEADER_LENGTH];
62 byte [] plainHeader = getHeader(type).getBytes();
63
64 for(int i = 0;i < HEADER_LENGTH;i++){
65 encHeader[i] = encodeByte(plainHeader[i], type);
66 }
67 return encHeader;
68 }
69
75 static int getHeaderLength(){
76 return HEADER_LENGTH;
77 }
78
86 static byte encodeByte(byte b, TskData.EncodingType type) throws IOException{
87 switch (type){
88 case XOR1:
89 return ((byte)(b ^ 0xca));
90 default:
91 throw new IOException("Can not encode byte with encoding type " + type.toString());
92 }
93 }
94
102 static byte decodeByte(byte b, TskData.EncodingType type) throws IOException{
103 switch (type){
104 case XOR1:
105 return ((byte)(b ^ 0xca));
106 default:
107 throw new IOException("Can not decode byte with encoding type " + type.toString());
108 }
109 }
110
117 static TskData.EncodingType getEncoding(RandomAccessFile fileHandle){
118 try{
119 long curOffset = fileHandle.getFilePointer();
120 if (curOffset != 0) {
121 fileHandle.seek(0);
122 }
123 byte[] header = new byte[HEADER_LENGTH];
124 int bytesRead = fileHandle.read(header, 0, HEADER_LENGTH);
125 if(bytesRead != HEADER_LENGTH){
126 return TskData.EncodingType.NONE;
127 }
128
129 return(getTypeFromHeader(header));
130 } catch (IOException ex){
131 return TskData.EncodingType.NONE;
132 }
133 }
134
140 static private TskData.EncodingType getTypeFromHeader(byte[] header){
141 if(header.length != HEADER_LENGTH){
142 return TskData.EncodingType.NONE;
143 }
144
145 if(Arrays.equals(header, XOR1_HEADER.getBytes())){
146 return TskData.EncodingType.XOR1;
147 } else {
148 return TskData.EncodingType.NONE;
149 }
150
151 }
152}

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