98a77c802a5052494839a6295f8e501f43fbd121
[feisty_meow.git] / nucleus / library / versions / version_checker.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : check_version                                                     *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "version_checker.h"
16
17 #include <application/windoze_helper.h>
18 #include <basis/astring.h>
19 #include <basis/environment.h>
20 #include <basis/functions.h>
21 #include <basis/guards.h>
22 #include <basis/utf_conversion.h>
23 #include <configuration/application_configuration.h>
24 #include <filesystem/filename.h>
25 #include <loggers/critical_events.h>
26
27 #include <stdio.h>
28
29 using namespace basis;
30 using namespace configuration;
31 using namespace loggers;
32 using namespace structures;
33
34 #ifndef BOOT_STRAPPING
35   // pull in the version specified for this build.
36 ///hmmm: on hold!  #include <__build_version.h>
37 #else
38   // plug in a fake version for our bootstrapping process.
39   #define __build_FILE_VERSION "108.420.1024.10008"
40 #endif
41
42 #ifdef _MSC_VER
43   #include <direct.h>
44   #include <winver.h>
45 #endif
46
47 #ifdef __WIN32__
48   // ensures that we handle the data properly regardless of unicode settings.
49   #ifdef UNICODE
50     #define render_ptr(ptr) from_unicode_temp( (UTF16 *) ptr)
51   #else
52     #define render_ptr(ptr) ( (char *) ptr)
53   #endif
54 #endif
55
56 //////////////
57
58 namespace versions {
59
60 version_checker::version_checker(const astring &library_file_name,
61     const version &expected_version, const astring &version_complaint)
62 : _library_file_name(new astring(library_file_name)),
63   _expected_version(new version(expected_version)),
64   _version_complaint(new astring(version_complaint))
65 {}
66
67 version_checker::~version_checker()
68 {
69   WHACK(_library_file_name);
70   WHACK(_expected_version);
71   WHACK(_version_complaint);
72 }
73
74 astring version_checker::text_form() const
75 {
76   return astring(class_name()) + ": library_file_name=" + *_library_file_name
77       + ", expected_version=" + _expected_version->text_form()
78       + ", complaint_message=" + *_version_complaint;
79 }
80
81 bool version_checker::good_version() const
82 {
83   astring version_disabler = environment::TMP();
84   version_disabler += "/no_version_check.txt";
85   FILE *always_okay = fopen(version_disabler.s(), "r");
86   if (always_okay) {
87     fclose(always_okay);
88     return true;
89   }
90
91   version version_found = retrieve_version(*_library_file_name);
92   if (version_found.compatible(*_expected_version)) return true;  // success.
93   complain_wrong_version(*_library_file_name, *_expected_version,
94       version_found);
95   return false;
96 }
97
98 bool version_checker::loaded(const astring &library_file_name)
99 {
100 #ifdef __WIN32__
101   return bool(get_handle(library_file_name) != 0); 
102 #else
103 //temp code. 
104   return true || library_file_name.t();
105 #endif
106 }
107
108 void *version_checker::get_handle(const astring &library_file_name)
109 {
110 #ifdef __WIN32__
111   return GetModuleHandle(to_unicode_temp(library_file_name));
112 #else
113 //hmmm: there really isn't this concept on OSes that i'm aware of.
114   if (library_file_name.t()) return NIL; else return NIL;
115 #endif
116 }
117
118 astring version_checker::module_name(const void *module_handle)
119 {
120 #ifdef __UNIX__
121 //hmmm: implement module name for linux if that makes sense.
122   if (module_handle) {}
123   return application_configuration::application_name();
124 #elif defined(__WIN32__)
125   flexichar low_buff[MAX_ABS_PATH + 1];
126   GetModuleFileName((HMODULE)module_handle, low_buff, MAX_ABS_PATH - 1);
127   astring buff = from_unicode_temp(low_buff);
128   buff.to_lower();
129   return buff;
130 #else
131   #pragma message("module_name unknown for this operating system.")
132   return application_configuration::application_name();
133 #endif
134 }
135
136 astring version_checker::get_name(const void *to_find)
137 { return module_name(to_find); }
138
139 bool version_checker::retrieve_version_info(const astring &filename,
140     byte_array &to_fill)
141 {
142   to_fill.reset();  // clear the buffer.
143
144   // determine the required size of the version info buffer.
145   int required_size;
146 #ifdef __WIN32__
147   un_long module_handle;  // filled with the dll or exe handle.
148   required_size = GetFileVersionInfoSize(to_unicode_temp(filename), &module_handle);
149 #else
150   required_size = 0 && filename.t();
151 #endif
152   if (!required_size) return false;
153   to_fill.reset(required_size);  // resize the buffer.
154   
155   // read the version info into our buffer.
156   bool success = false;
157 #ifdef __WIN32__
158   success = GetFileVersionInfo(to_unicode_temp(filename), module_handle,
159       required_size, to_fill.access());
160 #else
161   success = false;
162 #endif
163   return success;
164 }
165
166 bool version_checker::get_language(byte_array &version_chunk,
167     basis::un_short &high, basis::un_short &low)
168 {
169   high = 0;
170   low = 0;
171 #ifdef __WIN32__
172   // determine the language that the version's written in.
173   basis::un_int data_size;
174   void *pointer_to_language_structure;
175   // query the information from the version blob.
176   if (!VerQueryValue(version_chunk.access(),
177       to_unicode_temp("\\VarFileInfo\\Translation"),
178       &pointer_to_language_structure, &data_size))
179     return false;
180   // get the low & high shorts of the structure.
181   high = LOWORD(*(unsigned int *)pointer_to_language_structure);
182   low = HIWORD(*(unsigned int *)pointer_to_language_structure);
183 #else
184   high = 0 && version_chunk.length();
185   low = 0;
186 #endif
187
188   return true;
189 }
190
191 version version_checker::retrieve_version(const astring &filename)
192 {
193 #ifdef UNIX
194
195   // totally bogus stand-in; this just returns the version we were built with
196   // rather than the version that's actually tagged on the file.
197
198 //hmmm: fix this!  get the version header back.
199 //  return version(__build_FILE_VERSION);
200   return version();
201
202 #endif
203
204   byte_array version_info_found(0, NIL);
205   if (!retrieve_version_info(filename, version_info_found))
206     return version(0, 0, 0, 0);
207
208   basis::un_short high, low;  // holds the language of the version data.
209   if (!get_language(version_info_found, high, low))
210     return version(0, 0, 0, 0);
211
212   // retrieve the file version from version info using the appropriate
213   // language.
214   astring root_key(astring::SPRINTF, "\\StringFileInfo\\%04x%04x", high, low);
215   astring file_version_key(root_key + astring("\\FileVersion"));
216
217   astring version_string;
218 #ifdef __WIN32__
219   abyte *file_version_pointer;
220   basis::un_int data_size;
221   if (!VerQueryValue(version_info_found.access(),
222       to_unicode_temp(file_version_key),
223       (LPVOID *)&file_version_pointer, &data_size))
224     return version(0, 0, 0, 0);
225   version_string = render_ptr(file_version_pointer);
226   // clean any spaces out of the string; people sometimes format these
227   // very badly.
228   for (int i = 0; i < version_string.length(); i++) {
229     if (version_string[i] == ' ') {
230       version_string.zap(i, i);
231       i--;  // skip back a beat.
232     }
233   }
234 #else
235   return version(0, 0, 0, 0);
236 //tmp.
237 #endif
238   return version::from_text(version_string);
239 }
240
241 bool version_checker::get_record(const astring &filename, 
242     version_record &to_fill)
243 {
244   to_fill = version_record();
245   byte_array version_info_found(0, NIL);
246   if (!retrieve_version_info(filename, version_info_found))
247     return false;
248
249   basis::un_short high, low;  // holds the language of the version data.
250   if (!get_language(version_info_found, high, low))
251     return false;
252
253   // set the root key for all accesses of the version chunk.
254   astring root_key(astring::SPRINTF, "\\StringFileInfo\\%04x%04x", high, low);
255
256   // reports whether all lookups succeeded or not.
257   bool total_success = true;
258
259   // the various version pieces are retrieved...
260
261 #ifdef __WIN32__
262   basis::un_int data_size;
263   void *data_pointer;
264
265   // file version.
266   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
267       + astring("\\FileVersion")), &data_pointer, &data_size))
268     total_success = false;
269   else
270     to_fill.file_version = version::from_text(render_ptr(data_pointer));
271
272   // company name.
273   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
274       + astring("\\CompanyName")), &data_pointer, &data_size))
275     total_success = false;
276   else
277     to_fill.company_name = render_ptr(data_pointer);
278
279   // file description.
280   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
281       + astring("\\FileDescription")), &data_pointer, &data_size))
282     total_success = false;
283   else
284     to_fill.description = render_ptr(data_pointer);
285
286   // internal name.
287   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
288       + astring("\\InternalName")), &data_pointer, &data_size))
289     total_success = false;
290   else
291     to_fill.internal_name = render_ptr(data_pointer);
292
293   // copyright info.
294   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
295       + astring("\\LegalCopyright")), &data_pointer, &data_size))
296     total_success = false;
297   else
298     to_fill.copyright = render_ptr(data_pointer);
299
300   // trademark info.
301   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
302       + astring("\\LegalTrademarks")), &data_pointer, &data_size))
303     total_success = false;
304   else
305     to_fill.trademarks = render_ptr(data_pointer);
306
307   // original file name.
308   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
309       + astring("\\OriginalFilename")), &data_pointer, &data_size))
310     total_success = false;
311   else
312     to_fill.original_name = render_ptr(data_pointer);
313
314   // product name.
315   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
316       + astring("\\ProductName")), &data_pointer, &data_size))
317     total_success = false;
318   else
319     to_fill.product_name = render_ptr(data_pointer);
320
321   // product version.
322   if (!VerQueryValue(version_info_found.access(), to_unicode_temp(root_key
323       + astring("\\ProductVersion")), &data_pointer, &data_size))
324     total_success = false;
325   else
326     to_fill.product_version = version::from_text(render_ptr(data_pointer));
327 #else
328   // hmmm: chunks missing in version check.
329 #endif
330
331   return total_success;
332 }
333
334 void version_checker::complain_wrong_version(const astring &library_file_name,
335     const version &expected_version, const version &version_found) const
336 {
337   astring to_show("There has been a Version Mismatch: The module \"");
338   // use embedded module handle to retrieve name of dll or exe.
339   astring module_name = get_name(version::__global_module_handle());
340   if (!module_name) module_name = "Unknown";
341   to_show += module_name;
342   to_show += astring("\" cannot load.  This is because the file \"");
343   to_show += library_file_name;
344   to_show += astring("\" was expected to have a version of [");
345
346   to_show += expected_version.flex_text_form(version::DOTS);
347
348   to_show += astring("] but it instead had a version of [");
349   to_show += version_found.flex_text_form(version::DOTS);
350
351   to_show += astring("].  ");
352   to_show += *_version_complaint;
353 #ifdef __UNIX__
354   continuable_error("version checking", "failure", to_show.s());
355 #elif defined(__WIN32__)
356   MessageBox(0, to_unicode_temp(to_show),
357       to_unicode_temp("version_checking::failure"), MB_OK);
358 #endif
359 }
360
361 void version_checker::complain_cannot_load(const astring &lib_file) const
362 {
363   astring to_show("There has been a failure in Version Checking: The file \"");
364   to_show += lib_file;
365   to_show += astring("\" could not be loaded or found.  ");
366   to_show += *_version_complaint;
367   continuable_error("version checking", "loading dll", to_show.s());
368 }
369
370 } //namespace.
371
372