VirtualBox

source: vbox/trunk/src/VBox/Main/glue/string.cpp@ 17275

Last change on this file since 17275 was 17054, checked in by vboxsync, 16 years ago

Main: don't crash in string-to-int parsing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/* $Id: string.cpp 17054 2009-02-24 11:40:53Z vboxsync $ */
2
3/** @file
4 *
5 * MS COM / XPCOM Abstraction Layer:
6 * Smart string classes definition
7 */
8
9/*
10 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
21 * Clara, CA 95054 USA or visit http://www.sun.com if you need
22 * additional information or have any questions.
23 */
24
25#include "VBox/com/string.h"
26
27#include <iprt/err.h>
28
29namespace com
30{
31
32/* static */
33const Bstr Bstr::Null; /* default ctor is OK */
34
35/* static */
36const Utf8Str Utf8Str::Null; /* default ctor is OK */
37
38const size_t Utf8Str::npos = (size_t)-1;
39
40Utf8Str Utf8Str::substr(size_t pos /*= 0*/, size_t n /*= npos*/) const
41{
42 Utf8Str ret;
43
44 if (n)
45 {
46 const char *psz;
47
48 if ((psz = c_str()))
49 {
50 RTUNICP cp;
51
52 // walk the UTF-8 characters until where the caller wants to start
53 size_t i = pos;
54 while (*psz && i--)
55 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
56 return ret; // return empty string on bad encoding
57
58 const char *pFirst = psz;
59
60 if (n == npos)
61 // all the rest:
62 ret = pFirst;
63 else
64 {
65 i = n;
66 while (*psz && i--)
67 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
68 return ret; // return empty string on bad encoding
69
70 size_t cbCopy = psz - pFirst;
71 ret.alloc(cbCopy + 1);
72 memcpy(ret.str, pFirst, cbCopy);
73 ret.str[cbCopy] = '\0';
74 }
75 }
76 }
77
78 return ret;
79}
80
81int Utf8Str::toInt(uint64_t &i) const
82{
83 if (!str)
84 return VERR_NO_DIGITS;
85 return RTStrToUInt64Ex(str, NULL, 0, &i);
86}
87
88int Utf8Str::toInt(uint32_t &i) const
89{
90 if (!str)
91 return VERR_NO_DIGITS;
92 return RTStrToUInt32Ex(str, NULL, 0, &i);
93}
94
95struct FormatData
96{
97 static const size_t CacheIncrement = 256;
98 size_t size;
99 size_t pos;
100 char *cache;
101};
102
103void Utf8StrFmt::init (const char *format, va_list args)
104{
105 if (!format)
106 return;
107
108 // assume an extra byte for a terminating zero
109 size_t fmtlen = strlen (format) + 1;
110
111 FormatData data;
112 data.size = FormatData::CacheIncrement;
113 if (fmtlen >= FormatData::CacheIncrement)
114 data.size += fmtlen;
115 data.pos = 0;
116 data.cache = (char *) ::RTMemTmpAllocZ (data.size);
117
118 size_t n = ::RTStrFormatV (strOutput, &data, NULL, NULL, format, args);
119
120 AssertMsg (n == data.pos,
121 ("The number of bytes formatted doesn't match: %d and %d!",
122 n, data.pos));
123 NOREF (n);
124
125 // finalize formatting
126 data.cache [data.pos] = 0;
127 (*static_cast <Utf8Str *> (this)) = data.cache;
128 ::RTMemTmpFree (data.cache);
129}
130
131// static
132DECLCALLBACK(size_t) Utf8StrFmt::strOutput (void *pvArg, const char *pachChars,
133 size_t cbChars)
134{
135 Assert (pvArg);
136 FormatData &data = *(FormatData *) pvArg;
137
138 if (!(pachChars == NULL && cbChars == 0))
139 {
140 Assert (pachChars);
141
142 // append to cache (always assume an extra byte for a terminating zero)
143 size_t needed = cbChars + 1;
144 if (data.pos + needed > data.size)
145 {
146 data.size += FormatData::CacheIncrement;
147 if (needed >= FormatData::CacheIncrement)
148 data.size += needed;
149 data.cache = (char *) ::RTMemRealloc (data.cache, data.size);
150 }
151 strncpy (data.cache + data.pos, pachChars, cbChars);
152 data.pos += cbChars;
153 }
154
155 return cbChars;
156}
157
158
159} /* namespace com */
Note: See TracBrowser for help on using the repository browser.

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