author | ar <ar> | 2004-06-06 19:43:58 (UTC) |
---|---|---|
committer | ar <ar> | 2004-06-06 19:43:58 (UTC) |
commit | a1ef1c3fe8700a16329358288299276fd16c8fa0 (patch) (unidiff) | |
tree | 3db826a2b2df0fd407721aaefe03bfc61ffd35f0 | |
parent | d5f6057374caf1c81e02628f0fcca01394b1dc9d (diff) | |
download | opie-a1ef1c3fe8700a16329358288299276fd16c8fa0.zip opie-a1ef1c3fe8700a16329358288299276fd16c8fa0.tar.gz opie-a1ef1c3fe8700a16329358288299276fd16c8fa0.tar.bz2 |
- solve problems with padded memory - dont use memcpy
-rw-r--r-- | library/quuid.cpp | 133 | ||||
-rw-r--r-- | library/quuid.h | 147 |
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 | |||
@@ -53,3 +53,5 @@ | |||
53 | */ | 53 | */ |
54 | |||
54 | #ifndef QT_NO_QUUID_STRING | 55 | #ifndef QT_NO_QUUID_STRING |
56 | |||
55 | /*! | 57 | /*! |
@@ -65,5 +67,6 @@ QUuid::QUuid( const QString &text ) | |||
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 | } |
@@ -71,30 +74,36 @@ QUuid::QUuid( const QString &text ) | |||
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 | /*! |
@@ -116,3 +125,3 @@ QUuid::QUuid( const QString &text ) | |||
116 | */ | 125 | */ |
117 | #ifndef QT_NO_QUUID_STRING | 126 | |
118 | /*! | 127 | /*! |
@@ -132,3 +141,3 @@ QString QUuid::toString() const | |||
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 | ||
@@ -137,2 +146,3 @@ QString QUuid::toString() const | |||
137 | #endif | 146 | #endif |
147 | |||
138 | /*! | 148 | /*! |
@@ -143,4 +153,75 @@ bool QUuid::isNull() const | |||
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 | */ | ||
167 | QUuid::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); | ||
189 | bool 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); | ||
214 | bool 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 | |||
@@ -27,26 +27,15 @@ | |||
27 | 27 | ||
28 | #include <memory.h> | ||
29 | |||
30 | #if defined(Q_OS_WIN32) | ||
31 | #ifndef GUID_DEFINED | ||
32 | #define GUID_DEFINED | ||
33 | typedef 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 ) | ||
44 | typedef unsigned char uuid_t[16]; | ||
45 | #endif | ||
46 | |||
47 | struct Q_EXPORT QUuid | 28 | struct 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 | } |
@@ -54,13 +43,13 @@ struct Q_EXPORT QUuid | |||
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 | } |
@@ -68,4 +57,11 @@ struct Q_EXPORT QUuid | |||
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 |
@@ -78,80 +74,37 @@ struct Q_EXPORT QUuid | |||
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; |