17
17
#include < wincrypt.h>
18
18
#include < windows.h>
19
19
20
- #include < cstdlib> // Required for _wpgmptr
21
- #include < cstring> // For memcmp
22
- #include < string> // Required for std::wstring
20
+ #include < cstdlib>
21
+ #include < cstring>
22
+ #include < string>
23
23
#include < vector>
24
- #include < string> // Required for std::wstring
25
- #include < cstdlib> // Required for _wpgmptr
26
- #include < cstring> // For memcmp
27
- #include " app/src/log.h" // NOLINT
24
+
25
+ #include " app/src/log.h"
28
26
29
27
namespace firebase {
30
28
namespace analytics {
@@ -33,46 +31,50 @@ namespace internal {
33
31
// Helper function to retrieve the full path of the current executable.
34
32
// Returns an empty string on failure.
35
33
static std::wstring GetExecutablePath () {
36
- std::wstring executable_path_str;
37
- wchar_t * wpgmptr_val = nullptr ;
34
+ std::wstring executable_path_str;
35
+ wchar_t * wpgmptr_val = nullptr ;
38
36
39
- // Prefer _get_wpgmptr()
40
- errno_t err_w = _get_wpgmptr (&wpgmptr_val);
41
- if (err_w == 0 && wpgmptr_val != nullptr && wpgmptr_val[0 ] != L' \0 ' ) {
42
- executable_path_str = wpgmptr_val;
43
- } else {
44
- // Fallback to _get_pgmptr() and convert to wide string
45
- char * pgmptr_val = nullptr ;
46
- errno_t err_c = _get_pgmptr (&pgmptr_val);
47
- if (err_c == 0 && pgmptr_val != nullptr && pgmptr_val[0 ] != ' \0 ' ) {
48
- // Convert narrow string to wide string using CP_ACP (system default ANSI code page)
49
- int wide_char_count = MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1 , NULL , 0 );
50
- if (wide_char_count == 0 ) { // Failure if count is 0
51
- DWORD conversion_error = GetLastError ();
52
- LogError (" GetExecutablePath: MultiByteToWideChar failed to calculate size for _get_pgmptr path. Error: %u" , conversion_error);
53
- return L" " ;
54
- }
55
-
56
- std::vector<wchar_t > wide_path_buffer (wide_char_count);
57
- if (MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1 , wide_path_buffer.data (), wide_char_count) == 0 ) {
58
- DWORD conversion_error = GetLastError ();
59
- LogError (" GetExecutablePath: MultiByteToWideChar failed to convert _get_pgmptr path. Error: %u" , conversion_error);
60
- return L" " ;
61
- }
62
- executable_path_str = wide_path_buffer.data ();
63
- } else {
64
- // Both _get_wpgmptr and _get_pgmptr failed or returned empty/null
65
- LogError (" GetExecutablePath: Failed to retrieve executable path using both _get_wpgmptr (err: %d) and _get_pgmptr (err: %d)." , err_w, err_c);
66
- return L" " ;
67
- }
68
- }
37
+ // Prefer _get_wpgmptr()
38
+ errno_t err_w = _get_wpgmptr (&wpgmptr_val);
39
+ if (err_w == 0 && wpgmptr_val != nullptr && wpgmptr_val[0 ] != L' \0 ' ) {
40
+ executable_path_str = wpgmptr_val;
41
+ } else {
42
+ // Fallback to _get_pgmptr() and convert to wide string
43
+ char * pgmptr_val = nullptr ;
44
+ errno_t err_c = _get_pgmptr (&pgmptr_val);
45
+ if (err_c == 0 && pgmptr_val != nullptr && pgmptr_val[0 ] != ' \0 ' ) {
46
+ // Convert narrow string to wide string using CP_ACP (system default ANSI
47
+ // code page)
48
+ int wide_char_count = MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS,
49
+ pgmptr_val, -1 , NULL , 0 );
50
+ if (wide_char_count == 0 ) { // Failure if count is 0
51
+ DWORD conversion_error = GetLastError ();
52
+ LogError (
53
+ " VerifyAndLoadAnalyticsLibrary: Invalid executable path. Error: %u" ,
54
+ conversion_error);
55
+ return L" " ;
56
+ }
69
57
70
- // Safeguard if path somehow resolved to empty despite initial checks.
71
- if (executable_path_str.empty ()) {
72
- LogError (" GetExecutablePath: Executable path resolved to an empty string." );
58
+ std::vector<wchar_t > wide_path_buffer (wide_char_count);
59
+ if (MultiByteToWideChar (CP_ACP, MB_ERR_INVALID_CHARS, pgmptr_val, -1 ,
60
+ wide_path_buffer.data (), wide_char_count) == 0 ) {
61
+ DWORD conversion_error = GetLastError ();
62
+ LogError (
63
+ " VerifyAndLoadAnalyticsLibrary: Invalid executable path. Error: %u" ,
64
+ conversion_error);
73
65
return L" " ;
66
+ }
67
+ executable_path_str = wide_path_buffer.data ();
68
+ } else {
69
+ // Both _get_wpgmptr and _get_pgmptr failed or returned empty/null
70
+ LogError (
71
+ " VerifyAndLoadAnalyticsLibrary: Can't determine executable "
72
+ " directory. Errors: %d, %d" ,
73
+ err_w, err_c);
74
+ return L" " ;
74
75
}
75
- return executable_path_str;
76
+ }
77
+ return executable_path_str;
76
78
}
77
79
78
80
// Helper function to calculate SHA256 hash of a file.
@@ -83,7 +85,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
83
85
84
86
if (SetFilePointer (hFile, 0 , NULL , FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
85
87
DWORD dwError = GetLastError ();
86
- LogError (" CalculateFileSha256: SetFilePointer failed. Error: %u" , dwError);
88
+ LogError (
89
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.SetFilePointer "
90
+ " failed. Error: %u" ,
91
+ dwError);
87
92
return result_hash_value;
88
93
}
89
94
@@ -93,14 +98,19 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
93
98
if (!CryptAcquireContextW (&hProv, NULL , NULL , PROV_RSA_AES,
94
99
CRYPT_VERIFYCONTEXT)) {
95
100
DWORD dwError = GetLastError ();
96
- LogError (" CalculateFileSha256: CryptAcquireContextW failed. Error: %u" ,
97
- dwError);
101
+ LogError (
102
+ " VerifyAndLoadAnalyticsLibrary: "
103
+ " CalculateFileSha256.CryptAcquireContextW failed. Error: %u" ,
104
+ dwError);
98
105
return result_hash_value;
99
106
}
100
107
101
108
if (!CryptCreateHash (hProv, CALG_SHA_256, 0 , 0 , &hHash)) {
102
109
DWORD dwError = GetLastError ();
103
- LogError (" CalculateFileSha256: CryptCreateHash failed. Error: %u" , dwError);
110
+ LogError (
111
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptCreateHash "
112
+ " failed. Error: %u" ,
113
+ dwError);
104
114
CryptReleaseContext (hProv, 0 );
105
115
return result_hash_value;
106
116
}
@@ -113,7 +123,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
113
123
bReadSuccessLoop = ReadFile (hFile, rgbFile, sizeof (rgbFile), &cbRead, NULL );
114
124
if (!bReadSuccessLoop) {
115
125
DWORD dwError = GetLastError ();
116
- LogError (" CalculateFileSha256: ReadFile failed. Error: %u" , dwError);
126
+ LogError (
127
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.ReadFile failed. "
128
+ " Error: %u" ,
129
+ dwError);
117
130
CryptDestroyHash (hHash);
118
131
CryptReleaseContext (hProv, 0 );
119
132
return result_hash_value;
@@ -123,7 +136,10 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
123
136
}
124
137
if (!CryptHashData (hHash, rgbFile, cbRead, 0 )) {
125
138
DWORD dwError = GetLastError ();
126
- LogError (" CalculateFileSha256: CryptHashData failed. Error: %u" , dwError);
139
+ LogError (
140
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptHashData "
141
+ " failed. Error: %u" ,
142
+ dwError);
127
143
CryptDestroyHash (hHash);
128
144
CryptReleaseContext (hProv, 0 );
129
145
return result_hash_value;
@@ -136,7 +152,8 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
136
152
0 )) {
137
153
DWORD dwError = GetLastError ();
138
154
LogError (
139
- " CalculateFileSha256: CryptGetHashParam (HP_HASHSIZE) failed. Error: "
155
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptGetHashParam "
156
+ " (HP_HASHSIZE) failed. Error: "
140
157
" %u" ,
141
158
dwError);
142
159
CryptDestroyHash (hHash);
@@ -149,7 +166,8 @@ static std::vector<BYTE> CalculateFileSha256(HANDLE hFile) {
149
166
&cbHashValue, 0 )) {
150
167
DWORD dwError = GetLastError ();
151
168
LogError (
152
- " CalculateFileSha256: CryptGetHashParam (HP_HASHVAL) failed. Error: %u" ,
169
+ " VerifyAndLoadAnalyticsLibrary: CalculateFileSha256.CryptGetHashParam "
170
+ " (HP_HASHVAL) failed. Error: %u" ,
153
171
dwError);
154
172
result_hash_value.clear ();
155
173
CryptDestroyHash (hHash);
@@ -185,23 +203,27 @@ HMODULE VerifyAndLoadAnalyticsLibrary(
185
203
return nullptr ;
186
204
}
187
205
188
- std::wstring executable_path_str = GetExecutablePath ();
206
+ std::wstring executable_path_str = GetExecutablePath ();
189
207
190
- if (executable_path_str.empty ()) {
191
- // GetExecutablePath() is expected to log specific errors.
192
- // This log indicates the failure to proceed within this function.
193
- LogError (" VerifyAndLoadAnalyticsLibrary: Failed to determine executable path via GetExecutablePath(), cannot proceed." );
194
- return nullptr ;
195
- }
208
+ if (executable_path_str.empty ()) {
209
+ // GetExecutablePath() is expected to log specific errors.
210
+ // This log indicates the failure to proceed within this function.
211
+ LogError (
212
+ " VerifyAndLoadAnalyticsLibrary: Failed to determine executable path "
213
+ " via GetExecutablePath(), cannot proceed." );
214
+ return nullptr ;
215
+ }
196
216
197
- size_t last_slash_pos = executable_path_str.find_last_of (L" \\ " );
198
- if (last_slash_pos == std::wstring::npos) {
199
- // Log message updated to avoid using %ls for executable_path_str
200
- LogError (" VerifyAndLoadAnalyticsLibrary: Could not determine executable directory from retrieved path (no backslash found)." );
201
- return nullptr ;
202
- }
217
+ size_t last_slash_pos = executable_path_str.find_last_of (L" \\ " );
218
+ if (last_slash_pos == std::wstring::npos) {
219
+ // Log message updated to avoid using %ls for executable_path_str
220
+ LogError (
221
+ " VerifyAndLoadAnalyticsLibrary: Could not determine executable "
222
+ " directory from retrieved path (no backslash found)." );
223
+ return nullptr ;
224
+ }
203
225
204
- std::wstring full_dll_path_str =
226
+ std::wstring full_dll_path_str =
205
227
executable_path_str.substr (0 , last_slash_pos + 1 );
206
228
full_dll_path_str += library_filename; // library_filename is the filename
207
229
0 commit comments