summaryrefslogtreecommitdiffstats
path: root/src/tools/qcomlibrary.cpp
blob: bae4e7df8f34c5daa84feb253428e86832782e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/****************************************************************************
**
** Implementation of QComLibrary class
**
** Copyright (C) 2001-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the tools module of the Qt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free Qt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
** included in the packaging of this file.  Licensees holding valid Qt
** Commercial licenses may use this file in accordance with the Qt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "qcomlibrary_p.h"

#ifndef QT_NO_COMPONENT
#include <qapplication.h>
#include <qsettings.h>
#include <qfileinfo.h>
#include <qdatetime.h>
#include <qcleanuphandler.h>
#ifndef NO_ERRNO_H
#include <errno.h>
#endif // NO_ERROR_H

#ifdef QT_THREAD_SUPPORT
#  include "qmutexpool_p.h"
#endif // QT_THREAD_SUPPORT

#ifndef QT_DEBUG_COMPONENT
# if defined(QT_DEBUG)
#  define QT_DEBUG_COMPONENT 1
# endif
#endif


QComLibrary::QComLibrary( const QString &filename )
    : QLibrary( filename ), entry( 0 ), libiface( 0 ), qt_version( 0 )
{
}

QComLibrary::~QComLibrary()
{
    if ( autoUnload() )
	unload();
    if ( libiface )
	libiface->release();
    if ( entry )
	entry->release();
}

bool QComLibrary::unload()
{
    int refs = entry ? entry->release() : 0;
    entry = 0;
    if (refs || !libiface)
	return FALSE;

    libiface->cleanup();
    if ( !libiface->canUnload() )
	return FALSE;
    libiface->release();
    libiface = 0;

    return QLibrary::unload();
}

static bool qt_verify( const QString& library, uint version, uint flags,
		    const QCString &key, bool warn )
{
    uint our_flags = 1;
#if defined(QT_THREAD_SUPPORT)
    our_flags |= 2;
#endif

    if ( (flags & 1) == 0 ) {
	if ( warn )
	    tqWarning( "Conflict in %s:\n"
		      "  Plugin cannot be queried successfully!",
		      (const char*) QFile::encodeName(library) );
    } else if ( ( version > QT_VERSION ) ||
		( ( QT_VERSION & 0xff0000 ) > ( version & 0xff0000 ) ) ) {
	if ( warn )
	    tqWarning( "Conflict in %s:\n"
		      "  Plugin uses incompatible Qt library (%d.%d.%d)!",
		      (const char*) QFile::encodeName(library),
		      (version&0xff0000) >> 16, (version&0xff00) >> 8, version&0xff );
    } else if ( (flags & 2) != (our_flags & 2) ) {
	if ( warn )
	    tqWarning( "Conflict in %s:\n"
		      "  Plugin uses %s Qt library!",
		      (const char*) QFile::encodeName(library),
		      (flags & 2) ? "multi threaded" : "single threaded" );
    } else if ( key != QT_BUILD_KEY ) {
	if ( warn )
	    tqWarning( "Conflict in %s:\n"
		      "  Plugin uses incompatible Qt library!\n"
		      "  expected build key \"%s\", got \"%s\".",
		      (const char*) QFile::encodeName(library),
		      QT_BUILD_KEY,
		      key.isEmpty() ? "<null>" : (const char *) key );
    } else {
	return TRUE;
    }
    return FALSE;
}

struct qt_token_info
{
    qt_token_info( const char *f, const ulong fc )
	: fields( f ), field_count( fc ), results( fc ), lengths( fc )
    {
	results.fill( 0 );
	lengths.fill( 0 );
    }

    const char *fields;
    const ulong field_count;

    QMemArray<const char *> results;
    QMemArray<ulong> lengths;
};

/*
  return values:
       1 parse ok
       0 eos
      -1 parse error
*/
static int qt_tokenize( const char *s, ulong s_len, ulong *advance,
			const qt_token_info &token_info )
{
    ulong pos = 0, field = 0, fieldlen = 0;
    char current;
    int ret = -1;
    *advance = 0;
    for (;;) {
	current = s[ pos ];

	// next char
	++pos;
	++fieldlen;
	++*advance;

	if ( ! current || pos == s_len + 1 ) {
	    // save result
	    token_info.results[ (int)field ] = s;
	    token_info.lengths[ (int)field ] = fieldlen - 1;

	    // end of string
	    ret = 0;
	    break;
	}

	if ( current == token_info.fields[ field ] ) {
	    // save result
	    token_info.results[ (int)field ] = s;
	    token_info.lengths[ (int)field ] = fieldlen - 1;

	    // end of field
	    fieldlen = 0;
	    ++field;
	    if ( field == token_info.field_count - 1 ) {
		// parse ok
		ret = 1;
	    }
	    if ( field == token_info.field_count ) {
		// done parsing
		break;
	    }

	    // reset string and its length
	    s = s + pos;
	    s_len -= pos;
	    pos = 0;
	}
    }

    return ret;
}

