📄 ospasn1primitives.c
字号:
/**########################################################################*########################################################################*########################################################################* * COPYRIGHT (c) 1998, 1999 by TransNexus, LLC * * This software contains proprietary and confidential information * of TransNexus, LLC. Except as may be set forth in the license * agreement under which this software is supplied, use, disclosure, * or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC. * *******#########################################################################*#########################################################################*#########################################################################*//* * ospasn1primitives.c - ASN1 Primitive encode/decode functions */#include "osp.h"#include "ospasn1.h"#include "osptnlog.h"/* ---------------------------------------------------------*//* Member functions *//* ---------------------------------------------------------*//***************************************** GENERAL PRIMITIVE ROUTINES *****************************************/#ifdef PRIMITIVEDECODEint OSPPASN1PrimitiveDecode( OSPTASN1ELEMENTINFO *ospvElementInfo){ OSPTASN1ELEMENTINFO *eInfo; int errorcode = OSPC_ERR_NO_ERROR; char msg[100]; void *contentValue = OSPC_OSNULL; errorcode = OSPPASN1ElementTestContext(ospvElementInfo); if (errorcode == OSPC_ERR_NO_ERROR) { eInfo = ospvElementInfo; sprintf(msg, "PrimitiveDecode - Tag=%0x", eInfo->Tag); /* Decode the item and save the value */ switch(OSPM_BASE_TAG(eInfo->Tag)) { case OSPC_TAG_TYPE_EOC: break; case OSPC_TAG_TYPE_NULL: break; case OSPC_TAG_TYPE_INTEGER: errorcode = OSPPASN1IntegerDecode((OSPTASN1INTEGER**)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_OBJECT_IDENTIFIER: errorcode = OSPPASN1ObjectIdentifierDecode( (OSPTASN1BUFFER **)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_IA5STRING: case OSPC_TAG_TYPE_T61STRING: case OSPC_TAG_TYPE_PRINTABLESTRING: case OSPC_TAG_TYPE_BMPSTRING: errorcode = OSPPASN1PrintableStringDecode( (OSPTASN1BUFFER **)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_UTCTIME: errorcode = OSPPASN1UTCTimeDecode( (OSPTASN1BUFFER **)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_BIT_STRING: errorcode = OSPPASN1BitStringDecode( OSPTASN1BITSTRING **)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_OCTET_STRING: errorcode = OSPPASN1OctetStringDecode( (OSPTASN1BUFFER**)&contentValue, eInfo->Content, eInfo->ContentLength); break; case OSPC_TAG_TYPE_SEQUENCE: case OSPC_TAG_TYPE_SET: default: errorcode = OSPC_ERR_ASN1_INVALID_PRIMITIVE_TAG; sprintf(msg, "Invalid/Unsupported primitive tag %02x", eInfo->Tag); OSPM_DBGERRORLOG(errorcode, msg); break; } } return errorcode;}int OSPPASN1PrimitiveDelete( OSPTASN1ELEMENTINFO *ospvElementInfo){ OSPTASN1ELEMENTINFO *eInfo; int errorcode = OSPC_ERR_NO_ERROR; char msg[100]; errorcode = OSPPASN1ElementTestContext(ospvElementInfo); if (errorcode == OSPC_ERR_NO_ERROR) { eInfo = ospvElementInfo; if (eInfo->ContentValue != OSPC_OSNULL) { /* Decode the item and save the value */ switch(OSPM_BASE_TAG(eInfo->Tag)) { case OSPC_TAG_TYPE_EOC: case OSPC_TAG_TYPE_NULL: break; case OSPC_TAG_TYPE_INTEGER: OSPPASN1IntegerDelete((OSPTASN1INTEGER **)&contentValue); break; case OSPC_TAG_TYPE_OBJECT_IDENTIFIER: OSPPASN1ObjectIdentifierDelete( (OSPTASN1OBJECTID **)&contentValue); break; case OSPC_TAG_TYPE_PRINTABLESTRING: OSPPASN1BufferDelete((OSPTASN1BUFFER **)&contentValue)); break; case OSPC_TAG_TYPE_UTCTIME: OSPPASN1BufferDelete((OSPTASN1BUFFER **)&contentValue); break; case OSPC_TAG_TYPE_BIT_STRING: OSPPASN1BitStringDelete((OSPTASN1BITSTRING**)&contentValue)); break; case OSPC_TAG_TYPE_OCTET_STRING: OSPPASN1BufferDelete((OSPTASN1BUFFER**)&contentValue)); break; case OSPC_TAG_TYPE_SEQUENCE: case OSPC_TAG_TYPE_SET: default: errorcode = OSPC_ERR_ASN1_INVALID_PRIMITIVE_TAG; sprintf(msg, "Invalid/Unsupported primitive tag %02x", eInfo->Tag); OSPM_DBGERRORLOG(errorcode, msg); break; } } } return errorcode;}/**************************************** BUFFER ROUTINES****************************************/intOSPPASN1BufferCreate( unsigned char *ospvContent, unsigned ospvContentLength, OSPTASN1BUFFER **ospvBuffer){ int errorcode = OSPC_ERR_NO_ERROR; /* Allocate space for buffer structure */ OSPM_MALLOC(*ospvBuffer, OSPTASN1BUFFER, sizeof(OSPTASN1BUFFER)); if (*ospvBuffer == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate an ASN1 BUFFER"); } if (errorcode == OSPC_ERR_NO_ERROR) { if (ospvContentLength != 0) { /* Allocate space for buffer contents */ OSPM_MALLOC((*ospvBuffer)->Buffer, unsigned char, ospvContentLength); if ((*ospvBuffer)->Buffer == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate space for buffer content"); } } } if (errorcode == OSPC_ERR_NO_ERROR) { if (ospvContentLength != 0) { OSPM_MEMCPY((*ospvBuffer)->Buffer, ospvContent, ospvContentLength); } (*ospvBuffer)->BufferLength = ospvContentLength; } return errorcode;}void OSPPASN1BufferDelete( OSPTASN1BUFFER **ospvBuffer){ /* Make sure the buffer exists */ if ((ospvBuffer != OSPC_NULL) && (*ospvBuffer != OSPC_OSNULL) && ((*ospvBuffer)->Buffer != OSPC_OSNULL)) { /* Delete the buffer data */ OSPM_FREE((*ospvBuffer)->Buffer); } /* Delete the buffer structure */ OSPM_FREE(*ospvBuffer);}return;}/***************************************** OCTET STRING ROUTINES *****************************************/int OSPPASN1OctetStringDecode( OSPTASN1BUFFER **ospvOctetStringBUffer, unsigned char *ospvEncodedData, unsigned int ospvEncodedDataLength){ int errorcode = OSPC_ERR_NO_ERROR; if (ospvOctetStringBuffer == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_INVALID_PARAMETER; OSPM_DBGERRORLOG(errorcode, "Null pointer supplied for return value"); } else if ((ospvEncodedData != OSPC_OSNULL) && (ospvEncodedDataLength == 0)) { errorcode = OSPPASN1BufferCreate(ospvEncodedData, ospvEncodedDataLength, ospvOctetStringBuffer); } return errorcode;}/***************************************** INTEGER ROUTINES *****************************************/int OSPPASN1IntegerDecode( void **osvpContentValue, unsigned char *ospvEncodedData, unsigned int ospvEncodedDataLength){ int errorcode = OSPC_ERR_NO_ERROR; if (ospvContentValue == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_INVALID_PARAMETER; OSPM_DBGERRORLOG(errorcode, "Null pointer supplied for return value"); } else if ((ospvEncodedData != OSPC_OSNULL) && (ospvEncodedDataLength == 0)) { errorcode =OSPPASN1IntegerCreate(ospvEncodedData, ospvEncodedDataLength, (OSPTASN1INTEGER **)ospvContentValue); } return errorcode;}voidOSPPASN1IntegerDelete( OSPTASN1INTEGER **ospvInteger){ if (*ospvInteger != OSPC_OSNULL) { if ((*ospvInteger)->Digits != OSPC_OSNULL) { OSPM_FREE((*ospvInteger)->Digits); } if ((*ospvInteger)->SmallValue != OSPC_OSNULL) { OSPM_FREE((*ospvInteger)->SmallValue); } OSPM_FREE(*ospvInteger); } return;}intOSPPASN1IntegerCreate( unsigned char *ospvContent, unsigned int ospvContentLength, OSPTASN1INTEGER **ospvInteger){ OSPTASN1INTEGER *intBuf=OSPC_OSNULL; int smallValue = 0; int errorcode = OSPC_ERR_NO_ERROR; unsigned int i; OSPM_MALLOC(intBuf, OSPTASN1INTEGER, sizeof(OSPTASN1INTEGER)); if (intBuf == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate integer buffer"); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Set up Integer Buffer */ OSPM_MALLOC(intBuf->Digits, unsigned char, ospvContentLength); if (intBuf->Digits == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate integer buffer"); } } if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MEMCPY(intBuf->Digits, ospvContent, ospvContentLength); intBuf->DigitCount = ospvContentLength; if ((intBuf->Digits[0] & 0x8f) == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -