⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 per.c

📁 基于h323协议的软phone
💻 C
📖 第 1 页 / 共 3 页
字号:


/*
Desc: Encode buffer as an open type starting at offset.
10.2

*/
int
perEncodeOpenType(IN  HPER hPer,
                  IN  RvInt32 offset,  /* beginning of open type encoding */
                  IN  RvInt32 fieldId)
{
    perStruct *per =(perStruct *)hPer;
    RvUint8 octets[64]; /* space for tmp encoding */
    RvInt32 encLen = bbBitsInUse(per->hBB) - offset; /* encoding length */
    HBB tmpBB; /* for length encoding */
    RvInt32 tmpBBlen;
    if (fieldId);

#ifdef RV_CODER_DEBUG
    if (bbGetAllocationSize(10) >(int)sizeof(octets))
    {
        RvLogExcep(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeOpenType: Allocation space for length not enough [%d].",
            bbGetAllocationSize(10)));
        return RV_ERROR_UNKNOWN;
    }
#endif  /* RV_CODER_DEBUG */
    tmpBB = bbConstructFrom(10, (char *)octets, sizeof(octets));

    /* -- 10.2.2: length in octets */
    if (perEncodeLen(perLenTypeUNCONSTRAINED, bbSetByte(encLen), 0, 0, tmpBB) <0)
    {
        RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeOpenType: length encoding failed for '%s' [%ld].",
            pstGetFieldNamePtr(per->hSyn, fieldId), encLen));
        return RV_ERROR_UNKNOWN;
    }

    /* -- add length before open field encoding */
    /* move encoding tmpBBlen bits ahead */
    tmpBBlen = bbBitsInUse(tmpBB);
    if (bbMove(per->hBB, offset, encLen, offset + tmpBBlen) < 0)
        return RV_ERROR_UNKNOWN;
    if (bbSet(per->hBB, offset, tmpBBlen, bbOctets(tmpBB)) >= 0)
    {
        RvLogDebug(&rvPerLogSource, (&rvPerLogSource,
            "Encoding OPENTYPE: inserted %d bits in [%d].", tmpBBlen, offset));
    }
    else
    {
        RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeOpenType: set failed for '%s' [%d].",
            pstGetFieldNamePtr(per->hSyn, fieldId), encLen));
        return RV_ERROR_UNKNOWN;
    }

    return RV_TRUE;
}


/*____________________________________________________________________________*/
/*___________________________NODE_____________________________________________*/
/*____________________________________________________________________________*/

