VirtualBox

Changeset 9738 in vbox for trunk/src/VBox/Runtime/r3/win


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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