Hexadecimal files are basically of two types. Given the implementation of hex_group_formatter, it should be math.ceil(chunk_size/4) * 14 - 3.Modified: 13-12-2021 ~ Digital Forensics ~ 4 Minutes Reading You may also want to replace the magic number 53 with something that depend of chunk_size. Parser.add_argument('file', nargs='?', default='demo.exe', help='the file to process') Parser = argparse.ArgumentParser(description='Hexadeciaml viewer.') And you can also mix them with 0>? where ? is an integer to perform the role of zfill. Third, instead of building the whole string at once and returning it, you can yield each block of processed 16 bytes and let the caller be responsible of iterating over them to perform the desired operation (or feed them to '\n'.join for what it's worth).įourth, you don't necessarily need to use hex or chr to convert integers to characters before formatting them: format specifiers x and c for integers can perform the same operations. It will save a lot of memory and allow you to view very large files. Second, instead of pre-processing the file at once and printing it later, you could read it by blocks of 16 bytes and process them before going to the next block. File processingįirst off, you open the file but never close it: time to get yourself familiar with the with statement. You should at least use a set instead, but I would favor using two constants BEGIN_PRINTABLE = 33 and END_PRINTABLE = 126 as all these characters are contiguous in the ASCII table. In this file, bina is never used, lmap should be replaced by list-comprehensions at the calling point, and chunks very look like the itertools recipe grouper.Īlso, printable_ascii being a list is a poor choice knowing that it will be used for existence checking. utils.py is more common in the Python's world. Gen.py is a terrible name for a file holding general utilities functions as gen mostly associates to generate/generation. Especially if you plan on doing nothing and exit anyway… And the bare except to just print the exception… is just useless as it is the default behaviour anyway. Nothing in your code explicitly generate a SystemExit so you can drop this except clause. Lastly, I have a hard time understanding the logic behing your exceptions handling. You also fall into the bad habit of using from import * to try and avoid this complexity. There is no need for a class here, a simple function with tiny helpers should suffice.Īlso, even though separating concerns between files is a good thing for complex projects, I find it adds complexity for such small task. It's core is located withing Hexviewer._str_ with a little bit of preprocessing around. This code is way too complicated for what it is achieving. Self.ascii_chunks = chunks(self.ascii_data, 16) Self.hex_chunks = chunks(chunks(self.hex_data, 4), 4) Printable_ascii = lmap(ord, string.digits string.ascii_letters string.punctuation) Hex_digits_chunks = chunks(lmap(hexa, range(16)), 4) Return for i in range(0, len(arr), size)] The code is divided into 3 files, first one is general utils for the task, second is the main class and the third is the runner: How can I improve the code or considerate styling standards? Any other advice regarding the code or the functionality? However, I have used many list comprehensions and mapping to cut up times. I was pretty satisfied with the final result. My implementation was in the form of a class, with a separate main file running with argparse to allow choosing the file (runs with a demo file by default). I have created an hex viewer in python, as a timed-challenge by friend.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |