📄 ospasn1.c
字号:
*/ foundCount = 0; /* If there are any parse results left, try to find the primitive described in the rule. */ if (parseResults != OSPC_OSNULL) { /* Get the next element from the results list. */ OSPM_MEMCPY(dataReference, ospvDataReference, OSPC_ASN1_DATAREF_MAXLENGTH); errorcode = PTPDataRefAddRef( dataReference, parseRule->DataReference); if (errorcode == OSPC_ERR_NO_ERROR) { /* First parse result in the list (remainder of list really) should have a matching dataReference */; if (!PTPDataReferencesMatch(dataReference, parseResults->DataReference)) { /* ParseResult not found. Ok if it is optional */ if (!PTPRuleIsOptional(parseRule)) { /* So it isn't optional. Still ok if the element has a default value */ if (!parseRule->HasDefault) { /* Ok, so we have a problem */ errorcode = OSPC_ERR_ASN1_PARSE_ERROR; OSPM_DBGERRORLOG(errorcode, "DataReference mismatch"); } else { /* Rule has default value, so treat as optional */ foundElement = OSPC_OSNULL; } } else { /* Rule is optional, Null thing out */ foundElement = OSPC_OSNULL; } } else { /* Found at least one match. Capture it as root */ foundElement = parseResults->ElementInfo; foundCount ++; } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Hey we found something we were looking for. Capture the first one to the as the head of the list */ newElements = foundElement; /* Update the parse results ptr to consume the found element */ parseResults = parseResults->NextResult; while ( (errorcode == OSPC_ERR_NO_ERROR) && (parseResults != OSPC_OSNULL) ) { /* Is this another match */ if (PTPDataReferencesMatch(dataReference, parseResults->DataReference)) { /* Found another match. Is that ok? */ foundCount++; /* Add the element to the current list */ foundElement->NextElementInfo = parseResults->ElementInfo; /* Update the pointers */ parseResults = parseResults->NextResult; foundElement = foundElement->NextElementInfo; } else { break; } } } } /* Test # of occurances */ if (foundCount < parseRule->MinimumCount) { errorcode = OSPC_ERR_ASN1_PARSE_ERROR; OSPM_DBGERRORLOG(errorcode, "Found less than " "the minimum number of an element"); } else if (foundCount > parseRule->MaximumCount) { errorcode = OSPC_ERR_ASN1_PARSE_ERROR; OSPM_DBGERRORLOG(errorcode, "Found more than " "the maximum number of an element"); } foundCount = 0; foundElement = OSPC_OSNULL; } else { /* If not derived and not primitive, then it must be a constructed rule. */ if (errorcode == OSPC_ERR_NO_ERROR) { /* Check for a match with rule and consume the parse result. */ if (dataRefsMatch) { /* Rule is constructed, but we want to capture the completed encoding */ elementInfo = parseResults->ElementInfo; parseResults = parseResults->NextResult; if (elementInfo->Element != OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } } else { if (PTPResultIsRuleComponent( ospvDataReference, parseResults->DataReference)) { /* Wasn't a match, so assume that the construct needs to be added to the element list, leave the parseResults alone. */ } else { if (!PTPRuleIsOptional(parseRule)) { errorcode = OSPC_ERR_ASN1_PARSE_ERROR; OSPM_DBGERRORLOG(errorcode, "Required Rule has no results on list"); } else { errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; } } } } if (errorcode == OSPC_ERR_NO_ERROR) { if (elementInfo == OSPC_OSNULL) { errorcode = OSPPASN1ElementCreate(&elementInfo); } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Update the element tag with the tag from the parse rule, this element is constructed, so apply the constructed flags */ if (elementInfo->Tag == 0) { elementInfo->Tag = (unsigned char)OSPM_CONSTRUCTED_TAG( parseRule->Tag); } } /* Can't flesh out the rest of the fields until something gets built. That will happen when the rest of the rules are processed. */ newElements = OSPC_OSNULL; } /* If no errors, and there is new data, then add it to the list */ if (errorcode == OSPC_ERR_NO_ERROR) { /* Setup for next iteration */ if (newElements != OSPC_OSNULL) { if (elementInfo == OSPC_OSNULL) { elementInfo = newElements; } else { /* Add the new element(s) to the element list */ if (elementInfo->ContentElementInfo == OSPC_OSNULL) { /* Add it to the beginning of the list */ elementInfo->ContentElementInfo = newElements; } else { /* Add it as a subsequent element */ lastContentElement->NextElementInfo = newElements; } } lastContentElement = newElements; newElements = OSPC_OSNULL; } } } } if (errorcode == OSPC_ERR_ASN1_PARSE_COMPLETE) { errorcode = OSPC_ERR_NO_ERROR; } if (errorcode == OSPC_ERR_NO_ERROR) { *ospvElementInfo = elementInfo; *ospvParseResults = parseResults; errorcode = OSPPASN1ElementEncode(*ospvElementInfo); } return errorcode;}int OSPPASN1ElementFormat( OSPTASN1ELEMENTINFO **ospvElement, unsigned char *ospvTag, unsigned char ospvTagFlags, unsigned ospvTagLength, unsigned char *ospvData, unsigned ospvDataLength){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; unsigned char *lengthBuffer = OSPC_OSNULL; unsigned char *dataBuffer = OSPC_OSNULL; unsigned int dataBufferLength = 0; unsigned lengthLength = 0; unsigned i = 0; /* NOTE: For what we are doing, high order tags are not used. They are not supported. */ errorcode = OSPPASN1SmallInt2UnsignedChar(ospvDataLength, 256, &lengthBuffer, &lengthLength); dataBufferLength = ospvDataLength+ospvTagLength+lengthLength; if (ospvDataLength > 127) { dataBufferLength++; /* Add a byte for length mode indicator if length is >127 */ } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ElementCreate(&eInfo); } if (errorcode == OSPC_ERR_NO_ERROR) { OSPM_MALLOC(dataBuffer, unsigned char, dataBufferLength); if (dataBuffer == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Error allocating encoded element buffer"); } } if (errorcode == OSPC_ERR_NO_ERROR) { /* Initialize the element Info */ /* Set the tag */ if (ospvTagLength != 1) { errorcode = OSPC_ERR_ASN1_UNSUPPORTED_TAG; OSPM_DBGERRORLOG(errorcode, "Unsupported, high-order tag (size != 1) encountered"); } else { eInfo->Tag = (unsigned char)(ospvTag[0] | ospvTagFlags); } if (errorcode == OSPC_ERR_NO_ERROR) { i = 0; dataBuffer[i++] = (unsigned char)(ospvTag[0] | ospvTagFlags); /* Set the length */ if (ospvDataLength > 127) { dataBuffer[i++] = (unsigned char)(lengthLength | OSPC_BER_LENGTH_MODE_LONG); } /* Copy length buffer to the databuffer */ OSPM_MEMCPY(&(dataBuffer[i]), lengthBuffer, lengthLength); i+=lengthLength; /* Copy the data to the databuffer */ OSPM_MEMCPY(&(dataBuffer[i]), ospvData, ospvDataLength); /* Set element pointers appropriately */ eInfo->ContentLength = ospvDataLength; eInfo->Content = (ospvDataLength)? &(dataBuffer[i]) : OSPC_OSNULL; eInfo->Element = dataBuffer; eInfo->ElementLength = (dataBufferLength); eInfo->ElementSpaceAllocated = 1; /* Mark for deletion */ } } if(lengthBuffer != OSPC_OSNULL) { OSPM_FREE(lengthBuffer); } if (errorcode != OSPC_ERR_NO_ERROR) { OSPM_FREE(eInfo); OSPM_FREE(dataBuffer); } else { *ospvElement = eInfo; } return errorcode;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -