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

📄 osptnepinit.c

📁 mgcp协议源代码。支持多种编码:g711
💻 C
📖 第 1 页 / 共 5 页
字号:
            retVal = OSPC_ERR_ENROLL_INVALID_ARG;            OSPM_DBGERRORLOG(                 retVal,                 "The hex string on input is not entirely hex characters.\n" );            OSPM_DBGMISC((                 "character %c at offset %d of %s is not a hex character\n",                ospvHexStr[ hexStrIndex ],                hexStrIndex,                ospvHexStr ));        }    }    /* Try to allocate memory for the chunk of data that will be used for     * translating the hex to binary. Set an error code and complain      * if we can't.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MALLOC( nextByte, unsigned char, 3 );        if ( nextByte == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_NO_MEMORY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to allocate memory for translating hex ascii strings to binary.\n" );        }    }    /* If ( all of the parameters are ok ) then     *  o reset the length of the binary output string.     *  o initialize the memory for the binary string.     *  o for ( every octet in the hex  string that we want to copy ) do     *      - copy the next octet into some other space     *      - turn it into a long int.     *      - take the last 8 bits ( wherever they may be ) and put them     *        at the end of the binary string; increment the length of the     *        binary string by one.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        *ospvBinaryStrLen = 0;        OSPM_MEMSET( nextByte, 0, 3 );        for ( hexStrIndex = 0; ( hexStrIndex < ospvHexStrLen ); hexStrIndex+=2 )        {            OSPM_SPRINTF( nextByte, "%.2s", ospvHexStr + hexStrIndex );             nextWord = OSPM_STRTOL( nextByte, NULL, 16 );            OSPM_DBGMISC((                 "nextWord: %ld, %d\n",                 nextWord,                 ( nextWord  && OSPC_LONG_TO_BYTE_MASK ) ));            ospvBinaryStr[ (*ospvBinaryStrLen)++ ] =                 ( nextWord & OSPC_LONG_TO_BYTE_MASK );        }        OSPM_DBGMISC(( "final binary encoding:\n" ));        OSPPDumpHex( ospvBinaryStr, *ospvBinaryStrLen );    }    /* Free up any memory we allocated: */    if ( nextByte != OSPC_OSNULL )    {        OSPM_FREE( nextByte );    }    return retVal;}/* Given the response to scan and the field to search for, extract the * contents of that field into the outgoing value. We'll be careful * not to accidentally use names that look similar but which have different * lengths. For example, if we're looking for the field "def" and we're * given "abcdef=ghi&def=jkl", then we want to return "jkl" and not * "ghi". If we find an appropriate field, then we'll save it in  * the outgoing pointer ( which we'll also allocate. ) * * Input: the response to be scanned, the field to look for, and a pointer *        to a string that will contain the field's value ( if we find it. ) * * Output: *ospvValueOut will be non-null and will contain the contents of *         the field if the field can be found and is non-empty. Otherwise, *         if the field is empty or can't be found, then an error code *         other than OSPC_ERR_NO_ERROR will be returned and *ospvValueOut *         will be untouched. */int OSPPExtractFieldFromResponse (    unsigned char*  ospvResponse,    unsigned        ospvResponseLen,    unsigned char*  ospvSearchField,    unsigned char** ospvValueOut ){    int retVal = OSPC_ERR_NO_ERROR;    /* This will point to the value that we find, if we find it: */    unsigned char* beginValuePtr = OSPC_OSNULL;    /* This points to the name that we're trying to match on: */    unsigned char* nextNamePtr   = OSPC_OSNULL;    /* Flag for determining whether or not we found a value: */    OSPTBOOL       valueFound    = OSPC_FALSE;    OSPM_DBGENTER(( "ENTER: OSPPExtractFieldFromResponse\n" ));    /* Check for any null or empty parameters passed in first.     * We'll be allocating *ospvValueOut, so don't check its contents     * ( just check that ospvValueOut isn't null. )     */    if ( ( ospvResponse == OSPC_OSNULL ) ||         ( ospvResponseLen <= 0 ) ||         ( ospvSearchField == OSPC_OSNULL ) ||         ( ospvValueOut == OSPC_OSNULL ) ||         ( OSPM_STRLEN( ospvSearchField ) <= 0 )        )    {        retVal = OSPC_ERR_ENROLL_INVALID_ARG;        OSPM_DBGERRORLOG(             retVal,             "Invalid arguments for searching for a field\n" );    }    /* We know that the parameters are valid, so allocate enough memory     * to store the value that we find. The maximum possible size is     * strlen( ospvResponse ) - 2, which would happen if the response     * was a one-field value with a one-character name ( such as     * "a=1073741824" .) So, go ahead and allocate the size of the string.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MALLOC( beginValuePtr, unsigned char, ospvResponseLen );        if ( beginValuePtr == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_NO_MEMORY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to allocate memory for saving the field's value.\n" );        }    }    /* If ( we could allocate the memory ) then     *  o get the beginning of the string     *  o do:     *      - find the first occurrence of a name in the string.     *      - if ( the occurrence is null ) then     *          o we're done; we couldn't find a name-value pair for the     *            given name.     *      - otherwise:     *          o check the name to see if it matches a name-value pair     *            ( that is, we don't want to use "abcd=efgh" if we're looking     *              for the name "bc" )     *          o if ( we matched on one string ) then we must have found     *            something for a name and a value.     *              - if ( the value is null ) then     *                  o the value we found here is empty; we can't use it.     *              - else if ( the name was at the beginning of the response )     *                     OR ( the previous character is the field delimiter )     *                  o then we know that we've actually found the name     *                    and hence the correct value. We know that we      *                    haven't matched on "abcd=efgh" if we're looking     *                    for the name "bc", since we compare the name we     *                    found to the beginning of the string. Likewise,     *                    cases like "abcd=efgh&ijkl=mnop" will fall through     *                    the cracks if we're looking for "jk" ( since      *                    'i' != '&' )     *       - if ( we didn't find anything on this iteration ) then     *           o increment the pointer so that we don't get the same case.     *     while ( we haven't found anything ) and      *           ( we haven't run into any problems )     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        nextNamePtr = ospvResponse;        do        {            nextNamePtr = OSPM_STRSTR( nextNamePtr, ospvSearchField );            if ( nextNamePtr == OSPC_OSNULL )            {                retVal = OSPC_ERR_ENROLL_SEARCH_FIELD_NOT_FOUND;            }            if ( retVal == OSPC_ERR_NO_ERROR )            {				OSPM_DBGMISC(( "nextNamePtr: <%s>\n", nextNamePtr ));                if ( OSPM_SSCANF(                          nextNamePtr,                          OSPC_ENROLL_SEARCH_PATTERN,                          beginValuePtr )                     > 0 )                {                    if ( beginValuePtr == OSPC_OSNULL )                    {                        /* Do nothing; the field value was empty. */						OSPM_DBGMISC(( "The value for this field is empty; ignonring current value.\n" ));                    }                        else if ( ( nextNamePtr == ospvResponse ) ||                              ( OSPM_STRCHR( OSPC_ENROLL_URL_FIELD_DELIMITERS, *(nextNamePtr-1) ) ) )                     {                        valueFound = OSPC_TRUE;                    }					else					{						OSPM_DBGMISC(( "False hit.\n" ));					}                }				else				{					OSPM_DBGMISC(( "The given attribute was a false hit.\n" ));				}            }            /* Make sure that we don't get the same case again: */            if ( valueFound != OSPC_TRUE )            {                nextNamePtr++;            }        } while ( ( valueFound == OSPC_FALSE ) &&                   ( retVal == OSPC_ERR_NO_ERROR ) );    }    if ( ( valueFound != OSPC_TRUE ) ||          ( beginValuePtr == OSPC_OSNULL ) )    {        retVal = OSPC_ERR_ENROLL_SEARCH_FIELD_NOT_FOUND;        OSPM_DBGERRORLOG(             retVal,             "Unable to find the field requested.\n" );        OSPM_DBGMISC(( "Unable to find field <%s> in <%s>\n", ospvSearchField, ospvResponse ));    }    /* Copy the contents of the string found into the outgoing string if     * we found something we can use:     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        retVal = OSPPCopyString( ospvValueOut, beginValuePtr );        if ( retVal != OSPC_ERR_NO_ERROR )        {            OSPM_DBGERRORLOG(                 retVal,                 "Unable to copy value found into the outgoing pointer.\n" );        }    }    /* Now free up the pointer we found, regardless of whether or not     * we were successful:     */    if ( beginValuePtr != OSPC_OSNULL )    {        OSPM_FREE( beginValuePtr );    }    OSPM_DBGEXIT(( "EXIT: OSPPExtractFieldFromResponse\n" ));    return retVal;}/* copy the source string to the destination string; allocate and initialize * enough space in the destination string that we can fit everything in * the source string. * * Input: pointer to the destination string and the source string to be *        copied. * * Output: *ospvDestStr should be a replica of ospvSrcStr if everything goes *         well; in this case, OSPC_ERR_NO_ERROR is returned. Otherwise, *         an error code other than OSPC_ERR_NO_ERROR is returned. */int OSPPCopyString (    unsigned char** ospvDestStr,    unsigned char*  ospvSrcStr){    int retVal = OSPC_ERR_NO_ERROR;    /* Don't assign the source string to the destination string      * until the end; we'll perform all of our operations on this      * string instead:      */    unsigned char* tmpDestStr = OSPC_OSNULL;    /* This is the length of the source string: */    int            srcStrLen  = 0;        OSPM_DBGENTER(( "ENTER OSPPCopyString\n" ));    /* Check for valid inputs first: */    if ( ( ospvDestStr == OSPC_OSNULL ) ||         ( ospvSrcStr == OSPC_OSNULL ) )    {        retVal = OSPC_ERR_ENROLL_INVALID_ARG;        OSPM_DBGERRORLOG(             retVal,             "The parameters for copying a string were invalid.\n" );    }    /* if ( the parameters look ok ) then     *  o get the length of the source string     *  o if ( the source is empty ) then     *      - set an error code and complain.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        srcStrLen = OSPM_STRLEN( ospvSrcStr );        if ( srcStrLen <= 0 )        {            retVal = OSPC_ERR_ENROLL_SOURCE_STRING_EMPTY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to copy empty source string.\n" );        }    }    /* If ( the source string is ok ) then     *  o allocate enough memory for the output string     *  o if ( we couldn't do it ) then     *      - complain     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MALLOC( tmpDestStr, unsigned char, srcStrLen + 1 );        if ( tmpDestStr == OSPC_OSNULL )        {            retVal = OSPC_ERR_ENROLL_NO_MEMORY;            OSPM_DBGERRORLOG(                 retVal,                 "Unable to allocate memory for destination string.\n" );        }    }    /* If ( we could allocate the memory ) then     *  o initialize the memory     *  o copy the strings     *  o assign the string created to the outgoing pointer.     */    if ( retVal == OSPC_ERR_NO_ERROR )    {        OSPM_MEMSET( tmpDestStr, 0, srcStrLen + 1 );         OSPM_STRCPY( tmpDestStr, ospvSrcStr );        *ospvDestStr = tmpDestStr;    }    /* Else we had some problem so     *  o free up any memory we may have allocated:     */    else    {        if ( tmpDestStr != OSPC_OSNULL )        {            OSPM_FREE( tmpDestStr );        }    }    OSPM_DBGEXIT(( "EXIT OSPPCopyString\n" ));    return retVal;}

⌨️ 快捷键说明

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