~x2go/x2go/pinentry-x2go_master

« back to all changes in this revision

Viewing changes to qt/secqstring.h

  • Committer: Mike Gabriel
  • Date: 2012-06-13 12:55:37 UTC
  • Revision ID: git-v1:d2060291d5cc7beb92f78168e48ececfe765d552
Strip code project down to its essentials, remove a lot of unneeded cruft. / Make code tree fully build with autotools, see README file for further info.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* secqstring.h - Secure version of QString.
2
 
   Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
3
 
   Copyright (C) 2003 g10 Code GmbH
4
 
 
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.
10
 
 
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.
15
 
 
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.
20
 
 
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
24
 
   02111-1307, USA  */
25
 
 
26
 
/****************************************************************************
27
 
** $Id: secqstring.h 102 2003-12-19 23:28:11Z marcus $
28
 
**
29
 
** Definition of the SecQString class, and related Unicode functions.
30
 
**
31
 
** Created : 920609
32
 
**
33
 
** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
34
 
**
35
 
** This file is part of the tools module of the Qt GUI Toolkit.
36
 
**
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.
40
 
**
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.
45
 
**
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.
49
 
**
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.
52
 
**
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.
57
 
**
58
 
** Contact info@trolltech.com if any conditions of this licensing are
59
 
** not clear to you.
60
 
**
61
 
**********************************************************************/
62
 
 
63
 
#ifndef SECQSTRING_H
64
 
#define SECQSTRING_H
65
 
 
66
 
extern "C"
67
 
{
68
 
#include "memory.h"
69
 
}
70
 
 
71
 
/* We need the original qchar and qstring for transparent conversion
72
 
   from QChar to QChar and QString to SecQString (but not the other
73
 
   way round).  */
74
 
#include <qstring.h>
75
 
 
76
 
#ifndef QT_H
77
 
#include "qcstring.h"
78
 
#endif // QT_H
79
 
 
80
 
 
81
 
/*****************************************************************************
82
 
  SecQString class
83
 
 *****************************************************************************/
84
 
 
85
 
class SecQString;
86
 
class SecQCharRef;
87
 
template <class T> class QDeepCopy;
88
 
#include <stdio.h>
89
 
// internal
90
 
struct Q_EXPORT SecQStringData : public QShared {
91
 
    SecQStringData() :
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); }
96
 
 
97
 
    void deleteSelf();
98
 
    QChar *unicode;
99
 
#ifdef Q_OS_MAC9
100
 
    uint len;
101
 
#else
102
 
    uint len : 30;
103
 
#endif
104
 
#ifdef Q_OS_MAC9
105
 
    uint maxl;
106
 
#else
107
 
    uint maxl : 30;
108
 
#endif
109
 
};
110
 
 
111
 
 
112
 
class Q_EXPORT SecQString
113
 
{
114
 
public:
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
121
 
       memory.  */
122
 
    SecQString( const QString & ); // deep copy
123
 
    SecQString( const QChar* unicode, uint length ); // deep copy
124
 
    ~SecQString();
125
 
 
126
 
    SecQString    &operator=( const SecQString & );   // impl-shared copy
127
 
 
128
 
    QT_STATIC_CONST SecQString null;
129
 
 
130
 
    bool        isNull()        const;
131
 
    bool        isEmpty()       const;
132
 
    uint        length()        const;
133
 
    void        truncate( uint pos );
134
 
 
135
 
    SecQString     left( uint len )  const;
136
 
    SecQString     right( uint len ) const;
137
 
    SecQString     mid( uint index, uint len=0xffffffff) const;
138
 
 
139
 
 
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 );
145
 
 
146
 
    SecQString    &operator+=( const SecQString &str );
147
 
 
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 );
153
 
 
154
 
    QChar constref(uint i) const
155
 
        { return at(i); }
156
 
    QChar& ref(uint i)
157
 
        { // Optimized for easy-inlining by simple compilers.
158
 
            if ( d->count != 1 || i >= d->len )
159
 
                subat( i );
160
 
            return d->unicode[i];
161
 
        }
