📄 ospasn1.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. * *******#########################################################################*#########################################################################*#########################################################################*//* * ospasn1.c - Member functions for ASN1 decode/encode library */#include "osp.h"#include "ospasn1.h"#include "osptnlog.h"/* ---------------------------------------------------------*//* Member functions *//* ---------------------------------------------------------*/int OSPPASN1ElementGetContentData( OSPTASN1ELEMENTINFO *ospvElement, unsigned char **ospvContent, unsigned int *ospvContentLength){ int errorcode = OSPC_ERR_NO_ERROR; if ((ospvElement == OSPC_OSNULL) || (ospvContent == OSPC_OSNULL) || (ospvContentLength == OSPC_OSNULL)) { errorcode = OSPC_ERR_ASN1_NULL_POINTER; OSPM_DBGERRORLOG(errorcode, "Invalid parameters to ElementGetContent"); } if (errorcode == OSPC_ERR_NO_ERROR) { *ospvContent = ospvElement->Content; *ospvContentLength = ospvElement->ContentLength; } else { *ospvContent = OSPC_OSNULL; *ospvContentLength = 0; } return errorcode;}intOSPPASN1ElementCopyElementData( OSPTASN1ELEMENTINFO *ospvSrcElement, unsigned char **ospvData, unsigned int *ospvDataLength){ int errorcode = OSPC_ERR_NO_ERROR; if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MALLOC(*ospvData, unsigned char, ospvSrcElement->ElementLength); if (*ospvData == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate data storage"); } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Finish up the element */ *ospvDataLength = ospvSrcElement->ElementLength; OSPM_MEMCPY(*ospvData, ospvSrcElement->Element, *ospvDataLength); } return errorcode;}intOSPPASN1ElementGetElementData( OSPTASN1ELEMENTINFO *ospvSrcElement, unsigned char **ospvData, unsigned int *ospvDataLength){ int errorcode = OSPC_ERR_NO_ERROR; if(ospvSrcElement != OSPC_OSNULL) { /* Finish up the element */ *ospvDataLength = ospvSrcElement->ElementLength; *ospvData = ospvSrcElement->Element; } return errorcode;}int OSPPASN1ElementTestContext( OSPTASN1ELEMENTINFO *ospvElementInfo){ int errorcode = OSPC_ERR_NO_ERROR; if (ospvElementInfo == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_INVALID_CONTEXT; OSPM_DBGERRORLOG(errorcode, "Invalid ASN1 ElementInfo pointer"); } return errorcode;}int OSPPASN1ElementEncode( OSPTASN1ELEMENTINFO *ospvElementInfo){ int errorcode = OSPC_ERR_NO_ERROR; unsigned elementLength = 0; unsigned contentLength = 0; unsigned newLength = 0; unsigned i = 0; unsigned char *elementData = OSPC_OSNULL; unsigned char *contentData = OSPC_OSNULL; unsigned char *newData = OSPC_OSNULL; OSPTASN1ELEMENTINFO *contentElements = OSPC_OSNULL; unsigned char *lengthBuffer = OSPC_OSNULL; unsigned lengthLength = 0; if (ospvElementInfo == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } else { contentElements = ospvElementInfo->ContentElementInfo; if (contentElements == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } } for (; (errorcode == OSPC_ERR_NO_ERROR) && (contentElements!= OSPC_OSNULL); contentElements = contentElements->NextElementInfo) { errorcode = OSPPASN1ElementGetElementData(contentElements, &newData, &newLength); if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_REALLOC(contentData,contentData,unsigned char, (contentLength+newLength)); if (contentData == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to realloc element data store"); } } if (errorcode == OSPC_ERR_NO_ERROR) { unsigned char *tmpPtr; tmpPtr=(contentData+contentLength); OSPM_MEMCPY(tmpPtr, newData, newLength); contentLength += newLength; } } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1SmallInt2UnsignedChar(contentLength, 256, &lengthBuffer, &lengthLength); if (errorcode == OSPC_ERR_NO_ERROR) { elementLength = contentLength + 1 + lengthLength ; if (contentLength > 127) { elementLength++; } OSPM_MALLOC(elementData,unsigned char, elementLength); if (elementData == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to malloc space for element data"); } } if (errorcode == OSPC_ERR_NO_ERROR) { i = 0 ; elementData[i++] = ospvElementInfo->Tag; if (contentLength > 127) { elementData[i++] = (unsigned char)(lengthLength | OSPC_BER_LENGTH_MODE_LONG); } OSPM_MEMCPY(&elementData[i], lengthBuffer, lengthLength); i+=lengthLength; OSPM_MEMCPY(&(elementData[i]), contentData, contentLength); ospvElementInfo->Element = elementData; ospvElementInfo->ElementLength = elementLength; ospvElementInfo->ElementSpaceAllocated = 1; OSPM_FREE(contentData); } } if (errorcode == OSPC_ERR_ASN1_PARSE_COMPLETE) { errorcode = OSPC_ERR_NO_ERROR; } if(lengthBuffer != OSPC_OSNULL) { OSPM_FREE(lengthBuffer); } return(errorcode);}int OSPPASN1ElementDecode( unsigned char *ospvASN1Element, OSPTASN1ELEMENTINFO **ospvASN1ElementInfo, unsigned int ospvLevel){ int errorcode = OSPC_ERR_NO_ERROR; unsigned char *eptr = OSPC_OSNULL; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; OSPTASN1ELEMENTINFO *cInfo = OSPC_OSNULL; OSPTASN1ELEMENTINFO *hPtr = OSPC_OSNULL; OSPTASN1ELEMENTINFO *tPtr = OSPC_OSNULL; unsigned headerLength = 0; char msg[100]; unsigned length = 0; unsigned i = 0; unsigned char baseTag = 0; unsigned char dataByte = 0; OSPM_MEMSET(msg, 0, 100); /* Test for a valid element data pointer */ if (ospvASN1Element == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_NULL_POINTER; OSPM_DBGERRORLOG(errorcode, "Invalid element pointer"); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Allocate space for an element */ errorcode = OSPPASN1ElementCreate(&eInfo); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Initialize element Info Structure */ eptr = ospvASN1Element; /* Extract the tag */ eInfo->Tag = *(eptr++); headerLength++; /* Keep track of info bytes in element */ if (OSPM_IS_HIGH_TAG(eInfo->Tag)) { errorcode = OSPC_ERR_ASN1_UNEXPECTED_HIGH_TAG; OSPM_DBGERRORLOG(errorcode, "Unsupported high tag encountered."); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Validate Tag */ baseTag = (unsigned char)OSPM_BASE_TAG(eInfo->Tag); /* Test the tag */ switch(baseTag) { case OSPC_TAG_TYPE_SEQUENCE: case OSPC_TAG_TYPE_SET: case OSPC_TAG_TYPE_EOC: case OSPC_TAG_TYPE_NULL: case OSPC_TAG_TYPE_INTEGER: case OSPC_TAG_TYPE_OBJECT_IDENTIFIER: case OSPC_TAG_TYPE_PRINTABLESTRING: case OSPC_TAG_TYPE_T61STRING: case OSPC_TAG_TYPE_IA5STRING: case OSPC_TAG_TYPE_UTCTIME: case OSPC_TAG_TYPE_BIT_STRING: case OSPC_TAG_TYPE_OCTET_STRING: case OSPC_TAG_TYPE_BMPSTRING: errorcode = OSPC_ERR_NO_ERROR; break; default: errorcode = OSPC_ERR_ASN1_INVALID_ELEMENT_TAG; sprintf(msg, "Invalid ASN1 element tag: %02x at %06d", eInfo->Tag, ((int ) eptr - (int) (ospvASN1Element))) ; OSPM_DBGERRORLOG(errorcode, msg); sprintf(msg, "ERROR:ElementDecode - Tag=%0x", eInfo->Tag); break; } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Valid tag, get the length of the content */ dataByte = *(eptr++); headerLength++; length = OSPM_BER_LENGTH(dataByte); if (OSPM_IS_LONG_LENGTH(dataByte)) { /* Length is set to length of length */ unsigned value = 0; /* Long Length Format */ for (i = 0; i < length ; i ++, headerLength++) { value *= 256; value += *(eptr++); } length = value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -