print list of header candidates
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 22 Aug 2017 08:25:21 +0000 (10:25 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
deltatar/crypto.py

index bef3ced..c0896bb 100755 (executable)
@@ -1307,6 +1307,7 @@ PDTCRYPT_SCRYPT_FORMAT = \
     { "i2n"    : PDTCRYPT_SCRYPT_INTRANATOR
     , "params" : PDTCRYPT_SCRYPT_PARAMETERS }
 
+PDTCRYPT_TT_COLUMNS = 80 # assume standard terminal
 
 class PDTDecryptionError (Exception):
     """Decryption failed."""
@@ -1602,6 +1603,50 @@ def mode_scrypt (pw, ins=None, nacl=None, fmt=PDTCRYPT_SCRYPT_INTRANATOR):
     print (out)
 
 
+def noise_output_candidates (cands, indent=8, cols=PDTCRYPT_TT_COLUMNS):
+    """
+    Print a list of offsets without garbling the terminal too much.
+
+    The indent is counted from column zero; if it is wide enough, the “PDT: ”
+    marker will be prepended, considered part of the indentation.
+    """
+    wd   = cols - 1
+    nc   = len (cands)
+    idt  = " " * indent if indent < 5 else "PDT: " + " " * (indent - 5)
+    line = idt
+    lpos = indent
+    sep  = ","
+    lsep = len (sep)
+    init = True # prevent leading separator
+
+    if indent >= wd:
+        raise ValueError ("the requested indentation exceeds the line "
+                          "width by %d" % (indent - wd))
+
+    for n in cands:
+        ns  = "%d" % n
+        lns = len (ns)
+        if init is False:
+            line += sep
+            lpos += lsep
+
+        lpos += lns
+        if lpos > wd: # line break
+            noise (line)
+            line = idt
+            lpos = indent + lns
+        elif init is True:
+            init = False
+        else: # space
+            line += ' '
+            lpos += 1
+
+        line += ns
+
+    if lpos != indent:
+        noise (line)
+
+
 def mode_scan (pw, fname, nacl=None):
     """
     Dissect a binary file, looking for PDTCRYPT headers and objects.
@@ -1622,7 +1667,8 @@ def mode_scan (pw, fname, nacl=None):
                    "headers; giving up.")
             return -1
         if PDTCRYPT_VERBOSE is True:
-            noise ("PDT: scan complete: found %d candidates" % len (cands))
+            noise ("PDT: scan complete: found %d candidates:" % len (cands))
+            noise_output_candidates (cands)
     finally:
         os.close (fd)