📄 binarystreamer.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include "XmlWriter.h"#include "XmlReader.h"#include "XmlParser.h"#include "CIMName.h"#include "CIMNameUnchecked.h"#include "BinaryStreamer.h"#include "CIMClassRep.h"#include "CIMInstanceRep.h"#include "CIMMethodRep.h"#include "CIMParameterRep.h"#include "CIMPropertyRep.h"#include "CIMQualifierRep.h"#include "CIMValue.h"#include "CIMValueRep.h"#include "Packer.h"#define MAGIC_BYTE Uint8(0x11)#define VERSION_NUMBER Uint8(1)PEGASUS_USING_STD;PEGASUS_NAMESPACE_BEGINenum BinaryObjectType{ BINARY_CLASS, BINARY_INSTANCE, BINARY_QUALIFIER_DECL};static inline void _packMagicByte(Buffer& out){ Packer::packUint8(out, MAGIC_BYTE);}static void _checkMagicByte(const Buffer& in, Uint32& pos){ Uint8 magicByte; Packer::unpackUint8(in, pos, magicByte); if (magicByte != MAGIC_BYTE) throw BinException("Bad magic byte");}struct Header{ // A version number for this message. Uint8 versionNumber; // The object type (see BinaryObjectType enum). Uint8 objectType;};static void _packHeader(Buffer& out, Uint8 objectType){ Packer::packUint8(out, VERSION_NUMBER); Packer::packUint8(out, objectType);}static void _checkHeader( const Buffer& in, Uint32& pos, Uint8 expectedObjectType){ Header header; Packer::unpackUint8(in, pos, header.versionNumber); Packer::unpackUint8(in, pos, header.objectType); if (header.objectType != expectedObjectType) throw BinException("Unexpected object type"); if (header.versionNumber != VERSION_NUMBER) throw BinException("Unsupported version");}inline void _unpack(const Buffer& in, Uint32& pos, Boolean& x){ Packer::unpackBoolean(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Uint8& x){ Packer::unpackUint8(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Sint8& x){ Packer::unpackUint8(in, pos, (Uint8&)x);}inline void _unpack(const Buffer& in, Uint32& pos, Uint16& x){ Packer::unpackUint16(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Sint16& x){ Packer::unpackUint16(in, pos, (Uint16&)x);}inline void _unpack(const Buffer& in, Uint32& pos, Uint32& x){ Packer::unpackUint32(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Sint32& x){ Packer::unpackUint32(in, pos, (Uint32&)x);}inline void _unpack(const Buffer& in, Uint32& pos, Uint64& x){ Packer::unpackUint64(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Sint64& x){ Packer::unpackUint64(in, pos, (Uint64&)x);}inline void _unpack(const Buffer& in, Uint32& pos, Real32& x){ Packer::unpackReal32(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Real64& x){ Packer::unpackReal64(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, Char16& x){ Packer::unpackChar16(in, pos, x);}inline void _unpack(const Buffer& in, Uint32& pos, String& x){ Packer::unpackString(in, pos, x);}void _unpack(const Buffer& in, Uint32& pos, CIMDateTime& x){ String tmp; Packer::unpackString(in, pos, tmp); x.set(tmp);}void _unpack(const Buffer& in, Uint32& pos, CIMObjectPath& x){ String tmp; Packer::unpackString(in, pos, tmp); x.set(tmp);}void _unpack(const Buffer& in, Uint32& pos, CIMObject& x){ String tmp_String; Packer::unpackString(in, pos, tmp_String); if (tmp_String.size() == 0) { // This should not occur since _unpackValue() won't call _unpack() // if the value is Null. PEGASUS_ASSERT(false); } else { // Convert the non-NULL string to a CIMObject (containing either a // CIMInstance or a CIMClass). // First we need to create a new "temporary" XmlParser that is // just the value of the Embedded Object in String representation. CString cstr = tmp_String.getCString(); char* tmp_buffer = (char*)(const char*)cstr; XmlParser tmp_parser(tmp_buffer); // The next bit of logic constructs a CIMObject from the Embedded // Object String. // It is similar to the method XmlReader::getValueObjectElement(). CIMInstance cimInstance; CIMClass cimClass; if (XmlReader::getInstanceElement(tmp_parser, cimInstance)) { x = CIMObject(cimInstance); } else if (XmlReader::getClassElement(tmp_parser, cimClass)) { x = CIMObject(cimClass); } else { // change "element" to "embedded object" MessageLoaderParms mlParms( "Common.XmlReader.EXPECTED_INSTANCE_OR_CLASS_ELEMENT", "Expected INSTANCE or CLASS element"); throw XmlValidationError(0, mlParms); } }}#ifdef PEGASUS_EMBEDDED_INSTANCE_SUPPORTvoid _unpack(const Buffer& in, Uint32& pos, CIMInstance& x){ CIMObject tmp; _unpack(in, pos, tmp); x = CIMInstance(tmp);}#endif // PEGASUS_EMBEDDED_INSTANCE_SUPPORTtemplate<class T>struct UnpackArray{ static void func( const Buffer& in, Uint32& pos, Uint32 n, CIMValue& value) { Array<T> array; array.reserveCapacity(n); for (Uint32 i = 0; i < n; i++) { T tmp; _unpack(in, pos, tmp); array.append(tmp); } value.set(array); }};template<class T>struct UnpackScalar{ static void func( const Buffer& in, Uint32& pos, CIMValue& value) { T tmp; _unpack(in, pos, tmp); value.set(tmp); }};template<class OBJECT>struct UnpackQualifiers{ static void func(const Buffer& in, Uint32& pos, OBJECT& x) { Uint32 n; Packer::unpackSize(in, pos, n); CIMQualifier q; for (size_t i = 0; i < n; i++) { BinaryStreamer::_unpackQualifier(in, pos, q); x.addQualifier(q); } }};template<class REP>struct PackQualifiers{ static void func(Buffer& out, REP* rep) { Uint32 n = rep->getQualifierCount(); Packer::packSize(out, n); for (Uint32 i = 0; i < n; i++) BinaryStreamer::_packQualifier(out, rep->getQualifier(i)); }};template<class OBJECT>struct UnpackProperties{ static void func(const Buffer& in, Uint32& pos, OBJECT& x) { Uint32 n; Packer::unpackSize(in, pos, n); CIMProperty p; for (size_t i = 0; i < n; i++) { BinaryStreamer::_unpackProperty(in, pos, p); x.addProperty(p); } }};template<class OBJECT>struct UnpackMethods{ static void func(const Buffer& in, Uint32& pos, OBJECT& x) { Uint32 n; Packer::unpackSize(in, pos, n); CIMMethod m; for (size_t i = 0; i < n; i++) { BinaryStreamer::_unpackMethod(in, pos, m); x.addMethod(m); } }};void BinaryStreamer::_packName(Buffer& out, const CIMName& x){ Packer::packString(out, x.getString());}void BinaryStreamer::_unpackName( const Buffer& in, Uint32& pos, CIMName& x){ String tmp; Packer::unpackString(in, pos, tmp); x = tmp.size() ? CIMNameUnchecked(tmp) : CIMName();}void BinaryStreamer::_packQualifier(Buffer& out, const CIMQualifier& x){ CIMQualifierRep* rep = x._rep; _packMagicByte(out); _packName(out, rep->getName()); _packValue(out, rep->getValue()); _packFlavor(out, rep->getFlavor()); Packer::packBoolean(out, rep->getPropagated());}void BinaryStreamer::_unpackQualifier( const Buffer& in, Uint32& pos, CIMQualifier& x){ _checkMagicByte(in, pos); CIMName name; _unpackName(in, pos, name); CIMValue value; _unpackValue(in, pos, value); CIMFlavor flavor; BinaryStreamer::_unpackFlavor(in, pos, flavor); Boolean propagated; Packer::unpackBoolean(in, pos, propagated); x = CIMQualifier(name, value, flavor, propagated);}void BinaryStreamer::_packValue(Buffer& out, const CIMValue& x){ CIMValueRep* rep = x._rep; _packMagicByte(out); _packType(out, x.getType()); Packer::packBoolean(out, x.isArray()); Uint32 n = x.getArraySize(); if (x.isArray()) Packer::packSize(out, n); Packer::packBoolean(out, x.isNull()); if (x.isNull()) return; if (x.isArray()) { switch (x.getType()) { case CIMTYPE_BOOLEAN: Packer::packBoolean( out, CIMValueType<Boolean>::aref(rep).getData(), n); break; case CIMTYPE_SINT8: case CIMTYPE_UINT8: Packer::packUint8( out, CIMValueType<Uint8>::aref(rep).getData(), n); break; case CIMTYPE_SINT16: case CIMTYPE_UINT16: case CIMTYPE_CHAR16: Packer::packUint16( out, CIMValueType<Uint16>::aref(rep).getData(), n); break; case CIMTYPE_SINT32: case CIMTYPE_UINT32: case CIMTYPE_REAL32: Packer::packUint32( out, CIMValueType<Uint32>::aref(rep).getData(), n); break; case CIMTYPE_SINT64: case CIMTYPE_UINT64: case CIMTYPE_REAL64: Packer::packUint64( out, CIMValueType<Uint64>::aref(rep).getData(), n); break; case CIMTYPE_STRING: Packer::packString(out, CIMValueType<String>::aref(rep).getData(), n); break; case CIMTYPE_DATETIME: { const Array<CIMDateTime>& a = CIMValueType<CIMDateTime>::aref(rep); for (Uint32 i = 0; i < n; i++) Packer::packString(out, a[i].toString()); break; } case CIMTYPE_REFERENCE: { const Array<CIMObjectPath>& a = CIMValueType<CIMObjectPath>::aref(rep); for (Uint32 i = 0; i < n; i++) Packer::packString(out, a[i].toString()); break; } case CIMTYPE_OBJECT: { const Array<CIMObject>& a = CIMValueType<CIMObject>::aref(rep); for (Uint32 i = 0; i < n; i++) Packer::packString(out, a[i].toString()); break; }#ifdef PEGASUS_EMBEDDED_INSTANCE_SUPPORT case CIMTYPE_INSTANCE: { const Array<CIMInstance>& a = CIMValueType<CIMInstance>::aref(rep); for (Uint32 i = 0; i < n; i++) { CIMObject tmp(a[i]); Packer::packString(out, tmp.toString()); } break; }#endif // PEGASUS_EMBEDDED_INSTANCE_SUPPORT } } else { switch (x.getType()) { case CIMTYPE_BOOLEAN: Packer::packBoolean(out, rep->u._booleanValue); break; case CIMTYPE_SINT8: case CIMTYPE_UINT8: Packer::packUint8(out, rep->u._uint8Value); break; case CIMTYPE_SINT16: case CIMTYPE_UINT16: case CIMTYPE_CHAR16: Packer::packUint16(out, rep->u._uint16Value); break; case CIMTYPE_SINT32: case CIMTYPE_UINT32: case CIMTYPE_REAL32: Packer::packUint32(out, rep->u._uint32Value); break; case CIMTYPE_SINT64: case CIMTYPE_UINT64: case CIMTYPE_REAL64: Packer::packUint64(out, rep->u._uint64Value); break; case CIMTYPE_STRING: Packer::packString(out, CIMValueType<String>::ref(rep)); break; case CIMTYPE_DATETIME: Packer::packString( out, CIMValueType<CIMDateTime>::ref(rep).toString()); break; case CIMTYPE_REFERENCE: Packer::packString( out, CIMValueType<CIMObjectPath>::ref(rep).toString()); break; case CIMTYPE_OBJECT: Packer::packString( out, CIMValueType<CIMObject>::ref(rep).toString()); break;#ifdef PEGASUS_EMBEDDED_INSTANCE_SUPPORT case CIMTYPE_INSTANCE: { CIMObject tmp(CIMValueType<CIMInstance>::ref(rep)); Packer::packString( out, tmp.toString()); break; }#endif // PEGASUS_EMBEDDED_INSTANCE_SUPPORT } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -