]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSString.cpp
2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
29 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
31 #define IOKIT_ENABLE_SHARED_PTR
35 #include <libkern/c++/OSString.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSSharedPtr.h>
38 #include <libkern/c++/OSLib.h>
39 #include <libkern/c++/OSData.h>
42 #define super OSObject
44 OSDefineMetaClassAndStructorsWithZone(OSString
, OSObject
,
45 (zone_create_flags_t
) (ZC_CACHING
| ZC_ZFREE_CLEARMEM
))
46 OSMetaClassDefineReservedUnused(OSString
, 0);
47 OSMetaClassDefineReservedUnused(OSString
, 1);
48 OSMetaClassDefineReservedUnused(OSString
, 2);
49 OSMetaClassDefineReservedUnused(OSString
, 3);
50 OSMetaClassDefineReservedUnused(OSString
, 4);
51 OSMetaClassDefineReservedUnused(OSString
, 5);
52 OSMetaClassDefineReservedUnused(OSString
, 6);
53 OSMetaClassDefineReservedUnused(OSString
, 7);
54 OSMetaClassDefineReservedUnused(OSString
, 8);
55 OSMetaClassDefineReservedUnused(OSString
, 9);
56 OSMetaClassDefineReservedUnused(OSString
, 10);
57 OSMetaClassDefineReservedUnused(OSString
, 11);
58 OSMetaClassDefineReservedUnused(OSString
, 12);
59 OSMetaClassDefineReservedUnused(OSString
, 13);
60 OSMetaClassDefineReservedUnused(OSString
, 14);
61 OSMetaClassDefineReservedUnused(OSString
, 15);
64 OSString::initWithString(const OSString
*aString
)
66 return initWithCString(aString
->string
);
70 OSString::initWithCString(const char *cString
)
72 unsigned int newLength
;
75 if (!cString
|| !super::init()) {
79 newLength
= (unsigned int) strnlen(cString
, kMaxStringLength
);
80 if (newLength
>= kMaxStringLength
) {
85 newString
= (char *)kalloc_data_container(newLength
, Z_WAITOK
);
90 bcopy(cString
, newString
, newLength
);
92 if (!(flags
& kOSStringNoCopy
) && string
) {
93 kfree_data_container(string
, length
);
94 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
98 flags
&= ~kOSStringNoCopy
;
100 OSCONTAINER_ACCUMSIZE(length
);
106 OSString::initWithStringOfLength(const char *cString
, size_t inlength
)
108 unsigned int newLength
;
111 if (!cString
|| !super::init()) {
115 if (inlength
>= kMaxStringLength
) {
119 if (strnlen(cString
, inlength
) < inlength
) {
123 newLength
= (unsigned int) (inlength
+ 1);
124 newString
= (char *)kalloc_data_container(newLength
, Z_WAITOK
);
129 bcopy(cString
, newString
, inlength
);
130 newString
[inlength
] = 0;
132 if (!(flags
& kOSStringNoCopy
) && string
) {
133 kfree_data_container(string
, length
);
134 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
139 flags
&= ~kOSStringNoCopy
;
141 OSCONTAINER_ACCUMSIZE(length
);
147 OSString::initWithCStringNoCopy(const char *cString
)
149 if (!cString
|| !super::init()) {
153 length
= (unsigned int) strnlen(cString
, kMaxStringLength
);
154 if (length
>= kMaxStringLength
) {
159 flags
|= kOSStringNoCopy
;
160 string
= const_cast<char *>(cString
);
165 OSSharedPtr
<OSString
>
166 OSString::withString(const OSString
*aString
)
168 OSSharedPtr
<OSString
> me
= OSMakeShared
<OSString
>();
170 if (me
&& !me
->initWithString(aString
)) {
177 OSSharedPtr
<OSString
>
178 OSString::withCString(const char *cString
)
180 OSSharedPtr
<OSString
> me
= OSMakeShared
<OSString
>();
182 if (me
&& !me
->initWithCString(cString
)) {
189 OSSharedPtr
<OSString
>
190 OSString::withCStringNoCopy(const char *cString
)
192 OSSharedPtr
<OSString
> me
= OSMakeShared
<OSString
>();
194 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
201 OSSharedPtr
<OSString
>
202 OSString::withStringOfLength(const char *cString
, size_t length
)
204 OSSharedPtr
<OSString
> me
= OSMakeShared
<OSString
>();
206 if (me
&& !me
->initWithStringOfLength(cString
, length
)) {
218 OSString::stringWithFormat(const char *format
, ...)
220 #ifndef KERNEL // mach3xxx
228 va_start(argList
, format
);
229 me
= stringWithCapacity(256);
230 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
231 me
->length
++; // we include the null in the length
232 if (me
->Length
> 256) {
247 if (!(flags
& kOSStringNoCopy
) && string
) {
248 kfree_data_container(string
, length
);
249 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
256 OSString::getLength() const
262 OSString::getCStringNoCopy() const
268 OSString::setChar(char aChar
, unsigned int index
)
270 if (!(flags
& kOSStringNoCopy
) && index
< length
- 1) {
271 string
[index
] = aChar
;
280 OSString::getChar(unsigned int index
) const
282 if (index
< length
) {
283 return string
[index
];
291 OSString::isEqualTo(const OSString
*aString
) const
293 if (length
!= aString
->length
) {
296 return isEqualTo((const char *) aString
->string
);
301 OSString::isEqualTo(const char *aCString
) const
303 return strncmp(string
, aCString
, length
) == 0;
307 OSString::isEqualTo(const OSMetaClassBase
*obj
) const
312 if ((str
= OSDynamicCast(OSString
, obj
))) {
313 return isEqualTo(str
);
314 } else if ((data
= OSDynamicCast(OSData
, obj
))) {
315 return isEqualTo(data
);
322 OSString::isEqualTo(const OSData
*obj
) const
328 unsigned int dataLen
= obj
->getLength();;
329 const char * dataPtr
= (const char *) obj
->getBytesNoCopy();
331 if (dataLen
!= length
) {
332 // check for the fact that OSData may be a buffer that
333 // that includes a termination byte and will thus have
334 // a length of the actual string length PLUS 1. In this
335 // case we verify that the additional byte is a terminator
336 // and if so count the two lengths as being the same.
338 if ((dataLen
- length
) == 1) {
339 if (dataPtr
[dataLen
- 1] != 0) {
348 for (unsigned int i
= 0; i
< dataLen
; i
++) {
349 if (*dataPtr
++ != string
[i
]) {
358 OSString::serialize(OSSerialize
*s
) const
362 if (s
->previouslySerialized(this)) {
366 if (!s
->addXMLStartTag(this, "string")) {
371 if (!s
->addString("<")) {
374 } else if (*c
== '>') {
375 if (!s
->addString(">")) {
378 } else if (*c
== '&') {
379 if (!s
->addString("&")) {
383 if (!s
->addChar(*c
)) {
390 return s
->addXMLEndTag("string");