📄 serialization.c
字号:
//%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.////==============================================================================////%//////////////////////////////////////////////////////////////////////////////*! \file serialization.c \brief Binary serializer component for Remote CMPI. This file implements the functions defined in the function table contained in serialization.h. There are function pairs for each CMPI data type that can be fully serialized and deserialized using the CMPI APIs. The functions except file descriptors to be given as arguments, i.e. socket file descriptors to read from or write to. The serialization functions return the length of a successfully serialized object or data type, whereas the deserialization parts return the recreated data. \sa serialization.h \sa io.c \todo Implement CMPISelectExp serialization.*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#if defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU)#include <error.h>#endif#ifdef PEGASUS_PLATFORM_ZOS_ZSERIES_IBM#include <arpa/inet.h> // htonl();#endif#include <stdlib.h>#include <string.h>#include <strings.h>#include <netinet/in.h>#include "debug.h"#include "io.h"#include "serialization.h"#include "tool.h"#include "indication_objects.h"#include <Pegasus/Provider/CMPI/cmpimacs.h>#include <Pegasus/Provider/CMPI/cmpidt.h>#include <Pegasus/Provider/CMPI/cmpift.h>extern int nativeSide;/****************************************************************************/static ssize_t __serialize_UINT8 ( int, CMPIUint8 );static CMPIUint8 __deserialize_UINT8 ( int );static ssize_t __serialize_UINT16 ( int, CMPIUint16 );static CMPIUint16 __deserialize_UINT16 ( int );static ssize_t __serialize_UINT32 ( int, CMPIUint32 );static CMPIUint32 __deserialize_UINT32 ( int );static ssize_t __serialize_UINT64 ( int, CMPIUint64 );static CMPIUint64 __deserialize_UINT64 ( int );static int __serialized_NULL ( int fd, const void * ptr );static int __deserialized_NULL ( int fd );static ssize_t __serialize_CMPIType ( int, CMPIType );static CMPIType __deserialize_CMPIType ( int );static ssize_t __serialize_CMPIValue ( int, CONST CMPIType, CONST CMPIValue * );static CMPIValue __deserialize_CMPIValue ( int, CMPIType, CONST CMPIBroker * );static ssize_t __serialize_CMPIData ( int, CMPIData );static CMPIData __deserialize_CMPIData ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIStatus ( int, CMPIStatus * );static CMPIStatus __deserialize_CMPIStatus ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIArgs ( int, CONST CMPIArgs * );static CMPIArgs * __deserialize_CMPIArgs ( int, CONST CMPIBroker * );static ssize_t __serialize_string ( int, const char * );static char * __deserialize_string ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIString ( int, CONST CMPIString * );static CMPIString * __deserialize_CMPIString ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIObjectPath ( int, CONST CMPIObjectPath * );static CMPIObjectPath * __deserialize_CMPIObjectPath ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIArray ( int, CONST CMPIArray * );static CMPIArray * __deserialize_CMPIArray ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIInstance ( int, CONST CMPIInstance * );static CMPIInstance * __deserialize_CMPIInstance ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPISelectExp ( int, CONST CMPISelectExp *, CMPIUint32);static CMPISelectExp * __deserialize_CMPISelectExp ( int, CONST CMPIBroker *, CMPIUint32);static ssize_t __serialize_CMPIDateTime ( int, CMPIDateTime * );static CMPIDateTime * __deserialize_CMPIDateTime ( int, CONST CMPIBroker * );#ifdef CMPI_VER_200static ssize_t __serialize_CMPIError ( int, CMPIError * );static CMPIError * __deserialize_CMPIError ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIErrorSeverity ( int, CMPIErrorSeverity );static CMPIErrorSeverity __deserialize_CMPIErrorSeverity ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIErrorProbableCause ( int, CMPIErrorProbableCause );static CMPIErrorProbableCause __deserialize_CMPIErrorProbableCause ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIrc ( int, CMPIrc );static CMPIrc __deserialize_CMPIrc ( int, CONST CMPIBroker * );static ssize_t __serialize_CMPIMsgFileHandle ( int, CMPIMsgFileHandle );static CMPIMsgFileHandle __deserialize_CMPIMsgFileHandle ( int, CONST CMPIBroker * );#endif/****************************************************************************/const struct BinarySerializerFT binarySerializerFT = { __serialize_UINT8, __deserialize_UINT8, __serialize_UINT16, __deserialize_UINT16, __serialize_UINT32, __deserialize_UINT32, __serialize_UINT64, __deserialize_UINT64, __serialize_CMPIValue, __deserialize_CMPIValue, __serialize_CMPIType, __deserialize_CMPIType, __serialize_CMPIData, __deserialize_CMPIData, __serialize_CMPIStatus, __deserialize_CMPIStatus, __serialize_string, __deserialize_string, __serialize_CMPIString, __deserialize_CMPIString, __serialize_CMPIArgs, __deserialize_CMPIArgs, __serialize_CMPIObjectPath, __deserialize_CMPIObjectPath, __serialize_CMPIArray, __deserialize_CMPIArray, __serialize_CMPIInstance, __deserialize_CMPIInstance, __serialize_CMPISelectExp, __deserialize_CMPISelectExp, __serialize_CMPIDateTime, __deserialize_CMPIDateTime,#ifdef CMPI_VER_200 __serialize_CMPIError, __deserialize_CMPIError, __serialize_CMPIErrorSeverity, __deserialize_CMPIErrorSeverity, __serialize_CMPIErrorProbableCause, __deserialize_CMPIErrorProbableCause, __serialize_CMPIrc, __deserialize_CMPIrc, __serialize_CMPIMsgFileHandle, __deserialize_CMPIMsgFileHandle,#endif};/****************************************************************************/static ssize_t __serialize_UINT8 ( int fd, CMPIUint8 uint8 ){ if ( io_write_fixed_length ( fd, &uint8, sizeof ( CMPIUint8 ) ) ) { return -1; } return sizeof ( CMPIUint8 );}static CMPIUint8 __deserialize_UINT8 ( int fd ){ CMPIUint8 i; if ( io_read_fixed_length ( fd, &i, sizeof ( CMPIUint8 ) ) ) { return 0; } return i;}static ssize_t __serialize_UINT16 ( int fd, CMPIUint16 uint16 ){ return __serialize_UINT32 ( fd, uint16 );}static CMPIUint16 __deserialize_UINT16 ( int fd ){ return __deserialize_UINT32 ( fd );}static ssize_t __serialize_UINT32 ( int fd, CMPIUint32 uint32 ){ unsigned long int i = htonl ( uint32 ); if ( io_write_fixed_length ( fd, &i, sizeof ( unsigned long int ) ) ) { return -1; } return sizeof ( unsigned long int );}static CMPIUint32 __deserialize_UINT32 ( int fd ){ unsigned long int i; if ( io_read_fixed_length ( fd, &i, sizeof ( unsigned long int ) ) ) { return 0; } return ntohl ( i );}static ssize_t __serialize_UINT64 ( int fd, CMPIUint64 uint64 ){ ssize_t sl, sh; unsigned long int l = uint64 & 0xFFFFFFFF; unsigned long int h = uint64 >> 32; if ( ( sl = __serialize_UINT32 ( fd, l ) ) < 0 ) return -1; if ( ( sh = __serialize_UINT32 ( fd, h ) ) < 0 ) return -1; return sl + sh;}static CMPIUint64 __deserialize_UINT64 ( int fd ){ CMPIUint64 l = __deserialize_UINT32 ( fd ); CMPIUint64 h = __deserialize_UINT32 ( fd ); return ( h << 32 ) | l;}/****************************************************************************/static int __serialized_NULL ( int fd, const void * ptr ){ char is_null = ( ptr == NULL ); TRACE_VERBOSE(("entered function ser NULL. leaving function."));// TRACE_VERBOSE(("serializing NULL flag for %s object.",// ( ptr )? "non-NULL": "NULL" )); __serialize_UINT8 ( fd, is_null );// TRACE_VERBOSE(("leaving function.")); return is_null;}static int __deserialized_NULL ( int fd ){ char is_null; TRACE_VERBOSE(("entered function des NULL. leaving function.")); is_null = __deserialize_UINT8 ( fd );// TRACE_VERBOSE(("deserialized NULL flag for %s object.",// ( is_null )? "NULL": "non-NULL" ));// TRACE_VERBOSE(("leaving function.")); return is_null;}static ssize_t __serialize_CMPIType ( int fd, CMPIType type ){ return __serialize_UINT32 ( fd, type );}static CMPIType __deserialize_CMPIType ( int fd ){ return __deserialize_UINT32 ( fd );}static ssize_t __serialize_CMPIValue ( int fd, CONST CMPIType type, CONST CMPIValue * value ){ TRACE_NORMAL(("serializing type 0x%x CMPIValue.", type)); if ( type & CMPI_ARRAY ) { TRACE_INFO(("serializing array object type.")); return __serialize_CMPIArray ( fd, value->array ); } else if ( type & CMPI_ENC ) { TRACE_INFO(("trying to serialize encapsulated data type.")); switch ( type ) { case CMPI_instance: return __serialize_CMPIInstance ( fd, value->inst ); case CMPI_ref: return __serialize_CMPIObjectPath ( fd, value->ref ); case CMPI_args: return __serialize_CMPIArgs ( fd, value->args ); case CMPI_filter: return __serialize_CMPISelectExp ( fd, value->filter, PEGASUS_INDICATION_GLOBAL_CONTEXT); case CMPI_string: case CMPI_numericString: case CMPI_booleanString: case CMPI_dateTimeString: case CMPI_classNameString: return __serialize_CMPIString ( fd, value->string ); case CMPI_dateTime: return __serialize_CMPIDateTime ( fd, value->dateTime); } TRACE_CRITICAL(("non-supported encapsulated data type.")); } else if ( type & CMPI_SIMPLE ) { TRACE_INFO(("serializing simple value.")); switch ( type ) { case CMPI_boolean: return __serialize_UINT8 ( fd, value->boolean ); case CMPI_char16: return __serialize_UINT16 ( fd, value->char16 ); } TRACE_CRITICAL(("non-supported simple data type.")); } else if ( type & CMPI_INTEGER ) { TRACE_INFO(("serializing integer value.")); switch ( type ) { case CMPI_uint8: case CMPI_sint8: return __serialize_UINT8 ( fd, value->uint8 ); case CMPI_uint16: case CMPI_sint16: return __serialize_UINT16 ( fd, value->uint16 ); case CMPI_uint32: case CMPI_sint32: return __serialize_UINT32 ( fd, value->uint32 ); case CMPI_uint64: case CMPI_sint64: return __serialize_UINT64 ( fd, value->uint64 ); } TRACE_CRITICAL(("non-supported integer data type.")); } else if ( type & CMPI_REAL ) { char real_str[256]; switch ( type ) { case CMPI_real32: case CMPI_real64: sprintf ( real_str,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -