Skip to content

Commit c274e7c

Browse files
committed
Add hash constant.
1 parent d3271d4 commit c274e7c

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

analytics/generate_windows_stubs.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5151
def 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'\nextern 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
)

analytics/src/analytics_desktop_dynamic.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
// Generated from analytics.h by generate_windows_stubs.py
1616

1717
#include "analytics/src/analytics_desktop_dynamic.h"
18-
1918
#include <stddef.h>
2019

20+
// Google Analytics Windows DLL SHA256 hash, to be verified on load.
21+
const unsigned char FirebaseAnalytics_WindowsDllHash[] = {0xc1,0xb9,0xff,0x6e,0x91,0x19,0xc3,0x0b,0xbe,0xb7,0x47,0x23,0x26,0xdc,0xde,0x41,0x8f,0x45,0x68,0x2e,0x6b,0x82,0x2e,0x25,0xee,0xd9,0x22,0xfe,0x6e,0x3c,0xc6,0x98};
22+
2123
// clang-format off
2224

2325
static void* g_stub_memory = NULL;

analytics/src/analytics_desktop_dynamic.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,25 @@ extern void (*ptr_GoogleAnalytics_SetAnalyticsCollectionEnabled)(bool enabled);
112112
#define GoogleAnalytics_SetAnalyticsCollectionEnabled ptr_GoogleAnalytics_SetAnalyticsCollectionEnabled
113113
// clang-format on
114114

115+
115116
// --- Dynamic Loader Declaration for Windows ---
116117
#if defined(_WIN32)
117-
#include <windows.h> // For HMODULE
118+
#include <windows.h> // For HMODULE
118119

119120
// Google Analytics Windows DLL SHA256 hash, to be verified on load.
120-
#define FIREBASE_ANALYTICS_DYNAMIC_LIBRARY_HASH \
121-
"c1b9ff6e9119c30bbeb7472326dcde418f45682e6b822e25eed922fe6e3cc698"
121+
extern const unsigned char FirebaseAnalytics_WindowsDllHash[32];
122122

123123
// Number of Google Analytics functions expected to be loaded from the DLL.
124124
#define FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT 19
125125

126-
// Load Google Analytics functions from the given DLL handle into function
127-
// pointers. Returns the number of functions successfully loaded (out of
128-
// FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT).
126+
// Load Google Analytics functions from the given DLL handle into function pointers.
127+
// Returns the number of functions successfully loaded (out of FIREBASE_ANALYTICS_DYNAMIC_FUNCTION_COUNT).
129128
int FirebaseAnalytics_LoadDynamicFunctions(HMODULE dll_handle);
130129

131130
// Reset all function pointers back to stubs.
132131
void FirebaseAnalytics_UnloadDynamicFunctions(void);
133132

134-
#endif // defined(_WIN32)
133+
#endif // defined(_WIN32)
135134

136135
#ifdef __cplusplus
137136
}

0 commit comments

Comments
 (0)