/*
  returns TRUE if the string s was correctly parsed, FALSE otherwise.
*/
static bool qt_parse_pattern( const char *s, uint *version, uint *flags,
				   QCString *key )
{
    bool ret = TRUE;

    qt_token_info pinfo("=\n", 2);
    int parse;
    ulong at = 0, advance, parselen = qstrlen( s );
    do {
	parse = qt_tokenize( s + at, parselen, &advance, pinfo );
	if ( parse == -1 ) {
	    ret = FALSE;
	    break;
	}

	at += advance;
	parselen -= advance;

	if ( qstrncmp( "version", pinfo.results[ 0 ], pinfo.lengths[ 0 ] ) == 0 ) {
	    // parse version string
	    qt_token_info pinfo2("..-", 3);
	    if ( qt_tokenize( pinfo.results[ 1 ], pinfo.lengths[ 1 ],
			      &advance, pinfo2 ) != -1 ) {
		QCString m( pinfo2.results[ 0 ], pinfo2.lengths[ 0 ] + 1 );
		QCString n( pinfo2.results[ 1 ], pinfo2.lengths[ 1 ] + 1 );
		QCString p( pinfo2.results[ 2 ], pinfo2.lengths[ 2 ] + 1 );
		*version  = (m.toUInt() << 16) | (n.toUInt() << 8) | p.toUInt();
	    } else {
		ret = FALSE;
		break;
	    }
	} else if ( qstrncmp( "flags", pinfo.results[ 0 ], pinfo.lengths[ 0 ] ) == 0 ) {
	    // parse flags string
	    char ch;
	    *flags = 0;
	    ulong p = 0, c = 0, bit = 0;
	    while ( p < pinfo.lengths[ 1 ] ) {
		ch = pinfo.results[ 1 ][ p ];
		bit = pinfo.lengths[ 1 ] - p - 1;
		c = 1 << bit;
		if ( ch == '1' ) {
		    *flags |= c;
		} else if ( ch != '0' ) {
		    ret = FALSE;
		    break;
		}
		++p;
	    }
	} else if ( qstrncmp( "buildkey", pinfo.results[ 0 ],
			      pinfo.lengths[ 0 ] ) == 0 ){
	    // save buildkey
	    *key = QCString( pinfo.results[ 1 ], pinfo.lengths[ 1 ] + 1 );
	}
    } while ( parse == 1 && parselen > 0 );

    return ret;
}

#if defined(Q_OS_UNIX)

#if defined(Q_OS_FREEBSD) || defined(Q_OS_LINUX)
#  define USE_MMAP
#  include <sys/types.h>
#  include <sys/mman.h>
#endif // Q_OS_FREEBSD || Q_OS_LINUX

