VirtualBox

Changeset 60028 in vbox


Ignore:
Timestamp:
Mar 15, 2016 10:39:16 AM (9 years ago)
Author:
vboxsync
Message:

iprt: Added simple (and untested) RTAsn1Integer_ToString implemented. Added spacing flags to RTStrPrintHexBytes (also untested).

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/asn1.h

    r59665 r60028  
    831831RTDECL(int) RTAsn1Integer_FromBigNum(PRTASN1INTEGER pThis, PCRTBIGNUM pBigNum, PCRTASN1ALLOCATORVTABLE pAllocator);
    832832
     833/**
     834 * Converts the integer to a string.
     835 *
     836 * This will produce a hex represenation of the number.  If it fits in 64-bit, a
     837 * C style hex number will be produced.  If larger than 64-bit, it will be
     838 * printed as a space separated string of hex bytes.
     839 *
     840 * @returns IPRT status code.
     841 * @param   pThis               The ASN.1 integer.
     842 * @param   pszBuf              The output buffer.
     843 * @param   cbBuf               The buffer size.
     844 * @param   fFlags              Flags reserved for future exploits. MBZ.
     845 * @param   pcbActual           Where to return the amount of buffer space used
     846 *                              (i.e. including terminator). Optional.
     847 *
     848 * @remarks Currently assume unsigned number.
     849 */
     850RTDECL(int) RTAsn1Integer_ToString(PRTASN1INTEGER pThis, char *pszBuf, size_t cbBuf, uint32_t fFlags, size_t *pcbActual);
     851
    833852RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFINTEGERS, RTASN1INTEGER, RTDECL, RTAsn1SeqOfIntegers);
    834853RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFINTEGERS, RTASN1INTEGER, RTDECL, RTAsn1SetOfIntegers);
  • trunk/include/iprt/mangling.h

    r59972 r60028  
    24842484# define RTAsn1Integer_InitU64                          RT_MANGLER(RTAsn1Integer_InitU64)
    24852485# define RTAsn1Integer_ToBigNum                         RT_MANGLER(RTAsn1Integer_ToBigNum)
     2486# define RTAsn1Integer_ToString                         RT_MANGLER(RTAsn1Integer_ToString)
    24862487# define RTAsn1Integer_UnsignedCompare                  RT_MANGLER(RTAsn1Integer_UnsignedCompare)
    24872488# define RTAsn1Integer_UnsignedCompareWithU32           RT_MANGLER(RTAsn1Integer_UnsignedCompareWithU32)
  • trunk/include/iprt/string.h

    r59067 r60028  
    28282828 *
    28292829 * @param   pszBuf      Output string buffer.
    2830  * @param   cchBuf      The size of the output buffer.
     2830 * @param   cbBuf       The size of the output buffer.
    28312831 * @param   pv          Pointer to the bytes to stringify.
    28322832 * @param   cb          The number of bytes to stringify.
     
    28342834 * @sa      RTUtf16PrintHexBytes.
    28352835 */
    2836 RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
     2836RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
    28372837/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
    28382838 * @{ */
    28392839/** Upper case hex digits, the default is lower case. */
    28402840#define RTSTRPRINTHEXBYTES_F_UPPER      RT_BIT(0)
     2841/** Add a space between each group. */
     2842#define RTSTRPRINTHEXBYTES_F_SEP_SPACE  RT_BIT(1)
     2843/** Add a colon between each group. */
     2844#define RTSTRPRINTHEXBYTES_F_SEP_COLON  RT_BIT(2)
    28412845/** @} */
    28422846
  • trunk/src/VBox/Runtime/common/asn1/asn1-ut-integer.cpp

    r57358 r60028  
    55
    66/*
    7  * Copyright (C) 2006-2015 Oracle Corporation
     7 * Copyright (C) 2006-2016 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    338338
    339339
     340RTDECL(int) RTAsn1Integer_ToString(PRTASN1INTEGER pThis, char *pszBuf, size_t cbBuf, uint32_t fFlags, size_t *pcbActual)
     341{
     342    AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
     343    AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
     344
     345    /*
     346     * We only do hex conversions via this API.
     347     * Currently we consider all numbers to be unsigned.
     348     */
     349    /** @todo Signed ASN.1 INTEGER. */
     350    int rc;
     351    size_t cbActual;
     352    if (pThis->Asn1Core.cb <= 8)
     353    {
     354        cbActual = 2 + pThis->Asn1Core.cb*2 + 1;
     355        if (cbActual <= cbBuf)
     356        {
     357            ssize_t cchFormat = RTStrFormatU64(pszBuf, cbBuf, pThis->uValue.u, 16, cbActual - 1 /*cchWidth*/, 0,
     358                                               RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
     359            AssertStmt(cchFormat == (ssize_t)cbActual - 1, rc = VERR_INTERNAL_ERROR_3);
     360        }
     361        else
     362            rc = VERR_BUFFER_OVERFLOW;
     363    }
     364    else
     365    {
     366        cbActual = pThis->Asn1Core.cb * 3 - 1 /* save one separator */ + 1 /* terminator */;
     367        if (cbActual <= cbBuf)
     368        {
     369            rc = RTStrPrintHexBytes(pszBuf, cbBuf, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb, RTSTRPRINTHEXBYTES_F_SEP_SPACE);
     370            Assert(rc == VINF_SUCCESS);
     371        }
     372        else
     373            rc = VERR_BUFFER_OVERFLOW;
     374    }
     375    if (pcbActual)
     376        *pcbActual = cbActual;
     377    return rc;
     378}
     379
    340380
    341381/*
  • trunk/src/VBox/Runtime/common/string/RTStrPrintHexBytes.cpp

    r57358 r60028  
    55
    66/*
    7  * Copyright (C) 2009-2015 Oracle Corporation
     7 * Copyright (C) 2009-2016 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3636
    3737
    38 RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags)
     38RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags)
    3939{
    40     AssertReturn(!(fFlags & ~RTSTRPRINTHEXBYTES_F_UPPER), VERR_INVALID_PARAMETER);
     40    AssertReturn(   !(fFlags & ~(RTSTRPRINTHEXBYTES_F_UPPER | RTSTRPRINTHEXBYTES_F_SEP_SPACE | RTSTRPRINTHEXBYTES_F_SEP_COLON))
     41                 &&    (fFlags & (RTSTRPRINTHEXBYTES_F_SEP_SPACE | RTSTRPRINTHEXBYTES_F_SEP_COLON))
     42                    != (RTSTRPRINTHEXBYTES_F_SEP_SPACE | RTSTRPRINTHEXBYTES_F_SEP_COLON),
     43                 VERR_INVALID_FLAGS);
    4144    AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
    4245    AssertReturn(cb * 2 >= cb, VERR_BUFFER_OVERFLOW);
    43     AssertReturn(cchBuf >= cb * 2 + 1, VERR_BUFFER_OVERFLOW);
     46    char const chSep = fFlags & RTSTRPRINTHEXBYTES_F_SEP_SPACE ? ' '
     47                     : fFlags & RTSTRPRINTHEXBYTES_F_SEP_COLON ? ':' : '\0';
     48    AssertReturn(cbBuf >= cb * (2 + (chSep != '\0')) - (chSep != '\0') + 1, VERR_BUFFER_OVERFLOW);
    4449    if (cb)
    4550        AssertPtrReturn(pv, VERR_INVALID_POINTER);
     
    5055
    5156    uint8_t const *pb = (uint8_t const *)pv;
    52     while (cb-- > 0)
     57
     58    if (!chSep)
     59    {
     60        while (cb-- > 0)
     61        {
     62            uint8_t b = *pb++;
     63            *pszBuf++ = pszHexDigits[b >> 4];
     64            *pszBuf++ = pszHexDigits[b & 0xf];
     65        }
     66    }
     67    else if (cb-- > 0)
    5368    {
    5469        uint8_t b = *pb++;
    5570        *pszBuf++ = pszHexDigits[b >> 4];
    5671        *pszBuf++ = pszHexDigits[b & 0xf];
     72
     73        while (cb-- > 0)
     74        {
     75            b = *pb++;
     76            *pszBuf++ = chSep;
     77            *pszBuf++ = pszHexDigits[b >> 4];
     78            *pszBuf++ = pszHexDigits[b & 0xf];
     79        }
    5780    }
     81
    5882    *pszBuf = '\0';
    5983    return VINF_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette