1 karl 1.9 //%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 karl 1.9 // 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 //%/////////////////////////////////////////////////////////////////////////////
33
34 jim.wunderlich 1.1 #ifndef _Pegasus_Common_Packer_h
35 #define _Pegasus_Common_Packer_h
36
|
37 jim.wunderlich 1.8 #include <Pegasus/Common/PegasusAssert.h>
|
38 jim.wunderlich 1.1 #include <Pegasus/Common/Config.h>
|
39 david.dillard 1.2 #include <Pegasus/Common/ArrayInternal.h>
|
40 jim.wunderlich 1.1 #include <Pegasus/Common/String.h>
|
41 mike 1.3 #include <Pegasus/Common/Buffer.h>
|
42 jim.wunderlich 1.6 #include <Pegasus/Common/PegasusAssert.h>
|
43 jim.wunderlich 1.1
44 PEGASUS_NAMESPACE_BEGIN
45
46 /** This class packs the basic CIM data types into an array of characters.
47 The elements are packed in a binary big-endian format. This class also
48 provides unpacking functions as well.
49
50 <p>
51 This class is for internal use only.
52 */
|
53 mike 1.11 class Packer
|
54 jim.wunderlich 1.1 {
55 public:
56
|
57 mike 1.3 static void packBoolean(Buffer& out, Boolean x);
58 static void packUint8(Buffer& out, Uint8 x);
59 static void packUint16(Buffer& out, Uint16 x);
60 static void packUint32(Buffer& out, Uint32 x);
61 static void packUint64(Buffer& out, Uint64 x);
62 static void packReal32(Buffer& out, Real32 x);
63 static void packReal64(Buffer& out, Real64 x);
64 static void packChar16(Buffer& out, Char16 x);
65 static void packString(Buffer& out, const String& x);
66 static void packSize(Buffer& out, Uint32 x);
67
68 static void packBoolean(Buffer& out, const Boolean* x, Uint32 n);
69 static void packUint8(Buffer& out, const Uint8* x, Uint32 n);
70 static void packUint16(Buffer& out, const Uint16* x, Uint32 n);
71 static void packUint32(Buffer& out, const Uint32* x, Uint32 n);
72 static void packUint64(Buffer& out, const Uint64* x, Uint32 n);
73 static void packReal32(Buffer& out, const Real32* x, Uint32 n);
74 static void packReal64(Buffer& out, const Real64* x, Uint32 n);
75 static void packChar16(Buffer& out, const Char16* x, Uint32 n);
76 static void packString(Buffer& out, const String* x, Uint32 n);
77
78 mike 1.3 static void unpackBoolean(const Buffer& in, Uint32& pos, Boolean& x);
79 static void unpackUint8(const Buffer& in, Uint32& pos, Uint8& x);
80 static void unpackUint16(const Buffer& in, Uint32& pos, Uint16& x);
81 static void unpackUint32(const Buffer& in, Uint32& pos, Uint32& x);
82 static void unpackUint64(const Buffer& in, Uint32& pos, Uint64& x);
83 static void unpackReal32(const Buffer& in, Uint32& pos, Real32& x);
84 static void unpackReal64(const Buffer& in, Uint32& pos, Real64& x);
85 static void unpackChar16(const Buffer& in, Uint32& pos, Char16& x);
86 static void unpackString(const Buffer& in, Uint32& pos, String& x);
87 static void unpackSize(const Buffer& out, Uint32& pos, Uint32& x);
|
88 jim.wunderlich 1.1
89 static void unpackBoolean(
|
90 kumpf 1.12 Buffer& in, Uint32& pos, Boolean* x, Uint32 n);
|
91 jim.wunderlich 1.1 static void unpackUint8(
|
92 kumpf 1.12 Buffer& in, Uint32& pos, Uint8* x, Uint32 n);
|
93 jim.wunderlich 1.1 static void unpackUint16(
|
94 kumpf 1.12 Buffer& in, Uint32& pos, Uint16* x, Uint32 n);
|
95 jim.wunderlich 1.1 static void unpackUint32(
|
96 kumpf 1.12 Buffer& in, Uint32& pos, Uint32* x, Uint32 n);
|
97 jim.wunderlich 1.1 static void unpackUint64(
|
98 kumpf 1.12 Buffer& in, Uint32& pos, Uint64* x, Uint32 n);
|
99 jim.wunderlich 1.1 static void unpackReal32(
|
100 kumpf 1.12 Buffer& in, Uint32& pos, Real32* x, Uint32 n);
|
101 jim.wunderlich 1.1 static void unpackReal64(
|
102 kumpf 1.12 Buffer& in, Uint32& pos, Real64* x, Uint32 n);
|
103 jim.wunderlich 1.1 static void unpackChar16(
|
104 kumpf 1.12 Buffer& in, Uint32& pos, Char16* x, Uint32 n);
|
105 jim.wunderlich 1.1 static void unpackString(
|
106 kumpf 1.12 Buffer& in, Uint32& pos, String* x, Uint32 n);
|
107 jim.wunderlich 1.1
108 static bool isLittleEndian();
109
110 static Uint16 swapUint16(Uint16 x);
111 static Uint32 swapUint32(Uint32 x);
112 static Uint64 swapUint64(Uint64 x);
113
114 static Uint16 swap(Uint16 x) { return swapUint16(x); }
115 static Uint32 swap(Uint32 x) { return swapUint32(x); }
116 static Uint64 swap(Uint64 x) { return swapUint64(x); }
117
118 static void swap(Uint8& x, Uint8& y);
119 };
120
121 inline bool Packer::isLittleEndian()
122 {
123 #if defined(PEGASUS_PLATFORM_LINUX_IA64_GNU)
124 return true;
125 #elif defined(PEGASUS_PLATFORM_LINUX_X86_GNU)
126 return true;
|
127 marek 1.7 #elif defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
128 return false;
|
129 jim.wunderlich 1.1 #else
130 // You can add your platform above to avoid this calculation.
131 union U { Uint16 x; Uint8 a[2]; } u;
132 u.x = 1;
133 return u.a[0] == 1;
134 #endif
135 }
136
|
137 mike 1.3 inline void Packer::packBoolean(Buffer& out, Boolean x_)
|
138 jim.wunderlich 1.1 {
139 Uint8 x = Uint8(x_);
140 out.append((char*)&x, sizeof(x));
141 }
142
|
143 mike 1.3 inline void Packer::packUint8(Buffer& out, Uint8 x)
|
144 jim.wunderlich 1.1 {
145 out.append((char*)&x, sizeof(x));
146 }
147
|
148 mike 1.3 inline void Packer::packUint16(Buffer& out, Uint16 x)
|
149 jim.wunderlich 1.1 {
150 if (isLittleEndian())
|
151 kumpf 1.12 x = Packer::swapUint16(x);
|
152 jim.wunderlich 1.1
153 out.append((char*)&x, sizeof(x));
154 }
155
|
156 mike 1.3 inline void Packer::packUint32(Buffer& out, Uint32 x)
|
157 jim.wunderlich 1.1 {
158 if (isLittleEndian())
|
159 kumpf 1.12 x = Packer::swapUint32(x);
|
160 jim.wunderlich 1.1
161 out.append((char*)&x, sizeof(x));
162 }
163
|
164 mike 1.3 inline void Packer::packUint64(Buffer& out, Uint64 x)
|
165 jim.wunderlich 1.1 {
166 if (isLittleEndian())
|
167 kumpf 1.12 x = Packer::swapUint64(x);
|
168 jim.wunderlich 1.1
169 out.append((char*)&x, sizeof(x));
170 }
171
|
172 mike 1.3 inline void Packer::packReal32(Buffer& out, Real32 x)
|
173 jim.wunderlich 1.1 {
|
174 w.otsuka 1.10 packUint32(out, *(reinterpret_cast<Uint32*>(&x)));
|
175 jim.wunderlich 1.1 }
176
|
177 mike 1.3 inline void Packer::packReal64(Buffer& out, Real64 x)
|
178 jim.wunderlich 1.1 {
|
179 w.otsuka 1.10 packUint64(out, *(reinterpret_cast<Uint64*>(&x)));
|
180 jim.wunderlich 1.1 }
181
|
182 mike 1.3 inline void Packer::packChar16(Buffer& out, Char16 x)
|
183 jim.wunderlich 1.1 {
184 packUint16(out, Uint16(x));
185 }
186
|
187 mike 1.3 inline void Packer::packReal32(Buffer& out, const Real32* x, Uint32 n)
|
188 jim.wunderlich 1.1 {
189 Packer::packUint32(out, (const Uint32*)x, n);
190 }
191
|
192 mike 1.3 inline void Packer::packReal64(Buffer& out, const Real64* x, Uint32 n)
|
193 jim.wunderlich 1.1 {
194 Packer::packUint64(out, (const Uint64*)x, n);
195 }
196
|
197 mike 1.3 inline void Packer::packChar16(Buffer& out, const Char16* x, Uint32 n)
|
198 jim.wunderlich 1.1 {
199 Packer::packUint16(out, (const Uint16*)x, n);
200 }
201
202 inline void Packer::unpackBoolean(
|
203 mike 1.3 const Buffer& in, Uint32& pos, Boolean& x)
|
204 jim.wunderlich 1.1 {
205 x = in[pos++] ? true : false;
|
206 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
207 jim.wunderlich 1.1 }
208
209 inline void Packer::unpackUint8(
|
210 mike 1.3 const Buffer& in, Uint32& pos, Uint8& x)
|
211 jim.wunderlich 1.1 {
212 x = Uint8(in[pos++]);
|
213 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
214 jim.wunderlich 1.1 }
215
|
216 mike 1.3 inline void Packer::unpackReal32(const Buffer& in, Uint32& pos, Real32& x)
|
217 jim.wunderlich 1.1 {
218 Packer::unpackUint32(in, pos, *((Uint32*)&x));
|
219 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
220 jim.wunderlich 1.1 }
221
|
222 mike 1.3 inline void Packer::unpackReal64(const Buffer& in, Uint32& pos, Real64& x)
|
223 jim.wunderlich 1.1 {
224 Packer::unpackUint64(in, pos, *((Uint64*)&x));
|
225 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
226 jim.wunderlich 1.1 }
227
|
228 mike 1.3 inline void Packer::unpackChar16(const Buffer& in, Uint32& pos, Char16& x)
|
229 jim.wunderlich 1.1 {
230 Packer::unpackUint16(in, pos, *((Uint16*)&x));
|
231 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
232 jim.wunderlich 1.1 }
233
234 inline void Packer::unpackReal32(
|
235 mike 1.3 Buffer& in, Uint32& pos, Real32* x, Uint32 n)
|
236 jim.wunderlich 1.1 {
237 unpackUint32(in, pos, (Uint32*)x, n);
|
238 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
239 jim.wunderlich 1.1 }
240
241 inline void Packer::unpackReal64(
|
242 mike 1.3 Buffer& in, Uint32& pos, Real64* x, Uint32 n)
|
243 jim.wunderlich 1.1 {
244 unpackUint64(in, pos, (Uint64*)x, n);
|
245 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
246 jim.wunderlich 1.1 }
247
248 inline void Packer::unpackChar16(
|
249 mike 1.3 Buffer& in, Uint32& pos, Char16* x, Uint32 n)
|
250 jim.wunderlich 1.1 {
251 unpackUint16(in, pos, (Uint16*)x, n);
|
252 jim.wunderlich 1.6 PEGASUS_DEBUG_ASSERT(pos <= in.size());
|
253 jim.wunderlich 1.1 }
254
255 inline Uint16 Packer::swapUint16(Uint16 x)
256 {
257 return (Uint16)(
|
258 kumpf 1.12 (((Uint16)(x) & 0x00ffU) << 8) |
259 (((Uint16)(x) & 0xff00U) >> 8));
|
260 jim.wunderlich 1.1 }
261
262 inline Uint32 Packer::swapUint32(Uint32 x)
263 {
264 return (Uint32)(
|
265 kumpf 1.12 (((Uint32)(x) & 0x000000ffUL) << 24) |
266 (((Uint32)(x) & 0x0000ff00UL) << 8) |
267 (((Uint32)(x) & 0x00ff0000UL) >> 8) |
268 (((Uint32)(x) & 0xff000000UL) >> 24));
|
269 jim.wunderlich 1.1 }
270
271 inline void Packer::swap(Uint8& x, Uint8& y)
272 {
273 Uint8 t = x;
274 x = y;
275 y = t;
276 }
277
278 inline Uint64 Packer::swapUint64(Uint64 x)
279 {
280 union
281 {
|
282 kumpf 1.12 Uint64 x;
283 Uint8 bytes[8];
|
284 jim.wunderlich 1.1 }
285 u;
286
287 u.x = x;
288 Packer::swap(u.bytes[0], u.bytes[7]);
289 Packer::swap(u.bytes[1], u.bytes[6]);
290 Packer::swap(u.bytes[2], u.bytes[5]);
291 Packer::swap(u.bytes[3], u.bytes[4]);
292 return u.x;
293 }
294
295 PEGASUS_NAMESPACE_END
296
297 #endif /* _Pegasus_Common_Packer_h */
|