static long qt_find_pattern( const char *s, ulong s_len,
			     const char *pattern, ulong p_len )
{
    /*
      this uses the same algorithm as QString::findRev...

      we search from the end of the file because on the supported
      systems, the read-only data/text segments are placed at the end
      of the file.  HOWEVER, when building with debugging enabled, all
      the debug symbols are placed AFTER the data/text segments.

      what does this mean?  when building in release mode, the search
      is fast because the data we are looking for is at the end of the
      file... when building in debug mode, the search is slower
      because we have to skip over all the debugging symbols first
    */

    if ( ! s || ! pattern || p_len > s_len ) return -1;
    ulong i, hs = 0, hp = 0, delta = s_len - p_len;

    for (i = 0; i < p_len; ++i ) {
	hs += s[delta + i];
	hp += pattern[i];
    }
    i = delta;
    for (;;) {
	if ( hs == hp && qstrncmp( s + i, pattern, p_len ) == 0 )
	    return i;
	if ( i == 0 )
	    break;
	--i;
	hs -= s[i + p_len];
	hs += s[i];
    }

    return -1;
}

/*
  This opens the specified library, mmaps it into memory, and searches
  for the QT_UCM_VERIFICATION_DATA.  The advantage of this approach is that
  we can get the verification data without have to actually load the library.
  This lets us detect mismatches more safely.

  Returns FALSE if version/flags/key information is not present, or if the
                information could not be read.
  Returns  TRUE if version/flags/key information is present and succesfully read.
*/
static bool qt_unix_query( const QString &library, uint *version, uint *flags,
			   QCString *key )
{
    QFile file( library );
    if (! file.open( IO_ReadOnly ) ) {
	tqWarning( "%s: %s", (const char*) QFile::encodeName(library),
	    strerror( errno ) );
	return FALSE;
    }

    QByteArray data;
    char *filedata = 0;
    ulong fdlen = 0;

#ifdef USE_MMAP
    char *mapaddr = 0;
    size_t maplen = file.size();
    mapaddr = (char *) mmap( mapaddr, maplen, PROT_READ, MAP_PRIVATE, file.handle(), 0 );
    if ( mapaddr != MAP_FAILED ) {
	// mmap succeeded
	filedata = mapaddr;
	fdlen = maplen;
    } else {
	// mmap failed
	tqWarning( "mmap: %s", strerror( errno ) );
#endif // USE_MMAP
	// try reading the data into memory instead
	data = file.readAll();
	filedata = data.data();
	fdlen = data.size();
#ifdef USE_MMAP
    }
#endif // USE_MMAP

    // verify that the pattern is present in the plugin
    const char *pattern = "pattern=QT_UCM_VERIFICATION_DATA";
    const ulong plen = qstrlen( pattern );
    long pos = qt_find_pattern( filedata, fdlen, pattern, plen );

    bool ret = FALSE;
    if ( pos >= 0 ) {
	ret = qt_parse_pattern( filedata + pos, version, flags, key );
    }

#ifdef USE_MMAP
    if ( mapaddr != MAP_FAILED && munmap(mapaddr, maplen) != 0 ) {
	tqWarning( "munmap: %s", strerror( errno ) );
    }
#endif // USE_MMAP

    file.close();
    return ret;
}

#endif // Q_OS_UNIX


static QSettings *cache = 0;
static QSingleCleanupHandler<QSettings> cleanup_cache;

