📄 ospasn1.c
字号:
element Infos can be ignored. THERE MIGHT BE children, but the parsetable is not interested in the children, this is the case for an algorithm id - all we might be interested in an algorithm id for is for it's DER value for the entire structure. */ /* If primitive can be a list (max >= 1), then all peers of this element should be the same element and should all be added to the list. Add Parse Result assumes this and adds a result for the element passed, and then traverses the element's "next" pointer to add entries for each of the elements it finds. It returns when an element's "next" pointer is NULL. */ errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } else { /* The rule has sub-rules, process each of them */ /* Parse rule is for a constructed data type. These types are SEQUENCE, SEQUENCE-OF, SET, SET-OF, or CONSTRUCTED (contruction is derived from another parse table). */ /* Set an pointer to the element's child pointer. Follow the child pointer to find first element in the construction. Parse this element using the next parse rule in the table. Continue parsing elements, following NEXT pointers until NEXT is null. Use the next parse rule in the table for each jump following the next pointer. Always follow each rule left in the parse table until you run out, even if the NEXT pointer to be passed into the parse routine is NULL. If the rule REQUIRES a value, then an error will occur and the parse will fail. If the values are optional, then all is well. There should NOT be any elements left (following NEXT pointers) when the rules run out. If there is, then it is a parse error. */ /* Set pointer to first child component, other children are found by following this child's NEXT pointer.*/ eInfoContent = eInfo->ContentElementInfo; /* Loop until all children (following first child's NEXT pointers, are consumed */ while(errorcode == OSPC_ERR_NO_ERROR) { /* Get the next rule to follow */ errorcode = PTPTableGetRule(ospvParseTableId, &parseRule, &nextRule); if (errorcode == OSPC_ERR_NO_ERROR) { /* Rule might have been found. If not, then the eInfoContent pointer MUST be NULL */ if (parseRule == OSPC_OSNULL) { if (eInfoContent != OSPC_OSNULL) { /* No rule, and there are still more elements left to process - oh no! Maybe there are more than one of these allowed? Try that.*/ if ((--maxCount) > 0) { /* How about that, more allowed */ nextRule = 1; errorcode = PTPTableGetRule(ospvParseTableId, &parseRule, &nextRule); } else { errorcode = OSPC_ERR_ASN1_PARSE_ERROR; OSPM_DBGERRORLOG(errorcode, "Rules ran out before elements did."); } } else { /* No rules, no elements, Success */ errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } } } if (errorcode == OSPC_ERR_NO_ERROR) { /* We have at least a Rule and an ElementInfo (possible NULL). Let's try to parse it */ newResults = OSPC_OSNULL; if (PTPRuleIsPrimitive(parseRule)) { /* The element is primitive, save it on results */ if (errorcode == OSPC_ERR_NO_ERROR) { /* Parse Results Added */ errorcode = PTPAddParseResults(parseRule, eInfoContent, &newResults, parseRule->DataReference); } } else { errorcode = PTPRuleGetParseTableId( parseRule, &parseTableId); if (errorcode == OSPC_ERR_NO_ERROR) { /* Try to parse the element */ errorcode = OSPPASN1ElementParse( eInfoContent, parseTableId, parseRule, &newResults, parseRule->DataReference); } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Get ready to do next element */ if (eInfoContent != OSPC_OSNULL) { eInfoContent = eInfoContent->NextElementInfo; } } if (errorcode == OSPC_ERR_ASN1_PARSE_DEFAULTED) { /* Current element was not processed - Keep going through the loop with the same content - Result was created - indicating default value is used */ errorcode = OSPC_ERR_NO_ERROR; } if (errorcode == OSPC_ERR_NO_ERROR) { /* Prefix this element's DataRef value to results */ if (newResults != OSPC_OSNULL) { PTPResultUpdateDataRef(ospvDataRef, newResults); } } if (errorcode == OSPC_ERR_NO_ERROR) { /* If return parse results value is null, set it */ if (*ospvParseResult == OSPC_OSNULL) { /* First time for this element */ *ospvParseResult = newResults; lastResult = newResults; } else { /* Add another batch of results */ lastResult->NextResult = newResults; } /* Find the end of the current result list */ for (; lastResult->NextResult != OSPC_OSNULL; lastResult = lastResult->NextResult); } } } /* If successfully terminated parsing, then fix errorcode */ if (errorcode == OSPC_ERR_ASN1_PARSE_COMPLETE) { errorcode = OSPC_ERR_NO_ERROR; } } } /* errorcode will be a real error or OSP_ERR_ASN1_PARSE_COMPLETE */ if (errorcode == OSPC_ERR_ASN1_PARSE_COMPLETE) { errorcode = OSPC_ERR_NO_ERROR; } return errorcode;}void OSPPASN1ElementParseDelete( OSPTASN1PARSERESULT **ospvParseResults){ PTPResultsDelete(ospvParseResults); return;}intOSPPASN1ElementCopy( OSPTASN1ELEMENTINFO **ospvDstElement, OSPTASN1ELEMENTINFO *ospvSrcElement){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; /* NOTE: This function does not copy an entire element TREE. It only copies the the data for the element. */ /* Create a new element. */ errorcode = OSPPASN1ElementCreate(&eInfo); /* Copy the contents of the src element into the dst element. */ if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MEMCPY(eInfo, ospvSrcElement, sizeof(OSPTASN1ELEMENTINFO)); /* Create A buffer for the element data */ errorcode = OSPPASN1ElementCopyElementData(ospvSrcElement, &(eInfo->Element), &(eInfo->ElementLength)); } if (errorcode != OSPC_ERR_NO_ERROR) { /* Get rid of allocated space */ if (eInfo->Element) { OSPM_FREE(eInfo->Element); } if (eInfo) { OSPM_FREE(eInfo); } } else { /* Finish off the new element */ /* Create a pointer to the content buffer */ eInfo->ElementSpaceAllocated = 1; eInfo->Content = eInfo->Element+ (eInfo->ElementLength - eInfo->ContentLength); /* Null pointers:ContentElementInfo, NextElementInfo, NextElement*/ eInfo->ContentElementInfo = OSPC_OSNULL; eInfo->NextElementInfo = OSPC_OSNULL; *ospvDstElement = eInfo; } return errorcode;}intOSPPASN1ElementDeparse( OSPTASN1ELEMENTINFO **ospvElementInfo, OSPTASN1PARSERESULT **ospvParseResults, OSPEASN1PARSETABLEID ospvParseTableId, unsigned char *ospvDataReference){ int errorcode = OSPC_ERR_NO_ERROR; int dataRefsMatch = 0; unsigned nextRule = 0; OSPTASN1ELEMENTINFO *elementInfo = OSPC_OSNULL; OSPTASN1ELEMENTINFO *lastContentElement = OSPC_OSNULL; OSPTASN1PARSERULE *parseRule = OSPC_OSNULL; OSPTASN1PARSERESULT *parseResults = OSPC_OSNULL; OSPTASN1ELEMENTINFO *newElements = OSPC_OSNULL; OSPTASN1ELEMENTINFO *foundElement = OSPC_OSNULL; unsigned char dataReference[OSPC_ASN1_DATAREF_MAXLENGTH]; unsigned foundCount = 0; if (ospvElementInfo != OSPC_OSNULL) { elementInfo = *ospvElementInfo; } if (ospvParseResults != OSPC_OSNULL) { parseResults = *ospvParseResults; } /* Loop through the parse table entries */ for (nextRule = 0; errorcode == OSPC_ERR_NO_ERROR;) { /* Get the next rule - done if it returns PARSE_COMPLETE */ errorcode = PTPTableGetRule(ospvParseTableId, &parseRule, &nextRule); if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MEMCPY(dataReference, ospvDataReference, OSPC_ASN1_DATAREF_MAXLENGTH); dataRefsMatch = PTPDataReferencesMatch( ospvDataReference, parseResults->DataReference); /* Figure out what to do with this type of rule */ if (PTPRuleIsDerived(parseRule) && !dataRefsMatch) { /* Need to process these through another table. Won't add anything to element list for this rule right now */ /* Copy and update datareference with the value of the next element, start with a fresh copy - these guys are brothers */ errorcode = PTPDataRefAddRef( dataReference, parseRule->DataReference); if (errorcode == OSPC_ERR_NO_ERROR) { /* Deparse this section of the element list */ errorcode = OSPPASN1ElementDeparse(&newElements, &parseResults, parseRule->ParseTableId, dataReference); } } else if (PTPRuleIsPrimitive(parseRule)) { /* Rule is a primitive or a DER format element (one that would break down further if the parse rules called for it). There should be an entry in the result table for data reference for this element. If the result is not found, and it is optional, then skip it and process the next rule. If it is not optional, report an error. If the result is found, there may be more than one. If the rules allow for this, then add each one in order. They should follow each other in the result list. If more than one is not allowed, then report an error. If a result is found, then link it into the current position in the element list.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -