OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 | |
17 #include "omaha/tools/omahacompatibility/common/config.h" | |
18 #include <windows.h> | |
19 #include <wincrypt.h> | |
20 #include "base/scoped_ptr.h" | |
21 #include "omaha/common/constants.h" | |
22 #include "omaha/common/debug.h" | |
23 #include "omaha/common/file.h" | |
24 #include "omaha/common/logging.h" | |
25 #include "omaha/common/scope_guard.h" | |
26 #include "omaha/common/signatures.h" | |
27 #include "omaha/common/path.h" | |
28 #include "omaha/common/utils.h" | |
29 | |
30 namespace omaha { | |
31 | |
32 const TCHAR* const kConfigApplicationProfile = _T("Application"); | |
33 const TCHAR* const kConfigAppName = _T("AppName"); | |
34 const TCHAR* const kConfigAppGuid = _T("AppGuid"); | |
35 const TCHAR* const kConfigAppNeedsAdmin = _T("NeedsAdmin"); | |
36 const TCHAR* const kConfigAppLanguage = _T("Language"); | |
37 const TCHAR* const kConfigAppVersion1 = _T("Version1"); | |
38 const TCHAR* const kConfigAppInstaller1 = _T("Installer1"); | |
39 const TCHAR* const kConfigAppVersion2 = _T("Version2"); | |
40 const TCHAR* const kConfigAppInstaller2 = _T("Installer2"); | |
41 | |
42 bool ComputeSHA(const CString& file_name, ConfigResponse* response) { | |
43 ASSERT1(response); | |
44 | |
45 std::vector<CString> files; | |
46 std::vector<byte> hash_vector; | |
47 files.push_back(file_name); | |
48 | |
49 // Check if the file exists | |
50 WIN32_FILE_ATTRIBUTE_DATA attrs; | |
51 BOOL success = GetFileAttributesEx(file_name, GetFileExInfoStandard, &attrs); | |
52 if (!success) { | |
53 return false; | |
54 } | |
55 | |
56 // Calculate the hash | |
57 CryptoHash crypto; | |
58 crypto.Compute(files, 512000000L, &hash_vector); | |
59 CString encoded; | |
60 Base64::Encode(hash_vector, &encoded); | |
61 response->hash = encoded; | |
62 response->size = attrs.nFileSizeLow; | |
63 | |
64 return true; | |
65 } | |
66 | |
67 HRESULT ReadProfileString(const CString& file_name, | |
68 const CString& key_name, | |
69 CString* value) { | |
70 CString val; | |
71 DWORD ret = ::GetPrivateProfileString(kConfigApplicationProfile, | |
72 key_name, | |
73 _T(""), | |
74 CStrBuf(val, MAX_PATH), | |
75 MAX_PATH, | |
76 file_name); | |
77 if (ret == MAX_PATH - 1) { | |
78 return E_FAIL; | |
79 } | |
80 *value = val; | |
81 | |
82 return S_OK; | |
83 } | |
84 | |
85 HRESULT ReadConfigFile(const CString& file_name, | |
86 const CString& download_url_prefix, | |
87 ConfigResponses* config_responses) { | |
88 ASSERT1(config_responses); | |
89 | |
90 ConfigResponse config_response; | |
91 | |
92 CString app_name; | |
93 HRESULT hr = ReadProfileString(file_name, kConfigAppName, &app_name); | |
94 if (FAILED(hr)) { | |
95 return hr; | |
96 } | |
97 config_response.app_name = app_name; | |
98 | |
99 CString app_guid; | |
100 hr = ReadProfileString(file_name, kConfigAppGuid, &app_guid); | |
101 if (FAILED(hr)) { | |
102 return hr; | |
103 } | |
104 config_response.guid = StringToGuid(app_guid); | |
105 | |
106 CString needs_admin; | |
107 hr = ReadProfileString(file_name, kConfigAppNeedsAdmin, &needs_admin); | |
108 if (FAILED(hr)) { | |
109 return hr; | |
110 } | |
111 const TCHAR* const kFalse = _T("false"); | |
112 if (_wcsnicmp(kFalse, needs_admin, wcslen(kFalse)) == 0) { | |
113 config_response.needs_admin = false; | |
114 } else { | |
115 config_response.needs_admin = true; | |
116 } | |
117 | |
118 CString language; | |
119 hr = ReadProfileString(file_name, kConfigAppLanguage, &language); | |
120 if (FAILED(hr)) { | |
121 return hr; | |
122 } | |
123 config_response.language = language; | |
124 | |
125 | |
126 // Read the first config. | |
127 ConfigResponse config_response1 = config_response; | |
128 CString installer1; | |
129 hr = ReadProfileString(file_name, kConfigAppInstaller1, &installer1); | |
130 if (FAILED(hr)) { | |
131 return hr; | |
132 } | |
133 | |
134 if (!File::Exists(installer1)) { | |
135 printf("Error: Could not open file %s\n", installer1); | |
136 printf("Make sure you specify an absolute path to the file\n"); | |
137 return E_FAIL; | |
138 } | |
139 | |
140 if (!ComputeSHA(installer1, &config_response1)) { | |
141 return E_FAIL; | |
142 } | |
143 | |
144 CString version1; | |
145 hr = ReadProfileString(file_name, kConfigAppVersion1, &version1); | |
146 if (FAILED(hr)) { | |
147 return hr; | |
148 } | |
149 config_response1.version = version1; | |
150 config_response1.local_file_name = installer1; | |
151 config_response1.url = download_url_prefix + _T("/") + | |
152 GetFileFromPath(installer1); | |
153 | |
154 // Read the second config. | |
155 ConfigResponse config_response2 = config_response; | |
156 CString installer2; | |
157 hr = ReadProfileString(file_name, kConfigAppInstaller2, &installer2); | |
158 if (FAILED(hr)) { | |
159 return hr; | |
160 } | |
161 if (!File::Exists(installer2)) { | |
162 printf("Error: Could not open file %s\n", installer1); | |
163 printf("Make sure you specify an absolute path to the file\n"); | |
164 return E_FAIL; | |
165 } | |
166 if (!ComputeSHA(installer2, &config_response2)) { | |
167 return E_FAIL; | |
168 } | |
169 | |
170 CString version2; | |
171 hr = ReadProfileString(file_name, kConfigAppVersion2, &version2); | |
172 if (FAILED(hr)) { | |
173 return hr; | |
174 } | |
175 config_response2.version = version2; | |
176 config_response2.local_file_name = installer2; | |
177 config_response2.url = download_url_prefix + _T("/") + | |
178 GetFileFromPath(installer2); | |
179 | |
180 // Return the results. | |
181 config_responses->push_back(config_response1); | |
182 config_responses->push_back(config_response2); | |
183 | |
184 return S_OK; | |
185 } | |
186 | |
187 } // namespace omaha | |
188 | |
OLD | NEW |