summaryrefslogtreecommitdiff
authorar <ar>2004-06-06 19:43:58 (UTC)
committer ar <ar>2004-06-06 19:43:58 (UTC)
commita1ef1c3fe8700a16329358288299276fd16c8fa0 (patch) (unidiff)
tree3db826a2b2df0fd407721aaefe03bfc61ffd35f0
parentd5f6057374caf1c81e02628f0fcca01394b1dc9d (diff)
downloadopie-a1ef1c3fe8700a16329358288299276fd16c8fa0.zip
opie-a1ef1c3fe8700a16329358288299276fd16c8fa0.tar.gz
opie-a1ef1c3fe8700a16329358288299276fd16c8fa0.tar.bz2
- solve problems with padded memory - dont use memcpy
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--library/quuid.cpp133
-rw-r--r--library/quuid.h147
2 files changed, 157 insertions, 123 deletions
diff --git a/library/quuid.cpp b/library/quuid.cpp
index 46fd816..36469a8 100644
--- a/library/quuid.cpp
+++ b/library/quuid.cpp
@@ -42,105 +42,186 @@
42 Example: 42 Example:
43 \code 43 \code
44 // {67C8770B-44F1-410A-AB9A-F9B5446F13EE} 44 // {67C8770B-44F1-410A-AB9A-F9B5446F13EE}
45 QUuid IID_MyInterface( 0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee ) 45 QUuid IID_MyInterface( 0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee )
46 \endcode 46 \endcode
47*/ 47*/
48 48
49/*! 49/*!
50 \fn QUuid::QUuid( const QUuid &orig ) 50 \fn QUuid::QUuid( const QUuid &orig )
51 51
52 Creates a copy of the QUuid \a orig. 52 Creates a copy of the QUuid \a orig.
53*/ 53*/
54
54#ifndef QT_NO_QUUID_STRING 55#ifndef QT_NO_QUUID_STRING
56
55/*! 57/*!
56 Creates a QUuid object from the string \a text. Right now, the function 58 Creates a QUuid object from the string \a text. Right now, the function
57 can only convert the format {12345678-1234-1234-1234-123456789ABC} and 59 can only convert the format {12345678-1234-1234-1234-123456789ABC} and
58 will create the null UUID when the conversion fails. 60 will create the null UUID when the conversion fails.
59*/ 61*/
60QUuid::QUuid( const QString &text ) 62QUuid::QUuid( const QString &text )
61{ 63{
62 bool ok; 64 bool ok;
63 QString temp = text.upper(); 65 QString temp = text.upper();
64 66
65 data1 = temp.mid( 1, 8 ).toULong( &ok, 16 ); 67 data1 = temp.mid( 1, 8 ).toULong( &ok, 16 );
66 if ( !ok ) { 68 if ( !ok )
67 *this = QUuid(); 69 {
68 return; 70 *this = QUuid();
71 return;
69 } 72 }
70 73
71 data2 = temp.mid( 10, 4 ).toUInt( &ok, 16 ); 74 data2 = temp.mid( 10, 4 ).toUInt( &ok, 16 );
72 if ( !ok ) { 75 if ( !ok )
73 *this = QUuid(); 76 {
74 return; 77 *this = QUuid();
78 return;
75 } 79 }
76 data3 = temp.mid( 15, 4 ).toUInt( &ok, 16 ); 80 data3 = temp.mid( 15, 4 ).toUInt( &ok, 16 );
77 if ( !ok ) { 81 if ( !ok )
78 *this = QUuid(); 82 {
79 return; 83 *this = QUuid();
84 return;
80 } 85 }
81 data4[0] = temp.mid( 20, 2 ).toUInt( &ok, 16 ); 86 data4[0] = temp.mid( 20, 2 ).toUInt( &ok, 16 );
82 if ( !ok ) { 87 if ( !ok )
83 *this = QUuid(); 88 {
84 return; 89 *this = QUuid();
90 return;
85 } 91 }
86 data4[1] = temp.mid( 22, 2 ).toUInt( &ok, 16 ); 92 data4[1] = temp.mid( 22, 2 ).toUInt( &ok, 16 );
87 if ( !ok ) { 93 if ( !ok )
88 *this = QUuid(); 94 {
89 return; 95 *this = QUuid();
96 return;
90 } 97 }
91 for ( int i = 2; i<8; i++ ) { 98 for ( int i = 2; i<8; i++ )
92 data4[i] = temp.mid( 25 + (i-2)*2, 2 ).toUShort( &ok, 16 ); 99 {
93 if ( !ok ) { 100 data4[i] = temp.mid( 25 + (i-2)*2, 2 ).toUShort( &ok, 16 );
94 *this = QUuid(); 101 if ( !ok )
95 return; 102 {
96 } 103 *this = QUuid();
104 return;
105 }
97 } 106 }
98} 107}
99#endif 108
100/*! 109/*!
101 \fn QUuid QUuid::operator=(const QUuid &uuid ) 110 \fn QUuid QUuid::operator=(const QUuid &uuid )
102 111
103 Assigns the value of \a uuid to this QUuid object. 112 Assigns the value of \a uuid to this QUuid object.
104*/ 113*/
105 114
106/*! 115/*!
107 \fn bool QUuid::operator==(const QUuid &other) const 116 \fn bool QUuid::operator==(const QUuid &other) const
108 117
109 Returns TRUE if this QUuid and the \a other QUuid are identical, otherwise returns FALSE. 118 Returns TRUE if this QUuid and the \a other QUuid are identical, otherwise returns FALSE.
110*/ 119*/
111 120
112/*! 121/*!
113 \fn bool QUuid::operator!=(const QUuid &other) const 122 \fn bool QUuid::operator!=(const QUuid &other) const
114 123
115 Returns TRUE if this QUuid and the \a other QUuid are different, otherwise returns FALSE. 124 Returns TRUE if this QUuid and the \a other QUuid are different, otherwise returns FALSE.
116*/ 125*/
117#ifndef QT_NO_QUUID_STRING 126
118/*! 127/*!
119 QString QUuid::toString() const 128 QString QUuid::toString() const
120 129
121 Returns a string in {12345678-1234-1234-1234-123456789ABC} format. 130 Returns a string in {12345678-1234-1234-1234-123456789ABC} format.
122*/ 131*/
123QString QUuid::toString() const 132QString QUuid::toString() const
124{ 133{
125 QString result; 134 QString result;
126 135
127 result = "{" + QString::number( data1, 16 ).rightJustify( 8, '0' ) + "-"; 136 result = "{" + QString::number( data1, 16 ).rightJustify( 8, '0' ) + "-";
128 result += QString::number( (int)data2, 16 ).rightJustify( 4, '0' ) + "-"; 137 result += QString::number( (int)data2, 16 ).rightJustify( 4, '0' ) + "-";
129 result += QString::number( (int)data3, 16 ).rightJustify( 4, '0' ) + "-"; 138 result += QString::number( (int)data3, 16 ).rightJustify( 4, '0' ) + "-";
130 result += QString::number( (int)data4[0], 16 ).rightJustify( 2, '0' ); 139 result += QString::number( (int)data4[0], 16 ).rightJustify( 2, '0' );
131 result += QString::number( (int)data4[1], 16 ).rightJustify( 2, '0' ) + "-"; 140 result += QString::number( (int)data4[1], 16 ).rightJustify( 2, '0' ) + "-";
132 for ( int i = 2; i < 8; i++ ) 141 for ( int i = 2; i < 8; i++ )
133 result += QString::number( (int)data4[i], 16 ).rightJustify( 2, '0' ); 142 result += QString::number( (int)data4[i], 16 ).rightJustify( 2, '0' );
134 143
135 return result + "}"; 144 return result + "}";
136} 145}
137#endif 146#endif
147
138/*! 148/*!
139 Returns TRUE if this is the null UUID {00000000-0000-0000-0000-000000000000}, otherwise returns FALSE. 149 Returns TRUE if this is the null UUID {00000000-0000-0000-0000-000000000000}, otherwise returns FALSE.
140*/ 150*/
141bool QUuid::isNull() const 151bool QUuid::isNull() const
142{ 152{
143 return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && 153 return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 &&
144 data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && 154 data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 &&
145 data1 == 0 && data2 == 0 && data3 == 0; 155 data1 == 0 && data2 == 0 && data3 == 0;
156}
157
158
159/*!
160 \fn QUuid::Variant QUuid::variant() const
161
162 Returns the variant of the UUID.
163 The null UUID is considered to be of an unknown variant.
164
165 \sa version()
166*/
167QUuid::Variant QUuid::variant() const
168{
169 if ( isNull() )
170 return VarUnknown;
171 // Check the 3 MSB of data4[0]
172 if ( (data4[0] & 0x80) == 0x00 ) return NCS;
173 else if ( (data4[0] & 0xC0) == 0x80 ) return DCE;
174 else if ( (data4[0] & 0xE0) == 0xC0 ) return Microsoft;
175 else if ( (data4[0] & 0xE0) == 0xE0 ) return Reserved;
176 return VarUnknown;
177}
178
179/*!
180 \fn bool QUuid::operator<(const QUuid &other) const
181
182 Returns TRUE if this QUuid is of the same variant,
183 and lexicographically before the \a other QUuid;
184 otherwise returns FALSE.
185
186 \sa variant()
187*/
188#define ISLESS(f1, f2) if (f1!=f2) return (f1<f2);
189bool QUuid::operator<(const QUuid &other ) const
190{
191 if ( variant() != other.variant() )
192 return FALSE;
193
194 ISLESS( data1, other.data1 );
195 ISLESS( data2, other.data2 );
196 ISLESS( data3, other.data3 );
197 for ( int n = 0; n < 8; n++ )
198 {
199 ISLESS( data4[n], other.data4[n] );
200 }
201 return FALSE;
202}
203
204/*!
205 \fn bool QUuid::operator>(const QUuid &other) const
206
207 Returns TRUE if this QUuid is of the same variant,
208 and lexicographically after the \a other QUuid;
209 otherwise returns FALSE.
210
211 \sa variant()
212*/
213#define ISMORE(f1, f2) if (f1!=f2) return (f1>f2);
214bool QUuid::operator>(const QUuid &other ) const
215{
216 if ( variant() != other.variant() )
217 return FALSE;
218
219 ISMORE( data1, other.data1 );
220 ISMORE( data2, other.data2 );
221 ISMORE( data3, other.data3 );
222 for ( int n = 0; n < 8; n++ )
223 {
224 ISMORE( data4[n], other.data4[n] );
225 }
226 return FALSE;
146} 227}
diff --git a/library/quuid.h b/library/quuid.h
index 841a00f..1575711 100644
--- a/library/quuid.h
+++ b/library/quuid.h
@@ -16,148 +16,101 @@
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#ifndef QUUID_H 21#ifndef QUUID_H
22#define QUUID_H 22#define QUUID_H
23 23
24#ifndef QT_H 24#ifndef QT_H
25#include <qstring.h> 25#include <qstring.h>
26#endif // QT_H 26#endif // QT_H
27 27
28#include <memory.h>
29
30#if defined(Q_OS_WIN32)
31#ifndef GUID_DEFINED
32#define GUID_DEFINED
33typedef struct _GUID
34{
35 ulong Data1;
36 ushort Data2;
37 ushort Data3;
38 uchar Data4[ 8 ];
39} GUID;
40#endif
41#endif
42
43#if defined( Q_WS_QWS ) && !defined( UUID_H_INCLUDED )
44typedef unsigned char uuid_t[16];
45#endif
46
47struct Q_EXPORT QUuid 28struct Q_EXPORT QUuid
48{ 29{
30 enum Variant {
31 VarUnknown =-1,
32 NCS = 0, // 0 - -
33 DCE = 2, // 1 0 -
34 Microsoft = 6, // 1 1 0
35 Reserved = 7 // 1 1 1
36 };
37
49 QUuid() 38 QUuid()
50 { 39 {
51 memset( this, 0, sizeof(QUuid) ); 40 memset( this, 0, sizeof(QUuid) );
52 } 41 }
53 QUuid( uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8 ) 42 QUuid( uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8 )
54 { 43 {
55 data1 = l; 44 data1 = l;
56 data2 = w1; 45 data2 = w1;
57 data3 = w2; 46 data3 = w2;
58 data4[0] = b1; 47 data4[0] = b1;
59 data4[1] = b2; 48 data4[1] = b2;
60 data4[2] = b3; 49 data4[2] = b3;
61 data4[3] = b4; 50 data4[3] = b4;
62 data4[4] = b5; 51 data4[4] = b5;
63 data4[5] = b6; 52 data4[5] = b6;
64 data4[6] = b7; 53 data4[6] = b7;
65 data4[7] = b8; 54 data4[7] = b8;
66 } 55 }
67 QUuid( const QUuid &uuid ) 56 QUuid( const QUuid &uuid )
68 { 57 {
69 memcpy( this, &uuid, sizeof(QUuid) ); 58 uint i;
59
60 data1 = uuid.data1;
61 data2 = uuid.data2;
62 data3 = uuid.data3;
63 for( i = 0; i < 8; i++ )
64 data4[ i ] = uuid.data4[ i ];
70 } 65 }
66
71#ifndef QT_NO_QUUID_STRING 67#ifndef QT_NO_QUUID_STRING
72 QUuid( const QString & ); 68 QUuid( const QString & );
73 QString toString() const; 69 QString toString() const;
74#endif 70#endif
75 bool isNull() const; 71 bool isNull() const;
76 72
77 QUuid &operator=(const QUuid &orig ) 73 QUuid &operator=(const QUuid &orig )
78 { 74 {
79 memcpy( this, &orig, sizeof(QUuid) ); 75 uint i;
80 return *this;
81 }
82 76
83 bool operator==(const QUuid &orig ) const 77 data1 = orig.data1;
84 { 78 data2 = orig.data2;
85 return !memcmp( this, &orig, sizeof(QUuid) ); 79 data3 = orig.data3;
86 } 80 for( i = 0; i < 8; i++ )
81 data4[ i ] = orig.data4[ i ];
87 82
88 bool operator!=(const QUuid &orig ) const 83 return *this;
89 {
90 return !( *this == orig );
91 } 84 }
92 85
93 inline bool operator<(const QUuid &orig) const 86 bool operator==(const QUuid &orig ) const
94 {
95 return ( memcmp(this, &orig, sizeof(QUuid)) < 0);
96 }
97
98 inline bool operator>(const QUuid &orig) const
99 {
100 return ( memcmp(this, &orig, sizeof(QUuid) ) > 0);
101 }
102
103#if defined(Q_OS_WIN32)
104 // On Windows we have a type GUID that is used by the platform API, so we
105 // provide convenience operators to cast from and to this type.
106 QUuid( const GUID &guid )
107 { 87 {
108 memcpy( this, &guid, sizeof(GUID) ); 88 uint i;
109 } 89 if ( data1 != orig.data1 || data2 != orig.data2 ||
90 data3 != orig.data3 )
91 return FALSE;
110 92
111 QUuid &operator=(const GUID &orig ) 93 for( i = 0; i < 8; i++ )
112 { 94 if ( data4[i] != orig.data4[i] )
113 memcpy( this, &orig, sizeof(QUuid) ); 95 return FALSE;
114 return *this;
115 }
116 96
117 operator GUID() const 97 return TRUE;
118 {
119 GUID guid = { data1, data2, data3, { data4[0], data4[1], data4[2], data4[3], data4[4], data4[5], data4[6], data4[7] } };
120 return guid;
121 } 98 }
122 99
123 bool operator==( const GUID &guid ) const 100 bool operator!=(const QUuid &orig ) const
124 { 101 {
125 return !memcmp( this, &guid, sizeof(QUuid) ); 102 return !( *this == orig );
126 } 103 }
127 104
128 bool operator!=( const GUID &guid ) const 105 bool operator<(const QUuid &other ) const;
129 { 106 bool operator>(const QUuid &other ) const;
130 return !( *this == guid ); 107
131 } 108 QUuid::Variant variant() const;
132
133 inline bool operator<(const QUuid &orig) const
134 {
135 return ( memcmp(this, &orig, sizeof(QUuid) ) < 0);
136 }
137
138 inline bool operator>(const QUuid &orig) const
139 {
140 return ( memcmp(this, &orig, sizeof(QUuid) ) > 0);
141 }
142
143#endif
144#if defined (Q_WS_QWS)
145 QUuid( uuid_t uuid )
146 {
147 memcpy( this, uuid, sizeof(uuid_t) );
148 }
149 109
150 QUuid &operator=(const uuid_t &orig )
151 {
152 memcpy( this, &orig, sizeof(uuid_t) );
153 return *this;
154 }
155#endif
156
157 ulong data1; 110 ulong data1;
158 ushort data2; 111 ushort data2;
159 ushort data3; 112 ushort data3;
160 uchar data4[ 8 ]; 113 uchar data4[ 8 ];
161}; 114};
162 115
163#endif //QUUID_H 116#endif //QUUID_H