xref: /xnu-8020.140.41/libkern/c++/OSString.cpp (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2019 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions /* IOString.m created by rsulack on Wed 17-Sep-1997 */
29*27b03b36SApple OSS Distributions /* IOString.cpp converted to C++ on Tue 1998-9-22 */
30*27b03b36SApple OSS Distributions 
31*27b03b36SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
32*27b03b36SApple OSS Distributions 
33*27b03b36SApple OSS Distributions #include <string.h>
34*27b03b36SApple OSS Distributions 
35*27b03b36SApple OSS Distributions #include <libkern/c++/OSString.h>
36*27b03b36SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
37*27b03b36SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
38*27b03b36SApple OSS Distributions #include <libkern/c++/OSLib.h>
39*27b03b36SApple OSS Distributions #include <libkern/c++/OSData.h>
40*27b03b36SApple OSS Distributions #include <string.h>
41*27b03b36SApple OSS Distributions 
42*27b03b36SApple OSS Distributions #define super OSObject
43*27b03b36SApple OSS Distributions 
44*27b03b36SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSString, OSObject,
45*27b03b36SApple OSS Distributions     (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 0);
47*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 1);
48*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 2);
49*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 3);
50*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 4);
51*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 5);
52*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 6);
53*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 7);
54*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 8);
55*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 9);
56*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 10);
57*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 11);
58*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 12);
59*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 13);
60*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 14);
61*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 15);
62*27b03b36SApple OSS Distributions 
63*27b03b36SApple OSS Distributions bool
initWithString(const OSString * aString)64*27b03b36SApple OSS Distributions OSString::initWithString(const OSString *aString)
65*27b03b36SApple OSS Distributions {
66*27b03b36SApple OSS Distributions 	return initWithCString(aString->string);
67*27b03b36SApple OSS Distributions }
68*27b03b36SApple OSS Distributions 
69*27b03b36SApple OSS Distributions bool
initWithCString(const char * cString)70*27b03b36SApple OSS Distributions OSString::initWithCString(const char *cString)
71*27b03b36SApple OSS Distributions {
72*27b03b36SApple OSS Distributions 	unsigned int   newLength;
73*27b03b36SApple OSS Distributions 	char         * newString;
74*27b03b36SApple OSS Distributions 
75*27b03b36SApple OSS Distributions 	if (!cString || !super::init()) {
76*27b03b36SApple OSS Distributions 		return false;
77*27b03b36SApple OSS Distributions 	}
78*27b03b36SApple OSS Distributions 
79*27b03b36SApple OSS Distributions 	newLength = (unsigned int) strnlen(cString, kMaxStringLength);
80*27b03b36SApple OSS Distributions 	if (newLength >= kMaxStringLength) {
81*27b03b36SApple OSS Distributions 		return false;
82*27b03b36SApple OSS Distributions 	}
83*27b03b36SApple OSS Distributions 
84*27b03b36SApple OSS Distributions 	newLength++;
85*27b03b36SApple OSS Distributions 	newString = (char *)kalloc_data(newLength,
86*27b03b36SApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
87*27b03b36SApple OSS Distributions 	if (!newString) {
88*27b03b36SApple OSS Distributions 		return false;
89*27b03b36SApple OSS Distributions 	}
90*27b03b36SApple OSS Distributions 
91*27b03b36SApple OSS Distributions 	bcopy(cString, newString, newLength);
92*27b03b36SApple OSS Distributions 
93*27b03b36SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
94*27b03b36SApple OSS Distributions 		kfree_data(string, length);
95*27b03b36SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
96*27b03b36SApple OSS Distributions 	}
97*27b03b36SApple OSS Distributions 	string = newString;
98*27b03b36SApple OSS Distributions 	length = newLength;
99*27b03b36SApple OSS Distributions 	flags &= ~kOSStringNoCopy;
100*27b03b36SApple OSS Distributions 
101*27b03b36SApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(length);
102*27b03b36SApple OSS Distributions 
103*27b03b36SApple OSS Distributions 	return true;
104*27b03b36SApple OSS Distributions }
105*27b03b36SApple OSS Distributions 
106*27b03b36SApple OSS Distributions bool
initWithStringOfLength(const char * cString,size_t inlength)107*27b03b36SApple OSS Distributions OSString::initWithStringOfLength(const char *cString, size_t inlength)
108*27b03b36SApple OSS Distributions {
109*27b03b36SApple OSS Distributions 	unsigned int   newLength;
110*27b03b36SApple OSS Distributions 	unsigned int   cStringLength;
111*27b03b36SApple OSS Distributions 	char         * newString;
112*27b03b36SApple OSS Distributions 
113*27b03b36SApple OSS Distributions 	if (!cString || !super::init()) {
114*27b03b36SApple OSS Distributions 		return false;
115*27b03b36SApple OSS Distributions 	}
116*27b03b36SApple OSS Distributions 
117*27b03b36SApple OSS Distributions 	if (inlength >= kMaxStringLength) {
118*27b03b36SApple OSS Distributions 		return false;
119*27b03b36SApple OSS Distributions 	}
120*27b03b36SApple OSS Distributions 
121*27b03b36SApple OSS Distributions 	cStringLength = (unsigned int)strnlen(cString, inlength);
122*27b03b36SApple OSS Distributions 
123*27b03b36SApple OSS Distributions 	if (cStringLength < inlength) {
124*27b03b36SApple OSS Distributions 		inlength = cStringLength;
125*27b03b36SApple OSS Distributions 	}
126*27b03b36SApple OSS Distributions 
127*27b03b36SApple OSS Distributions 	newLength = (unsigned int) (inlength + 1);
128*27b03b36SApple OSS Distributions 	newString = (char *)kalloc_data(newLength,
129*27b03b36SApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
130*27b03b36SApple OSS Distributions 	if (!newString) {
131*27b03b36SApple OSS Distributions 		return false;
132*27b03b36SApple OSS Distributions 	}
133*27b03b36SApple OSS Distributions 
134*27b03b36SApple OSS Distributions 	bcopy(cString, newString, inlength);
135*27b03b36SApple OSS Distributions 	newString[inlength] = 0;
136*27b03b36SApple OSS Distributions 
137*27b03b36SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
138*27b03b36SApple OSS Distributions 		kfree_data(string, length);
139*27b03b36SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
140*27b03b36SApple OSS Distributions 	}
141*27b03b36SApple OSS Distributions 
142*27b03b36SApple OSS Distributions 	string = newString;
143*27b03b36SApple OSS Distributions 	length = newLength;
144*27b03b36SApple OSS Distributions 	flags &= ~kOSStringNoCopy;
145*27b03b36SApple OSS Distributions 
146*27b03b36SApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(length);
147*27b03b36SApple OSS Distributions 
148*27b03b36SApple OSS Distributions 	return true;
149*27b03b36SApple OSS Distributions }
150*27b03b36SApple OSS Distributions 
151*27b03b36SApple OSS Distributions bool
initWithCStringNoCopy(const char * cString)152*27b03b36SApple OSS Distributions OSString::initWithCStringNoCopy(const char *cString)
153*27b03b36SApple OSS Distributions {
154*27b03b36SApple OSS Distributions 	if (!cString || !super::init()) {
155*27b03b36SApple OSS Distributions 		return false;
156*27b03b36SApple OSS Distributions 	}
157*27b03b36SApple OSS Distributions 
158*27b03b36SApple OSS Distributions 	length = (unsigned int) strnlen(cString, kMaxStringLength);
159*27b03b36SApple OSS Distributions 	if (length >= kMaxStringLength) {
160*27b03b36SApple OSS Distributions 		return false;
161*27b03b36SApple OSS Distributions 	}
162*27b03b36SApple OSS Distributions 
163*27b03b36SApple OSS Distributions 	length++;
164*27b03b36SApple OSS Distributions 	flags |= kOSStringNoCopy;
165*27b03b36SApple OSS Distributions 	string = const_cast<char *>(cString);
166*27b03b36SApple OSS Distributions 
167*27b03b36SApple OSS Distributions 	return true;
168*27b03b36SApple OSS Distributions }
169*27b03b36SApple OSS Distributions 
170*27b03b36SApple OSS Distributions OSSharedPtr<OSString>
withString(const OSString * aString)171*27b03b36SApple OSS Distributions OSString::withString(const OSString *aString)
172*27b03b36SApple OSS Distributions {
173*27b03b36SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
174*27b03b36SApple OSS Distributions 
175*27b03b36SApple OSS Distributions 	if (me && !me->initWithString(aString)) {
176*27b03b36SApple OSS Distributions 		return nullptr;
177*27b03b36SApple OSS Distributions 	}
178*27b03b36SApple OSS Distributions 
179*27b03b36SApple OSS Distributions 	return me;
180*27b03b36SApple OSS Distributions }
181*27b03b36SApple OSS Distributions 
182*27b03b36SApple OSS Distributions OSSharedPtr<OSString>
withCString(const char * cString)183*27b03b36SApple OSS Distributions OSString::withCString(const char *cString)
184*27b03b36SApple OSS Distributions {
185*27b03b36SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
186*27b03b36SApple OSS Distributions 
187*27b03b36SApple OSS Distributions 	if (me && !me->initWithCString(cString)) {
188*27b03b36SApple OSS Distributions 		return nullptr;
189*27b03b36SApple OSS Distributions 	}
190*27b03b36SApple OSS Distributions 
191*27b03b36SApple OSS Distributions 	return me;
192*27b03b36SApple OSS Distributions }
193*27b03b36SApple OSS Distributions 
194*27b03b36SApple OSS Distributions OSSharedPtr<OSString>
withCStringNoCopy(const char * cString)195*27b03b36SApple OSS Distributions OSString::withCStringNoCopy(const char *cString)
196*27b03b36SApple OSS Distributions {
197*27b03b36SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
198*27b03b36SApple OSS Distributions 
199*27b03b36SApple OSS Distributions 	if (me && !me->initWithCStringNoCopy(cString)) {
200*27b03b36SApple OSS Distributions 		return nullptr;
201*27b03b36SApple OSS Distributions 	}
202*27b03b36SApple OSS Distributions 
203*27b03b36SApple OSS Distributions 	return me;
204*27b03b36SApple OSS Distributions }
205*27b03b36SApple OSS Distributions 
206*27b03b36SApple OSS Distributions OSSharedPtr<OSString>
withCString(const char * cString,size_t length)207*27b03b36SApple OSS Distributions OSString::withCString(const char *cString, size_t length)
208*27b03b36SApple OSS Distributions {
209*27b03b36SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
210*27b03b36SApple OSS Distributions 
211*27b03b36SApple OSS Distributions 	if (me && !me->initWithStringOfLength(cString, length)) {
212*27b03b36SApple OSS Distributions 		return nullptr;
213*27b03b36SApple OSS Distributions 	}
214*27b03b36SApple OSS Distributions 
215*27b03b36SApple OSS Distributions 	return me;
216*27b03b36SApple OSS Distributions }
217*27b03b36SApple OSS Distributions 
218*27b03b36SApple OSS Distributions 
219*27b03b36SApple OSS Distributions 
220*27b03b36SApple OSS Distributions /* @@@ gvdl */
221*27b03b36SApple OSS Distributions #if 0
222*27b03b36SApple OSS Distributions OSString *
223*27b03b36SApple OSS Distributions OSString::stringWithFormat(const char *format, ...)
224*27b03b36SApple OSS Distributions {
225*27b03b36SApple OSS Distributions #ifndef KERNEL                  // mach3xxx
226*27b03b36SApple OSS Distributions 	OSString *me;
227*27b03b36SApple OSS Distributions 	va_list argList;
228*27b03b36SApple OSS Distributions 
229*27b03b36SApple OSS Distributions 	if (!format) {
230*27b03b36SApple OSS Distributions 		return 0;
231*27b03b36SApple OSS Distributions 	}
232*27b03b36SApple OSS Distributions 
233*27b03b36SApple OSS Distributions 	va_start(argList, format);
234*27b03b36SApple OSS Distributions 	me = stringWithCapacity(256);
235*27b03b36SApple OSS Distributions 	me->length = vsnprintf(me->string, 256, format, argList);
236*27b03b36SApple OSS Distributions 	me->length++;   // we include the null in the length
237*27b03b36SApple OSS Distributions 	if (me->Length > 256) {
238*27b03b36SApple OSS Distributions 		me->Length = 256;
239*27b03b36SApple OSS Distributions 	}
240*27b03b36SApple OSS Distributions 	va_end(argList);
241*27b03b36SApple OSS Distributions 
242*27b03b36SApple OSS Distributions 	return me;
243*27b03b36SApple OSS Distributions #else
244*27b03b36SApple OSS Distributions 	return 0;
245*27b03b36SApple OSS Distributions #endif
246*27b03b36SApple OSS Distributions }
247*27b03b36SApple OSS Distributions #endif /* 0 */
248*27b03b36SApple OSS Distributions 
249*27b03b36SApple OSS Distributions void
free()250*27b03b36SApple OSS Distributions OSString::free()
251*27b03b36SApple OSS Distributions {
252*27b03b36SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
253*27b03b36SApple OSS Distributions 		kfree_data(string, length);
254*27b03b36SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
255*27b03b36SApple OSS Distributions 	}
256*27b03b36SApple OSS Distributions 
257*27b03b36SApple OSS Distributions 	super::free();
258*27b03b36SApple OSS Distributions }
259*27b03b36SApple OSS Distributions 
260*27b03b36SApple OSS Distributions unsigned int
getLength() const261*27b03b36SApple OSS Distributions OSString::getLength()  const
262*27b03b36SApple OSS Distributions {
263*27b03b36SApple OSS Distributions 	return length - 1;
264*27b03b36SApple OSS Distributions }
265*27b03b36SApple OSS Distributions 
266*27b03b36SApple OSS Distributions const char *
getCStringNoCopy() const267*27b03b36SApple OSS Distributions OSString::getCStringNoCopy() const
268*27b03b36SApple OSS Distributions {
269*27b03b36SApple OSS Distributions 	return string;
270*27b03b36SApple OSS Distributions }
271*27b03b36SApple OSS Distributions 
272*27b03b36SApple OSS Distributions bool
setChar(char aChar,unsigned int index)273*27b03b36SApple OSS Distributions OSString::setChar(char aChar, unsigned int index)
274*27b03b36SApple OSS Distributions {
275*27b03b36SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && index < length - 1) {
276*27b03b36SApple OSS Distributions 		string[index] = aChar;
277*27b03b36SApple OSS Distributions 
278*27b03b36SApple OSS Distributions 		return true;
279*27b03b36SApple OSS Distributions 	} else {
280*27b03b36SApple OSS Distributions 		return false;
281*27b03b36SApple OSS Distributions 	}
282*27b03b36SApple OSS Distributions }
283*27b03b36SApple OSS Distributions 
284*27b03b36SApple OSS Distributions char
getChar(unsigned int index) const285*27b03b36SApple OSS Distributions OSString::getChar(unsigned int index) const
286*27b03b36SApple OSS Distributions {
287*27b03b36SApple OSS Distributions 	if (index < length) {
288*27b03b36SApple OSS Distributions 		return string[index];
289*27b03b36SApple OSS Distributions 	} else {
290*27b03b36SApple OSS Distributions 		return '\0';
291*27b03b36SApple OSS Distributions 	}
292*27b03b36SApple OSS Distributions }
293*27b03b36SApple OSS Distributions 
294*27b03b36SApple OSS Distributions 
295*27b03b36SApple OSS Distributions bool
isEqualTo(const OSString * aString) const296*27b03b36SApple OSS Distributions OSString::isEqualTo(const OSString *aString) const
297*27b03b36SApple OSS Distributions {
298*27b03b36SApple OSS Distributions 	if (length != aString->length) {
299*27b03b36SApple OSS Distributions 		return false;
300*27b03b36SApple OSS Distributions 	} else {
301*27b03b36SApple OSS Distributions 		return isEqualTo((const char *) aString->string);
302*27b03b36SApple OSS Distributions 	}
303*27b03b36SApple OSS Distributions }
304*27b03b36SApple OSS Distributions 
305*27b03b36SApple OSS Distributions bool
isEqualTo(const char * aCString) const306*27b03b36SApple OSS Distributions OSString::isEqualTo(const char *aCString) const
307*27b03b36SApple OSS Distributions {
308*27b03b36SApple OSS Distributions 	return strncmp(string, aCString, length) == 0;
309*27b03b36SApple OSS Distributions }
310*27b03b36SApple OSS Distributions 
311*27b03b36SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const312*27b03b36SApple OSS Distributions OSString::isEqualTo(const OSMetaClassBase *obj) const
313*27b03b36SApple OSS Distributions {
314*27b03b36SApple OSS Distributions 	OSString *  str;
315*27b03b36SApple OSS Distributions 	OSData *    data;
316*27b03b36SApple OSS Distributions 
317*27b03b36SApple OSS Distributions 	if ((str = OSDynamicCast(OSString, obj))) {
318*27b03b36SApple OSS Distributions 		return isEqualTo(str);
319*27b03b36SApple OSS Distributions 	} else if ((data = OSDynamicCast(OSData, obj))) {
320*27b03b36SApple OSS Distributions 		return isEqualTo(data);
321*27b03b36SApple OSS Distributions 	} else {
322*27b03b36SApple OSS Distributions 		return false;
323*27b03b36SApple OSS Distributions 	}
324*27b03b36SApple OSS Distributions }
325*27b03b36SApple OSS Distributions 
326*27b03b36SApple OSS Distributions bool
isEqualTo(const OSData * obj) const327*27b03b36SApple OSS Distributions OSString::isEqualTo(const OSData *obj) const
328*27b03b36SApple OSS Distributions {
329*27b03b36SApple OSS Distributions 	if (NULL == obj) {
330*27b03b36SApple OSS Distributions 		return false;
331*27b03b36SApple OSS Distributions 	}
332*27b03b36SApple OSS Distributions 
333*27b03b36SApple OSS Distributions 	unsigned int dataLen = obj->getLength();
334*27b03b36SApple OSS Distributions 	const char * dataPtr = (const char *) obj->getBytesNoCopy();
335*27b03b36SApple OSS Distributions 
336*27b03b36SApple OSS Distributions 	if (dataLen != length) {
337*27b03b36SApple OSS Distributions 		// check for the fact that OSData may be a buffer that
338*27b03b36SApple OSS Distributions 		// that includes a termination byte and will thus have
339*27b03b36SApple OSS Distributions 		// a length of the actual string length PLUS 1. In this
340*27b03b36SApple OSS Distributions 		// case we verify that the additional byte is a terminator
341*27b03b36SApple OSS Distributions 		// and if so count the two lengths as being the same.
342*27b03b36SApple OSS Distributions 
343*27b03b36SApple OSS Distributions 		if ((dataLen - length) == 1) {
344*27b03b36SApple OSS Distributions 			if (dataPtr[dataLen - 1] != 0) {
345*27b03b36SApple OSS Distributions 				return false;
346*27b03b36SApple OSS Distributions 			}
347*27b03b36SApple OSS Distributions 			dataLen--;
348*27b03b36SApple OSS Distributions 		} else {
349*27b03b36SApple OSS Distributions 			return false;
350*27b03b36SApple OSS Distributions 		}
351*27b03b36SApple OSS Distributions 	}
352*27b03b36SApple OSS Distributions 
353*27b03b36SApple OSS Distributions 	for (unsigned int i = 0; i < dataLen; i++) {
354*27b03b36SApple OSS Distributions 		if (*dataPtr++ != string[i]) {
355*27b03b36SApple OSS Distributions 			return false;
356*27b03b36SApple OSS Distributions 		}
357*27b03b36SApple OSS Distributions 	}
358*27b03b36SApple OSS Distributions 
359*27b03b36SApple OSS Distributions 	return true;
360*27b03b36SApple OSS Distributions }
361*27b03b36SApple OSS Distributions 
362*27b03b36SApple OSS Distributions bool
serialize(OSSerialize * s) const363*27b03b36SApple OSS Distributions OSString::serialize(OSSerialize *s) const
364*27b03b36SApple OSS Distributions {
365*27b03b36SApple OSS Distributions 	char *c = string;
366*27b03b36SApple OSS Distributions 
367*27b03b36SApple OSS Distributions 	if (s->previouslySerialized(this)) {
368*27b03b36SApple OSS Distributions 		return true;
369*27b03b36SApple OSS Distributions 	}
370*27b03b36SApple OSS Distributions 
371*27b03b36SApple OSS Distributions 	if (!s->addXMLStartTag(this, "string")) {
372*27b03b36SApple OSS Distributions 		return false;
373*27b03b36SApple OSS Distributions 	}
374*27b03b36SApple OSS Distributions 	while (*c) {
375*27b03b36SApple OSS Distributions 		if (*c == '<') {
376*27b03b36SApple OSS Distributions 			if (!s->addString("&lt;")) {
377*27b03b36SApple OSS Distributions 				return false;
378*27b03b36SApple OSS Distributions 			}
379*27b03b36SApple OSS Distributions 		} else if (*c == '>') {
380*27b03b36SApple OSS Distributions 			if (!s->addString("&gt;")) {
381*27b03b36SApple OSS Distributions 				return false;
382*27b03b36SApple OSS Distributions 			}
383*27b03b36SApple OSS Distributions 		} else if (*c == '&') {
384*27b03b36SApple OSS Distributions 			if (!s->addString("&amp;")) {
385*27b03b36SApple OSS Distributions 				return false;
386*27b03b36SApple OSS Distributions 			}
387*27b03b36SApple OSS Distributions 		} else {
388*27b03b36SApple OSS Distributions 			if (!s->addChar(*c)) {
389*27b03b36SApple OSS Distributions 				return false;
390*27b03b36SApple OSS Distributions 			}
391*27b03b36SApple OSS Distributions 		}
392*27b03b36SApple OSS Distributions 		c++;
393*27b03b36SApple OSS Distributions 	}
394*27b03b36SApple OSS Distributions 
395*27b03b36SApple OSS Distributions 	return s->addXMLEndTag("string");
396*27b03b36SApple OSS Distributions }
397