void QComLibrary::createInstanceInternal()
{
    if ( library().isEmpty() )
	return;

    QFileInfo fileinfo( library() );
    QString lastModified  = fileinfo.lastModified().toString(Qt::ISODate);
    QString regkey = QString("/Qt Plugins %1.%2/%3")
		     .arg( ( QT_VERSION & 0xff0000 ) >> 16 )
		     .arg( ( QT_VERSION & 0xff00 ) >> 8 )
		     .arg( library() );
    QStringList reg;
    uint flags = 0;
    QCString key;
    bool query_done = FALSE;
    bool warn_mismatch = TRUE;

#ifdef QT_THREAD_SUPPORT
    QMutexLocker locker( qt_global_mutexpool ?
			 qt_global_mutexpool->get( &cache ) : 0 );
#endif // QT_THREAD_SUPPORT

    if ( ! cache ) {
	cache = new QSettings;
	cache->insertSearchPath( QSettings::Windows, "/Trolltech" );
	cleanup_cache.set( &cache );
    }

    reg = cache->readListEntry( regkey );
    if ( reg.count() == 4 ) {
	// check timestamp
	if ( lastModified == reg[3] ) {
	    qt_version = reg[0].toUInt(0, 16);
	    flags = reg[1].toUInt(0, 16);
	    key = reg[2].latin1();

	    query_done = TRUE;
	    warn_mismatch = FALSE;
	}
    }

#if defined(Q_OS_UNIX)
    if ( ! query_done ) {
	// get the query information directly from the plugin without loading
	if ( qt_unix_query( library(), &qt_version, &flags, &key ) ) {
 	    // info read succesfully from library
 	    query_done = TRUE;
	}
    }
#else // !Q_OS_UNIX
    if ( ! query_done ) {
	// get the query information by loading the plugin
	if ( !isLoaded() ) {
	    Q_ASSERT( entry == 0 );
	    if ( !load() )
		return;
	}

#  ifdef Q_CC_BOR
	typedef const char * __stdcall (*UCMQueryVerificationDataProc)();
#  else
	typedef const char * (*UCMQueryVerificationDataProc)();
#  endif
	UCMQueryVerificationDataProc ucmQueryVerificationdataProc;
	ucmQueryVerificationdataProc =
	    (UCMQueryVerificationDataProc) resolve( "qt_ucm_query_verification_data" );

	if ( !ucmQueryVerificationdataProc ||
	     !qt_parse_pattern( ucmQueryVerificationdataProc(),
				&qt_version, &flags, &key ) ) {
	    qt_version = flags = 0;
	    key = "unknown";
	} else {
	    query_done = TRUE;
	}
    }
#endif // Q_OS_UNIX

    QStringList queried;
    queried << QString::number( qt_version,16 )
	    << QString::number( flags, 16 )
	    <<  key
	    << lastModified;

    if ( queried != reg ) {
	cache->writeEntry( regkey, queried );
	// delete the cache, which forces the settings to be written
	delete cache;
	cache = 0;
    }

    if ( ! query_done ) {
	if ( warn_mismatch ) {
	    tqWarning( "Conflict in %s:\n Plugin cannot be queried successfully!",
		      (const char*) QFile::encodeName( library() ) );
	}
	unload();
	return;
    }

    if ( ! qt_verify( library(), qt_version, flags, key, warn_mismatch ) ) {
	unload();
	return;
    } else if ( !isLoaded() ) {
	Q_ASSERT( entry == 0 );
	if ( !load() )
	    return;
    }

#ifdef Q_CC_BOR
    typedef QUnknownInterface* __stdcall (*UCMInstanceProc)();
#else
    typedef QUnknownInterface* (*UCMInstanceProc)();
#endif
    UCMInstanceProc ucmInstanceProc;
    ucmInstanceProc = (UCMInstanceProc) resolve( "ucm_instantiate" );
#if defined(QT_DEBUG_COMPONENT)
    if ( !ucmInstanceProc )
	tqWarning( "%s: Not a UCOM library.", (const char*) QFile::encodeName(library()) );
#endif
    entry = ucmInstanceProc ? ucmInstanceProc() : 0;

    if ( entry ) {
	if ( entry->queryInterface( IID_QLibrary, (QUnknownInterface**)&libiface ) == QS_OK ) {
	    if ( libiface && !libiface->init() ) {
		libiface->release();
		libiface = 0;
		unload();
		return;
	    }
	}
    } else {
#if defined(QT_DEBUG_COMPONENT)
	tqWarning( "%s: No exported component provided.", (const char*) QFile::encodeName(library()) );
#endif
	unload();
    }
}

QRESULT QComLibrary::queryInterface( const QUuid& request, QUnknownInterface** iface )
{
    if ( !entry )
	createInstanceInternal();
    return entry ? entry->queryInterface( request, iface ) : QE_NOCOMPONENT;
}

uint QComLibrary::qtVersion()
{
    if ( !entry )
	createInstanceInternal();
    return entry ? qt_version : 0;
}


#endif // QT_NO_COMPONENT