/** * print a magic match * * @param stream DOCUMENT ME! * @param match DOCUMENT ME! * @param spacing DOCUMENT ME! */ public static void printMagicMatch(PrintStream stream, MagicMatch match, String spacing) { stream.println(spacing + "============================="); stream.println(spacing + "mime type: " + match.getMimeType()); stream.println(spacing + "description: " + match.getDescription()); stream.println(spacing + "extension: " + match.getExtension()); stream.println(spacing + "test: " + new String(match.getTest().array())); stream.println(spacing + "bitmask: " + match.getBitmask()); stream.println(spacing + "offset: " + match.getOffset()); stream.println(spacing + "length: " + match.getLength()); stream.println(spacing + "type: " + match.getType()); stream.println(spacing + "comparator: " + match.getComparator()); stream.println(spacing + "============================="); Collection submatches = match.getSubMatches(); Iterator i = submatches.iterator(); while (i.hasNext()) { printMagicMatch(stream, (MagicMatch) i.next(), spacing + " "); } }
@Override protected Object clone() { final MagicMatch clone = new MagicMatch(); clone.setBitmask(Long.toString(this.bitmask, 8)); clone.setComparator("" + this.comparator); //$NON-NLS-1$ clone.setDescription(this.description); clone.setExtension(this.extension); clone.setLength(this.length); clone.setMimeType(this.mimeType); clone.setOffset(this.offset); clone.setProperties(m); clone.setSubMatches(a); clone.setTest(ByteBuffer.wrap(this.test)); clone.setType(this.type);
/** * determine if this match or any submatches has the description * * @param desc DOCUMENT ME! * * @return whether or not the description matches */ public boolean descriptionMatches(String desc) { if ((description != null) && description.equals(desc)) { return true; } Collection submatches = getSubMatches(); Iterator i = submatches.iterator(); MagicMatch m = null; while (i.hasNext()) { m = (MagicMatch) i.next(); if (m.descriptionMatches(desc)) { return true; } } return false; }
/** Test to see if everything is in order for this match. * @return whether or not this match has enough data to be valid. */ public boolean isValid() { if (this.match == null || this.match.getTest() == null) { return false; } final String type = new String(this.match.getTest().array()).trim(); final char comparator = this.match.getComparator(); final String description = this.match.getDescription(); final String test = new String(this.match.getTest().array()).trim(); if (!type.equals("") && comparator != '\0' && //$NON-NLS-1$ (comparator == '=' || comparator == '!' || comparator == '>' || comparator == '<') && description != null && !description.equals("") && //$NON-NLS-1$ !test.equals("")) { //$NON-NLS-1$ return true; } return false; }
int offset = match.getOffset(); String description = match.getDescription(); String type = match.getType(); String test = new String(match.getTest().array()); String mimeType = match.getMimeType(); log.debug(match.print()); log.debug("test(byte[]): \n=== END MATCH INFO ====\n"); length = 8; } else if (type.equals("string")) { length = match.getTest().capacity(); } else if (type.equals("regex")) { .clone() : null; } catch (CloneNotSupportedException e) { submatch.getDescription() + "'"); match.addSubMatch(submatch); } else { log.debug("test(byte[]): submatch " + i + " doesn't match");
/** Test the data using a detector. * @param data the data we are testing. * @return if we have a match. */ private boolean testDetector(final ByteBuffer data) { final String detectorClassName = new String(this.match.getTest().array()).trim(); try { final Class<?> detectorClass = classForName(detectorClassName); final Object detectorObject = detectorClass.getDeclaredConstructor().newInstance(); final Method processMethod = detectorClass.getDeclaredMethod("process", byte[].class, //$NON-NLS-1$ Integer.TYPE, Integer.TYPE, Long.TYPE, Character.TYPE, String.class, Map.class); final String[] types = (String[]) processMethod.invoke(detectorObject, data.array(), Integer.valueOf(this.match.getOffset()), Integer.valueOf(this.match.getLength()), Long.valueOf(this.match.getBitmask()), Character.valueOf(this.match.getComparator()), this.match.getMimeType(), this.match.getProperties()); if (types != null && types.length > 0) { // the match object has no mime type set, so set from the detector class processing this.match.setMimeType(types[0]); return true; } } catch (final Throwable e) { java.util.logging.Logger.getLogger("es.gob.afirma").warning(e.toString()); //$NON-NLS-1$ } return false; }
byte[] data = FileUtils.toByteArray("file.msg"); MagicMatch match = Magic.getMagicMatch(data); String mimeType = match.getMimeType();
String type = match.getType(); String test = new String(match.getTest().array()); String mimeType = match.getMimeType(); String description = match.getDescription();
int offset = match.getOffset(); String description = match.getDescription(); String type = match.getType(); log.debug(match.print()); log.debug("test(File): \n=== END MATCH INFO ====\n"); length = 8; } else if (type.equals("string")) { length = match.getTest().capacity(); } else if (type.equals("regex")) { final int matchLength = match.getLength(); length = (matchLength == 0) ? (int) file.length() - offset : matchLength; .clone() : null; } catch (CloneNotSupportedException e) { submatch.getDescription() + "'"); match.addSubMatch(submatch); } else { log.debug("test(File): submatch " + i + " doesn't match");
this.match = new MagicMatch(); this.match.setOffset(Integer.valueOf(attrValue).intValue()); this.match.setLength(Integer.valueOf(attrValue).intValue()); this.match.setType(attrValue); this.match.setBitmask(attrValue); this.match.setComparator(attrValue);
/** Test the data against the test byte. * @param data the data we are testing. * @return if we have a match. */ private boolean testByte(final ByteBuffer data) { final String test = new String(this.match.getTest().array()).trim(); final char comparator = this.match.getComparator(); final long bitmask = this.match.getBitmask(); final byte b = (byte) (data.get(0) & bitmask); final int tst = Integer.decode(test).byteValue(); final byte t = (byte) (tst & 0xff); switch (comparator) { case '=': return t == b; case '!': return t != b; case '>': return t > b; case '<': return t < b; default: return false; } }
match.setMimeType(finalValue); log.debug("characters(): setting mimetype to '" + finalValue + "'"); } else if (isExtension) { isExtension = false; match.setExtension(finalValue); log.debug("characters(): setting extension to '" + finalValue + "'"); } else if (isDescription) { isDescription = false; match.setDescription(finalValue); log.debug("characters(): setting description to '" + finalValue + "'"); } else if (isTest) { isTest = false; match.setTest(convertOctals(finalValue)); log.debug("characters(): setting test to '" + convertOctals(finalValue) + "'"); } else { match.setProperties(properties); log.info("endElement(): not adding invalid matcher '" + match.getDescription() + "'");
private String guessMimeTypeFromDescription(MagicMatch match) { if ("MS-DOS executable (EXE)".equals(match.getDescription())) { return "application/x-dosexec"; } return "???"; } }
/** Create a parser and initialize it. */ public static synchronized void initialize() throws MagicParseException { if (!initialized) { magicParser = new MagicParser(); magicParser.initialize(); // build hint map final Iterator<MagicMatcher> i = magicParser.getMatchers().iterator(); while (i.hasNext()) { final MagicMatcher matcher = i.next(); final String ext = matcher.getMatch().getExtension(); if (ext != null && !ext.trim().equals("")) { //$NON-NLS-1$ addHint(ext, matcher); } else if (matcher.getMatch().getType().equals("detector")) { //$NON-NLS-1$ final String[] exts = matcher.getDetectorExtensions(); for (final String ext2 : exts) { addHint(ext2, matcher); } } } initialized = true; } }
private Optional<String> guessExtension(byte[] bytes) { try { // No extension : try to guess it MagicMatch match = Magic.getMagicMatch(bytes); String guessedExtension = match.getExtension(); if (!Strings.isNullOrEmpty(guessedExtension)) { return Optional.of(guessedExtension); } else { return Optional.absent(); } } catch (Exception e) { this.logger.warn("Error while attempting to guess attachment extension", e); return Optional.absent(); } }
@Override protected Object clone() throws CloneNotSupportedException { final MagicMatcher clone = new MagicMatcher(); clone.setMatch((MagicMatch) this.match.clone()); final Iterator<MagicMatcher> i = this.subMatchers.iterator(); final ArrayList<MagicMatcher> sub = new ArrayList<>(); while (i.hasNext()) { final MagicMatcher m = i.next(); sub.add((MagicMatcher) m.clone()); } clone.setSubMatchers(sub); return clone; }
int offset = match.getOffset(); String description = match.getDescription(); String type = match.getType(); String test = new String(match.getTest().array()); String mimeType = match.getMimeType(); log.debug(match.print()); log.debug("test(byte[]): \n=== END MATCH INFO ====\n"); length = 8; } else if (type.equals("string")) { length = match.getTest().capacity(); } else if (type.equals("regex")) { submatch.getDescription() + "'"); match.addSubMatch(submatch); } else { log.debug("test(byte[]): submatch " + i + " doesn't match");
String detectorClass = new String(match.getTest().array()); String[] types = detector.process(data.array(), match.getOffset(), match.getLength(), match.getBitmask(), match.getComparator(), match.getMimeType(), match.getProperties()); match.setMimeType(types[0]);
MagicMatch match = parser.getMagicMatch(f); System.out.println(match.getMimeType()) ;
String type = match.getType(); String test = new String(match.getTest().array()); String mimeType = match.getMimeType(); String description = match.getDescription();