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

📄 psyntree.c

📁 基于h323协议的软phone
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
        Copyright (c) 2002 RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION RADVISION Ltd.. No part of this document may be reproduced
in any form whatsoever without written prior approval by RADVISION Ltd..

RADVISION Ltd. reserve the right to revise this publication and make
changes without obligation to notify any person of such revisions or
changes.
***********************************************************************/

/*
  psyntree.c

  Provides access to syntax tree array.

  Syntax tree format:
  ===================

   -- Header:
    - idSize (small) #bytes for id
    - syntax tree offset (id)
    - field array offset (id)
    - from array offset (id)
    - total size (id)

   -- Syntax tree:
    -- list of nodes:
     -- node:
      - type (small). e.g. SEQUENCE, SET, CHOICE.
      - tag (int)
      - tag class (small)
      - extension exists (bool).
      - number of childs (small).
      - from (int)
      - to (int)
      - OF (id). from node list.
      - FROM (id). from from string array.

      - list of childs:
       -- child:
        - field id (id). from field name array.
    - struct id (id). from node list.
    - is optional (bool)
    - is extended (bool)

   -- Field array:
    - list of strings, each terminated by NULL.

   -- from array:
    - list of strings, each terminated by NULL.

  */

#include "rvstdio.h"
#include "rvmemory.h"
#include "psyntreeDef.h"
#include "psyntreeStackApi.h"
#include "psyntreeDb.h"
#include "psyntree.h"

#include "intutils.h"
#include "strutils.h"

