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

  1 jim.wunderlich 1.1 //%2005////////////////////////////////////////////////////////////////////////
  2                    //
  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                    //
 12                    // Permission is hereby granted, free of charge, to any person obtaining a copy
 13                    // of this software and associated documentation files (the "Software"), to
 14                    // deal in the Software without restriction, including without limitation the
 15                    // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16                    // sell copies of the Software, and to permit persons to whom the Software is
 17                    // furnished to do so, subject to the following conditions:
 18                    // 
 19                    // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20                    // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21                    // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 jim.wunderlich 1.1 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23                    // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24                    // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25                    // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26                    // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27                    //
 28                    //==============================================================================
 29                    //
 30                    // Author: Michael E. Brasher
 31                    //
 32                    //%/////////////////////////////////////////////////////////////////////////////
 33                    
 34                    #ifndef _Pegasus_Common_Packer_h
 35                    #define _Pegasus_Common_Packer_h
 36                    
 37                    #include <cassert>
 38                    #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                    
 42 mike           1.2.2.2 #define PACKER_ASSERT(X) /* empty */
 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                        class PEGASUS_COMMON_LINKAGE Packer
 54                        {
 55                        public:
 56                        
 57 mike           1.2.2.1     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.2.2.1     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 mike           1.2.2.1 	Buffer& in, Uint32& pos, Boolean* x, Uint32 n);
 91 jim.wunderlich 1.1         static void unpackUint8(
 92 mike           1.2.2.1 	Buffer& in, Uint32& pos, Uint8* x, Uint32 n);
 93 jim.wunderlich 1.1         static void unpackUint16(
 94 mike           1.2.2.1 	Buffer& in, Uint32& pos, Uint16* x, Uint32 n);
 95 jim.wunderlich 1.1         static void unpackUint32(
 96 mike           1.2.2.1 	Buffer& in, Uint32& pos, Uint32* x, Uint32 n);
 97 jim.wunderlich 1.1         static void unpackUint64(
 98 mike           1.2.2.1 	Buffer& in, Uint32& pos, Uint64* x, Uint32 n);
 99 jim.wunderlich 1.1         static void unpackReal32(
100 mike           1.2.2.1 	Buffer& in, Uint32& pos, Real32* x, Uint32 n);
101 jim.wunderlich 1.1         static void unpackReal64(
102 mike           1.2.2.1 	Buffer& in, Uint32& pos, Real64* x, Uint32 n);
103 jim.wunderlich 1.1         static void unpackChar16(
104 mike           1.2.2.1 	Buffer& in, Uint32& pos, Char16* x, Uint32 n);
105 jim.wunderlich 1.1         static void unpackString(
106 mike           1.2.2.1 	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                        #else
128 jim.wunderlich 1.1         // 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 mike           1.2.2.1 inline void Packer::packBoolean(Buffer& out, Boolean x_)
136 jim.wunderlich 1.1     {
137                            Uint8 x = Uint8(x_);
138                            out.append((char*)&x, sizeof(x));
139                        }
140                        
141 mike           1.2.2.1 inline void Packer::packUint8(Buffer& out, Uint8 x)
142 jim.wunderlich 1.1     {
143                            out.append((char*)&x, sizeof(x));
144                        }
145                        
146 mike           1.2.2.1 inline void Packer::packUint16(Buffer& out, Uint16 x)
147 jim.wunderlich 1.1     {
148                            if (isLittleEndian())
149                        	x = Packer::swapUint16(x);
150                        
151                            out.append((char*)&x, sizeof(x));
152                        }
153                        
154 mike           1.2.2.1 inline void Packer::packUint32(Buffer& out, Uint32 x)
155 jim.wunderlich 1.1     {
156                            if (isLittleEndian())
157                        	x = Packer::swapUint32(x);
158                        
159                            out.append((char*)&x, sizeof(x));
160                        }
161                        
162 mike           1.2.2.1 inline void Packer::packUint64(Buffer& out, Uint64 x)
163 jim.wunderlich 1.1     {
164                            if (isLittleEndian())
165                        	x = Packer::swapUint64(x);
166                        
167                            out.append((char*)&x, sizeof(x));
168                        }
169                        
170 mike           1.2.2.1 inline void Packer::packReal32(Buffer& out, Real32 x)
171 jim.wunderlich 1.1     {
172                            packUint32(out, *((Uint32*)&x));
173                        }
174                        
175 mike           1.2.2.1 inline void Packer::packReal64(Buffer& out, Real64 x)
176 jim.wunderlich 1.1     {
177                            packUint64(out, *((Uint64*)&x));
178                        }
179                        
180 mike           1.2.2.1 inline void Packer::packChar16(Buffer& out, Char16 x)
181 jim.wunderlich 1.1     {
182                            packUint16(out, Uint16(x));
183                        }
184                        
185 mike           1.2.2.1 inline void Packer::packReal32(Buffer& out, const Real32* x, Uint32 n)
186 jim.wunderlich 1.1     {
187                            Packer::packUint32(out, (const Uint32*)x, n);
188                        }
189                        
190 mike           1.2.2.1 inline void Packer::packReal64(Buffer& out, const Real64* x, Uint32 n)
191 jim.wunderlich 1.1     {
192                            Packer::packUint64(out, (const Uint64*)x, n);
193                        }
194                        
195 mike           1.2.2.1 inline void Packer::packChar16(Buffer& out, const Char16* x, Uint32 n)
196 jim.wunderlich 1.1     {
197                            Packer::packUint16(out, (const Uint16*)x, n);
198                        }
199                        
200                        inline void Packer::unpackBoolean(
201 mike           1.2.2.1     const Buffer& in, Uint32& pos, Boolean& x)
202 jim.wunderlich 1.1     {
203                            x = in[pos++] ? true : false;
204                            PACKER_ASSERT(pos <= in.size());
205                        }
206                        
207                        inline void Packer::unpackUint8(
208 mike           1.2.2.1     const Buffer& in, Uint32& pos, Uint8& x)
209 jim.wunderlich 1.1     {
210                            x = Uint8(in[pos++]);
211                            PACKER_ASSERT(pos <= in.size());
212                        }
213                        
214 mike           1.2.2.1 inline void Packer::unpackReal32(const Buffer& in, Uint32& pos, Real32& x)
215 jim.wunderlich 1.1     {
216                            Packer::unpackUint32(in, pos, *((Uint32*)&x));
217                            PACKER_ASSERT(pos <= in.size());
218                        }
219                        
220 mike           1.2.2.1 inline void Packer::unpackReal64(const Buffer& in, Uint32& pos, Real64& x)
221 jim.wunderlich 1.1     {
222                            Packer::unpackUint64(in, pos, *((Uint64*)&x));
223                            PACKER_ASSERT(pos <= in.size());
224                        }
225                        
226 mike           1.2.2.1 inline void Packer::unpackChar16(const Buffer& in, Uint32& pos, Char16& x)
227 jim.wunderlich 1.1     {
228                            Packer::unpackUint16(in, pos, *((Uint16*)&x));
229                            PACKER_ASSERT(pos <= in.size());
230                        }
231                        
232                        inline void Packer::unpackReal32(
233 mike           1.2.2.1     Buffer& in, Uint32& pos, Real32* x, Uint32 n)
234 jim.wunderlich 1.1     {
235                            unpackUint32(in, pos, (Uint32*)x, n);
236                            PACKER_ASSERT(pos <= in.size());
237                        }
238                        
239                        inline void Packer::unpackReal64(
240 mike           1.2.2.1     Buffer& in, Uint32& pos, Real64* x, Uint32 n)
241 jim.wunderlich 1.1     {
242                            unpackUint64(in, pos, (Uint64*)x, n);
243                            PACKER_ASSERT(pos <= in.size());
244                        }
245                        
246                        inline void Packer::unpackChar16(
247 mike           1.2.2.1     Buffer& in, Uint32& pos, Char16* x, Uint32 n)
248 jim.wunderlich 1.1     {
249                            unpackUint16(in, pos, (Uint16*)x, n);
250                            PACKER_ASSERT(pos <= in.size());
251                        }
252                        
253                        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 jim.wunderlich 1.1     inline void Packer::swap(Uint8& x, Uint8& y)
270                        {
271                            Uint8 t = x;
272                            x = y;
273                            y = t;
274                        }
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 jim.wunderlich 1.1         return u.x;
291                        }
292                        
293                        PEGASUS_NAMESPACE_END
294                        
295                        #endif /* _Pegasus_Common_Packer_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2