162
 
 
163
 
    const QChar* unicode() const { return d->unicode; }
164
 
 
165
 
    uchar* utf8() const;
166
 
 
167
 
    void setLength( uint newLength );
168
 
 
169
 
    bool isRightToLeft() const;
170
 
 
171
 
 
172
 
private:
173
 
    SecQString( int size, bool /* dummy */ );   // allocate size incl. \0
174
 
 
175
 
    void deref();
176
 
    void real_detach();
177
 
    void subat( uint );
178
 
 
179
 
    void grow( uint newLength );
180
 
 
181
 
    SecQStringData *d;
182
 
    static SecQStringData* shared_null;
183
 
    static SecQStringData* makeSharedNull();
184
 
 
185
 
    friend class SecQConstString;
186
 
    friend class QTextStream;
187
 
    SecQString( SecQStringData* dd, bool /* dummy */ ) : d(dd) { }
188
 
 
189
 
    // needed for QDeepCopy
190
 
    void detach();
191
 
    friend class QDeepCopy<SecQString>;
192
 
};
193
 
 
194
 
class Q_EXPORT SecQCharRef {
195
 
    friend class SecQString;
196
 
    SecQString& s;
197
 
    uint p;
198
 
    SecQCharRef(SecQString* str, uint pos) : s(*str), p(pos) { }
199
 
 
200
 
public:
201
 
    // most QChar operations repeated here
202
 
 
203
 
    // all this is not documented: We just say "like QChar" and let it be.
204
 
#ifndef Q_QDOC
205
 
    ushort unicode() const { return s.constref(p).unicode(); }
206
 
 
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; }
216
 
 
217
 
    operator QChar () const { return s.constref(p); }
218
 
 
219
 
    // each function...
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(); }
229
 
 
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(); }
233
 
 
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(); }
242
 
 
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(); }
246
 
#endif
247
 
};
248
 
 
249
 
inline SecQCharRef SecQString::at( uint i ) { return SecQCharRef(this,i); }
250
 
inline SecQCharRef SecQString::operator[]( int i ) { return at((uint)i); }
251
 
 
252
 
class Q_EXPORT SecQConstString : private SecQString {
253
 
public:
254
 
    SecQConstString( const QChar* unicode, uint length );
255
 
    ~SecQConstString();
256
 
    const SecQString& string() const { return *this; }
257
 
};
258
 
 
259
 
 
260
 
/*****************************************************************************
261
 
  SecQString inline functions
262
 
 *****************************************************************************/
263
 
 
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.
267
 
//
268
 
// No safe way to pre-init shared_null on ALL compilers/linkers.
269
 
inline SecQString::SecQString() :
270
 
    d(shared_null ? shared_null : makeSharedNull())
271
 
{
272
 
    d->ref();
273
 
}
274
 
//
275
 
inline SecQString::~SecQString()
276
 
{
277
 
    if ( d->deref() ) {
278
 
        if ( d != shared_null )
279
 
            d->deleteSelf();
280
 
    }
281
 
}
282
 
 
283
 
// needed for QDeepCopy
284
 
inline void SecQString::detach()
285
 
{ real_detach(); }
286
 
 
287
 
inline bool SecQString::isNull() const
288
 
{ return unicode() == 0; }
289
 
 
290
 
inline uint SecQString::length() const
291
 
{ return d->len; }
292
 
 
293
 
inline bool SecQString::isEmpty() const
294
 
{ return length() == 0; }
295
 
 
296
 
/*****************************************************************************
297
 
  SecQString non-member operators
298
 
 *****************************************************************************/
299
 
 
300
 
Q_EXPORT inline const SecQString operator+( const SecQString &s1, const SecQString &s2 )
301
 
{
302
 
    SecQString tmp( s1 );
303
 
    tmp += s2;
304
 
    return tmp;
305
 
}
306
 
 
307
 
#endif // SECQSTRING_H