Counting 0x00 and 0xFF filled sectors in an image file

12.10.2020, 17:55 - Autor: Mark B.
The TRIM command makes data recovery often impossible and I wanted to be able to check a drive fast. Scrolling by in an hexeditor gives you a indication but the guesswork is not so easy with partially filled drives. Thats where the following script come into play:

C:\Users\DISK DOCTOR\Desktop>py.exe -3 count_0x00_and_0xFF_sectors.py test.dd
1.0 GB scanned
SECTORS TOTAL:         1953125
SECTORS with 0x00:       36379 (1.86%)
SECTORS with 0xFF:       33198 (1.70%)
---------------------------------------
2.0 GB scanned
SECTORS TOTAL:         3906250
SECTORS with 0x00:       42990 (1.10%)
SECTORS with 0xFF:       57014 (1.46%)
---------------------------------------
3.0 GB scanned
SECTORS TOTAL:         5859375
SECTORS with 0x00:       45051 (0.77%)
SECTORS with 0xFF:     1686779 (28.79%)
---------------------------------------
4.0 GB scanned
SECTORS TOTAL:         7812500
SECTORS with 0x00:       45051 (0.58%)
SECTORS with 0xFF:     3578869 (45.81%)
---------------------------------------
5.0 GB scanned
SECTORS TOTAL:         9765625
SECTORS with 0x00:       45051 (0.46%)
SECTORS with 0xFF:     5470959 (56.02%)
---------------------------------------
6.0 GB scanned
SECTORS TOTAL:        11718750
SECTORS with 0x00:       45051 (0.38%)
SECTORS with 0xFF:     7363049 (62.83%)
---------------------------------------
7.0 GB scanned
SECTORS TOTAL:        13671875
SECTORS with 0x00:       45051 (0.33%)
SECTORS with 0xFF:     9255139 (67.69%)
---------------------------------------
[CTRL] + [C]
SECTORS TOTAL:        15213276
SECTORS with 0x00:       45051 (0.30%)
SECTORS with 0xFF:    10748371 (70.65%)
---------------------------------------
Done in 54.499178647994995 Sec.

Source:

import os, sys, time

def out(lba, ctr00, ctrFF):
    print("SECTORS TOTAL:     {:>11}".format(lba))
    print("SECTORS with 0x00: {:>11} ({:2.2f}%)".format(ctr00, ctr00/lba*100))
    print("SECTORS with 0xFF: {:>11} ({:2.2f}%)".format(ctrFF, ctrFF/lba*100))
    print("---------------------------------------")

if len(sys.argv) != 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
    print("\nUSAGE:")
    print("count_0x00_and_0xFF_sectors.py filename.img\n")
    quit()

filename = sys.argv[1]
ctr00 = 0
ctrFF = 0
lba = 0

GB = 1000*1000*1000/512
ts = time.time()

block_0x00 = b'\x00' * 512
block_0xFF = b'\xFF' * 512
        
with open(filename, "rb") as f:
    while True:
        try:
            block = f.read(512)
            if not block:
                break

            lba += 1
            if lba % GB == 0:
                print(str(lba/GB) + " GB scanned")
                out(lba, ctr00, ctrFF)
            if block == block_0x00:
                ctr00 += 1 
            elif block == block_0xFF:
                ctrFF += 1
        except KeyboardInterrupt:
            break

out(lba, ctr00, ctrFF)
print("Done in " + str(time.time() - ts) + " Sec.")

Download

System requrements:

Windows, OSX or Linux with