def __call__(self, prev): name = self.path + "[]" address, size, last = self.cluster.next() if self.filesize: if self.done >= self.filesize: error("(FAT) bad metadata for " + self.path) return field = File(self.root, name, size=size) if prev.first is None: field._description = 'File size: %s' % humanFilesize(self.filesize//8) field.setSubIStream(self.createInputStream) field.datasize = min(self.filesize - self.done, size) self.done += field.datasize else: field = Directory(self.root, name, size=size) padding = self.root.getFieldByAddress(address, feed=False) if not isinstance(padding, (PaddingBytes, RawBytes)): error("(FAT) address %u doesn't point to a padding field" % address) return if last: next = None else: next = lambda: self(field) field.setLinks(prev.first, next) self.root.writeFieldsIn(padding, address, (field,)) return field
def _getContentSize(self): if not hasattr(self, "_content_size"): try: self._content_size = self.createContentSize() except HACHOIR_ERRORS, err: error("Unable to compute %s content size: %s" % (self.__class__.__name__, err)) self._content_size = None
def _getDescription(self): if self._description is None: try: self._description = self.createDescription() if isinstance(self._description, str): self._description = makeUnicode(self._description) except HACHOIR_ERRORS, err: error("Error getting description of %s: %s" \ % (self.path, unicode(err))) self._description = self.PARSER_TAGS["description"]
def processFile(filename, quality=0.5): charset = getTerminalCharset() filename, real_filename = unicodeFilename(filename, charset), filename # Create parser try: tags = None parser = createParser(filename, real_filename=real_filename, tags=tags) except InputStreamError, err: error(unicode(err)) return False
def getMetaDataOther(values, filename, display_filename=False, priority=None, human=True, display=True, charset='utf-8'): real_filename = filename # Create parser try: if values['force_parser']: tags = [("id", values['force_parser']), None] else: tags = None parser = createParser(filename, real_filename=real_filename, tags=tags) except InputStreamError, err: error(unicode(err)) return False
def processFileReturn(filename, display_filename=False, priority=None, human=True, display=True): charset = getTerminalCharset() # filename, real_filename = unicode(filename, charset), filename if type(filename) == str: filename, real_filename = unicodeFilename(filename, charset), filename else: real_filename = filename.encode(getTerminalCharset()) try: parser = createParser(filename, real_filename=real_filename, tags=None) except InputStreamError, err: error(unicode(err)) try: del(parser) except: pass return False
def extractMetadata(parser, quality=QUALITY_NORMAL): """ Create a Metadata class from a parser. Returns None if no metadata extractor does exist for the parser class. """ try: extractor = extractors[parser.__class__] except KeyError: return None metadata = extractor(quality) try: metadata.extract(parser) except HACHOIR_ERRORS, err: error("Error during metadata extraction: %s" % unicode(err))
def processFile(values, filename, display_filename=False, priority=None, human=True, display=True): charset = getTerminalCharset() filename, real_filename = unicodeFilename(filename, charset), filename # Create parser try: if values.force_parser: tags = [ ("id", values.force_parser), None ] else: tags = None parser = createParser(filename, real_filename=real_filename, tags=tags) except InputStreamError, err: error(unicode(err)) return False
def writeFile(self, filename, stream, offset, size): # Create directory (only on first call) if not self.mkdir: self.createDirectory() self.mkdir = True # Create output file filename, real_filename = path.join(self.directory_unicode, filename), \ path.join(self.directory_raw, filename) output = FileOutputStream(filename, real_filename=real_filename) # Write output try: output.copyBytesFrom(stream, offset, size // 8) except StreamError, err: error(u"copyBytesFrom() error: %s" % err)
def writeFile(self, filename, stream, offset, size): # Create directory (only on first call) if not self.mkdir: self.createDirectory() self.mkdir = True # Create output file filename, real_filename = path.join(self.directory_unicode, filename), \ path.join(self.directory_raw, filename) output = FileOutputStream(filename, real_filename=real_filename) # Write output try: output.copyBytesFrom(stream, offset, size//8) except StreamError, err: error(u"copyBytesFrom() error: %s" % err)
def processFile(values, filename, display_filename=False, priority=None, human=True, display=True): charset = getTerminalCharset() filename, real_filename = unicodeFilename(filename, charset), filename # Create parser try: if values.force_parser: tags = [("id", values.force_parser), None] else: tags = None parser = createParser(filename, real_filename=real_filename, tags=tags) except InputStreamError, err: error(unicode(err)) return False
def extractMetadata(parser, quality=QUALITY_NORMAL, **kwargs): """ Create a Metadata class from a parser. Returns None if no metadata extractor does exist for the parser class. """ try: extractor = extractors[parser.__class__] except KeyError: return None metadata = extractor(quality) meta_extract_error = True try: if 'scan_index' in kwargs: metadata.extract(parser, scan_index=kwargs['scan_index']) else: metadata.extract(parser) meta_extract_error = False except HACHOIR_ERRORS, err: error("Error during metadata extraction: %s" % unicode(err))
def processFileReturn(filename, display_filename=False, priority=None, human=True, display=True): charset = getTerminalCharset() # filename, real_filename = unicode(filename, charset), filename if type(filename) == str: filename, real_filename = unicodeFilename(filename, charset), filename else: real_filename = filename.encode(getTerminalCharset()) try: parser = createParser(filename, real_filename=real_filename, tags=None) except InputStreamError, err: error(unicode(err)) try: del (parser) except: pass return False
def add(self, parser): tags = parser.getParserTags() err = self.validParser(parser, tags) if err: error("Skip parser %s: %s" % (parser.__name__, err)) return _tags = [] for tag in tags.iteritems(): tag = self.translate(*tag) if isinstance(tag, tuple): _tags.append(tag) elif tag is not True: error("[%s] %s" % (parser.__name__, tag)) return self.parser_list.append(parser) for name, values in _tags: byname = self.bytag.setdefault(name,{}) for value in values: byname.setdefault(value,[]).append(parser)
def testRandom(seed=0, tests=(1,8)): random.seed(seed) a = array('L') parser_list = HachoirParserList() n = max(tests) * max(parser.getParserTags()["min_size"] for parser in parser_list) k = 8 * a.itemsize for i in xrange((n - 1) // k + 1): a.append(random.getrandbits(k)) a = StringInputStream(a.tostring(), source="<random data>") ok = True for parser in parser_list: size = parser.getParserTags()["min_size"] for test in tests: a._size = a._current_size = size * test try: parser(a, validate=True) error("[%s] Parser didn't reject random data" % parser.__name__) except ValidateError: continue except HACHOIR_ERRORS, err: error(u"[%s] %s" % (parser.__name__, err)) ok = False
# filename, real_filename = unicode(filename, charset), filename if type(filename) == str: filename, real_filename = unicodeFilename(filename, charset), filename else: real_filename = filename.encode(getTerminalCharset()) try: parser = createParser(filename, real_filename=real_filename, tags=None) except InputStreamError, err: error(unicode(err)) try: del (parser) except: pass return False if not parser: error(_("Unable to parse file: %s") % filename) try: del (parser) except: pass return False try: metadata = extractMetadata(parser, 1.0) except HachoirError, err: error(unicode(err)) metadata = None if not metadata: parser.error( _("Hachoir can't extract metadata, but is able to parse: %s") % filename) try:
def registerExtractor(parser, extractor): assert parser not in extractors assert issubclass(extractor, RootMetadata) extractors[parser] = extractor def extractMetadata(parser, quality=QUALITY_NORMAL): """ Create a Metadata class from a parser. Returns None if no metadata extractor does exist for the parser class. """ try: extractor = extractors[parser.__class__] except KeyError: return None metadata = extractor(quality) try: metadata.extract(parser) except HACHOIR_ERRORS, err: error("Error during metadata extraction: %s" % unicode(err)) return None except Exception, err: error("Error during metadata extraction: %s" % unicode(err)) return None if metadata: metadata.mime_type = parser.mime_type metadata.endian = endian_name[parser.endian] return metadata
for field in object[struct.name]: print "%08X: %s = %s" % ((object.address + struct.address + field.address)/8, field.path, field.display) else: print "%08X: %s = %s" % ((struct.address)/8, struct.path, struct.display) object = data["raw_file_info"] for struct in object: print "%08X: %s = %s" % ((object.address + struct.address)/8, struct.path, struct.display) try: iter_exists = getattr(struct, "__iter__", None) except AttributeError: pass if iter_exists: for field in object[struct.name]: print "%08X: %s = %s" % ((object.address + struct.address + field.address)/8, field.path, field.display) else: print "%08X: %s = %s" % ((struct.address)/8, struct.path, struct.display) object = data["trailer"] print "%08X: %s = %s" % ((object.address)/8, object.path, object.display) except ValidateError, err: error(u"%s" % (err)) # except HACHOIR_ERRORS, err: # error(u"%s" % (err)) if __name__ == "__main__": main()
object = data["raw_file_info"] for struct in object: print "%08X: %s = %s" % ((object.address + struct.address) / 8, struct.path, struct.display) try: iter_exists = getattr(struct, "__iter__", None) except AttributeError: pass if iter_exists: for field in object[struct.name]: print "%08X: %s = %s" % ( (object.address + struct.address + field.address) / 8, field.path, field.display) else: print "%08X: %s = %s" % ( (struct.address) / 8, struct.path, struct.display) object = data["trailer"] print "%08X: %s = %s" % ( (object.address) / 8, object.path, object.display) except ValidateError, err: error(u"%s" % (err)) # except HACHOIR_ERRORS, err: # error(u"%s" % (err)) if __name__ == "__main__": main()
extractor does exist for the parser class. """ try: extractor = extractors[parser.__class__] except KeyError: return None metadata = extractor(quality) meta_extract_error = True try: if 'scan_index' in kwargs: metadata.extract(parser, scan_index=kwargs['scan_index']) else: metadata.extract(parser) meta_extract_error = False except HACHOIR_ERRORS, err: error("Error during metadata extraction: %s" % unicode(err)) except Exception, err: error("Error during metadata extraction: %s" % unicode(err)) if meta_extract_error: try: parser.stream._input.close() except: pass return None if metadata: metadata.mime_type = parser.mime_type metadata.endian = endian_name[parser.endian] return metadata
sys.exit(1) from hachoir_metadata import extractMetadata from hachoir_metadata.metadata import extractors as metadata_extractors def processFile(filename, quality=0.5): charset = getTerminalCharset() filename, real_filename = unicodeFilename(filename, charset), filename # Create parser try: tags = None parser = createParser(filename, real_filename=real_filename, tags=tags) except InputStreamError, err: error(unicode(err)) return False if not parser: error(_("Unable to parse file: %s") % filename) return False # Extract metadata try: return extractMetadata(parser, quality) except HachoirError, err: error(unicode(err)) parser.error(_("Hachoir can't extract metadata, but is able to parse: %s") % filename) return None def print_exif(path): img = PIL.Image.open(path)