/* Desc: encode a node in the static syntax tree.
Logic: switch between types.
*/
int
perEncNode(IN  HPER hPer,
           IN  int synParent,
           IN  int valParent,
           IN  RvInt32 fieldId,
           IN  RvBool wasTypeResolvedInRunTime)
{
    perStruct *per =(perStruct *)hPer;
    RvInt32 value=-1;
    int ret = RV_ERROR_UNKNOWN;
    RvInt32 offset=-1;
    pstNodeType type;
    int  to,from;
    RvBool toAbsent,fromAbsent;


    if ((type=pstGetNodeType(per->hSyn,synParent)) <0)
    {
        RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeNode: Illegal syntax node: [%s:%d].",
            pstGetFieldNamePtr(per->hSyn, fieldId), synParent));
        return RV_ERROR_UNKNOWN;
    }

    if (pvtGet(per->hVal, valParent, NULL, NULL, &value, NULL) <0)
    {
        RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeNode: Illegal value node: [%s:%d].",
            pstGetFieldNamePtr(per->hSyn, fieldId), valParent));
        return RV_ERROR_UNKNOWN;
    }
    pstGetNodeRangeExt(per->hSyn,synParent,&from,&to,&fromAbsent,&toAbsent);
    per->isOuterMostValueEmpty = RV_FALSE;

    RvLogDebug(&rvPerLogSource, (&rvPerLogSource,
        "Encoding %s: %s [%d].",
        (fieldId >= 0) ? pstGetFieldNamePtr(per->hSyn, fieldId):"(null)",
        nprn(pstGetTokenName(type)),
        bbBitsInUse(per->hBB)));

    if (IS_OPEN_TYPE(synParent))
    {
        RvUint8 ch = 0;
        if (bbAddTail(per->hBB, &ch, 0, RV_TRUE) < 0) /* align buffer */
            return RV_ERROR_UNKNOWN;
        offset = bbBitsInUse(per->hBB);
    }

    switch (type)
    {
        case pstNull:
            per->isOuterMostValueEmpty = RV_TRUE;
            ret = RV_TRUE;
            break;
        case pstInteger:
            ret = perEncodeInt((RvUint32)value, from,to, fromAbsent, toAbsent,
                pstGetIsExtended(per->hSyn,synParent), per->hBB);
            if (from == to && !fromAbsent && !toAbsent)
                per->isOuterMostValueEmpty = RV_TRUE;
            break;
        case pstBoolean:
            ret = perEncodeBool(((value) ? (RV_TRUE):(RV_FALSE)), per->hBB);
            break;

        case pstUniversalString:
        case pstGeneralString:
        case pstBMPString:
        case pstIA5String:
        case pstNumericString:
        case pstPrintableString:
        case pstVisibleString:
            ret = perEncodeCharString(hPer, synParent, valParent, fieldId);
            break;
        case pstOctetString:
            ret = perEncodeOctetString(hPer, synParent, valParent, fieldId);
            break;

        case pstBitString:
            ret = perEncodeBitString(hPer, synParent, valParent, fieldId);
            break;

        case pstObjectIdentifier:
            ret = perEncodeOID(hPer, synParent, valParent, fieldId);
            break;

        case pstEnumeration:
        case pstChoice:
            ret = perEncodeChoice(hPer, synParent, valParent, fieldId);
            break;

        case pstSet:
        case pstSequence:
            ret = perEncodeSequece(hPer, synParent, valParent, fieldId);
            break;

        case pstSetOf:
        case pstSequenceOf:
            ret = perEncodeSequeceOF(hPer, synParent, valParent, fieldId);
            break;
        default:
            RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
                "perEncodeNode: %s TYPE unrecognized: %s.",
                pstGetFieldNamePtr(per->hSyn, fieldId),
                nprn(pstGetTokenName(type))));
            return RV_ERROR_UNKNOWN;
    }

    if (ret >= 0)
    {
        if (IS_OPEN_TYPE(synParent))
        {
            perEncodeComplete(hPer, (int)bbBitsInUse(per->hBB) - offset);
            ret = perEncodeOpenType(hPer, offset, fieldId);
        }
    }

    if (ret < 0)
    {
        RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
            "perEncodeNode: '%s' %s encoding Error!.",
            pstGetFieldNamePtr(per->hSyn, fieldId),
            nprn(pstGetTokenName(type))));
        return RV_ERROR_UNKNOWN;
    }

    return RV_TRUE;
}



int perPushElemToArrayOfSpecialNodes(
                                     IN HPER hPer,
                                     IN int element
                                     )
{
    perStruct *per =(perStruct *)hPer;
    if (per->currentPositionInArrayOfSpecialNodes == MAX_SPECIAL_NODES)
        return RV_ERROR_UNKNOWN;
    per->arrayOfSpecialNodes[per->currentPositionInArrayOfSpecialNodes++] = element;
    return RV_TRUE;
}


int perPopElemFromArrayOfSpecialNodes(
                                      IN HPER hPer
                                      )
{
    perStruct *per =(perStruct *)hPer;
    if (per->currentPositionInArrayOfSpecialNodes == 0)
        return RV_ERROR_UNKNOWN;
    per->currentPositionInArrayOfSpecialNodes--;
    return RV_TRUE;
}


