VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestCtrlPrivate.cpp@ 42657

Last change on this file since 42657 was 42634, checked in by vboxsync, 13 years ago

Guest Control 2.0: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
1/* $Id: GuestCtrlPrivate.cpp 42634 2012-08-06 17:29:38Z vboxsync $ */
2/** @file
3 *
4 * Internal helpers/structures for guest control functionality.
5 */
6
7/*
8 * Copyright (C) 2011-2012 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/******************************************************************************
20 * Header Files *
21 ******************************************************************************/
22#include "GuestCtrlImplPrivate.h"
23
24#include <iprt/asm.h>
25#include <iprt/ctype.h>
26#ifdef DEBUG
27# include <iprt/file.h>
28#endif /* DEBUG */
29
30/******************************************************************************
31 * Structures and Typedefs *
32 ******************************************************************************/
33
34GuestCtrlEvent::GuestCtrlEvent(void)
35 : fCanceled(false),
36 fCompleted(false),
37 hEventSem(NIL_RTSEMEVENT),
38 mRC(VINF_SUCCESS)
39{
40}
41
42GuestCtrlEvent::~GuestCtrlEvent(void)
43{
44 Destroy();
45}
46
47int GuestCtrlEvent::Cancel(void)
48{
49 int rc = VINF_SUCCESS;
50 if (!ASMAtomicReadBool(&fCompleted))
51 {
52 if (!ASMAtomicReadBool(&fCanceled))
53 {
54 ASMAtomicXchgBool(&fCanceled, true);
55
56 LogFlowThisFunc(("Cancelling event ...\n"));
57 rc = hEventSem != NIL_RTSEMEVENT
58 ? RTSemEventSignal(hEventSem) : VINF_SUCCESS;
59 }
60 }
61
62 return rc;
63}
64
65bool GuestCtrlEvent::Canceled(void)
66{
67 return ASMAtomicReadBool(&fCanceled);
68}
69
70void GuestCtrlEvent::Destroy(void)
71{
72 int rc = Cancel();
73 AssertRC(rc);
74
75 if (hEventSem != NIL_RTSEMEVENT)
76 {
77 RTSemEventDestroy(hEventSem);
78 hEventSem = NIL_RTSEMEVENT;
79 }
80}
81
82int GuestCtrlEvent::Init(void)
83{
84 return RTSemEventCreate(&hEventSem);
85}
86
87int GuestCtrlEvent::Signal(int rc /*= VINF_SUCCESS*/)
88{
89 AssertReturn(hEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
90
91 mRC = rc;
92
93 return RTSemEventSignal(hEventSem);
94}
95
96int GuestCtrlEvent::Wait(ULONG uTimeoutMS)
97{
98 LogFlowFuncEnter();
99
100 AssertReturn(hEventSem != NIL_RTSEMEVENT, VERR_CANCELLED);
101
102 RTMSINTERVAL msInterval = uTimeoutMS;
103 if (!uTimeoutMS)
104 msInterval = RT_INDEFINITE_WAIT;
105 int rc = RTSemEventWait(hEventSem, msInterval);
106 if (RT_SUCCESS(rc))
107 ASMAtomicWriteBool(&fCompleted, true);
108
109 LogFlowFuncLeaveRC(rc);
110 return rc;
111}
112
113///////////////////////////////////////////////////////////////////////////////
114
115GuestCtrlCallback::GuestCtrlCallback(void)
116 : pvData(NULL),
117 cbData(0),
118 mType(VBOXGUESTCTRLCALLBACKTYPE_UNKNOWN),
119 uFlags(0),
120 pvPayload(NULL),
121 cbPayload(0)
122{
123}
124
125GuestCtrlCallback::GuestCtrlCallback(eVBoxGuestCtrlCallbackType enmType)
126 : pvData(NULL),
127 cbData(0),
128 mType(VBOXGUESTCTRLCALLBACKTYPE_UNKNOWN),
129 uFlags(0),
130 pvPayload(NULL),
131 cbPayload(0)
132{
133 int rc = Init(enmType);
134 AssertRC(rc);
135}
136
137GuestCtrlCallback::~GuestCtrlCallback(void)
138{
139 Destroy();
140}
141
142int GuestCtrlCallback::Init(eVBoxGuestCtrlCallbackType enmType)
143{
144 AssertReturn(enmType > VBOXGUESTCTRLCALLBACKTYPE_UNKNOWN, VERR_INVALID_PARAMETER);
145 Assert((pvData == NULL) && !cbData);
146
147 switch (enmType)
148 {
149 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_START:
150 {
151 pvData = (PCALLBACKDATAEXECSTATUS)RTMemAlloc(sizeof(CALLBACKDATAEXECSTATUS));
152 AssertPtrReturn(pvData, VERR_NO_MEMORY);
153 RT_BZERO(pvData, sizeof(CALLBACKDATAEXECSTATUS));
154 cbData = sizeof(CALLBACKDATAEXECSTATUS);
155 break;
156 }
157
158 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT:
159 {
160 pvData = (PCALLBACKDATAEXECOUT)RTMemAlloc(sizeof(CALLBACKDATAEXECOUT));
161 AssertPtrReturn(pvData, VERR_NO_MEMORY);
162 RT_BZERO(pvData, sizeof(CALLBACKDATAEXECOUT));
163 cbData = sizeof(CALLBACKDATAEXECOUT);
164 break;
165 }
166
167 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_INPUT_STATUS:
168 {
169 pvData = (PCALLBACKDATAEXECINSTATUS)RTMemAlloc(sizeof(CALLBACKDATAEXECINSTATUS));
170 AssertPtrReturn(pvData, VERR_NO_MEMORY);
171 RT_BZERO(pvData, sizeof(CALLBACKDATAEXECINSTATUS));
172 cbData = sizeof(CALLBACKDATAEXECINSTATUS);
173 break;
174 }
175
176 default:
177 AssertMsgFailed(("Unknown callback type specified (%d)\n", enmType));
178 break;
179 }
180
181 int rc = GuestCtrlEvent::Init();
182 if (RT_SUCCESS(rc))
183 mType = enmType;
184
185 return rc;
186}
187
188void GuestCtrlCallback::Destroy(void)
189{
190 GuestCtrlEvent::Destroy();
191
192 switch (mType)
193 {
194 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT:
195 {
196 PCALLBACKDATAEXECOUT pThis = (PCALLBACKDATAEXECOUT)pvData;
197 AssertPtr(pThis);
198 if (pThis->pvData)
199 RTMemFree(pThis->pvData);
200 }
201
202 default:
203 break;
204 }
205
206 mType = VBOXGUESTCTRLCALLBACKTYPE_UNKNOWN;
207 if (pvData)
208 {
209 RTMemFree(pvData);
210 pvData = NULL;
211 }
212 cbData = 0;
213
214 if (pvPayload)
215 {
216 RTMemFree(pvPayload);
217 pvPayload = NULL;
218 }
219 cbPayload = 0;
220}
221
222int GuestCtrlCallback::SetData(const void *pvCallback, size_t cbCallback)
223{
224 if (!cbCallback)
225 return VINF_SUCCESS;
226 AssertPtr(pvCallback);
227
228 switch (mType)
229 {
230 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_START:
231 {
232 PCALLBACKDATAEXECSTATUS pThis = (PCALLBACKDATAEXECSTATUS)pvData;
233 PCALLBACKDATAEXECSTATUS pCB = (PCALLBACKDATAEXECSTATUS)pvCallback;
234 Assert(cbCallback == sizeof(CALLBACKDATAEXECSTATUS));
235
236 pThis->u32Flags = pCB->u32Flags;
237 pThis->u32PID = pCB->u32PID;
238 pThis->u32Status = pCB->u32Status;
239 break;
240 }
241
242 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT:
243 {
244 PCALLBACKDATAEXECOUT pThis = (PCALLBACKDATAEXECOUT)pvData;
245 PCALLBACKDATAEXECOUT pCB = (PCALLBACKDATAEXECOUT)pvCallback;
246 Assert(cbCallback == sizeof(CALLBACKDATAEXECOUT));
247
248 pThis->cbData = pCB->cbData;
249 if (pThis->cbData)
250 {
251 pThis->pvData = RTMemAlloc(pCB->cbData);
252 AssertPtrReturn(pThis->pvData, VERR_NO_MEMORY);
253 memcpy(pThis->pvData, pCB->pvData, pCB->cbData);
254 }
255 pThis->u32Flags = pCB->u32Flags;
256 pThis->u32PID = pCB->u32PID;
257 break;
258 }
259
260 case VBOXGUESTCTRLCALLBACKTYPE_EXEC_INPUT_STATUS:
261 {
262 PCALLBACKDATAEXECINSTATUS pThis = (PCALLBACKDATAEXECINSTATUS)pvData;
263 PCALLBACKDATAEXECINSTATUS pCB = (PCALLBACKDATAEXECINSTATUS)pvCallback;
264 Assert(cbCallback == sizeof(CALLBACKDATAEXECINSTATUS));
265
266 pThis->cbProcessed = pCB->cbProcessed;
267 pThis->u32Flags = pCB->u32Flags;
268 pThis->u32PID = pCB->u32PID;
269 pThis->u32Status = pCB->u32Status;
270 break;
271 }
272
273 default:
274 AssertMsgFailed(("Callback type not handled (%d)\n", mType));
275 break;
276 }
277
278 return VINF_SUCCESS;
279}
280
281int GuestCtrlCallback::SetPayload(const void *pvToWrite, size_t cbToWrite)
282{
283 if (!cbToWrite)
284 return VINF_SUCCESS;
285 AssertPtr(pvToWrite);
286
287 Assert(pvPayload == NULL); /* Can't reuse callbacks! */
288 pvPayload = RTMemAlloc(cbToWrite);
289 if (!pvPayload)
290 return VERR_NO_MEMORY;
291
292 memcpy(pvPayload, pvToWrite, cbToWrite);
293 cbPayload = cbToWrite;
294
295 return VINF_SUCCESS;
296}
297
298///////////////////////////////////////////////////////////////////////////////
299
300GuestProcessEvent::GuestProcessEvent(void)
301 : mWaitFlags(0)
302{
303}
304
305GuestProcessEvent::GuestProcessEvent(uint32_t uWaitFlags)
306 : mWaitFlags(uWaitFlags)
307{
308 int rc = GuestCtrlEvent::Init();
309 AssertRC(rc);
310}
311
312GuestProcessEvent::~GuestProcessEvent(void)
313{
314 Destroy();
315}
316
317void GuestProcessEvent::Destroy(void)
318{
319 GuestCtrlEvent::Destroy();
320
321 mWaitFlags = ProcessWaitForFlag_None;
322}
323
324int GuestProcessEvent::Signal(ProcessWaitResult_T enmResult, int rc /*= VINF_SUCCESS*/)
325{
326 mWaitResult.mRC = rc;
327 mWaitResult.mResult = enmResult;
328
329 return GuestCtrlEvent::Signal(rc);
330}
331
332///////////////////////////////////////////////////////////////////////////////
333
334int GuestEnvironment::BuildEnvironmentBlock(void **ppvEnv, size_t *pcbEnv, uint32_t *pcEnvVars)
335{
336 AssertPtrReturn(ppvEnv, VERR_INVALID_POINTER);
337 /* Rest is optional. */
338
339 size_t cbEnv = 0;
340 uint32_t cEnvVars = 0;
341
342 int rc = VINF_SUCCESS;
343
344 size_t cEnv = mEnvironment.size();
345 if (cEnv)
346 {
347 std::map<Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.begin();
348 for (; itEnv != mEnvironment.end() && RT_SUCCESS(rc); itEnv++)
349 {
350 char *pszEnv;
351 if (!RTStrAPrintf(&pszEnv, "%s=%s", itEnv->first.c_str(), itEnv->second.c_str()))
352 {
353 rc = VERR_NO_MEMORY;
354 break;
355 }
356 AssertPtr(pszEnv);
357 rc = appendToEnvBlock(pszEnv, ppvEnv, &cbEnv, &cEnvVars);
358 RTStrFree(pszEnv);
359 }
360 Assert(cEnv == cEnvVars);
361 }
362
363 if (pcbEnv)
364 *pcbEnv = cbEnv;
365 if (pcEnvVars)
366 *pcEnvVars = cEnvVars;
367
368 return rc;
369}
370
371void GuestEnvironment::Clear(void)
372{
373 mEnvironment.clear();
374}
375
376int GuestEnvironment::CopyFrom(const GuestEnvironmentArray &environment)
377{
378 int rc = VINF_SUCCESS;
379
380 for (GuestEnvironmentArray::const_iterator it = environment.begin();
381 it != environment.end() && RT_SUCCESS(rc);
382 ++it)
383 {
384 rc = Set((*it));
385 }
386
387 return rc;
388}
389
390int GuestEnvironment::CopyTo(GuestEnvironmentArray &environment)
391{
392 size_t s = 0;
393 for (std::map<Utf8Str, Utf8Str>::const_iterator it = mEnvironment.begin();
394 it != mEnvironment.end();
395 ++it, ++s)
396 {
397 environment[s] = Bstr(it->first + "=" + it->second).raw();
398 }
399
400 return VINF_SUCCESS;
401}
402
403/* static */
404void GuestEnvironment::FreeEnvironmentBlock(void *pvEnv)
405{
406 if (pvEnv)
407 RTMemFree(pvEnv);
408}
409
410Utf8Str GuestEnvironment::Get(size_t nPos)
411{
412 size_t curPos = 0;
413 std::map<Utf8Str, Utf8Str>::const_iterator it = mEnvironment.begin();
414 for (; it != mEnvironment.end() && curPos < nPos;
415 ++it, ++curPos) { }
416
417 if (it != mEnvironment.end())
418 return Utf8Str(it->first + "=" + it->second);
419
420 return Utf8Str("");
421}
422
423Utf8Str GuestEnvironment::Get(const Utf8Str &strKey)
424{
425 std::map <Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.find(strKey);
426 Utf8Str strRet;
427 if (itEnv != mEnvironment.end())
428 strRet = itEnv->second;
429 return strRet;
430}
431
432bool GuestEnvironment::Has(const Utf8Str &strKey)
433{
434 std::map <Utf8Str, Utf8Str>::const_iterator itEnv = mEnvironment.find(strKey);
435 return (itEnv != mEnvironment.end());
436}
437
438int GuestEnvironment::Set(const Utf8Str &strKey, const Utf8Str &strValue)
439{
440 /** @todo Do some validation using regex. */
441 if (strKey.isEmpty())
442 return VERR_INVALID_PARAMETER;
443
444 int rc = VINF_SUCCESS;
445 const char *pszString = strKey.c_str();
446 while (*pszString != '\0' && RT_SUCCESS(rc))
447 {
448 if ( !RT_C_IS_ALNUM(*pszString)
449 && !RT_C_IS_GRAPH(*pszString))
450 rc = VERR_INVALID_PARAMETER;
451 *pszString++;
452 }
453
454 if (RT_SUCCESS(rc))
455 mEnvironment[strKey] = strValue;
456
457 return rc;
458}
459
460int GuestEnvironment::Set(const Utf8Str &strPair)
461{
462 RTCList<RTCString> listPair = strPair.split("=", RTCString::KeepEmptyParts);
463 /* Skip completely empty pairs. Note that we still need pairs with a valid
464 * (set) key and an empty value. */
465 if (listPair.size() <= 1)
466 return VINF_SUCCESS;
467
468 int rc = VINF_SUCCESS;
469 size_t p = 0;
470 while(p < listPair.size() && RT_SUCCESS(rc))
471 {
472 Utf8Str strKey = listPair.at(p++);
473 if ( strKey.isEmpty()
474 || strKey.equals("=")) /* Skip pairs with empty keys (e.g. "=FOO"). */
475 {
476 break;
477 }
478 Utf8Str strValue;
479 if (p < listPair.size()) /* Does the list also contain a value? */
480 strValue = listPair.at(p++);
481
482#ifdef DEBUG
483 LogFlowFunc(("strKey=%s, strValue=%s\n",
484 strKey.c_str(), strValue.c_str()));
485#endif
486 rc = Set(strKey, strValue);
487 }
488
489 return rc;
490}
491
492size_t GuestEnvironment::Size(void)
493{
494 return mEnvironment.size();
495}
496
497int GuestEnvironment::Unset(const Utf8Str &strKey)
498{
499 std::map <Utf8Str, Utf8Str>::iterator itEnv = mEnvironment.find(strKey);
500 if (itEnv != mEnvironment.end())
501 {
502 mEnvironment.erase(itEnv);
503 return VINF_SUCCESS;
504 }
505
506 return VERR_NOT_FOUND;
507}
508
509GuestEnvironment& GuestEnvironment::operator=(const GuestEnvironmentArray &that)
510{
511 CopyFrom(that);
512 return *this;
513}
514
515GuestEnvironment& GuestEnvironment::operator=(const GuestEnvironment &that)
516{
517 for (std::map<Utf8Str, Utf8Str>::const_iterator it = that.mEnvironment.begin();
518 it != that.mEnvironment.end();
519 ++it)
520 {
521 mEnvironment[it->first] = it->second;
522 }
523
524 return *this;
525}
526
527/**
528 * Appends environment variables to the environment block.
529 *
530 * Each var=value pair is separated by the null character ('\\0'). The whole
531 * block will be stored in one blob and disassembled on the guest side later to
532 * fit into the HGCM param structure.
533 *
534 * @returns VBox status code.
535 *
536 * @param pszEnvVar The environment variable=value to append to the
537 * environment block.
538 * @param ppvList This is actually a pointer to a char pointer
539 * variable which keeps track of the environment block
540 * that we're constructing.
541 * @param pcbList Pointer to the variable holding the current size of
542 * the environment block. (List is a misnomer, go
543 * ahead a be confused.)
544 * @param pcEnvVars Pointer to the variable holding count of variables
545 * stored in the environment block.
546 */
547int GuestEnvironment::appendToEnvBlock(const char *pszEnv, void **ppvList, size_t *pcbList, uint32_t *pcEnvVars)
548{
549 int rc = VINF_SUCCESS;
550 size_t cchEnv = strlen(pszEnv); Assert(cchEnv >= 2);
551 if (*ppvList)
552 {
553 size_t cbNewLen = *pcbList + cchEnv + 1; /* Include zero termination. */
554 char *pvTmp = (char *)RTMemRealloc(*ppvList, cbNewLen);
555 if (pvTmp == NULL)
556 rc = VERR_NO_MEMORY;
557 else
558 {
559 memcpy(pvTmp + *pcbList, pszEnv, cchEnv);
560 pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
561 *ppvList = (void **)pvTmp;
562 }
563 }
564 else
565 {
566 char *pszTmp;
567 if (RTStrAPrintf(&pszTmp, "%s", pszEnv) >= 0)
568 {
569 *ppvList = (void **)pszTmp;
570 /* Reset counters. */
571 *pcEnvVars = 0;
572 *pcbList = 0;
573 }
574 }
575 if (RT_SUCCESS(rc))
576 {
577 *pcbList += cchEnv + 1; /* Include zero termination. */
578 *pcEnvVars += 1; /* Increase env variable count. */
579 }
580 return rc;
581}
582
583int GuestFsObjData::From(const GuestProcessStreamBlock &strmBlk)
584{
585 int rc = VINF_SUCCESS;
586
587 try
588 {
589 Utf8Str strType(strmBlk.GetString("ftype"));
590 if (strType.equalsIgnoreCase("-"))
591 mType = FsObjType_File;
592 else if (strType.equalsIgnoreCase("d"))
593 mType = FsObjType_Directory;
594 /** @todo Add more types! */
595 else
596 mType = FsObjType_Undefined;
597
598 rc = strmBlk.GetInt64Ex("st_size", &mObjectSize);
599 if (RT_FAILURE(rc)) throw rc;
600
601 /** @todo Add complete GuestFsObjData info! */
602 }
603 catch (int rc2)
604 {
605 rc = rc2;
606 }
607
608 return rc;
609}
610
611///////////////////////////////////////////////////////////////////////////////
612
613/** @todo *NOT* thread safe yet! */
614/** @todo Add exception handling for STL stuff! */
615
616GuestProcessStreamBlock::GuestProcessStreamBlock(void)
617{
618
619}
620
621/*
622GuestProcessStreamBlock::GuestProcessStreamBlock(const GuestProcessStreamBlock &otherBlock)
623{
624 for (GuestCtrlStreamPairsIter it = otherBlock.m_mapPairs.begin();
625 it != otherBlock.end(); it++)
626 {
627 m_mapPairs[it->first] = new
628 if (it->second.pszValue)
629 {
630 RTMemFree(it->second.pszValue);
631 it->second.pszValue = NULL;
632 }
633 }
634}*/
635
636GuestProcessStreamBlock::~GuestProcessStreamBlock()
637{
638 Clear();
639}
640
641/**
642 * Destroys the currently stored stream pairs.
643 *
644 * @return IPRT status code.
645 */
646void GuestProcessStreamBlock::Clear(void)
647{
648 m_mapPairs.clear();
649}
650
651#ifdef DEBUG
652void GuestProcessStreamBlock::Dump(void)
653{
654 LogFlowFunc(("Dumping contents of stream block=0x%p (%ld items):\n",
655 this, m_mapPairs.size()));
656
657 for (GuestCtrlStreamPairMapIterConst it = m_mapPairs.begin();
658 it != m_mapPairs.end(); it++)
659 {
660 LogFlowFunc(("\t%s=%s\n", it->first.c_str(), it->second.mValue.c_str()));
661 }
662}
663#endif
664
665/**
666 * Returns a 64-bit signed integer of a specified key.
667 *
668 * @return IPRT status code. VERR_NOT_FOUND if key was not found.
669 * @param pszKey Name of key to get the value for.
670 * @param piVal Pointer to value to return.
671 */
672int GuestProcessStreamBlock::GetInt64Ex(const char *pszKey, int64_t *piVal) const
673{
674 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
675 AssertPtrReturn(piVal, VERR_INVALID_POINTER);
676 const char *pszValue = GetString(pszKey);
677 if (pszValue)
678 {
679 *piVal = RTStrToInt64(pszValue);
680 return VINF_SUCCESS;
681 }
682 return VERR_NOT_FOUND;
683}
684
685/**
686 * Returns a 64-bit integer of a specified key.
687 *
688 * @return int64_t Value to return, 0 if not found / on failure.
689 * @param pszKey Name of key to get the value for.
690 */
691int64_t GuestProcessStreamBlock::GetInt64(const char *pszKey) const
692{
693 int64_t iVal;
694 if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
695 return iVal;
696 return 0;
697}
698
699/**
700 * Returns the current number of stream pairs.
701 *
702 * @return uint32_t Current number of stream pairs.
703 */
704size_t GuestProcessStreamBlock::GetCount(void) const
705{
706 return m_mapPairs.size();
707}
708
709/**
710 * Returns a string value of a specified key.
711 *
712 * @return uint32_t Pointer to string to return, NULL if not found / on failure.
713 * @param pszKey Name of key to get the value for.
714 */
715const char* GuestProcessStreamBlock::GetString(const char *pszKey) const
716{
717 AssertPtrReturn(pszKey, NULL);
718
719 try
720 {
721 GuestCtrlStreamPairMapIterConst itPairs = m_mapPairs.find(Utf8Str(pszKey));
722 if (itPairs != m_mapPairs.end())
723 return itPairs->second.mValue.c_str();
724 }
725 catch (const std::exception &ex)
726 {
727 NOREF(ex);
728 }
729 return NULL;
730}
731
732/**
733 * Returns a 32-bit unsigned integer of a specified key.
734 *
735 * @return IPRT status code. VERR_NOT_FOUND if key was not found.
736 * @param pszKey Name of key to get the value for.
737 * @param puVal Pointer to value to return.
738 */
739int GuestProcessStreamBlock::GetUInt32Ex(const char *pszKey, uint32_t *puVal) const
740{
741 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
742 AssertPtrReturn(puVal, VERR_INVALID_POINTER);
743 const char *pszValue = GetString(pszKey);
744 if (pszValue)
745 {
746 *puVal = RTStrToUInt32(pszValue);
747 return VINF_SUCCESS;
748 }
749 return VERR_NOT_FOUND;
750}
751
752/**
753 * Returns a 32-bit unsigned integer of a specified key.
754 *
755 * @return uint32_t Value to return, 0 if not found / on failure.
756 * @param pszKey Name of key to get the value for.
757 */
758uint32_t GuestProcessStreamBlock::GetUInt32(const char *pszKey) const
759{
760 uint32_t uVal;
761 if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
762 return uVal;
763 return 0;
764}
765
766/**
767 * Sets a value to a key or deletes a key by setting a NULL value.
768 *
769 * @return IPRT status code.
770 * @param pszKey Key name to process.
771 * @param pszValue Value to set. Set NULL for deleting the key.
772 */
773int GuestProcessStreamBlock::SetValue(const char *pszKey, const char *pszValue)
774{
775 AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
776
777 int rc = VINF_SUCCESS;
778 try
779 {
780 Utf8Str Utf8Key(pszKey);
781
782 /* Take a shortcut and prevent crashes on some funny versions
783 * of STL if map is empty initially. */
784 if (!m_mapPairs.empty())
785 {
786 GuestCtrlStreamPairMapIter it = m_mapPairs.find(Utf8Key);
787 if (it != m_mapPairs.end())
788 m_mapPairs.erase(it);
789 }
790
791 if (pszValue)
792 {
793 GuestProcessStreamValue val(pszValue);
794 m_mapPairs[Utf8Key] = val;
795 }
796 }
797 catch (const std::exception &ex)
798 {
799 NOREF(ex);
800 }
801 return rc;
802}
803
804///////////////////////////////////////////////////////////////////////////////
805
806GuestProcessStream::GuestProcessStream(void)
807 : m_cbAllocated(0),
808 m_cbSize(0),
809 m_cbOffset(0),
810 m_pbBuffer(NULL)
811{
812
813}
814
815GuestProcessStream::~GuestProcessStream(void)
816{
817 Destroy();
818}
819
820/**
821 * Adds data to the internal parser buffer. Useful if there
822 * are multiple rounds of adding data needed.
823 *
824 * @return IPRT status code.
825 * @param pbData Pointer to data to add.
826 * @param cbData Size (in bytes) of data to add.
827 */
828int GuestProcessStream::AddData(const BYTE *pbData, size_t cbData)
829{
830 AssertPtrReturn(pbData, VERR_INVALID_POINTER);
831 AssertReturn(cbData, VERR_INVALID_PARAMETER);
832
833 int rc = VINF_SUCCESS;
834
835 /* Rewind the buffer if it's empty. */
836 size_t cbInBuf = m_cbSize - m_cbOffset;
837 bool const fAddToSet = cbInBuf == 0;
838 if (fAddToSet)
839 m_cbSize = m_cbOffset = 0;
840
841 /* Try and see if we can simply append the data. */
842 if (cbData + m_cbSize <= m_cbAllocated)
843 {
844 memcpy(&m_pbBuffer[m_cbSize], pbData, cbData);
845 m_cbSize += cbData;
846 }
847 else
848 {
849 /* Move any buffered data to the front. */
850 cbInBuf = m_cbSize - m_cbOffset;
851 if (cbInBuf == 0)
852 m_cbSize = m_cbOffset = 0;
853 else if (m_cbOffset) /* Do we have something to move? */
854 {
855 memmove(m_pbBuffer, &m_pbBuffer[m_cbOffset], cbInBuf);
856 m_cbSize = cbInBuf;
857 m_cbOffset = 0;
858 }
859
860 /* Do we need to grow the buffer? */
861 if (cbData + m_cbSize > m_cbAllocated)
862 {
863 size_t cbAlloc = m_cbSize + cbData;
864 cbAlloc = RT_ALIGN_Z(cbAlloc, _64K);
865 void *pvNew = RTMemRealloc(m_pbBuffer, cbAlloc);
866 if (pvNew)
867 {
868 m_pbBuffer = (uint8_t *)pvNew;
869 m_cbAllocated = cbAlloc;
870 }
871 else
872 rc = VERR_NO_MEMORY;
873 }
874
875 /* Finally, copy the data. */
876 if (RT_SUCCESS(rc))
877 {
878 if (cbData + m_cbSize <= m_cbAllocated)
879 {
880 memcpy(&m_pbBuffer[m_cbSize], pbData, cbData);
881 m_cbSize += cbData;
882 }
883 else
884 rc = VERR_BUFFER_OVERFLOW;
885 }
886 }
887
888 return rc;
889}
890
891/**
892 * Destroys the internal data buffer.
893 */
894void GuestProcessStream::Destroy(void)
895{
896 if (m_pbBuffer)
897 {
898 RTMemFree(m_pbBuffer);
899 m_pbBuffer = NULL;
900 }
901
902 m_cbAllocated = 0;
903 m_cbSize = 0;
904 m_cbOffset = 0;
905}
906
907#ifdef DEBUG
908void GuestProcessStream::Dump(const char *pszFile)
909{
910 LogFlowFunc(("Dumping contents of stream=0x%p (cbAlloc=%u, cbSize=%u, cbOff=%u) to %s\n",
911 m_pbBuffer, m_cbAllocated, m_cbSize, m_cbOffset, pszFile));
912
913 RTFILE hFile;
914 int rc = RTFileOpen(&hFile, pszFile, RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
915 if (RT_SUCCESS(rc))
916 {
917 rc = RTFileWrite(hFile, m_pbBuffer, m_cbSize, NULL /* pcbWritten */);
918 RTFileClose(hFile);
919 }
920}
921#endif
922
923/**
924 * Returns the current offset of the parser within
925 * the internal data buffer.
926 *
927 * @return uint32_t Parser offset.
928 */
929uint32_t GuestProcessStream::GetOffset()
930{
931 return m_cbOffset;
932}
933
934uint32_t GuestProcessStream::GetSize()
935{
936 return m_cbSize;
937}
938
939/**
940 * Tries to parse the next upcoming pair block within the internal
941 * buffer.
942 *
943 * Returns VERR_NO_DATA is no data is in internal buffer or buffer has been
944 * completely parsed already.
945 *
946 * Returns VERR_MORE_DATA if current block was parsed (with zero or more pairs
947 * stored in stream block) but still contains incomplete (unterminated)
948 * data.
949 *
950 * Returns VINF_SUCCESS if current block was parsed until the next upcoming
951 * block (with zero or more pairs stored in stream block).
952 *
953 * @return IPRT status code.
954 * @param streamBlock Reference to guest stream block to fill.
955 *
956 */
957int GuestProcessStream::ParseBlock(GuestProcessStreamBlock &streamBlock)
958{
959 if ( !m_pbBuffer
960 || !m_cbSize)
961 {
962 return VERR_NO_DATA;
963 }
964
965 AssertReturn(m_cbOffset <= m_cbSize, VERR_INVALID_PARAMETER);
966 if (m_cbOffset == m_cbSize)
967 return VERR_NO_DATA;
968
969 int rc = VINF_SUCCESS;
970
971 char *pszOff = (char*)&m_pbBuffer[m_cbOffset];
972 char *pszStart = pszOff;
973 uint32_t uDistance;
974 while (*pszStart)
975 {
976 size_t pairLen = strlen(pszStart);
977 uDistance = (pszStart - pszOff);
978 if (m_cbOffset + uDistance + pairLen + 1 >= m_cbSize)
979 {
980 rc = VERR_MORE_DATA;
981 break;
982 }
983 else
984 {
985 char *pszSep = strchr(pszStart, '=');
986 char *pszVal = NULL;
987 if (pszSep)
988 pszVal = pszSep + 1;
989 if (!pszSep || !pszVal)
990 {
991 rc = VERR_MORE_DATA;
992 break;
993 }
994
995 /* Terminate the separator so that we can
996 * use pszStart as our key from now on. */
997 *pszSep = '\0';
998
999 rc = streamBlock.SetValue(pszStart, pszVal);
1000 if (RT_FAILURE(rc))
1001 return rc;
1002 }
1003
1004 /* Next pair. */
1005 pszStart += pairLen + 1;
1006 }
1007
1008 /* If we did not do any movement but we have stuff left
1009 * in our buffer just skip the current termination so that
1010 * we can try next time. */
1011 uDistance = (pszStart - pszOff);
1012 if ( !uDistance
1013 && *pszStart == '\0'
1014 && m_cbOffset < m_cbSize)
1015 {
1016 uDistance++;
1017 }
1018 m_cbOffset += uDistance;
1019
1020 return rc;
1021}
1022
Note: See TracBrowser for help on using the repository browser.

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