VirtualBox

Changeset 9738 in vbox


Ignore:
Timestamp:
Jun 16, 2008 10:38:49 PM (16 years ago)
Author:
vboxsync
Message:

Some UUID cleanup; added RTUuidCompareStr(PCRTUUID, const char *).

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/Guid.h

    r9332 r9738  
    103103    }
    104104
    105     bool isEmpty() const { return ::RTUuidIsNull (&uuid) != 0; }
     105    bool isEmpty() const { return ::RTUuidIsNull (&uuid); }
    106106    operator bool() const { return !isEmpty(); }
    107107
     
    173173    static bool isEmpty (const GUID &guid)
    174174    {
    175         return ::RTUuidIsNull ((PRTUUID) &guid) != 0;
     175        return ::RTUuidIsNull ((PRTUUID) &guid);
    176176    }
    177177
  • trunk/include/iprt/uuid.h

    r8245 r9738  
    6363 * @param   pUuid           uuid to check.
    6464 */
    65 RTDECL(int)  RTUuidIsNull(PCRTUUID pUuid);
     65RTDECL(bool)  RTUuidIsNull(PCRTUUID pUuid);
    6666
    6767/**
     
    7575
    7676/**
     77 * Compares a UUID value with a UUID string.
     78 *
     79 * @returns 0 if eq, < 0 or > 0.
     80 * @param   pUuid1          First value to compare.
     81 * @param   pszString2      The 2nd UUID in string form.
     82 */
     83RTDECL(int)  RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString);
     84
     85/**
    7786 * Converts binary UUID to its string representation.
    7887 *
     
    8291 * @param   cchString       pszString buffer length, must be >= RTUUID_STR_LENGTH.
    8392 */
    84 RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString);
     93RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString);
    8594
    8695/**
  • trunk/src/VBox/Runtime/generic/uuid-generic.cpp

    r8245 r9738  
    4444
    4545
    46 /**
    47  * Generates a new UUID value.
    48  *
    49  * @returns iprt status code.
    50  * @param   pUuid           Where to store generated uuid.
    51  */
     46/** @todo move to a different file. */
    5247RTDECL(int)  RTUuidCreate(PRTUUID pUuid)
    5348{
    5449    /* validate input. */
    55     AssertReturn(pUuid, VERR_INVALID_PARAMETER);
     50    AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
    5651
    5752    RTRandBytes(pUuid, sizeof(*pUuid));
     
    6358
    6459
    65 /**
    66  * Makes a null UUID value.
    67  *
    68  * @returns iprt status code.
    69  * @param   pUuid           Where to store generated null uuid.
    70  */
    7160RTDECL(int)  RTUuidClear(PRTUUID pUuid)
    7261{
    73     AssertReturn(pUuid, VERR_INVALID_PARAMETER);
     62    AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
    7463    pUuid->au64[0] = 0;
    7564    pUuid->au64[1] = 0;
     
    7867
    7968
    80 /**
    81  * Checks if UUID is null.
    82  *
    83  * @returns true if UUID is null.
    84  * @param   pUuid           uuid to check.
    85  */
    86 RTDECL(int)  RTUuidIsNull(PCRTUUID pUuid)
    87 {
    88     AssertReturn(pUuid, VERR_INVALID_PARAMETER);
     69RTDECL(bool)  RTUuidIsNull(PCRTUUID pUuid)
     70{
     71    AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
    8972    return !pUuid->au64[0]
    9073        && !pUuid->au64[1];
     
    9275
    9376
    94 /**
    95  * Compares two UUID values.
    96  *
    97  * @returns 0 if eq, < 0 or > 0.
    98  * @param   pUuid1          First value to compare.
    99  * @param   pUuid2          Second value to compare.
    100  */
    10177RTDECL(int)  RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
    10278{
     
    10480     * Special cases.
    10581     */
     82    /** @todo This differs in the windows implementation... check out which behavior we really want. */
    10683    if (pUuid1 == pUuid2)
    10784        return 0;
     
    11087    if (!pUuid2)
    11188        return RTUuidIsNull(pUuid1) ? 0 : 1;
     89    AssertPtr(pUuid1);
     90    AssertPtr(pUuid2);
    11291
    11392    /*
     
    138117
    139118
    140 /**
    141  * Converts binary UUID to its string representation.
    142  *
    143  * @returns iprt status code.
    144  * @param   pUuid           Uuid to convert.
    145  * @param   pszString       Where to store result string.
    146  * @param   cchString       pszString buffer length, must be >= RTUUID_STR_LENGTH.
    147  */
    148 RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
     119RTDECL(int)  RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString)
     120{
     121    /* check params */
     122    AssertPtrReturn(pUuid1, -1);
     123    AssertPtrReturn(pszString, 1);
     124
     125    /*
     126     * Try convert the string to a UUID and then compare the two.
     127     */
     128    RTUUID Uuid2;
     129    int rc = RTUuidFromStr(&Uuid2, pszString);
     130    AssertRCReturn(rc, 1);
     131
     132    return RTUuidCompare(pUuid1, &Uuid2);
     133}
     134
     135
     136RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
    149137{
    150138    /* validate parameters */
    151     AssertReturn(pUuid, VERR_INVALID_PARAMETER);
    152     AssertReturn(pszString, VERR_INVALID_PARAMETER);
     139    AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
     140    AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
    153141    AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
    154142
     
    214202
    215203
    216 /**
    217  * Converts UUID from its string representation to binary format.
    218  *
    219  * @returns iprt status code.
    220  * @param   pUuid           Where to store result Uuid.
    221  * @param   pszString       String with UUID text data.
    222  */
    223204RTDECL(int)  RTUuidFromStr(PRTUUID pUuid, const char *pszString)
    224205{
     
    247228     * Validate parameters.
    248229     */
    249     AssertReturn(pUuid, VERR_INVALID_PARAMETER);
    250     AssertReturn(pszString, VERR_INVALID_PARAMETER);
     230    AssertPtrReturn(pUuid, VERR_INVALID_PARAMETER);
     231    AssertPtrReturn(pszString, VERR_INVALID_PARAMETER);
    251232
    252233#define MY_CHECK(expr) do { if (RT_UNLIKELY(!(expr))) return VERR_INVALID_UUID_FORMAT; } while (0)
  • trunk/src/VBox/Runtime/r3/win/uuid-win.cpp

    r8245 r9738  
    4242
    4343
    44 /**
    45  * Generates a new UUID value.
    46  *
    47  * @returns iprt status code.
    48  * @param   pUuid           Where to store generated uuid.
    49  */
     44/** @todo split out this guy */
    5045RTDECL(int)  RTUuidCreate(PRTUUID pUuid)
    5146{
    5247    /* check params */
    53     if (pUuid == NULL)
    54     {
    55         AssertMsgFailed(("pUuid=NULL\n"));
    56         return VERR_INVALID_PARAMETER;
    57     }
     48    AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
    5849
    5950    RPC_STATUS rc = UuidCreate((UUID *)pUuid);
    60     if ((rc == RPC_S_OK) || (rc == RPC_S_UUID_LOCAL_ONLY))
     51    if (    rc == RPC_S_OK
     52        || rc == RPC_S_UUID_LOCAL_ONLY)
    6153        return VINF_SUCCESS;
    6254
     
    6557}
    6658
    67 /**
    68  * Makes null UUID value.
    69  *
    70  * @returns iprt status code.
    71  * @param   pUuid           Where to store generated null uuid.
    72  */
     59
    7360RTDECL(int)  RTUuidClear(PRTUUID pUuid)
    7461{
    7562    /* check params */
    76     if (pUuid == NULL)
    77     {
    78         AssertMsgFailed(("pUuid=NULL\n"));
    79         return VERR_INVALID_PARAMETER;
    80     }
     63    AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
    8164
    8265    return RTErrConvertFromWin32(UuidCreateNil((UUID *)pUuid));
    8366}
    8467
    85 /**
    86  * Checks if UUID is null.
    87  *
    88  * @returns true if UUID is null.
    89  * @param   pUuid           uuid to check.
    90  */
    91 RTDECL(int)  RTUuidIsNull(PCRTUUID pUuid)
     68
     69RTDECL(bool)  RTUuidIsNull(PCRTUUID pUuid)
    9270{
    9371    /* check params */
    94     if (pUuid == NULL)
    95     {
    96         AssertMsgFailed(("pUuid=NULL\n"));
    97         return TRUE;
    98     }
     72    AssertPtrReturn(pUuid, true);
    9973
    10074    RPC_STATUS status;
    101     return UuidIsNil((UUID *)pUuid, &status);
     75    return !!UuidIsNil((UUID *)pUuid, &status);
    10276}
    10377
    104 /**
    105  * Compares two UUID values.
    106  *
    107  * @returns 0 if eq, < 0 or > 0.
    108  * @param   pUuid1          First value to compare.
    109  * @param   pUuid2          Second value to compare.
    110  */
     78
    11179RTDECL(int)  RTUuidCompare(PCRTUUID pUuid1, PCRTUUID pUuid2)
    11280{
    11381    /* check params */
    114     if ((pUuid1 == NULL) || (pUuid2 == NULL))
    115     {
    116         AssertMsgFailed(("Invalid parameters\n"));
    117         return 1;
    118     }
     82    AssertPtrReturn(pUuid1, -1);
     83    AssertPtrReturn(pUuid1, 1);
    11984
    12085    RPC_STATUS status;
     
    12287}
    12388
    124 /**
    125  * Converts binary UUID to its string representation.
    126  *
    127  * @returns iprt status code.
    128  * @param   pUuid           Uuid to convert.
    129  * @param   pszString       Where to store result string.
    130  * @param   cchString       pszString buffer length, must be >= RTUUID_STR_LENGTH.
    131  */
    132 RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, unsigned cchString)
     89
     90RTDECL(int)  RTUuidCompareStr(PCRTUUID pUuid1, const char *pszString)
    13391{
    13492    /* check params */
    135     if ((pUuid == NULL) || (pszString == NULL) || (cchString < RTUUID_STR_LENGTH))
    136     {
    137         AssertMsgFailed(("Invalid parameters\n"));
    138         return VERR_INVALID_PARAMETER;
    139     }
     93    AssertPtrReturn(pUuid1, -1);
     94    AssertPtrReturn(pszString, 1);
    14095
    141     RPC_STATUS rc;
    142     char *pStr = NULL;
     96    /*
     97     * Try convert the string to a UUID and then compare the two.
     98     */
     99    RTUUID Uuid2;
     100    int rc = RTUuidFromStr(&Uuid2, pszString);
     101    AssertRCReturn(rc, 1);
     102
     103    return RTUuidCompare(pUuid1, &Uuid2);
     104}
     105
     106
     107RTDECL(int)  RTUuidToStr(PCRTUUID pUuid, char *pszString, size_t cchString)
     108{
     109    /* check params */
     110    AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
     111    AssertPtrReturn(pszString, VERR_INVALID_POINTER);
     112    AssertReturn(cchString >= RTUUID_STR_LENGTH, VERR_INVALID_PARAMETER);
     113
     114    /*
     115     * Try convert it.
     116     *
     117     * The API allocates a new string buffer for us, so we can do our own
     118     * buffer overflow handling.
     119     */
     120    RPC_STATUS Status;
     121    unsigned char *pszTmpStr = NULL;
    143122#ifdef RPC_UNICODE_SUPPORTED
    144123    /* always use ASCII version! */
    145     rc = UuidToStringA((UUID *)pUuid, (unsigned char **)&pStr);
     124    Status = UuidToStringA((UUID *)pUuid, &pszTmpStr);
    146125#else
    147     rc = UuidToString((UUID *)pUuid, (unsigned char **)&pStr);
     126    Status = UuidToString((UUID *)pUuid, &pszTmpStr);
    148127#endif
    149     if (rc != RPC_S_OK)
    150         return RTErrConvertFromWin32(rc);
     128    if (Status != RPC_S_OK)
     129        return RTErrConvertFromWin32(Status);
    151130
    152     if (strlen(pStr) >= cchString)
    153     {
    154         /* out of buffer space */
    155 #ifdef RPC_UNICODE_SUPPORTED
    156         /* always use ASCII version! */
    157         RpcStringFreeA((unsigned char **)&pStr);
    158 #else
    159         RpcStringFree((unsigned char **)&pStr);
    160 #endif
    161         AssertMsgFailed(("Buffer overflow\n"));
    162         return ERROR_BUFFER_OVERFLOW;
    163     }
    164 
    165     /* copy str to user buffer */
    166     pszString[0] = '\0';
    167     strncat(pszString, pStr, cchString);
     131    /* copy it. */
     132    int rc = VINF_SUCCESS;
     133    size_t cch = strlen((char *)pszTmpStr);
     134    if (cch < cchString)
     135        memcpy(pszString, pszTmpStr, cchTmpStr + 1);
     136    else
     137        rc = ERROR_BUFFER_OVERFLOW;
    168138
    169139    /* free buffer */
    170140#ifdef RPC_UNICODE_SUPPORTED
    171141    /* always use ASCII version! */
    172     RpcStringFreeA((unsigned char **)&pStr);
     142    RpcStringFreeA(&pszTmpStr);
    173143#else
    174     RpcStringFree((unsigned char **)&pStr);
     144    RpcStringFree(&pszTmpStr);
    175145#endif
    176146
    177147    /* all done */
    178     return VINF_SUCCESS;
     148    return rc;
    179149}
    180150
    181 /**
    182  * Converts UUID from its string representation to binary format.
    183  *
    184  * @returns iprt status code.
    185  * @param   pUuid           Where to store result Uuid.
    186  * @param   pszString       String with UUID text data.
    187  */
     151
    188152RTDECL(int)  RTUuidFromStr(PRTUUID pUuid, const char *pszString)
    189153{
    190154    /* check params */
    191     if ((pUuid == NULL) || (pszString == NULL))
    192     {
    193         AssertMsgFailed(("Invalid parameters\n"));
    194         return VERR_INVALID_PARAMETER;
    195     }
     155    AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
     156    AssertPtrReturn(pszString, VERR_INVALID_POINTER);
    196157
    197158    RPC_STATUS rc;
     
    206167}
    207168
     169
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