Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddFileTypeSignaturePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2020 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.autopsy.modules.filetypeid;
20 
21 import java.io.UnsupportedEncodingException;
22 import java.nio.charset.Charset;
23 import javax.swing.DefaultComboBoxModel;
24 import javax.swing.JOptionPane;
25 import javax.xml.bind.DatatypeConverter;
26 import org.openide.util.NbBundle;
27 import org.openide.util.NbBundle.Messages;
29 
33 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
34 class AddFileTypeSignaturePanel extends javax.swing.JPanel {
35 
36  private static final String RAW_SIGNATURE_TYPE_COMBO_BOX_ITEM = NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.signatureComboBox.rawItem");
37  private static final String START_OFFSET_RELATIVE_COMBO_BOX_ITEM = NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.offsetComboBox.startItem");
38  private static final String END_OFFSET_RELATIVE_COMBO_BOX_ITEM = NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.offsetComboBox.endItem");
39  private static final String ASCII_SIGNATURE_TYPE_COMBO_BOX_ITEM = NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.signatureComboBox.asciiItem");
40 
44  AddFileTypeSignaturePanel() {
45  initComponents();
46  customizeComponents();
47  }
48 
54  AddFileTypeSignaturePanel(Signature toEdit) {
55  this();
56  this.setComponentValues(toEdit);
57  }
58 
62  private void customizeComponents() {
63  setSignatureTypeComboBoxModel();
64  setOffsetRealtiveToComboBoxModel();
65  }
66 
70  private void setSignatureTypeComboBoxModel() {
71  DefaultComboBoxModel<String> sigTypeComboBoxModel = new DefaultComboBoxModel<>();
72  sigTypeComboBoxModel.addElement(RAW_SIGNATURE_TYPE_COMBO_BOX_ITEM);
73  sigTypeComboBoxModel.addElement(ASCII_SIGNATURE_TYPE_COMBO_BOX_ITEM);
74  signatureTypeComboBox.setModel(sigTypeComboBoxModel);
75  signatureTypeComboBox.setSelectedItem(RAW_SIGNATURE_TYPE_COMBO_BOX_ITEM);
76  }
77 
81  private void setOffsetRealtiveToComboBoxModel() {
82  DefaultComboBoxModel<String> offsetRelComboBoxModel = new DefaultComboBoxModel<>();
83  offsetRelComboBoxModel.addElement(START_OFFSET_RELATIVE_COMBO_BOX_ITEM);
84  offsetRelComboBoxModel.addElement(END_OFFSET_RELATIVE_COMBO_BOX_ITEM);
85  offsetRelativeToComboBox.setModel(offsetRelComboBoxModel);
86  offsetRelativeToComboBox.setSelectedItem(START_OFFSET_RELATIVE_COMBO_BOX_ITEM);
87  }
88 
94  @Messages({"AddFileTypeSignaturePanel.signatureStringFail.text=Couldn't get signatures string"})
95  private void setComponentValues(Signature toEdit) {
96  if (toEdit.isRelativeToStart()) {
97  this.offsetRelativeToComboBox.setSelectedIndex(0);
98  } else {
99  this.offsetRelativeToComboBox.setSelectedIndex(1);
100  }
101  this.offsetTextField.setText(toEdit.getOffset() + "");
102  if (Signature.Type.RAW == toEdit.getType()) {
103  this.signatureTypeComboBox.setSelectedIndex(0);
104  this.signatureTextField.setText(DatatypeConverter.printHexBinary(toEdit.getSignatureBytes()));
105  } else {
106  this.signatureTypeComboBox.setSelectedIndex(1);
107  try {
108  this.signatureTextField.setText(new String(toEdit.getSignatureBytes(), "UTF-8"));
109  } catch (UnsupportedEncodingException ex) {
110  JOptionPane.showMessageDialog(this,
111  ex.getLocalizedMessage(),
112  Bundle.AddFileTypeSignaturePanel_signatureStringFail_text(),
113  JOptionPane.ERROR_MESSAGE);
114  }
115  }
116  }
117 
125  Signature getSignature() {
126 
130  FileType.Signature.Type sigType = signatureTypeComboBox.getSelectedItem() == RAW_SIGNATURE_TYPE_COMBO_BOX_ITEM ? FileType.Signature.Type.RAW : FileType.Signature.Type.ASCII;
131 
135  String sigString = signatureTextField.getText();
136 
137  if (sigString.isEmpty()) {
138  JOptionPane.showMessageDialog(this,
139  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidSignature.message"),
140  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidSignature.title"),
141  JOptionPane.ERROR_MESSAGE);
142 
143  return null;
144  }
145  byte[] signatureBytes;
146  if (FileType.Signature.Type.RAW == sigType) {
147  try {
148  sigString = sigString.replaceAll("\\s", ""); //NON-NLS
149  signatureBytes = DatatypeConverter.parseHexBinary(sigString);
150  } catch (IllegalArgumentException ex) {
151  JOptionPane.showMessageDialog(this,
152  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidRawSignatureBytes.message"),
153  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidSignatureBytes.title"),
154  JOptionPane.ERROR_MESSAGE);
155  return null;
156  }
157  } else {
158  signatureBytes = sigString.getBytes(Charset.forName("UTF-8"));
159  }
160 
164  long offset;
165  boolean isRelativeToStart = offsetRelativeToComboBox.getSelectedItem() == START_OFFSET_RELATIVE_COMBO_BOX_ITEM;
166 
167  try {
168  offset = Long.parseUnsignedLong(offsetTextField.getText());
169  if (!isRelativeToStart && signatureBytes.length > offset + 1) {
170  JOptionPane.showMessageDialog(this,
171  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidOffset.length"),
172  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidOffset.title"),
173  JOptionPane.ERROR_MESSAGE);
174  return null;
175  }
176  } catch (NumberFormatException ex) {
177  JOptionPane.showMessageDialog(this,
178  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidOffset.message"),
179  NbBundle.getMessage(FileTypeIdGlobalSettingsPanel.class, "FileTypeIdGlobalSettingsPanel.JOptionPane.invalidOffset.title"),
180  JOptionPane.ERROR_MESSAGE);
181  return null;
182  }
183 
190  FileType.Signature signature = new FileType.Signature(signatureBytes, offset, sigType, isRelativeToStart);
191  return signature;
192  }
193 
199  @SuppressWarnings("unchecked")
200  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
201  private void initComponents() {
202 
203  offsetLabel = new javax.swing.JLabel();
204  offsetTextField = new javax.swing.JTextField();
205  offsetRelativeToComboBox = new javax.swing.JComboBox<>();
206  offsetRelativeToLabel = new javax.swing.JLabel();
207  hexPrefixLabel = new javax.swing.JLabel();
208  signatureTypeComboBox = new javax.swing.JComboBox<>();
209  signatureLabel = new javax.swing.JLabel();
210  signatureTypeLabel = new javax.swing.JLabel();
211  signatureTextField = new javax.swing.JTextField();
212 
213  org.openide.awt.Mnemonics.setLocalizedText(offsetLabel, org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.offsetLabel.text")); // NOI18N
214 
215  offsetTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.offsetTextField.text")); // NOI18N
216 
217  org.openide.awt.Mnemonics.setLocalizedText(offsetRelativeToLabel, org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.offsetRelativeToLabel.text")); // NOI18N
218 
219  org.openide.awt.Mnemonics.setLocalizedText(hexPrefixLabel, org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.hexPrefixLabel.text")); // NOI18N
220 
221  signatureTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
222  public void actionPerformed(java.awt.event.ActionEvent evt) {
223  signatureTypeComboBoxActionPerformed(evt);
224  }
225  });
226 
227  org.openide.awt.Mnemonics.setLocalizedText(signatureLabel, org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.signatureLabel.text")); // NOI18N
228 
229  org.openide.awt.Mnemonics.setLocalizedText(signatureTypeLabel, org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.signatureTypeLabel.text")); // NOI18N
230 
231  signatureTextField.setText(org.openide.util.NbBundle.getMessage(AddFileTypeSignaturePanel.class, "AddFileTypeSignaturePanel.signatureTextField.text")); // NOI18N
232  signatureTextField.addActionListener(new java.awt.event.ActionListener() {
233  public void actionPerformed(java.awt.event.ActionEvent evt) {
234  signatureTextFieldActionPerformed(evt);
235  }
236  });
237 
238  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
239  this.setLayout(layout);
240  layout.setHorizontalGroup(
241  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
242  .addGroup(layout.createSequentialGroup()
243  .addContainerGap()
244  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
245  .addGroup(layout.createSequentialGroup()
246  .addComponent(signatureTypeLabel)
247  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
248  .addComponent(signatureTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
249  .addGroup(layout.createSequentialGroup()
250  .addComponent(signatureLabel)
251  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
252  .addComponent(hexPrefixLabel)
253  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
254  .addComponent(signatureTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
255  .addGroup(layout.createSequentialGroup()
256  .addComponent(offsetLabel)
257  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
258  .addComponent(offsetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
259  .addGroup(layout.createSequentialGroup()
260  .addComponent(offsetRelativeToLabel)
261  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
262  .addComponent(offsetRelativeToComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
263  .addContainerGap(46, Short.MAX_VALUE))
264  );
265  layout.setVerticalGroup(
266  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267  .addGroup(layout.createSequentialGroup()
268  .addContainerGap()
269  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
270  .addComponent(signatureTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
271  .addComponent(signatureTypeLabel))
272  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
273  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
274  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
275  .addComponent(hexPrefixLabel)
276  .addComponent(signatureTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
277  .addComponent(signatureLabel))
278  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
279  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
280  .addComponent(offsetLabel)
281  .addComponent(offsetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
282  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
283  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
284  .addComponent(offsetRelativeToComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
285  .addComponent(offsetRelativeToLabel))
286  .addContainerGap())
287  );
288  }// </editor-fold>//GEN-END:initComponents
289 
290  private void signatureTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signatureTypeComboBoxActionPerformed
291  if (signatureTypeComboBox.getSelectedItem() == RAW_SIGNATURE_TYPE_COMBO_BOX_ITEM) {
292  hexPrefixLabel.setVisible(true);
293  signatureTextField.setText("0000");
294  } else {
295  hexPrefixLabel.setVisible(false);
296  signatureTextField.setText("");
297  }
298  }//GEN-LAST:event_signatureTypeComboBoxActionPerformed
299 
300  private void signatureTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signatureTextFieldActionPerformed
301  // TODO add your handling code here:
302  }//GEN-LAST:event_signatureTextFieldActionPerformed
303 
304 
305  // Variables declaration - do not modify//GEN-BEGIN:variables
306  private javax.swing.JLabel hexPrefixLabel;
307  private javax.swing.JLabel offsetLabel;
308  private javax.swing.JComboBox<String> offsetRelativeToComboBox;
309  private javax.swing.JLabel offsetRelativeToLabel;
310  private javax.swing.JTextField offsetTextField;
311  private javax.swing.JLabel signatureLabel;
312  private javax.swing.JTextField signatureTextField;
313  private javax.swing.JComboBox<String> signatureTypeComboBox;
314  private javax.swing.JLabel signatureTypeLabel;
315  // End of variables declaration//GEN-END:variables
316 }

Copyright © 2012-2022 Basis Technology. Generated on: Mon Dec 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.