1 a.dunfey 1.8.2.1 //%2006////////////////////////////////////////////////////////////////////////
|
2 jim.wunderlich 1.1 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 a.dunfey 1.8.2.1 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 jim.wunderlich 1.1 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Michael E. Brasher
33 //
34 jim.wunderlich 1.1 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef _Pegasus_Common_Packer_h
37 #define _Pegasus_Common_Packer_h
38
|
39 jim.wunderlich 1.8 #include <Pegasus/Common/PegasusAssert.h>
|
40 jim.wunderlich 1.1 #include <Pegasus/Common/Config.h>
|
41 david.dillard 1.2 #include <Pegasus/Common/ArrayInternal.h>
|
42 jim.wunderlich 1.1 #include <Pegasus/Common/String.h>
|
43 mike 1.3 #include <Pegasus/Common/Buffer.h>
|
44 jim.wunderlich 1.6 #include <Pegasus/Common/PegasusAssert.h>
|
45 jim.wunderlich 1.1
46 PEGASUS_NAMESPACE_BEGIN
47
48 /** This class packs the basic CIM data types into an array of characters.
49 The elements are packed in a binary big-endian format. This class also
50 provides unpacking functions as well.
51
52 <p>
53 This class is for internal use only.
54 */
55 class PEGASUS_COMMON_LINKAGE Packer
56 {
57 public:
58
|
59 mike 1.3 static void packBoolean(Buffer& out, Boolean x);
60 static void packUint8(Buffer& out, Uint8 x);
61 static void packUint16(Buffer& out, Uint16 x);
62 static void packUint32(Buffer& out, Uint32 x);
63 static void packUint64(Buffer& out, Uint64 x);
64 static void packReal32(Buffer& out, Real32 x);
65 static void packReal64(Buffer& out, Real64 x);
66 static void packChar16(Buffer& out, Char16 x);
67 static void packString(Buffer& out, const String& x);
68 static void packSize(Buffer& out, Uint32 x);
69
70 static void packBoolean(Buffer& out, const Boolean* x, Uint32 n);
71 static void packUint8(Buffer& out, const Uint8* x, Uint32 n);
72 static void packUint16(Buffer& out, const Uint16* x, Uint32 n);
73 static void packUint32(Buffer& out, const Uint32* x, Uint32 n);
74 static void packUint64(Buffer& out, const Uint64* x, Uint32 n);
75 static void packReal32(Buffer& out, const Real32* x, Uint32 n);
76 static void packReal64(Buffer& out, const Real64* x, Uint32 n);
77 static void packChar16(Buffer& out, const Char16* x, Uint32 n);
78 static void packString(Buffer& out, const String* x, Uint32 n);
79
80 mike 1.3 static void unpackBoolean(const Buffer& in, Uint32& pos, Boolean& x);
81 static void unpackUint8(const Buffer& in, Uint32& pos, Uint8& x);
82 static void unpackUint16(const Buffer& in, Uint32& pos, Uint16& x);
83 static void unpackUint32(const Buffer& in, Uint32& pos, Uint32& x);
84 static void unpackUint64(const Buffer& in, Uint32& pos, Uint64& x);
85 static void unpackReal32(const Buffer& in, Uint32& pos, Real32& x);
86 static void unpackReal64(const Buffer& in, Uint32& pos, Real64& x);
87 static void unpackChar16(const Buffer& in, Uint32& pos, Char16& x);
88 static void unpackString(const Buffer& in, Uint32& pos, String& x);
89 static void unpackSize(const Buffer& out, Uint32& pos, Uint32& x);
|
90 jim.wunderlich 1.1
91 static void unpackBoolean(
|
92 mike 1.3 Buffer& in, Uint32& pos, Boolean* x, Uint32 n);
|
93 jim.wunderlich 1.1 static void unpackUint8(
|
94 mike 1.3 Buffer& in, Uint32& pos, Uint8* x, Uint32 n);
|
95 jim.wunderlich 1.1 static void unpackUint16(
|
96 mike 1.3 Buffer& in, Uint32& pos, Uint16* x, Uint32 n);
|
97 jim.wunderlich 1.1 static void unpackUint32(
|
98 mike 1.3 Buffer& in, Uint32& pos, Uint32* x, Uint32 n);
|
99 jim.wunderlich 1.1 static void unpackUint64(
|
100 mike 1.3 Buffer& in, Uint32& pos, Uint64* x, Uint32 n);
|
101 jim.wunderlich 1.1 static void unpackReal32(
|
102 mike 1.3 Buffer& in, Uint32& pos, Real32* x, Uint32 n);
|
103 jim.wunderlich 1.1 static void unpackReal64(
|
104 mike 1.3 Buffer& in, Uint32& pos, Real64* x, Uint32 n);
|
105 jim.wunderlich 1.1 static void unpackChar16(
|
106 mike 1.3 Buffer& in, Uint32& pos, Char16* x, Uint32 n);
|
107 jim.wunderlich 1.1 static void unpackString(
|
108 mike 1.3 Buffer& in, Uint32& pos, String* x, Uint32 n);
|
109 jim.wunderlich 1.1
110 static bool isLittleEndian();
111
112 static Uint16 swapUint16(Uint16 x);
113 static Uint32 swapUint32(Uint32 x);
114 static Uint64 swapUint64(Uint64 x);
115
116 static Uint16 swap(Uint16 x) { return swapUint16(x); }
117 static Uint32 swap(Uint32 x) { return swapUint32(x); }
118 static Uint64 swap(Uint64 x) { return swapUint64(x); }
119
120 static void swap(Uint8& x, Uint8& y);
121 };
122
123 inline bool Packer::isLittleEndian()
124 {
125 #if defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
126 return true;
127 #elif defined(PEGASUS_PLATFORM_LINUX_X86_GNU)
128 return true;
|
129 marek 1.7 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
130 return false;
|
131 jim.wunderlich 1.1 #else
132 // You can add your platform above to avoid this calculation.
133 union U { Uint16 x; Uint8 a[2]; } u;
134 u.x = 1;
135 return u.a[0] == 1;
136 #endif
137 }
138
|
139 mike 1.3 inline void Packer::packBoolean(Buffer& out, Boolean x_)
|
140 jim.wunderlich 1.1 {
141 Uint8 x = Uint8(x_);
142 out.append((char*)&x, sizeof(x));
143 }
144
|
145 mike 1.3 inline void Packer::packUint8(Buffer& out, Uint8 x)
|
146 jim.wunderlich 1.1 {
147 out.append((char*)&x, sizeof(x));
148 }
149
|
150 mike 1.3 inline void Packer::packUint16(Buffer& out, Uint16 x)
|
151 jim.wunderlich 1.1 {
152 if (isLittleEndian())
153 x = Packer::swapUint16(x);
154
155 out.append((char*)&x, sizeof(x));
156 }
157
|
158 mike 1.3 inline void Packer::packUint32(Buffer& out, Uint32 x)
|
159 jim.wunderlich 1.1 {
160 if (isLittleEndian())
161 x = Packer::swapUint32(x);
162
163 out.append((char*)&x, sizeof(x));
164 }
165
|
166 mike 1.3 inline void Packer::packUint64(Buffer& out, Uint64 x)
|
167 jim.wunderlich 1.1 {
168 if (isLittleEndian())
169 x = Packer::swapUint64(x);
170
171 out.append((char*)&x, sizeof(x));
172 }
173
|
174 mike 1.3 inline void Packer::packReal32(Buffer& out, Real32 x)
|
175 jim.wunderlich 1.1 {
176 packUint32(out, *((Uint32*)&x));
177 }
178
|
179 mike 1.3 inline void Packer::packReal64(Buffer& out, Real64 x)
|
180 jim.wunderlich 1.1 {
181 packUint64(out, *((Uint64*)&x));
182 }
183
|
184 mike 1.3 inline void Packer::packChar16(Buffer& out, Char16 x)
|
185 jim.wunderlich 1.1 {
186 packUint16(out, Uint16(x));
187 }
188
|
189 mike 1.3 inline void Packer::packReal32(Buffer& out, const Real32* x, Uint32 n)
|
190 jim.wunderlich 1.1 {
191 Packer::packUint32(out, (const Uint32*)x, n);
192 }
193
|
194 mike 1.3 inline void Packer::packReal64(Buffer& out, const Real64* x, Uint32 n)
|
195 jim.wunderlich 1.1 {
196 Packer::packUint64(out, (const Uint64*)x, n);
197 }
198
|
199 mike 1.3 inline void Packer::packChar16(Buffer& out, const Char16* x, Uint32 n)
|
200 jim.wunderlich 1.1 {
201 Packer::packUint16(out, (const Uint16*)x, n);
202 }
203
204 inline void Packer::unpackBoolean(
|
205 mike 1.3 const Buffer& in, Uint32& pos, Boolean& x)
|
206 jim.wunderlich 1.1 {
207 x = in[pos++] ? true : false;
|
208 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
209 jim.wunderlich 1.1 }
210
211 inline void Packer::unpackUint8(
|
212 mike 1.3 const Buffer& in, Uint32& pos, Uint8& x)
|
213 jim.wunderlich 1.1 {
214 x = Uint8(in[pos++]);
|
215 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
216 jim.wunderlich 1.1 }
217
|
218 mike 1.3 inline void Packer::unpackReal32(const Buffer& in, Uint32& pos, Real32& x)
|
219 jim.wunderlich 1.1 {
220 Packer::unpackUint32(in, pos, *((Uint32*)&x));
|
221 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
222 jim.wunderlich 1.1 }
223
|
224 mike 1.3 inline void Packer::unpackReal64(const Buffer& in, Uint32& pos, Real64& x)
|
225 jim.wunderlich 1.1 {
226 Packer::unpackUint64(in, pos, *((Uint64*)&x));
|
227 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
228 jim.wunderlich 1.1 }
229
|
230 mike 1.3 inline void Packer::unpackChar16(const Buffer& in, Uint32& pos, Char16& x)
|
231 jim.wunderlich 1.1 {
232 Packer::unpackUint16(in, pos, *((Uint16*)&x));
|
233 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
234 jim.wunderlich 1.1 }
235
236 inline void Packer::unpackReal32(
|
237 mike 1.3 Buffer& in, Uint32& pos, Real32* x, Uint32 n)
|
238 jim.wunderlich 1.1 {
239 unpackUint32(in, pos, (Uint32*)x, n);
|
240 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
241 jim.wunderlich 1.1 }
242
243 inline void Packer::unpackReal64(
|
244 mike 1.3 Buffer& in, Uint32& pos, Real64* x, Uint32 n)
|
245 jim.wunderlich 1.1 {
246 unpackUint64(in, pos, (Uint64*)x, n);
|
247 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
248 jim.wunderlich 1.1 }
249
250 inline void Packer::unpackChar16(
|
251 mike 1.3 Buffer& in, Uint32& pos, Char16* x, Uint32 n)
|
252 jim.wunderlich 1.1 {
253 unpackUint16(in, pos, (Uint16*)x, n);
|
254 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
255 jim.wunderlich 1.1 }
256
257 inline Uint16 Packer::swapUint16(Uint16 x)
258 {
259 return (Uint16)(
260 (((Uint16)(x) & 0x00ffU) << 8) |
261 (((Uint16)(x) & 0xff00U) >> 8));
262 }
263
264 inline Uint32 Packer::swapUint32(Uint32 x)
265 {
266 return (Uint32)(
267 (((Uint32)(x) & 0x000000ffUL) << 24) |
268 (((Uint32)(x) & 0x0000ff00UL) << 8) |
269 (((Uint32)(x) & 0x00ff0000UL) >> 8) |
270 (((Uint32)(x) & 0xff000000UL) >> 24));
271 }
272
273 inline void Packer::swap(Uint8& x, Uint8& y)
274 {
275 Uint8 t = x;
276 jim.wunderlich 1.1 x = y;
277 y = t;
278 }
279
280 inline Uint64 Packer::swapUint64(Uint64 x)
281 {
282 union
283 {
284 Uint64 x;
285 Uint8 bytes[8];
286 }
287 u;
288
289 u.x = x;
290 Packer::swap(u.bytes[0], u.bytes[7]);
291 Packer::swap(u.bytes[1], u.bytes[6]);
292 Packer::swap(u.bytes[2], u.bytes[5]);
293 Packer::swap(u.bytes[3], u.bytes[4]);
294 return u.x;
295 }
296
297 jim.wunderlich 1.1 PEGASUS_NAMESPACE_END
298
299 #endif /* _Pegasus_Common_Packer_h */
|