int perGetDependentTypeForDecoding(
                                   IN HPER hPer,
                                   IN int specialTypeNodeId, /* nodeid of "special" type */
                                   OUT int* dependentTypeNodeId /* nodeid of "real" type */
                                   )
{
    int i, objectId;
    pstTypeFromConstraint specialType;
    pstChild field;
    pstConstrainingField constrainingField;
    pstFieldOfObjectReference fieldOfObject;
    perStruct *per =(perStruct *)hPer;

    /* getting special type */
    pstGetTypeFromConstraint(per->hSyn, specialTypeNodeId, &specialType);

    /* getting constraining field */
    pstGetConstrainingField(per->hSyn, specialTypeNodeId, &constrainingField);

    /* getting field itself(node) */
    /* here very intimate knowledge of the internal database is used*/
    /* it is known that fields are placed at nodeId+fieldIndex position */

    pstGetChild(per->hSyn, 0, constrainingField.fieldId, &field);

    if (pvtFindObject(per->hVal,
           per->arrayOfSpecialNodes[per->currentPositionInArrayOfSpecialNodes - 1 -
                                    constrainingField.relativeIndex],
           per->hSyn, specialType.objectSetId, &objectId) == RV_ERROR_UNKNOWN)      /* finding object */
           /* object was not found */
           return (RV_PST_ERROR_UNDEFINED_TYPE);


    for (i = 1;; i++)                                                           /* finding field of object */
    {
        if (pstGetFieldOfObjectReference(per->hSyn, objectId, i, &fieldOfObject) < 0)
            return RV_ERROR_UNKNOWN;
        if (fieldOfObject.fieldInClassNumber == specialType.fieldInClassNumber)
            break;
    }

    if (dependentTypeNodeId)
        *dependentTypeNodeId = fieldOfObject.settingId;
    return fieldOfObject.settingId;
}



int
perDecNode(IN  HPER hPer,
           IN  int synParent,
           IN  pstFieldSpeciality speciality,
           IN  int valParent,
           IN  RvInt32 fieldId)
{
    perStruct *per =(perStruct *)hPer;
    RvUint32 value = 0;
    int i, ret = RV_ERROR_UNKNOWN,  vtPath=-1, startListLength, finishListLength;
    RvUint32 dec = 0;
    RvBool boola = RV_FALSE, wasTypeResolvedInRunTime = RV_FALSE;
    RvInt32 saveLocation=-1;
    RvUint32 fieldLen = 0;
    pstNodeType type = (pstNodeType)0;
    int  to=0, from=0;
    RvBool toAbsent=RV_TRUE, fromAbsent=RV_TRUE;
    int tmpSynParent = synParent;

    if (valParent < 0)
    {
        per->encodingDecodingErrorBitMask |= encDecErrorsMessageIsInvalid;
        return RV_ERROR_UNKNOWN;
    }

    if (speciality == pstDependingDependent || speciality == pstDependent)
    {
        if ((tmpSynParent = perGetDependentTypeForDecoding(hPer, tmpSynParent, &tmpSynParent)) == RV_ERROR_UNKNOWN)
            return RV_ERROR_UNKNOWN;
        wasTypeResolvedInRunTime = RV_TRUE;

        if (tmpSynParent == RV_PST_ERROR_UNDEFINED_TYPE)
        {
            /* Seems like H.450 message contains parts we can't understand. */
            RvLogWarning(&rvPerErrLogSource, (&rvPerErrLogSource,
                "perDecodeNode: Uknown dependent type: [%s:%d].",
                pstGetFieldNamePtr(per->hSyn, fieldId), synParent));
        }
    }

    if (tmpSynParent != RV_PST_ERROR_UNDEFINED_TYPE)
    {
        pstGetNodeRangeExt(per->hSyn,tmpSynParent,&from,&to,&fromAbsent,&toAbsent);
        type=pstGetNodeType(per->hSyn,tmpSynParent);

        if (type < 0)
        {
            RvLogError(&rvPerErrLogSource, (&rvPerErrLogSource,
                "perDecodeNode: Illegal syntax node: [%s:%d].",
                pstGetFieldNamePtr(per->hSyn, fieldId), tmpSynParent));
            return RV_ERROR_UNKNOWN;
        }

        RvLogDebug(&rvPerLogSource, (&rvPerLogSource,
            "Decoding %s: %s [%ld].",
            pstGetFieldNamePtr(per->hSyn, fieldId), nprn(pstGetTokenName(type)),
            per->decodingPosition));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -