1
/* secqstring.h - Secure version of QString.
2
Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
3
Copyright (C) 2003 g10 Code GmbH
5
The license of the original qstring.h file from which this file is
6
derived can be found below. Modified by Marcus Brinkmann
7
<marcus@g10code.de>. All modifications are licensed as follows, so
8
that the intersection of the two licenses is then the GNU General
9
Public License version 2.
11
This program is free software; you can redistribute it and/or
12
modify it under the terms of the GNU General Public License as
13
published by the Free Software Foundation; either version 2 of the
14
License, or (at your option) any later version.
16
This program is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
General Public License for more details.
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26
/****************************************************************************
27
** $Id: secqstring.h 102 2003-12-19 23:28:11Z marcus $
29
** Definition of the SecQString class, and related Unicode functions.
33
** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
35
** This file is part of the tools module of the Qt GUI Toolkit.
37
** This file may be distributed under the terms of the Q Public License
38
** as defined by Trolltech AS of Norway and appearing in the file
39
** LICENSE.QPL included in the packaging of this file.
41
** This file may be distributed and/or modified under the terms of the
42
** GNU General Public License version 2 as published by the Free Software
43
** Foundation and appearing in the file LICENSE.GPL included in the
44
** packaging of this file.
46
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
47
** licenses may use this file in accordance with the Qt Commercial License
48
** Agreement provided with the Software.
50
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
51
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
53
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
54
** information about Qt Commercial License Agreements.
55
** See http://www.trolltech.com/qpl/ for QPL licensing information.
56
** See http://www.trolltech.com/gpl/ for GPL licensing information.
58
** Contact info@trolltech.com if any conditions of this licensing are
61
**********************************************************************/
71
/* We need the original qchar and qstring for transparent conversion
72
from QChar to QChar and QString to SecQString (but not the other
81
/*****************************************************************************
83
*****************************************************************************/
87
template <class T> class QDeepCopy;
90
struct Q_EXPORT SecQStringData : public QShared {
92
QShared(), unicode(0), len(0), maxl(0) { ref(); }
93
SecQStringData(QChar *u, uint l, uint m) :
94
QShared(), unicode(u), len(l), maxl(m) { }
95
~SecQStringData() { if ( unicode ) ::secmem_free ((char*) unicode); }
112
class Q_EXPORT SecQString
115
SecQString(); // make null string
116
SecQString( QChar ); // one-char string
117
SecQString( const SecQString & ); // impl-shared copy
118
/* We need a way to convert a QString to a SecQString ("importing"
119
it). Having no conversion for the other way prevents
120
accidential bugs where the secure string is copied to insecure
122
SecQString( const QString & ); // deep copy
123
SecQString( const QChar* unicode, uint length ); // deep copy
126
SecQString &operator=( const SecQString & ); // impl-shared copy
128
QT_STATIC_CONST SecQString null;
131
bool isEmpty() const;
133
void truncate( uint pos );
135
SecQString left( uint len ) const;
136
SecQString right( uint len ) const;
137
SecQString mid( uint index, uint len=0xffffffff) const;
140
SecQString &insert( uint index, const SecQString & );
141
SecQString &insert( uint index, const QChar*, uint len );
142
SecQString &remove( uint index, uint len );
143
SecQString &replace( uint index, uint len, const SecQString & );
144
SecQString &replace( uint index, uint len, const QChar*, uint clen );
146
SecQString &operator+=( const SecQString &str );
148
QChar at( uint i ) const
149
{ return i < d->len ? d->unicode[i] : QChar::null; }
150
QChar operator[]( int i ) const { return at((uint)i); }
151
SecQCharRef at( uint i );
152
SecQCharRef operator[]( int i );
154
QChar constref(uint i) const
157
{ // Optimized for easy-inlining by simple compilers.
158
if ( d->count != 1 || i >= d->len )
160
return d->unicode[i];
163
const QChar* unicode() const { return d->unicode; }
167
void setLength( uint newLength );
169
bool isRightToLeft() const;
173
SecQString( int size, bool /* dummy */ ); // allocate size incl. \0
179
void grow( uint newLength );
182
static SecQStringData* shared_null;
183
static SecQStringData* makeSharedNull();
185
friend class SecQConstString;
186
friend class QTextStream;
187
SecQString( SecQStringData* dd, bool /* dummy */ ) : d(dd) { }
189
// needed for QDeepCopy
191
friend class QDeepCopy<SecQString>;
194
class Q_EXPORT SecQCharRef {
195
friend class SecQString;
198
SecQCharRef(SecQString* str, uint pos) : s(*str), p(pos) { }
201
// most QChar operations repeated here
203
// all this is not documented: We just say "like QChar" and let it be.
205
ushort unicode() const { return s.constref(p).unicode(); }
207
// An operator= for each QChar cast constructors
208
SecQCharRef operator=(char c ) { s.ref(p)=c; return *this; }
209
SecQCharRef operator=(uchar c ) { s.ref(p)=c; return *this; }
210
SecQCharRef operator=(QChar c ) { s.ref(p)=c; return *this; }
211
SecQCharRef operator=(const SecQCharRef& c ) { s.ref(p)=c.unicode(); return *this; }
212
SecQCharRef operator=(ushort rc ) { s.ref(p)=rc; return *this; }
213
SecQCharRef operator=(short rc ) { s.ref(p)=rc; return *this; }
214
SecQCharRef operator=(uint rc ) { s.ref(p)=rc; return *this; }
215
SecQCharRef operator=(int rc ) { s.ref(p)=rc; return *this; }
217
operator QChar () const { return s.constref(p); }
220
bool isNull() const { return unicode()==0; }
221
bool isPrint() const { return s.constref(p).isPrint(); }
222
bool isPunct() const { return s.constref(p).isPunct(); }
223
bool isSpace() const { return s.constref(p).isSpace(); }
224
bool isMark() const { return s.constref(p).isMark(); }
225
bool isLetter() const { return s.constref(p).isLetter(); }
226
bool isNumber() const { return s.constref(p).isNumber(); }
227
bool isLetterOrNumber() { return s.constref(p).isLetterOrNumber(); }
228
bool isDigit() const { return s.constref(p).isDigit(); }
230
int digitValue() const { return s.constref(p).digitValue(); }
231
QChar lower() const { return s.constref(p).lower(); }
232
QChar upper() const { return s.constref(p).upper(); }
234
QChar::Category category() const { return s.constref(p).category(); }
235
QChar::Direction direction() const { return s.constref(p).direction(); }
236
QChar::Joining joining() const { return s.constref(p).joining(); }
237
bool mirrored() const { return s.constref(p).mirrored(); }
238
QChar mirroredChar() const { return s.constref(p).mirroredChar(); }
239
// const SecQString &decomposition() const { return s.constref(p).decomposition(); }
240
QChar::Decomposition decompositionTag() const { return s.constref(p).decompositionTag(); }
241
unsigned char combiningClass() const { return s.constref(p).combiningClass(); }
243
// Not the non-const ones of these.
244
uchar cell() const { return s.constref(p).cell(); }
245
uchar row() const { return s.constref(p).row(); }
249
inline SecQCharRef SecQString::at( uint i ) { return SecQCharRef(this,i); }
250
inline SecQCharRef SecQString::operator[]( int i ) { return at((uint)i); }
252
class Q_EXPORT SecQConstString : private SecQString {
254
SecQConstString( const QChar* unicode, uint length );
256
const SecQString& string() const { return *this; }
260
/*****************************************************************************
261
SecQString inline functions
262
*****************************************************************************/
264
// These two move code into makeSharedNull() and deletesData()
265
// to improve cache-coherence (and reduce code bloat), while
266
// keeping the common cases fast.
268
// No safe way to pre-init shared_null on ALL compilers/linkers.
269
inline SecQString::SecQString() :
270
d(shared_null ? shared_null : makeSharedNull())
275
inline SecQString::~SecQString()
278
if ( d != shared_null )
283
// needed for QDeepCopy
284
inline void SecQString::detach()
287
inline bool SecQString::isNull() const
288
{ return unicode() == 0; }
290
inline uint SecQString::length() const
293
inline bool SecQString::isEmpty() const
294
{ return length() == 0; }
296
/*****************************************************************************
297
SecQString non-member operators
298
*****************************************************************************/
300
Q_EXPORT inline const SecQString operator+( const SecQString &s1, const SecQString &s2 )
302
SecQString tmp( s1 );
307
#endif // SECQSTRING_H