@@ -46,7 +46,7 @@ def hash_file(filename):
4646 with open (filename , "rb" ) as file :
4747 while chunk := file .read (4096 ):
4848 sha256_hash .update (chunk )
49- return sha256_hash .hexdigest ()
49+ return sha256_hash .digest ()
5050
5151def generate_function_pointers (dll_file_path , header_file_path , output_h_path , output_c_path ):
5252 """
@@ -62,7 +62,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
6262 """
6363 print (f"Reading DLL file: { dll_file_path } " )
6464 dll_hash = hash_file (dll_file_path )
65-
65+
6666 print (f"Reading header file: { header_file_path } " )
6767 try :
6868 with open (header_file_path , 'r' , encoding = 'utf-8' ) as f :
@@ -96,7 +96,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
9696 return_type = match .group (1 ).strip ()
9797 function_name = match .group (2 ).strip ()
9898 params_str = match .group (3 ).strip ()
99-
99+
100100 cleaned_params_for_decl = re .sub (r'\s+' , ' ' , params_str ) if params_str else ""
101101 stub_name = f"Stub_{ function_name } "
102102
@@ -107,7 +107,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
107107 return_statement = f' return ({ return_type } )(&g_stub_memory);'
108108 else : # bool, int64_t, etc.
109109 return_statement = " return 1;"
110-
110+
111111 stub_function = (
112112 f"// Stub for { function_name } \n "
113113 f"static { return_type } { stub_name } ({ params_str } ) {{\n "
@@ -124,7 +124,7 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
124124
125125 pointer_init = f"{ return_type } (*ptr_{ function_name } )({ cleaned_params_for_decl } ) = &{ stub_name } ;"
126126 pointer_initializations .append (pointer_init )
127-
127+
128128 function_details_for_loader .append ((function_name , return_type , cleaned_params_for_decl ))
129129
130130 print (f"Found { len (pointer_initializations )} functions. Generating output files..." )
@@ -137,12 +137,12 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
137137 f .write (f"#ifndef { header_guard } \n " )
138138 f .write (f"#define { header_guard } \n \n " )
139139 f .write ("#include <stdbool.h> // needed for bool type in pure C\n \n " )
140-
140+
141141 f .write ("// --- Copied from original header ---\n " )
142142 f .write ("\n " .join (includes ) + "\n \n " )
143143 f .write ("" .join (typedefs ))
144144 f .write ("// --- End of copied section ---\n \n " )
145-
145+
146146 f .write ("#ifdef __cplusplus\n " )
147147 f .write ('extern "C" {\n ' )
148148 f .write ("#endif\n \n " )
@@ -156,7 +156,8 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
156156 f .write ("#if defined(_WIN32)\n " )
157157 f .write ('#include <windows.h> // For HMODULE\n ' )
158158 f .write (f'\n // Google Analytics Windows DLL SHA256 hash, to be verified on load.' )
159- f .write (f'\n #define FIREBASE_ANALYTICS_DYNAMIC_LIBRARY_HASH "{ dll_hash } "\n ' )
159+ f .write (f'\n extern const unsigned char FirebaseAnalytics_WindowsDllHash[{ len (dll_hash )} ];\n ' );
160+
160161 f .write (f'\n // Number of Google Analytics functions expected to be loaded from the DLL.' )
161162 f .write (f'\n #define FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT { len (function_details_for_loader )} \n \n ' )
162163 f .write ('// Load Google Analytics functions from the given DLL handle into function pointers.\n ' )
@@ -178,6 +179,10 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
178179 f .write (f"// Generated from { os .path .basename (header_file_path )} by { os .path .basename (sys .argv [0 ])} \n \n " )
179180 f .write (f'#include "{ INCLUDE_PREFIX } { os .path .basename (output_h_path )} "\n ' )
180181 f .write ('#include <stddef.h>\n \n ' )
182+ f .write ('// Google Analytics Windows DLL SHA256 hash, to be verified on load.\n ' )
183+ f .write ('const unsigned char FirebaseAnalytics_WindowsDllHash[] = {' )
184+ f .write (',' .join (["0x%02x" % s for s in dll_hash ]))
185+ f .write ('};\n \n ' )
181186 f .write ("// clang-format off\n \n " )
182187 f .write ("static void* g_stub_memory = NULL;\n \n " )
183188 f .write ("// --- Stub Function Definitions ---\n " )
@@ -245,10 +250,10 @@ def generate_function_pointers(dll_file_path, header_file_path, output_h_path, o
245250 help = "Path for the generated output source file."
246251 )
247252 args = parser .parse_args ()
248-
253+
249254 generate_function_pointers (
250255 args .windows_dll ,
251- args .windows_header ,
252- args .output_header ,
256+ args .windows_header ,
257+ args .output_header ,
253258 args .output_source
254259 )
0 commit comments