From 982e3f3fc950daf3b14a99fdea9c0eb18ae96fc4 Mon Sep 17 00:00:00 2001
From: platomav <platomav@users.noreply.github.com>
Date: Thu, 21 Apr 2022 13:59:40 +0300
Subject: [PATCH] Format detectors now accept input bytes or path

---
 AMI_PFAT_Extract.py |  7 +++++--
 AMI_UCP_Extract.py  | 10 ++++++----
 Dell_PFS_Extract.py |  9 ++++++---
 common/text_ops.py  | 10 ++++++++++
 4 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/AMI_PFAT_Extract.py b/AMI_PFAT_Extract.py
index 1ce55f9..dd22a66 100644
--- a/AMI_PFAT_Extract.py
+++ b/AMI_PFAT_Extract.py
@@ -7,7 +7,7 @@ AMI BIOS Guard Extractor
 Copyright (C) 2018-2022 Plato Mavropoulos
 """
 
-title = 'AMI BIOS Guard Extractor v4.0_a7'
+title = 'AMI BIOS Guard Extractor v4.0_a8'
 
 import os
 import re
@@ -23,6 +23,7 @@ from common.path_ops import safe_name, make_dirs
 from common.patterns import PAT_AMI_PFAT
 from common.struct_ops import get_struct, char, uint8_t, uint16_t, uint32_t
 from common.system import script_init, argparse_init, printer
+from common.text_ops import file_to_bytes
 
 class AmiBiosGuardHeader(ctypes.LittleEndianStructure):
     _pack_ = 1
@@ -127,7 +128,9 @@ class IntelBiosGuardSignature2k(ctypes.LittleEndianStructure):
         printer(['Exponent :', '0x%X' % self.Exponent], p, False)
         printer(['Signature:', '%s [...]' % Signature[:32]], p, False)
 
-def is_ami_pfat(input_buffer):
+def is_ami_pfat(in_file):
+    input_buffer = file_to_bytes(in_file)
+    
     return bool(get_ami_pfat(input_buffer)[0])
 
 def get_ami_pfat(input_buffer):
diff --git a/AMI_UCP_Extract.py b/AMI_UCP_Extract.py
index ae2587e..76386ba 100644
--- a/AMI_UCP_Extract.py
+++ b/AMI_UCP_Extract.py
@@ -7,7 +7,7 @@ AMI UCP BIOS Extractor
 Copyright (C) 2021-2022 Plato Mavropoulos
 """
 
-title = 'AMI UCP BIOS Extractor v2.0_a9'
+title = 'AMI UCP BIOS Extractor v2.0_a10'
 
 import os
 import re
@@ -26,7 +26,7 @@ from common.path_ops import agnostic_path, safe_name, safe_path, make_dirs
 from common.patterns import PAT_AMI_UCP, PAT_INTEL_ENG
 from common.struct_ops import get_struct, char, uint8_t, uint16_t, uint32_t
 from common.system import script_init, argparse_init, printer
-from common.text_ops import to_string
+from common.text_ops import file_to_bytes, to_string
 
 from AMI_PFAT_Extract import get_ami_pfat, parse_pfat_file
 
@@ -159,8 +159,10 @@ def chk16_validate(data, tag, padd=0):
     else:
         printer('Checksum of UCP Module %s is valid!' % tag, padd)
 
-# Check if input file is AMI UCP image
-def is_ami_ucp(buffer):
+# Check if input path or buffer is AMI UCP image
+def is_ami_ucp(in_file):
+    buffer = file_to_bytes(in_file)
+    
     return bool(get_ami_ucp(buffer)[0])
 
 # Get all input file AMI UCP patterns
diff --git a/Dell_PFS_Extract.py b/Dell_PFS_Extract.py
index 2e0610f..5e27f6a 100644
--- a/Dell_PFS_Extract.py
+++ b/Dell_PFS_Extract.py
@@ -7,7 +7,7 @@ Dell PFS Update Extractor
 Copyright (C) 2018-2022 Plato Mavropoulos
 """
 
-title = 'Dell PFS Update Extractor v6.0_a6'
+title = 'Dell PFS Update Extractor v6.0_a7'
 
 import os
 import io
@@ -25,6 +25,7 @@ from common.path_ops import safe_name, make_dirs
 from common.patterns import PAT_DELL_HDR, PAT_DELL_FTR, PAT_DELL_PKG
 from common.struct_ops import get_struct, char, uint8_t, uint16_t, uint32_t, uint64_t
 from common.system import script_init, argparse_init, printer
+from common.text_ops import file_to_bytes
 
 from AMI_PFAT_Extract import IntelBiosGuardHeader, IntelBiosGuardSignature2k, parse_bg_script
 
@@ -205,8 +206,10 @@ def is_pfs_hdr(in_buffer):
 def is_pfs_ftr(in_buffer):
     return PAT_DELL_FTR.search(in_buffer)
 
-# Check if input buffer is Dell PFS/PKG image
-def is_dell_pfs(in_buffer):
+# Check if input path or buffer is Dell PFS/PKG image
+def is_dell_pfs(in_file):
+    in_buffer = file_to_bytes(in_file)
+    
     return bool(is_pfs_hdr(in_buffer) or is_pfs_pkg(in_buffer))
 
 # Get PFS ZLIB Section Offsets
diff --git a/common/text_ops.py b/common/text_ops.py
index 4a8ea41..8384ee8 100644
--- a/common/text_ops.py
+++ b/common/text_ops.py
@@ -17,3 +17,13 @@ def to_string(input_object, sep_char=''):
         output_string = str(input_object)
     
     return output_string
+
+# Get Bytes from given buffer or file path
+def file_to_bytes(in_object):
+    object_bytes = in_object
+    
+    if type(in_object).__name__ not in ('bytes','bytearray'):
+        with open(to_string(in_object), 'rb') as object_data:
+            object_bytes = object_data.read()
+    
+    return object_bytes