#ifdef __cplusplus
extern "C" {
#endif


#define RV_MAX_PATH 300

#define nprn(s) ((s)?(s):"(null)")


char *
pstGetTagClassName(pstTagClass tagClass)
{
    static char const* const asnTags[]=
    {
        "UNIVERSAL", "APPLICATION", "PRIVATE",  "EMPTY"
    };
    if (tagClass<=0 || tagClass > (int)(sizeof(asnTags)/sizeof(*asnTags)))
        return NULL;
    return (char *)asnTags[tagClass-1];
}


RVAPI char* RVCALLCONV /* null terminated token name */
pstGetTokenName(
      IN pstNodeType type)
{
    static char const* const asnTokens[]=
    {
        "INTEGER", "NULL",      "BOOLEAN",  "ENUMERATED",
        "OBJECT IDENTIFIER",    "OCTET STRING",

        "BIT STRING",           "GeneralString",
        "UniversalString",      "BMPString",
        "IA5String",            "VisibleString",
        "NumericString",        "PrintableString",

        "CHOICE",  "SEQUENCE",  "SET",      "OF",
        "SET OF",               "SEQUENCE OF"
    };
    if ((type <= 0) || (type > (int)(sizeof(asnTokens)/sizeof(*asnTokens))))
        return NULL;
    return (char *)asnTokens[type-1];
}





/*_____________________________________child_______________________________________*/


static int /* child index or negative value on failure */
stGetField(
       /* get child by field id (offset) */
       IN  HPST hSyn,
       IN  int nodeId,
       IN  RvInt32 fieldId, /* offset of child name */
       OUT stChildExt **child
       )
{
    stNodeExt* elementBuffer;
    int childNodeId, index;

    if(hSyn==NULL || fieldId<0) return RV_ERROR_UNKNOWN;

    elementBuffer = (stNodeExt *)stGetNodeDataByNodeId(hSyn, nodeId);

    index = stGetChildByNodeAndFieldName(hSyn, nodeId, (int)m_numOfChilds(elementBuffer), fieldId, &childNodeId);
    if (index < 0) return index;

    if (child != NULL)
    {
        *child = (stNodeExt *)stGetNodeDataByNodeId(hSyn, childNodeId);
        if (*child == NULL)
            return RV_ERROR_UNKNOWN;
    }

    return index;
}






/*______________________________display_________________________*/

/************************************************************************
 * pstPrintNode
 * purpose: Print the information of a syntax tree node to a buffer
 * input  : hSyn    - Syntax tree handle
 *          nodeId  - Node to print
 *          buf     - Result buffer to use
 *          len     - Length of buffer
 * output : none
 * return : Number of characters printed
 ************************************************************************/
int pstPrintNode(
    IN  HPST            hSyn,
    IN  RvPstNodeId     nodeId,
    IN  char*           buf,
    IN  int             len)
{
    stNodeExt*  node;
    char*       ptr = buf;
    char*       fromString;
    stNodeExt*  ofNode;

    /* Get the node itself */
    node = (stNodeExt *)stGetNodeDataByNodeId(hSyn, (int)nodeId);
    if (node == NULL) return 0;

    /* 1. GENERAL TYPE */
    ptr += RvSprintf(ptr, "%s ", nprn(pstGetTokenName((pstNodeType)(m_type(node)))));

    /* 2. TAG */
    if (m_tag(node) >= 0)
      ptr += RvSprintf(ptr, "[%s %d] ", nprn(pstGetTagClassName((pstTagClass)m_tagClass(node))), m_tag(node));

    /* 3. FROM-TO */
    if ((m_from(node) > 0) || (m_to(node) > 0))
    {
        /* Check for negative boundaries */
        if (m_from(node) <= m_to(node))
            ptr += RvSprintf(ptr, "(%d..%d) ", m_from(node), m_to(node));
        else
            ptr += RvSprintf(ptr, "(%u..%u)or(%d..%d) ", m_from(node), m_to(node), m_from(node), m_to(node));
    }

    /* 4. FROM CHARS */
    if (m_fromId(node) >= 0)
    {
        fromString = stGetNameByNameId(hSyn, m_fromId(node), NULL);
        if (fromString[0])
            ptr += RvSprintf(ptr, "FROM '%s' ", fromString);
    }

    /* 5. EXTENSION */
    if (m_isExtension(node))
    {
        strcpy(ptr, "... ");
        ptr += 4;
    }

    /* 6. SEQUENCE OF recursion */
    switch (m_type(node))
    {
        case pstOf:
        case pstSequenceOf:
        case pstSetOf:
            ofNode = (stNodeExt *)stGetNodeDataByNodeId(hSyn, m_ofId(node));
            if (ofNode != NULL)
            {
                int printedChars = (ptr - buf);
                return printedChars + pstPrintNode(hSyn, (RvPstNodeId)m_ofId(node), ptr, len - printedChars);
            }
        default:
            break;
    }

    return (ptr - buf);
}

/*______________________________tree_________________________*/


/************************************************************************
 * pstConstruct
 * purpose: Create a PST handle for a type in an ASN.1 definition.
 *          This function uses dynamic allocation when called.
 * input  : syntax      - Syntax buffer to use (the output of the ASN.1
 *                        compiler)
 *                        The buffer is the parameter returned by
 *                        cmEmGetCommonSyntax(), cmEmGetH245Syntax() and
 *                        cmEmGetQ931Syntax()
 *          rootName    - Root name of the type to create a PST handle.
 * output : none
 * return : PST handle on success
 *          NULL on failure
 * examples:
 *      hPst = pstConstruct(cmEmGetQ931Syntax(), (char*)"AliasAddress");
 *      hPst = pstConstruct(cmEmGetH245Syntax(), (char*)"Setup-UUIE.fastStart");
 ************************************************************************/
RVAPI HPST RVCALLCONV
pstConstruct(
    IN  unsigned char*  syntax,
    IN  char*           rootName)
{
    synStruct *syn;
    char rootType[128];
    RvChar* path=NULL;
    int nameId;

    if (!syntax || !rootName) return NULL;

    if(RvMemoryAlloc(NULL, (void**)&syn, sizeof(synStruct)) != RV_OK)
        return NULL;
    memset(syn, 0, sizeof(synStruct));

#if defined(RV_PST_DEBUG)
    strncpy(syn->syntaxName, rootName, sizeof(syn->syntaxName));
#endif
    syn->syntax = (fmt2Struct *)syntax;

    /* handle internal root definition (path handling) */
    if ((path = (RvChar*) strchr(rootName, '.')))
    {
        strncpy(rootType, rootName, (RvSize_t)(path-rootName+1));
        rootType[path-rootName] = 0;
        path++;
    }
    else
    {
        strcpy(rootType, rootName);
    }

    syn->rootNodeId = (RvPstNodeId)stGetNodeIdByName((HPST)syn, rootType);
    syn->rootNameId = stGetNameIdByNodeId((HPST)syn, (int)syn->rootNodeId);

    if (path)
    {
        syn->rootNodeId = (RvPstNodeId)pstGetNodeIdByPath((HPST)syn, path);
        nameId = stGetNameIdByNodeId((HPST)syn, (int)syn->rootNodeId);
        syn->rootNameId = pstGetFieldId((HPST)syn, stGetNameByNameId((HPST)syn, nameId, NULL));
    }

    return (HPST)syn;
}


/************************************************************************
 * pstDestruct
 * purpose: Destruct a PST handle created by pstConstruct().
 * input  : hSyn    - PST handle to destruct
 * output : none
 * return : Non-negative value on success
 *          Negative value on failure
 ************************************************************************/
RVAPI int RVCALLCONV
pstDestruct(IN HPST hSyn)
{
    synStruct *syn = (synStruct *)hSyn;

    if (!syn) return RV_ERROR_UNKNOWN;
    RvMemoryFree(syn);
    return RV_TRUE;
}


⌨️ 快捷键说明

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