Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "sunversion.hxx"
22 : #include "osl/thread.h"
23 : #include "osl/process.h"
24 : #include "osl/security.hxx"
25 : #include <string.h>
26 : #include <ctype.h>
27 : #include "diagnostics.h"
28 : using namespace osl;
29 :
30 : namespace jfw_plugin { //stoc_javadetect
31 :
32 :
33 : #if OSL_DEBUG_LEVEL >= 2
34 : class SelfTest
35 : {
36 : public:
37 : SelfTest();
38 : } test;
39 : #endif
40 :
41 232 : SunVersion::SunVersion(const OUString &usVer):
42 : m_nUpdateSpecial(0), m_preRelease(Rel_NONE),
43 232 : usVersion(usVer)
44 : {
45 232 : memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
46 232 : OString sVersion= OUStringToOString(usVer, osl_getThreadTextEncoding());
47 232 : m_bValid = init(sVersion.getStr());
48 232 : }
49 0 : SunVersion::SunVersion(const char * szVer):
50 0 : m_nUpdateSpecial(0), m_preRelease(Rel_NONE)
51 : {
52 0 : memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
53 0 : m_bValid = init(szVer);
54 0 : usVersion= OUString(szVer,strlen(szVer),osl_getThreadTextEncoding());
55 0 : }
56 :
57 :
58 : /**Format major.minor.maintainance_update
59 : */
60 232 : bool SunVersion::init(const char *szVersion)
61 : {
62 232 : if ( ! szVersion || strlen(szVersion) == 0)
63 0 : return false;
64 :
65 : //first get the major,minor,maintenance
66 232 : const char * pLast = szVersion;
67 232 : const char * pCur = szVersion;
68 : //pEnd point to the position after the last character
69 232 : const char * pEnd = szVersion + strlen(szVersion);
70 : // 0 = major, 1 = minor, 2 = maintenance, 3 = update
71 232 : int nPart = 0;
72 : // position within part beginning with 0
73 232 : int nPartPos = 0;
74 : char buf[128];
75 :
76 : //char must me a number 0 - 999 and no leading
77 : while (true)
78 : {
79 1392 : if (pCur < pEnd && isdigit(*pCur))
80 : {
81 696 : if (pCur < pEnd)
82 696 : pCur ++;
83 696 : nPartPos ++;
84 : }
85 : //if correct separator then form integer
86 696 : else if (
87 : ! (nPartPos == 0) // prevents: ".4.1", "..1", part must start with digit
88 696 : && (
89 : //separators after maintainance (1.4.1_01, 1.4.1-beta, or1.4.1
90 580 : ((pCur == pEnd || *pCur == '_' || *pCur == '-') && (nPart == 2 ))
91 464 : ||
92 : //separators between major-minor and minor-maintainance
93 464 : (nPart < 2 && *pCur == '.') )
94 696 : && (
95 : //prevent 1.4.0. 1.4.0-
96 696 : pCur + 1 == pEnd ? isdigit(*(pCur)) : 1) )
97 : {
98 696 : int len = pCur - pLast;
99 696 : if (len >= 127)
100 0 : return false;
101 :
102 696 : strncpy(buf, pLast, len);
103 696 : buf[len] = 0;
104 696 : pCur ++;
105 696 : pLast = pCur;
106 :
107 696 : m_arVersionParts[nPart] = atoi(buf);
108 696 : nPart ++;
109 696 : nPartPos = 0;
110 696 : if (nPart == 3)
111 232 : break;
112 :
113 : //check next character
114 464 : if (! ( (pCur < pEnd)
115 464 : && ( (nPart < 3) && isdigit(*pCur))))
116 0 : return false;
117 : }
118 : else
119 : {
120 0 : return false;
121 : }
122 : }
123 232 : if (pCur >= pEnd)
124 116 : return true;
125 : //We have now 1.4.1. This can be followed by _01, -beta, etc.
126 : // _01 (update) According to docu must not be followed by any other
127 : //characters, but on Solaris 9 we have a 1.4.1_01a!!
128 116 : if (* (pCur - 1) == '_')
129 : {// _01, _02
130 : // update is the last part _01, _01a, part 0 is the digits parts and 1 the trailing alpha
131 : while (true)
132 : {
133 348 : if (pCur <= pEnd)
134 : {
135 348 : if ( ! isdigit(*pCur))
136 : {
137 : //1.4.1_01-, 1.4.1_01a, the numerical part may only be 2 chars.
138 116 : int len = pCur - pLast;
139 116 : if (len > 2)
140 0 : return false;
141 : //we've got the update: 01, 02 etc
142 116 : strncpy(buf, pLast, len);
143 116 : buf[len] = 0;
144 116 : m_arVersionParts[nPart] = atoi(buf);
145 116 : if (pCur == pEnd)
146 : {
147 116 : break;
148 : }
149 0 : if (*pCur == 'a' && (pCur + 1) == pEnd)
150 : {
151 : //check if it s followed by a simple "a" (not specified)
152 0 : m_nUpdateSpecial = *pCur;
153 0 : break;
154 : }
155 0 : else if (*pCur == '-' && pCur < pEnd)
156 : {
157 : //check 1.5.0_01-ea
158 0 : PreRelease pr = getPreRelease(++pCur);
159 0 : if (pr == Rel_NONE)
160 0 : return false;
161 : //just ignore -ea because its no official release
162 0 : break;
163 : }
164 : else
165 : {
166 0 : return false;
167 : }
168 : }
169 232 : if (pCur < pEnd)
170 232 : pCur ++;
171 : else
172 0 : break;
173 : }
174 232 : }
175 : }
176 : // 1.4.1-ea
177 0 : else if (*(pCur - 1) == '-')
178 : {
179 0 : m_preRelease = getPreRelease(pCur);
180 0 : if (m_preRelease == Rel_NONE)
181 0 : return false;
182 : #if defined(FREEBSD)
183 : if (m_preRelease == Rel_FreeBSD)
184 : {
185 : pCur++; //eliminate 'p'
186 : if (pCur < pEnd && isdigit(*pCur))
187 : pCur ++;
188 : int len = pCur - pLast -1; //eliminate 'p'
189 : if (len >= 127)
190 : return false;
191 : strncpy(buf, (pLast+1), len); //eliminate 'p'
192 : buf[len] = 0;
193 : m_nUpdateSpecial = atoi(buf)+100; //hack for FBSD #i56953#
194 : return true;
195 : }
196 : #endif
197 : }
198 : else
199 : {
200 0 : return false;
201 : }
202 1276 : return true;
203 : }
204 :
205 0 : SunVersion::PreRelease SunVersion::getPreRelease(const char *szRelease)
206 : {
207 0 : if (szRelease == NULL)
208 0 : return Rel_NONE;
209 0 : if( ! strcmp(szRelease,"internal"))
210 0 : return Rel_INTERNAL;
211 0 : else if( ! strcmp(szRelease,"ea"))
212 0 : return Rel_EA;
213 0 : else if( ! strcmp(szRelease,"ea1"))
214 0 : return Rel_EA1;
215 0 : else if( ! strcmp(szRelease,"ea2"))
216 0 : return Rel_EA2;
217 0 : else if( ! strcmp(szRelease,"ea3"))
218 0 : return Rel_EA3;
219 0 : else if ( ! strcmp(szRelease,"beta"))
220 0 : return Rel_BETA;
221 0 : else if ( ! strcmp(szRelease,"beta1"))
222 0 : return Rel_BETA1;
223 0 : else if ( ! strcmp(szRelease,"beta2"))
224 0 : return Rel_BETA2;
225 0 : else if ( ! strcmp(szRelease,"beta3"))
226 0 : return Rel_BETA3;
227 0 : else if (! strcmp(szRelease, "rc"))
228 0 : return Rel_RC;
229 0 : else if (! strcmp(szRelease, "rc1"))
230 0 : return Rel_RC1;
231 0 : else if (! strcmp(szRelease, "rc2"))
232 0 : return Rel_RC2;
233 0 : else if (! strcmp(szRelease, "rc3"))
234 0 : return Rel_RC3;
235 : #if defined (FREEBSD)
236 : else if (! strncmp(szRelease, "p", 1))
237 : return Rel_FreeBSD;
238 : #endif
239 : else
240 0 : return Rel_NONE;
241 : }
242 :
243 232 : SunVersion::~SunVersion()
244 : {
245 :
246 232 : }
247 :
248 : /* Examples:
249 : a) 1.0 < 1.1
250 : b) 1.0 < 1.0.0
251 : c) 1.0 < 1.0_00
252 :
253 : returns false if both values are equal
254 : */
255 116 : bool SunVersion::operator > (const SunVersion& ver) const
256 : {
257 116 : if( &ver == this)
258 0 : return false;
259 :
260 : //compare major.minor.maintenance
261 232 : for( int i= 0; i < 4; i ++)
262 : {
263 : // 1.4 > 1.3
264 232 : if(m_arVersionParts[i] > ver.m_arVersionParts[i])
265 : {
266 116 : return true;
267 : }
268 116 : else if (m_arVersionParts[i] < ver.m_arVersionParts[i])
269 : {
270 0 : return false;
271 : }
272 : }
273 : //major.minor.maintainance_update are equal. test for a trailing char
274 0 : if (m_nUpdateSpecial > ver.m_nUpdateSpecial)
275 : {
276 0 : return true;
277 : }
278 :
279 : //Until here the versions are equal
280 : //compare pre -release values
281 0 : if ((m_preRelease == Rel_NONE && ver.m_preRelease == Rel_NONE)
282 0 : ||
283 0 : (m_preRelease != Rel_NONE && ver.m_preRelease == Rel_NONE))
284 0 : return false;
285 0 : else if (m_preRelease == Rel_NONE && ver.m_preRelease != Rel_NONE)
286 0 : return true;
287 0 : else if (m_preRelease > ver.m_preRelease)
288 0 : return true;
289 :
290 0 : return false;
291 : }
292 :
293 0 : bool SunVersion::operator < (const SunVersion& ver) const
294 : {
295 0 : return (! operator > (ver)) && (! operator == (ver));
296 : }
297 :
298 116 : bool SunVersion::operator == (const SunVersion& ver) const
299 : {
300 116 : bool bRet= true;
301 232 : for(int i= 0; i < 4; i++)
302 : {
303 232 : if( m_arVersionParts[i] != ver.m_arVersionParts[i])
304 : {
305 116 : bRet= false;
306 116 : break;
307 : }
308 : }
309 116 : bRet = m_nUpdateSpecial == ver.m_nUpdateSpecial && bRet;
310 116 : bRet = m_preRelease == ver.m_preRelease && bRet;
311 116 : return bRet;
312 : }
313 :
314 :
315 : #if OSL_DEBUG_LEVEL >= 2
316 : SelfTest::SelfTest()
317 : {
318 : bool bRet = true;
319 :
320 : char const * versions[] = {"1.4.0", "1.4.1", "1.0.0", "10.0.0", "10.10.0",
321 : "10.2.2", "10.10.0", "10.10.10", "111.0.999",
322 : "1.4.1_01", "9.90.99_09", "1.4.1_99",
323 : "1.4.1_00a",
324 : "1.4.1-ea", "1.4.1-beta", "1.4.1-rc1",
325 : "1.5.0_01-ea", "1.5.0_01-rc2"};
326 : char const * badVersions[] = {".4.0", "..1", "", "10.0", "10.10.0.", "10.10.0-", "10.10.0.",
327 : "10.2-2", "10_10.0", "10..10","10.10", "a.0.999",
328 : "1.4b.1_01", "9.90.-99_09", "1.4.1_99-",
329 : "1.4.1_00a2", "1.4.0_z01z", "1.4.1__99A",
330 : "1.4.1-1ea", "1.5.0_010", "1.5.0._01-", "1.5.0_01-eac"};
331 : char const * orderedVer[] = { "1.3.1-ea", "1.3.1-beta", "1.3.1-rc1",
332 : "1.3.1", "1.3.1_00a", "1.3.1_01", "1.3.1_01a",
333 : "1.3.2", "1.4.0", "1.5.0_01-ea", "2.0.0"};
334 :
335 : int num = sizeof (versions) / sizeof(char*);
336 : int numBad = sizeof (badVersions) / sizeof(char*);
337 : int numOrdered = sizeof (orderedVer) / sizeof(char*);
338 : //parsing test (positive)
339 : for (int i = 0; i < num; i++)
340 : {
341 : SunVersion ver(versions[i]);
342 : if ( ! ver)
343 : {
344 : bRet = false;
345 : break;
346 : }
347 : }
348 : OSL_ENSURE(bRet, "SunVersion selftest failed");
349 : //Parsing test (negative)
350 : for ( int i = 0; i < numBad; i++)
351 : {
352 : SunVersion ver(badVersions[i]);
353 : if (ver)
354 : {
355 : bRet = false;
356 : break;
357 : }
358 : }
359 : OSL_ENSURE(bRet, "SunVersion selftest failed");
360 :
361 : // Ordering test
362 : bRet = true;
363 : int j = 0;
364 : for (int i = 0; i < numOrdered; i ++)
365 : {
366 : SunVersion curVer(orderedVer[i]);
367 : if ( ! curVer)
368 : {
369 : bRet = false;
370 : break;
371 : }
372 : for (j = 0; j < numOrdered; j++)
373 : {
374 : SunVersion compVer(orderedVer[j]);
375 : if (i < j)
376 : {
377 : if ( !(curVer < compVer))
378 : {
379 : bRet = false;
380 : break;
381 : }
382 : }
383 : else if ( i == j)
384 : {
385 : if (! (curVer == compVer
386 : && ! (curVer > compVer)
387 : && ! (curVer < compVer)))
388 : {
389 : bRet = false;
390 : break;
391 : }
392 : }
393 : else if (i > j)
394 : {
395 : if ( !(curVer > compVer))
396 : {
397 : bRet = false;
398 : break;
399 : }
400 : }
401 : }
402 : if ( ! bRet)
403 : break;
404 : }
405 : if (bRet)
406 : JFW_TRACE2("Testing class SunVersion succeeded.");
407 : else
408 : OSL_ENSURE(bRet, "[Java framework] sunjavaplugin: SunVersion self test failed.\n");
409 : }
410 : #endif
411 :
412 : }
413 :
414 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|