VirtualBox

Changeset 21623 in vbox for trunk/include


Ignore:
Timestamp:
Jul 15, 2009 7:41:01 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
50148
Message:

HostServices: added getBuffer and getString methods to the host service parameter structure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/hgcmsvc.h

    r21217 r21623  
    3131#define ___VBox_hgcm_h
    3232
     33#include <iprt/assert.h>
     34#include <iprt/string.h>
    3335#include <VBox/cdefs.h>
    3436#include <VBox/types.h>
    3537#include <VBox/err.h>
     38#ifdef VBOX_TEST_HGCM_PARMS
     39# include <iprt/test.h>
     40#endif
    3641
    3742/** @todo proper comments. */
     
    110115    int getUInt32 (uint32_t *u32)
    111116    {
     117        AssertPtrReturn(u32, VERR_INVALID_POINTER);
    112118        int rc = VINF_SUCCESS;
    113119        if (type != VBOX_HGCM_SVC_PARM_32BIT)
     
    121127    int getUInt64 (uint64_t *u64)
    122128    {
     129        AssertPtrReturn(u64, VERR_INVALID_POINTER);
    123130        int rc = VINF_SUCCESS;
    124131        if (type != VBOX_HGCM_SVC_PARM_64BIT)
     
    132139    int getPointer (void **ppv, uint32_t *pcb)
    133140    {
     141        AssertPtrReturn(ppv, VERR_INVALID_POINTER);
     142        AssertPtrReturn(pcb, VERR_INVALID_POINTER);
    134143        if (type == VBOX_HGCM_SVC_PARM_PTR)
    135144        {
     
    143152
    144153    /** Extract a constant pointer value from an HGCM parameter structure */
    145     int getPointer (const void **ppv, uint32_t *pcb)
    146     {
    147         if (type == VBOX_HGCM_SVC_PARM_PTR)
     154    int getPointer (const void **ppcv, uint32_t *pcb)
     155    {
     156        AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
     157        AssertPtrReturn(pcb, VERR_INVALID_POINTER);
     158        void *pv;
     159        int rc = getPointer(&pv, pcb);
     160        *ppcv = pv;
     161        return rc;
     162    }
     163
     164    /** Extract a pointer value to a non-empty buffer from an HGCM parameter
     165     * structure */
     166    int getBuffer (void **ppv, uint32_t *pcb)
     167    {
     168        AssertPtrReturn(ppv, VERR_INVALID_POINTER);
     169        AssertPtrReturn(pcb, VERR_INVALID_POINTER);
     170        void *pv = NULL;
     171        uint32_t cb = 0;
     172        int rc = getPointer(&pv, &cb);
     173        if (   RT_SUCCESS(rc)
     174            && VALID_PTR(pv)
     175            && cb > 0)
    148176        {
    149             *ppv = u.pointer.addr;
    150             *pcb = u.pointer.size;
     177            *ppv = pv;
     178            *pcb = cb;
    151179            return VINF_SUCCESS;
    152180        }
    153181
    154182        return VERR_INVALID_PARAMETER;
     183    }
     184
     185    /** Extract a pointer value to a non-empty constant buffer from an HGCM
     186     * parameter structure */
     187    int getBuffer (const void **ppcv, uint32_t *pcb)
     188    {
     189        AssertPtrReturn(ppcv, VERR_INVALID_POINTER);
     190        AssertPtrReturn(pcb, VERR_INVALID_POINTER);
     191        void *pcv = NULL;
     192        int rc = getBuffer(&pcv, pcb);
     193        *ppcv = pcv;
     194        return rc;
     195    }
     196
     197    /** Extract a string value from an HGCM parameter structure */
     198    int getString (char **ppch, uint32_t *pcb)
     199    {
     200        uint32_t cb = 0;
     201        char *pch = NULL;
     202        int rc = getBuffer((void **)&pch, &cb);
     203        if (RT_FAILURE(rc))
     204            return rc;
     205        rc = RTStrValidateEncodingEx(pch, cb,
     206                                     RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED);
     207        *ppch = pch;
     208        *pcb = cb;
     209        return rc;
     210    }
     211
     212    /** Extract a constant string value from an HGCM parameter structure */
     213    int getString (const char **ppcch, uint32_t *pcb)
     214    {
     215        char *pch = NULL;
     216        int rc = getString(&pch, pcb);
     217        *ppcch = pch;
     218        return rc;
    155219    }
    156220
     
    176240        u.pointer.size = cb;
    177241    }
     242
     243#ifdef VBOX_TEST_HGCM_PARMS
     244    /** Test the getString member function.  Indirectly tests the getPointer
     245     * and getBuffer APIs.
     246     * @param  hTest  an running IPRT test
     247     * @param  aType  the type that the parameter should be set to before
     248     *                calling getString
     249     * @param  apcc   the value that the parameter should be set to before
     250     *                calling getString, and also the address (!) which we
     251     *                expect getString to return.  Stricter than needed of
     252     *                course, but I was feeling lazy.
     253     * @param  acb    the size that the parameter should be set to before
     254     *                calling getString, and also the size which we expect
     255     *                getString to return.
     256     * @param  rcExp  the expected return value of the call to getString.
     257     */
     258    void doTestGetString(RTTEST hTest, uint32_t aType, const char *apcc,
     259                         uint32_t acb, int rcExp)
     260    {
     261        /* An RTTest API like this, which would print out an additional line
     262         * of context if a test failed, would be nice.  This is because the
     263         * line number alone doesn't help much here, given that this is a
     264         * subroutine called many times. */
     265        /*
     266        RTTestContextF(hTest,
     267                       ("doTestGetString, aType=%u, apcc=%p, acp=%u, rcExp=%Rrc",
     268                        aType, apcc, acp, rcExp));
     269         */
     270        setPointer((void *)apcc, acb);
     271        type = aType;  /* in case we don't want VBOX_HGCM_SVC_PARM_PTR */
     272        const char *pcc = NULL;
     273        uint32_t cb = 0;
     274        int rc = getString(&pcc, &cb);
     275        RTTEST_CHECK_RC(hTest, rc, rcExp);
     276        if (RT_SUCCESS(rcExp))
     277        {
     278            RTTEST_CHECK_MSG_RETV(hTest, (pcc == apcc),
     279                                  (hTest, "expected %p, got %p", apcc, pcc));
     280            RTTEST_CHECK_MSG_RETV(hTest, (cb == acb),
     281                                  (hTest, "expected %u, got %u", acb, cb));
     282        }
     283    }
     284
     285    /** Run some unit tests on the getString method and indirectly test
     286     * getPointer and getBuffer as well. */
     287    void testGetString(RTTEST hTest)
     288    {
     289        RTTestSub(hTest, "HGCM string parameter handling");
     290        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_32BIT, "test", 3,
     291                        VERR_INVALID_PARAMETER);
     292        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 5,
     293                        VINF_SUCCESS);
     294        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 3,
     295                        VERR_BUFFER_OVERFLOW);
     296        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test\xf0", 6,
     297                        VERR_INVALID_UTF8_ENCODING);
     298        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, "test", 0,
     299                        VERR_INVALID_PARAMETER);
     300        doTestGetString(hTest, VBOX_HGCM_SVC_PARM_PTR, (const char *)0x1, 5,
     301                        VERR_INVALID_PARAMETER);
     302        RTTestSubDone(hTest);
     303    }
     304#endif
    178305
    179306    VBOXHGCMSVCPARM() : type(VBOX_HGCM_SVC_PARM_INVALID) {}
Note: See TracChangeset for help on using the changeset viewer.

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