(file) Return to Packer.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Repository

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2