1 /* version.c - Version check routines.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #ifdef HAVE_W32_SYSTEM
37 /* For _gpgme_sema_subsystem_init (). */
44 #ifdef HAVE_W32_SYSTEM
48 /* We implement this function, so we have to disable the overriding
50 #undef gpgme_check_version
53 /* Bootstrap the subsystems needed for concurrent operation. This
54 must be done once at startup. We can not guarantee this using a
55 lock, though, because the semaphore subsystem needs to be
56 initialized itself before it can be used. So we expect that the
57 user performs the necessary synchronization. */
59 do_subsystem_inits (void)
66 #ifdef HAVE_W32_SYSTEM
70 WSAStartup (0x202, &wsadat);
74 _gpgme_sema_subsystem_init ();
76 assuan_set_assuan_err_source (GPG_ERR_SOURCE_GPGME);
77 #endif /*HAVE_ASSUAN_H*/
78 _gpgme_debug_subsystem_init ();
79 _gpgme_io_subsystem_init ();
80 #if defined(HAVE_W32_SYSTEM) && defined(HAVE_ASSUAN_H)
81 /* We need to make sure that the sockets are initialized. */
85 WSAStartup (0x202, &wsadat);
87 #endif /*HAVE_W32_SYSTEM && HAVE_ASSUAN_H*/
93 /* Read the next number in the version string STR and return it in
94 *NUMBER. Return a pointer to the tail of STR after parsing, or
95 *NULL if the version string was invalid. */
97 parse_version_number (const char *str, int *number)
99 #define MAXVAL ((INT_MAX - 10) / 10)
102 /* Leading zeros are not allowed. */
103 if (*str == '0' && isdigit(str[1]))
106 while (isdigit (*str) && val <= MAXVAL)
109 val += *(str++) - '0';
112 return val > MAXVAL ? NULL : str;
116 /* Parse the version string STR in the format MAJOR.MINOR.MICRO (for
117 example, 9.3.2) and return the components in MAJOR, MINOR and MICRO
118 as integers. The function returns the tail of the string that
119 follows the version number. This might be te empty string if there
120 is nothing following the version number, or a patchlevel. The
121 function returns NULL if the version string is not valid. */
123 parse_version_string (const char *str, int *major, int *minor, int *micro)
125 str = parse_version_number (str, major);
126 if (!str || *str != '.')
130 str = parse_version_number (str, minor);
131 if (!str || *str != '.')
135 str = parse_version_number (str, micro);
139 /* A patchlevel might follow. */
144 /* Return true if MY_VERSION is at least REQ_VERSION, and false
147 _gpgme_compare_versions (const char *my_version,
148 const char *rq_version)
150 int my_major, my_minor, my_micro;
151 int rq_major, rq_minor, rq_micro;
152 const char *my_plvl, *rq_plvl;
159 my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
163 rq_plvl = parse_version_string (rq_version, &rq_major, &rq_minor, &rq_micro);
167 if (my_major > rq_major
168 || (my_major == rq_major && my_minor > rq_minor)
169 || (my_major == rq_major && my_minor == rq_minor
170 && my_micro > rq_micro)
171 || (my_major == rq_major && my_minor == rq_minor
172 && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0))
179 /* Check that the the version of the library is at minimum the
180 requested one and return the version string; return NULL if the
181 condition is not met. If a NULL is passed to this function, no
182 check is done and the version string is simply returned.
184 This function must be run once at startup, as it also initializes
185 some subsystems. Its invocation must be synchronized against
186 calling any of the other functions in a multi-threaded
189 gpgme_check_version (const char *req_version)
192 do_subsystem_inits ();
194 /* Catch-22: We need to get at least the debug subsystem ready
195 before using the tarce facility. If we won't the tarce would
196 automagically initialize the debug system with out the locks
197 being initialized and missing the assuan log level setting. */
198 TRACE2 (DEBUG_INIT, "gpgme_check_version: ", 0,
199 "req_version=%s, VERSION=%s",
200 req_version? req_version:"(null)", VERSION);
202 result = _gpgme_compare_versions (VERSION, req_version) ? VERSION : NULL;
209 /* Check the version and also at runtime if the struct layout of the
210 library matches the one of the user. This is particular useful for
211 Windows targets (-mms-bitfields). */
213 gpgme_check_version_internal (const char *req_version,
214 size_t offset_sig_validity)
218 TRACE2 (DEBUG_INIT, "gpgme_check_version_internal: ", 0,
219 "req_version=%s, offset_sig_validity=%i",
220 req_version ? req_version : "(null)", offset_sig_validity);
222 result = gpgme_check_version (req_version);
226 if (offset_sig_validity != offsetof (struct _gpgme_signature, validity))
228 TRACE1 (DEBUG_INIT, "gpgme_check_version_internal: ", 0,
229 "offset_sig_validity mismatch: expected %i",
230 offsetof (struct _gpgme_signature, validity));
231 _gpgme_selftest = GPG_ERR_SELFTEST_FAILED;
238 #define LINELENGTH 80
240 /* Extract the version string of a program from STRING. The version
241 number is expected to be in GNU style format:
244 foo (bar system) 1.2.3
246 foo (bar system) 1.2.3 cruft.
248 Spaces and tabs are skipped and used as delimiters, a term in
249 (nested) parenthesis before the version string is skipped, the
250 version string may consist of any non-space and non-tab characters
251 but needs to bstart with a digit.
254 extract_version_string (const char *string, size_t *r_len)
259 for (s=string; *s; s++)
260 if (*s == ' ' || *s == '\t')
262 while (*s == ' ' || *s == '\t')
266 for (count=1, s++; count && *s; s++)
272 /* For robustness we look for a digit. */
273 while ( *s && !(*s >= '0' && *s <= '9') )
275 if (*s >= '0' && *s <= '9')
277 for (len=0; s[len]; len++)
278 if (s[len] == ' ' || s[len] == '\t')
289 /* Retrieve the version number from the --version output of the
290 program FILE_NAME. */
292 _gpgme_get_program_version (const char *const file_name)
294 char line[LINELENGTH] = "";
299 char *argv[] = {NULL /* file_name */, "--version", 0};
300 struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0},
306 argv[0] = (char *) file_name;
308 if (_gpgme_io_pipe (rp, 1) < 0)
313 status = _gpgme_io_spawn (file_name, argv, cfd, NULL);
316 _gpgme_io_close (rp[0]);
317 _gpgme_io_close (rp[1]);
323 nread = _gpgme_io_read (rp[0], &line[linelen], LINELENGTH - linelen - 1);
326 line[linelen + nread] = '\0';
327 mark = strchr (&line[linelen], '\n');
330 if (mark > &line[0] && *mark == '\r')
338 while (nread > 0 && linelen < LINELENGTH - 1);
340 _gpgme_io_close (rp[0]);
347 s = extract_version_string (line, &len);
350 mark = malloc (len + 1);
353 memcpy (mark, s, len);