📄 ospasn1.c
字号:
} eInfo->ElementLength = headerLength + length; eInfo->ContentLength = length; } } if (ospvLevel == 0) { /* Allocate space for the element so we don't rely on some other functions storage (only top level) */ OSPM_MALLOC(eInfo->Element, unsigned char,eInfo->ElementLength); if (eInfo->Element == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate space for element"); } else { eInfo->ElementSpaceAllocated = 1; OSPM_MEMCPY(eInfo->Element, ospvASN1Element, eInfo->ElementLength); } } else { /* Rely on higher level EINFO to maintain storage for element list - just set pointer here */ eInfo->Element = ospvASN1Element; } eInfo->Content = eInfo->Element+headerLength; if (errorcode == OSPC_ERR_NO_ERROR) { /* Finish the elementInfo structure */ if (OSPM_IS_PRIMITIVE(eInfo->Tag)) { /* Finish decoding a primitive element - possible expansion here if necessary */ } else { /* Parse the content you found */ hPtr = OSPC_OSNULL; /* Pointer to first element in content */ tPtr = OSPC_OSNULL; /* Pointer to current last element in content */ /* Process chunks of content till done */ for (i = 0; i < eInfo->ContentLength ; i += length, eptr += length) { /* Parse next content element into new structure */ errorcode = OSPPASN1ElementDecode(eptr, &cInfo, ospvLevel+1); if (errorcode == OSPC_ERR_NO_ERROR) { /* Establish the list head pointer */ if (hPtr == OSPC_OSNULL) { hPtr = cInfo; } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Parsing content element done */ /* Get the Length of content element */ length = cInfo->ElementLength; /* Attach content info to tail of list */ if (tPtr != OSPC_OSNULL) { tPtr->NextElementInfo = cInfo; } /* Establish/Update tail pointer */ tPtr = cInfo; } if (errorcode != OSPC_ERR_NO_ERROR) { break; } } eInfo->ContentElementInfo = hPtr; } } if (errorcode != OSPC_ERR_NO_ERROR) { /* Get rid of base element info structure */ if (eInfo) { OSPPASN1ElementDelete(&eInfo,ospvLevel); } } else { *ospvASN1ElementInfo = eInfo; } return errorcode;}intOSPPASN1ElementCreate( OSPTASN1ELEMENTINFO **ospvElementInfo){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; /* Create next content info structure */ OSPM_MALLOC(eInfo, OSPTASN1ELEMENTINFO, sizeof(OSPTASN1ELEMENTINFO)); if (eInfo == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate parse element"); } if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MEMSET(eInfo, 0, sizeof(OSPTASN1ELEMENTINFO)); } *ospvElementInfo = eInfo; return errorcode;}voidOSPPASN1ElementDelete( OSPTASN1ELEMENTINFO **ospvElement, unsigned int ospvLevel){ OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; eInfo = *ospvElement; if (eInfo != OSPC_OSNULL) { /* Traverse and Delete the peer elements */ if(eInfo->NextElementInfo != OSPC_OSNULL) { OSPPASN1ElementDelete(&(eInfo->NextElementInfo),ospvLevel+1); } /* Traverse and Delete the content elements */ if (eInfo->ContentElementInfo != OSPC_OSNULL) { OSPPASN1ElementDelete(&(eInfo->ContentElementInfo),ospvLevel+1); } /* All done with elements lists, clean up this element */ if ((ospvLevel == 0) || (eInfo->ElementSpaceAllocated == 1)) { if(eInfo->Element != OSPC_OSNULL) { /* Top eInfo in list contains storage for data that was decoded */ OSPM_FREE(eInfo->Element); eInfo->ElementLength = 0; } } if(eInfo != OSPC_OSNULL) { /* Everything else is freed up - get rid of the element storage */ OSPM_FREE(eInfo); } *ospvElement=OSPC_OSNULL; } return;} /* OSPPASN1ElementDelete */intOSPPASN1ElementGet( OSPEASN1DATAREFID ospvDataReferenceId, OSPTASN1PARSERESULT *ospvParseResults, OSPTASN1ELEMENTINFO **ospvElementInfo){ int errorcode = OSPC_ERR_NO_ERROR; errorcode = PTPResultsGetElement(ospvDataReferenceId, ospvParseResults, ospvElementInfo); return errorcode ; }/* OSPPASN1ElementParse Walk through element tree in a depth first search (child ptr then next ptr). Validate each element against the parse table. Process each element by starting another parse with the appropriate parsetable, generate a primitive and add it to the return list, etc. Element Info is a pointer to the current ElementInfo Tree node (possibly NULL) that is being processed. ParseTableId is the reference code for the parse table that is to be used to parse ElementInfo. ParseResult is a list of data references that were generated while parsing ElementInfo with ParseTableId. The dynamically allocated results list is returned to the caller where they are appended to the inprogress list. DataReferenceIdPrefix is the most recent value of the DataReferenceId. It is an array of bytes (allowing 255 values per level). Initially NULL, it is extended with the tag for the element whose value is being added to the parse results list, and passed to AddParseResults. AddParseResults includes the parameter in the parse results record. If the rule allows multiple occurances of the element, then the same reference is used for each parse result added to the list. The extraction routine will know to retrieve all occurances of the reference from the parse result table. If the part to be added to the DataReferenceId is 0, then don't append anything. This behavior may Cascade somewhat. (see SignatureAlgorithm Parse Table Definition)*/int OSPPASN1ElementParse( OSPTASN1ELEMENTINFO *ospvElementInfo, OSPEASN1PARSETABLEID ospvParseTableId, OSPTASN1PARSERULE *ospvParentParseRule, OSPTASN1PARSERESULT **ospvParseResult, unsigned char ospvDataRef){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1PARSERULE *parseRule = OSPC_OSNULL; /* Pointer to the parse table to use */ unsigned nextRule = 0; char msg[100]; unsigned maxCount = 0; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; OSPTASN1ELEMENTINFO *eInfoContent = OSPC_OSNULL; OSPTASN1PARSERESULT *newResults = OSPC_OSNULL; OSPTASN1PARSERESULT *lastResult = OSPC_OSNULL; OSPEASN1PARSETABLEID parseTableId = OSPEPTID_NOTDEFINED; OSPM_ARGUSED(ospvParentParseRule); /* It is ok to have a null ElementInfo pointer. Might be processing an rule for OPTIONAL data and there isn't any, i.e. Extensions, etc.*/ eInfo = ospvElementInfo; /* Get the first rule from the specified table */ nextRule = 0; errorcode = PTPTableGetRule(ospvParseTableId, &parseRule, &nextRule); maxCount = parseRule->MaximumCount; if ((errorcode == OSPC_ERR_NO_ERROR) && (parseRule == OSPC_OSNULL)) { errorcode = OSPC_ERR_ASN1_PARSE_ERROR ; OSPM_DBGERRORLOG(errorcode, "No rules, but there are elements to process."); } if(errorcode == OSPC_ERR_NO_ERROR) { /* Now we have a rule to process. */ /* Compare the PT entry tag against the current element tag. */ if ((eInfo == OSPC_OSNULL) || (OSPM_BASE_TAG(parseRule->Tag) != OSPM_BASE_TAG(eInfo->Tag))) { if (PTPRuleIsDerived(parseRule)||PTPRuleIsDERFormat(parseRule)) { /* Falls through, handled below */ errorcode = OSPC_ERR_NO_ERROR; } else if (PTPRuleIsOptional(parseRule)) { /* Element is optional, not a parse error - YET, we are done parseing this RULE - bye */ errorcode = OSPC_ERR_ASN1_PARSE_DEFAULTED; } else if (parseRule->HasDefault) { /* Parse Results Added */ errorcode = PTPAddParseResults(parseRule, (OSPTASN1ELEMENTINFO *)OSPC_OSNULL, ospvParseResult, ospvDataRef); if (errorcode == OSPC_ERR_NO_ERROR) { /* We are done with this element */ errorcode = OSPC_ERR_ASN1_PARSE_DEFAULTED; } } else { /* Tag mismatch and element is not optional - parse error */ errorcode = OSPC_ERR_ASN1_PARSE_ERROR; if (eInfo != OSPC_OSNULL) { sprintf(msg, "Tag mismatch, pt = %02x, e = %02x", parseRule->Tag, eInfo->Tag); } else { sprintf(msg, "Tag mismatch, pt = %02x, eInfo=NULL", parseRule->Tag); } OSPM_DBGERRORLOG(errorcode, msg); } } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Parse rule matches element, Process it. Change Tables */ /* Parse Results Added for all elements */ errorcode = PTPAddParseResults(parseRule, eInfo, ospvParseResult, ospvDataRef); } if (errorcode == OSPC_ERR_NO_ERROR) { lastResult = *ospvParseResult; newResults = OSPC_OSNULL; /* If rule is derived from another set of rules, then need to parse the element with the new rule. */ if (PTPRuleIsDerived(parseRule)) { errorcode = OSPPASN1ElementParse( eInfo, parseRule->ParseTableId, parseRule, &newResults, parseRule->DataReference); if (errorcode == OSPC_ERR_NO_ERROR) { /* Prefix generated DataRef's with this elements data ref */ PTPResultUpdateDataRef(ospvDataRef, newResults); lastResult->NextResult = newResults; } } else if (PTPRuleIsPrimitive(parseRule)) { /* If the parseTable says this element is a primitive, then the child pointers in the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -